/var/www/myprog.com.ua/site/app
Edit: /var/www/myprog.com.ua/site/app/websocket.js (15503B)
function WsClient(host, port) {
var __self = this;
this._host = host;
this._port = port;
this._url = 'wss://' + this._host + ':' + this._port + '/ws';
this._connected = false;
this._reconnect = false;
this._socket = null;
var modal_error = webix.ui({
view: 'window',
id: 'm_ws_error',
modal: true,
width: 600,
height: 200,
position: 'center',
body: {
template: '
Разрыв соеденения!
'
}
});
if (isDebug) console.debug('WsClient : Create class : ' + this._url);
this.UpdateStatusSocket = function (ws) {
if (isDebug) {
console.debug('WsClient:UpdateStatusSocket:');
console.debug(ws);
}
if (ws) {
webix.html.removeCss($$('top_socket_status').getNode(), 'btn_success');
webix.html.removeCss($$('top_socket_status').getNode(), 'btn_danger');
webix.html.removeCss($$('top_socket_status').getNode(), 'btn_warning');
if (ws.readyState === 0) {
$$('top_socket_status').define('css', 'btn_warning');
$$('top_socket_status').refresh();
} else if (ws.readyState === 1) {
$$('top_socket_status').define('css', 'btn_success');
$$('top_socket_status').refresh();
modal_error.hide();
__self._connected = true;
} else {
$$('top_socket_status').define('css', 'btn_danger');
$$('top_socket_status').refresh();
modal_error.show();
__self._connected = false;
}
}
};
this.Connect = function (reconnect) {
if (isDebug) console.debug('WsClient:Connect: ' + __self._url);
if (false === __self._connected) {
__self.UpdateStatusSocket(__self._socket);
__self._reconnect = reconnect;
__self._socket = new WebSocket(__self._url);
__self._socket.onopen = this.OnOpen;
__self._socket.onclose = this.OnClose;
__self._socket.onerror = this.OnError;
__self._socket.onmessage = this.OnMessage;
}
};
// socket events
this.OnOpen = function (e) {
__self._connected = true;
if (isDebug) console.debug("WsClient:OnOpen: Соединение установлено. " + e.target.url);
if (isDebug) console.debug(e);
webix.message({type: 'success', text: 'WsClient:OnOpen: Соединение установлено.'});
// добавить оператора
if (isDebug) console.debug('WsClient : Регистрация оператора');
if (config && config.client_id > 0 && config.city_id > 0 && config.client_key.length > 0) {
__self.AddOperator({
client_id: config.client_id,
client_key: config.client_key,
user_name: config.user_name,
city_id: config.city_id
});
if (httpClient) {
webix.message('WsClient:Sync:Begin');
// get data
httpClient.GetSectors();
httpClient.GetCars();
httpClient.GetCalls();
httpClient.GetChannels();
httpClient.GetLines();
httpClient.GetTarifs();
httpClient.GetOrders();
httpClient.GetCancelCauses();
httpClient.GetPenalties();
httpClient.GetUslugi();
httpClient.GetChat();
if (map) map.UpdateData();
if (toolBarTop) {
toolBarTop.UpdateOperatorBalance(1);
toolBarTop.UpdateOrdersTime(1);
toolBarTop.ClearPhoneStatus();
}
webix.message('WsClient:Sync:End');
}
} else {
if (isDebug) console.error('WsClient:OnOpen: Ошибка данных оператора');
}
};
this.OnClose = function (e) {
__self._connected = false;
__self.UpdateStatusSocket(__self._socket);
if (e.wasClean) {
/*if (isDebug)*/ console.debug('WsClient:OnClose: Соединение закрыто.');
webix.message({type: 'error', text: 'WsClient:OnClose: Соединение закрыто.'})
} else {
/*if (isDebug)*/ console.debug('WsClient:OnClose: Разрыв соединения!');
webix.message({type: 'error', text: 'WsClient:OnClose: Разрыв соединения!'});
}
/*if (isDebug)*/ console.debug(e);
if (__self._reconnect) {
setTimeout(function () {
__self.Connect(__self._reconnect);
}, 5000);
}
};
this.OnError = function (e) {
/*if (isDebug)*/ console.debug('WsClient:OnError: Ошибка');
/*if (isDebug)*/ console.debug(e);
__self.UpdateStatusSocket(__self._socket);
};
this.OnMessage = function (e) {
__self.UpdateStatusSocket(__self._socket);
var message = JSON.parse(e.data);
if (isDebug) console.debug('WsClient:OnOpen: получены данные от: ' + __self._url);
/*if (isDebug)*/
console.debug(message);
var a = new Accept();
switch (message.cmd) {
case 'addOperator':
a.AddOperator(message.data);
break;
case 'carStatus':
if (config.city_id == message.city_id || 0 === parseInt(message.city_id)) a.CarStatus(message.data);
break;
case 'parkMoveCar':
if (config.city_id == message.city_id || 0 === parseInt(message.city_id)) a.CarMovePark(message.data);
break;
//
// ORDER
//
case 'orderCreate':
if (config.city_id == message.city_id || 0 === parseInt(message.city_id)) a.UpdateOrder(message.data);
break;
case 'orderAccept':
if (config.city_id == message.city_id || 0 === parseInt(message.city_id)) a.UpdateOrder(message.data);
break;
case 'orderCancel':
if (config.city_id == message.city_id || 0 === parseInt(message.city_id)) a.UpdateOrder(message.data);
break;
case 'orderArrive':
if (config.city_id == message.city_id || 0 === parseInt(message.city_id)) a.UpdateOrder(message.data);
break;
case 'orderRide':
if (config.city_id == message.city_id || 0 === parseInt(message.city_id)) a.UpdateOrder(message.data);
break;
case 'orderComplete':
if (config.city_id == message.city_id || 0 === parseInt(message.city_id)) a.UpdateOrder(message.data);
break;
case 'orderStatus':
if (config.city_id == message.city_id || 0 === parseInt(message.city_id)) a.UpdateOrder(message.data);
break;
//
// CALL
//
case 'RING':
a.Ring(message);
break;
case 'ANSWER':
a.Answer(message);
break;
case 'HANGUP':
a.Hungup(message);
break;
case 'robo':
a.Robo(message);
break;
case 'chat_message':
a.ChatMessage(message);
break;
}
};
// send to ws
this.AddOperator = function (data) {
if (__self._connected) {
__self._socket.send(JSON.stringify({
'cmd': 'addOperator',
'data': {
'id': data.client_id,
'client_key': data.client_key,
'name': data.user_name,
'city_id': data.city_id
}
})
);
}
};
this.Ping = function () {
if (__self._connected) {
var m = {
cmd: 'ping',
data: {
id: config.client_id,
client_key: config.client_key,
city_id: config.city_id
}
};
__self._socket.send(JSON.stringify(m));
//console.debug('WsClient:Ping:');
console.debug(m);
}
}
}
function Accept() {
this.AddOperator = function (data) {
if (isDebug) console.debug('WsClient:Accept.AddOperator:');
if (isDebug) console.debug(data);
if (data && config && data.client_key == config.client_key) {
wsClient.connected = true;
if (isDebug) console.debug('WsClient:Accept.AddOperator:OK');
} else {
if (isDebug) console.error('WsClient:Accept.AddOperator:ERROR');
}
};
this.CarStatus = function (data) {
if (isDebug) console.debug('WsClient:Accept.CarStatus');
if (isDebug) console.debug(data);
if(data && data.id > 0) {
var car = cars.getItem(data.id);
if (car) {
car.Update(data);
//cars.updateItem(data.id);
} else {
car = new Car(data);
cars.add(car);
}
}
};
this.CarMovePark = function (data) {
if (isDebug) console.debug('WsClient:Accept.CarMovePark');
if (isDebug) console.debug(data);
if(data && data.id > 0) {
var car = cars.getItem(data.id);
if (car) {
car.Update(data);
var pos = 0;
if (data.park == data.old_park) {
car.park_status = 1;
car.park = data.old_park;
car.time = 2 * 60 * 60;
pos = 1;
} else {
car.time = 0;
}
if (sideCars) sideCars.Update();
if (parking) parking.CarMovePark(car, pos);
}
}
};
//
// ORDER
//
this.UpdateOrder = function (data) {
if (isDebug) console.debug('WsClient:UpdateOrder:Update');
if(data && data.id > 0) {
var order = orders.getItem(data.id);
if (order) {
if (isDebug) console.debug('WsClient:UpdateOrder:Update:' + data.id);
order.Update(data);
var car = cars.getItem(order.car_id);
if (car) {
if (order.status == 'complite' || order.status == 'cancel' || order.status == 'search') {
car.Update({last_order_id: 0});
} else {
car.Update({last_order_id: order.id});
}
}
// если отменен выбраный заказ снять выделение на карте
if (order.status == 'cancel' && selected_order != null && selected_order.id == order.id) {
if (map) map.UnselectOrder(order);
}
} else {
if (isDebug) console.debug('WsClient:UpdateOrder:Add:' + data.id);
order = new Order(data);
orders.add(order, 0);
orders.remove(orders.getLastId());
}
if (activeOrders) activeOrders.Update();
}
};
//
// CALL
//
this.Answer = function (data) {
data.p = normalize_phone(data.p);
// ansline or sipch ???
if (data.ansline == config.default.sip_user || data.sipch == config.default.sip_user) {
if (windowOrder) windowOrder.Show({id: 0, phone: data.p, call_id: data.id});
if (toolBarTop) toolBarTop.UpdatePhoneStatus(data.phone, 'accept');
} else {
p = config.country_code + data.p;
}
/*if (data.sipch === config.default.sip_user) {
}*/
if (data.id) {
var call = calls.getItem(data.id);
if (!call) {
call = new Call(data);
calls.add(call, 0);
calls.remove(calls.getLastId());
}
call.Update({
ansline: data.ansline,
city_id: data.city_id,
gsmch: data.gsmch,
id: data.id,
phone: data.p,
status: 2
});
if (sideCalls) sideCalls.Update(call);
//console.log(call);
//if (historyCalls) historyCalls.Update();
}
};
this.Hungup = function (data) {
// ansline or sipch ???
if (data.id) {
var call = calls.getItem(data.id);
if (call) {
call.Update(data);
if (sideCalls) sideCalls.Update(call);
}
if (toolBarTop) toolBarTop.UpdatePhoneStatus(data.p, 'hungup');
//if (sideCalls) sideCalls.Update();
//if (historyCalls) historyCalls.Update();
}
};
this.Ring = function (data) {
data.p = normalize_phone(data.p);
if (data) {
var call = new Call(data);
if (call) {
calls.add(call, 0);
if (calls.count() > config.default.calls_history_count) {
if (calls.getLastId() > 0) {
calls.remove(calls.getLastId());
}
}
if (sideCalls) sideCalls.Update(call);
}
if (toolBarTop) toolBarTop.UpdatePhoneStatus(data.p, 'invite');
//if (sideCalls) sideCalls.Update();
//if (historyCalls) historyCalls.Update();
}
};
this.Robo = function (data) {
if (data) {
var order = orders.getItem(data.id);
if (order) {
order.Update(data);
if (historyOrders) historyOrders.Update();
if (sideCalls) sideCalls.Update(call);
}
}
};
this.ChatMessage = function (data) {
if (data && data.city_id == config.city_id) {
chat.remove(chat.getFirstId());
chat.add(data.data, chat.count());
if (data.data.client_id_from != config.client_id) {
var audio = document.querySelector("audio");
if (data.data.type == 'operators') {
audio.src = "sounds/mid_confirm.wav";
} else {
audio.src = "sounds/beep.wav";
}
audio.pause();
audio.volume = 0.3;
audio.play();
}
if (sideChat) sideChat.Update();
webix.message({type: 'chat', text: ui_template_chat_message_simple(data.data)});
/*if (currSide != 'chat') {
$$('toolbar_side').updateItem('chat', {value: 'images/speech-bubble-red.png'});
}*/
if (!$$('winChat').isVisible()) {
$$('btnChat').define('image', 'images/speech-bubble-red.png');
$$('btnChat').refresh();
}
if (windowCar) windowCar.Update();
}
}
}