/var/www/myprog.com.ua/site/app
Edit: /var/www/myprog.com.ua/site/app/socket_io_client.js (25823B)
//isDebug = true;
function SocketIoClient() {
var _self = this;
this.socket = null;
this.host = null;
this.port = null;
var modal_error = webix.ui({
view: 'window',
id: 'm_ws_error',
modal: true,
width: 600,
height: 200,
position: 'center',
body: {
template: '
Установка соединения...
'
}
});
this.init = function (host, port) {
this.host = host;
this.port = port;
this.socket = io.connect('//' + this.host + ':' + this.port);
modal_error.show();
this.socket.on('connect', () => {
on_connect()
});
this.socket.on('disconnect', () => {
on_disconnect()
});
this.socket.on(WHO.USER.PREFIX + 'join', (data) => {
on_join(data)
});
this.socket.on(WHO.USER.PREFIX + 'info', (data) => {
console.debug('SocketIoClient::info()::' + data);
});
this.socket.on(WHO.USER.PREFIX + 'load_hosts', (json) => {
console.debug('SocketIoClient::load_hosts()');
let data = JSON.parse(json);
//console.debug(data);
data.forEach(function (e) {
var h = new Host(e);
if (h.id > 0 && !hosts.getItem(h.id)) {
hosts.add(h);
} else {
hosts.updateItem(h.id, h);
}
});
if(hostsTab)hostsTab.Show();
});
this.socket.on(WHO.USER.PREFIX + 'load_devices', (json) => {
console.debug('SocketIoClient::load_devices()');
let data = JSON.parse(json);
//console.debug(data);
data.forEach(function (e) {
var h = new Device(e);
if (h.id > 0 && !devices.getItem(h.id)) {
if(h.event_id>0){
h.ch_color = "#997777";
}
devices.add(h);
} else {
if(h.event_id>0){
h.ch_color = "#997777";
}
devices.updateItem(h.id, h);
}
});
if(hostsTab)hostsTab.Update();
});
this.socket.on(WHO.USER.PREFIX + 'load_events', (json) => {
console.debug('SocketIoClient::load_events()');
let data = JSON.parse(json);
//console.debug(data);
data.forEach(function (e) {
var h = new Event(e);
if (h.id > 0 && !events.getItem(h.id)) {
events.add(h);
} else {
events.updateItem(h.id, h);
}
});
//if(hostsTab)hostsTab.Update();
});
this.socket.on(WHO.USER.PREFIX + 'load_values', (json) => {
console.debug('SocketIoClient::load_values()');
let data = JSON.parse(json);
//console.debug(data);
data.forEach(function (e) {
var h = new Value(e);
if (h.id > 0 && !values.getItem(h.id)) {
values.add(h);
} else {
values.updateItem(h.id, h);
}
});
if(hostsTab)hostsTab.Update();
});
this.socket.on(WHO.USER.PREFIX + 'load_commands', (json) => {
console.debug('SocketIoClient::load_commands()');
let data = JSON.parse(json);
//console.debug(data);
data.forEach(function (e) {
var h = new Command(e);
if (h.id > 0 && !commands.getItem(h.id)) {
commands.add(h);
} else {
commands.updateItem(h.id, h);
}
});
// if(hostsTab)hostsTab.Update();
});
this.socket.on(WHO.USER.PREFIX + 'value', (json) => {
//console.debug(json);
//console.debug('SocketIoClient::value()');
let data = JSON.parse(json);
//console.debug(data);
var h = new Value(data);
if (h.id > 0 && !values.getItem(h.id)) {
values.add(h);
} else {
values.updateItem(h.id, h);
}
//if(hostsTab)hostsTab.Update();
});
this.socket.on(WHO.USER.PREFIX + 'event', (json) => {
//console.debug(json);
//console.debug('SocketIoClient::event()');
let data = JSON.parse(json);
//console.debug(data);
var h = new Event(data);
if (h.id > 0) {
if(devices.data.each){
devices.data.each(function (e) {
//console.debug(e);
var d = new Device(e);
if (h.host_id==d.host_id&&h.n==d.n&&h.type==d.type) {
let dev = devices.getItem(d.id);
//let ev = events.getItem(h.id);
//console.debug(ev);
if (h.event=="on") {
dev.ch_color = "#922";
}
if (h.event=="click") {
dev.ch_color = "#922";
}
if (h.event=="open") {
dev.ch_color = "#922";
}
if (h.event=="lock") {
dev.ch_color = "#922";
}
if (h.event=="unlock") {
dev.ch_color = "#292";
}
if (h.event=="release") {
dev.ch_color = "#3c3c3c";
}
if (h.event=="close") {
dev.ch_color = "#3c3c3c";
}
if (h.event=="autooff") {
dev.ch_color = "#3c3c3c";
}
if (h.event=="off") {
dev.ch_color = "#3c3c3c";
}
devices.updateItem(dev.id, dev);
//console.debug('SocketIoClient::event() --> device found');
}
});
}
}
/*
if (h.id > 0 && !values.getItem(h.id)) {
values.add(h);
} else {
values.updateItem(h.id, h);
}*/
//if(hostsTab)hostsTab.Update();
});
this.socket.on('log', (json) => {
console.debug(json);
console_.add(json, console_.count());
if (windowConsole) {
windowConsole.Update();
}
//if (sideConsole) sideConsole.Update();
/*
let data = JSON.parse(json);
if (data) {
if (console_.find(p => p.mid == data.mid).length == 0) {
console_.remove(console_.getFirstId());
console_.add(data, console_.count());
if (sideConsole) sideConsole.Update();
if (currSide != 'chat') {
$$('toolbar_side').updateItem('chat', {value: 'images/speech-bubble-red.png'});
}
// if (!$$('winConsole').isVisible()) {
// $$('btnConsole').define('image', 'images/speech-bubble-red.png');
// $$('btnConsole').refresh();
// }
}
}*/
});
this.socket.on(WHO.USER.PREFIX + 'error', (data) => {
console.log(data);
});
this.socket.on(WHO.USER.PREFIX + 'need_login', (data) => {
console.log(data);
});
this.socket.on(WHO.USER.PREFIX + 'need_activation', (data) => {
console.log(data);
});
// orders
this.socket.on(WHO.USER.PREFIX + 'order_create', (data) => {
on_order_update(JSON.parse(data));
});
this.socket.on(WHO.USER.PREFIX + 'order_create_ok', () => {
console.debug('SocketIoClient::order_create_ok::');
if (windowOrder && windowOrder.isVisible()) {
windowOrder.Hide();
}
});
this.socket.on(WHO.USER.PREFIX + 'order_accept', (json) => {
on_order_update(JSON.parse(json));
});
this.socket.on(WHO.USER.PREFIX + 'order_arrive', (json) => {
on_order_update(JSON.parse(json));
});
this.socket.on(WHO.USER.PREFIX + 'order_ride', (json) => {
on_order_update(JSON.parse(json));
});
this.socket.on(WHO.USER.PREFIX + 'order_complete', (json) => {
on_order_update(JSON.parse(json));
});
this.socket.on(WHO.USER.PREFIX + 'order_cancel', (json) => {
on_order_update(JSON.parse(json));
});
this.socket.on(WHO.USER.PREFIX + 'order_status', (json) => {
on_order_update(JSON.parse(json));
});
// cars
this.socket.on(WHO.USER.PREFIX + 'car_status', (json) => {
//console.log(json);
on_car_status(JSON.parse(json));
});
this.socket.on(WHO.USER.PREFIX + 'park_move_car', (json) => {
on_park_move_car(json);
});
this.socket.on(WHO.USER.PREFIX + 'jparks', (json) => {
//console.log('JPARKS...');
on_jparks(JSON.parse(json));
});
this.socket.on(WHO.USER.PREFIX + 'call_event', (json) => {
let data = JSON.parse(json);
console.debug('SocketIoClient:: call_event ::', json);
switch (data.cmd) {
case 'RING':
_self.call_ring(data);
break;
case 'RINGOUT':
_self.call_ring(data);
break;
case 'ANSWER':
_self.call_answer(data);
break;
case 'ROBO':
_self.call_robo(data);
break;
case 'HANGUP':
_self.call_hungup(data);
break;
}
});
this.socket.on(WHO.USER.PREFIX + 'robo', (json) => {
console.debug('SocketIoClient:: robo ::', json);
let data = JSON.parse(json);
if (data.order_id) {
var order = orders.getItem(data.order_id);
if (order) {
order.Update({
id: data.order_id,
robo: data.robo
});
if (historyOrders) historyOrders.Update();
}
}
});
this.socket.on('chat_send', (json) => {
console.debug(json);
let data = JSON.parse(json);
if (data) {
if (chat.find(p => p.mid == data.mid).length == 0) {
chat.remove(chat.getFirstId());
chat.add(data, chat.count());
if (data.client_id_from != config.client_id) {
var audio = document.querySelector("audio");
if (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) });
/*if (currSide != 'chat') {
$$('toolbar_side').updateItem('chat', {value: 'images/speech-bubble-red.png'});
}*/
if (!$$('winChat').isVisible()) {
$$('btnChat').define('image', 'images/icons8-chat-bubble-32o.png');
$$('btnChat').refresh();
}
if (windowCar) windowCar.Update();
}
}
})
};
// region accept
function on_connect() {
if (isDebug) {
console.debug('SocketIoClient::connect()');
}
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');
_self.socket.emit(WHO.USER.PREFIX + 'join', config.client_key);
$$('top_socket_status').define('css', 'btn_success');
$$('top_socket_status').refresh();
modal_error.hide();
};
function on_disconnect() {
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');
$$('top_socket_status').define('css', 'btn_danger');
$$('top_socket_status').refresh();
modal_error.show();
console.log(this.socket)
};
function on_join(data) {
if (isDebug) {
console.debug('SocketIoClient::join()');
console.debug(data);
}
webix.message({ type: 'success', text: 'SocketIoClient::join()::Соединение установлено.' });
if (httpClient) {
webix.message('SocketIoClient:::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.Show();
map.UpdateData();
}
if (toolBarTop) {
//toolBarTop.UpdateOperatorBalance(1);
//toolBarTop.UpdateOrdersTime(1);
//toolBarTop.ClearPhoneStatus();
}
webix.message('SocketIoClient::Sync::End');
}
};
function on_jparks(data) {
if (isDebug) {
console.debug('SocketIoClient::on_jparks()');
console.debug(data);
}
data.forEach(park => {
park.queue.forEach(c => {
var car = cars.getItem(c.i);
if (car) {
car.Update({
id: c.i,
on: c.on,
lat: c.la,
lng: c.lo,
lifetime: c.t,
park_status: c.s
});
}
})
});
if (sideCars) sideCars.Update();
if (parking) parking.Update();
}
function on_park_move_car(data) {
if (isDebug) {
console.debug('SocketIoClient::park_move_car()');
console.debug(data);
}
if (data && data.car_id > 0) {
var car = cars.getItem(data.car_id);
if (car) {
car.Update(data);
if (sideCars) sideCars.Update();
if (parking) parking.Update();
}
}
}
function on_car_status(data) {
if (isDebug) {
console.debug('SocketIoClient::car_status()');
console.debug(data);
}
if (data && data.id > 0) {
var car = cars.getItem(data.id);
if (car) {
car.Update(data);
} else {
car = new Car(data);
cars.add(car);
}
if (parking) parking.Update();
if (sideCars) sideCars.Update();
}
}
function on_order_update(data) {
//console.debug(data);
if (data && data.id > 0) {
var order = orders.getItem(data.id);
if (order) {
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 });
}
order.ch = car.ch;
order.poz = car.poz;
}
// если отменен выбраный заказ снять выделение на карте
if (order.status == 'cancel' && selected_order != null && selected_order.id == order.id) {
if (map) map.UnselectOrder(order);
}
if (activeOrders) activeOrders.Update();
} else {
order = new Order(data);
orders.add(order, 0);
orders.remove(orders.getLastId());
}
}
};
//endregion accept
this.console_cmd = (cmd) => {
if (isDebug) {
console.debug('SocketIoClient::console_cmd():: ' + cmd);
}
this.socket.emit(WHO.USER.PREFIX + 'console_cmd', cmd);
}
this.park_move_car = (car_id, park_id, park_status, penalty_id, cause) => {
if (isDebug) {
console.debug('SocketIoClient::park_move_car():: ' +
'car_id:' + car_id +
', park_id:' + park_id +
', park_status: ' + park_status +
', penalty_id:' + penalty_id +
', cause:' + cause);
}
if (car_id) {
let car = cars.getItem(car_id);
if (car) {
this.socket.emit(WHO.USER.PREFIX + 'park_move_car', JSON.stringify({
car_id: car.id,
park_id: park_id != null ? park_id : 0,
old_park: car.park,
park_status: park_status != null ? park_status : 1,
penalty_id: penalty_id != null ? penalty_id : 0,
cause: cause != null ? cause : 0,
who_id: config.client_id
}));
}
}
}
this.device_command = (host_id, command, value) => {
if (isDebug) {
console.debug('SocketIoClient::device_command():: ' +
'host_id:' + host_id +
', command:' + command +
', value: ' + value);
}
if (host_id) {
let host = hosts.getItem(host_id);
if (host) {
this.socket.emit(WHO.USER.PREFIX + 'device_command', JSON.stringify({
host_id: host.id,
command: command,
value: value
}));
}
}
}
this.order_create = function (order) {
if (isDebug) console.debug('SocketIoClient::order_create()::', order);
order.who_create = config.who;
order.who_id_create = config.who_id;
order.who = config.who;
order.who_id = config.who_id;
order.city_id = config.city_id;
order.order_id = order.id;
order.j_uslugi = order.uslugi.join('-');
order.city_id = config.city_id;
var post_data = {};
for (var i in order) {
if (i != 'isValid' && i != 'Create' && i != 'Update' && i != 'recive_count') {
post_data[i] = order[i];
}
}
this.socket.emit(WHO.USER.PREFIX + 'order_create', JSON.stringify(post_data));
};
this.order_arrive = function (order_id, car_id) {
let json = JSON.stringify({
order_id: order_id,
car_id: car_id
});
if (isDebug) console.debug('SocketIoClient::order_arrive()::', json);
this.socket.emit(WHO.USER.PREFIX + 'order_arrive', json);
}
this.order_ride = function (order_id, car_id) {
let json = JSON.stringify({
order_id: order_id,
car_id: car_id
});
if (isDebug) console.debug('SocketIoClient::order_ride()::', json);
this.socket.emit(WHO.USER.PREFIX + 'order_ride', json);
}
this.order_complete = function (order_id, cost, distance) {
let json = JSON.stringify({
order_id: order_id,
cost: 0,
distance: 0
});
if (isDebug) console.debug('SocketIoClient::order_complete()::', json);
this.socket.emit(WHO.USER.PREFIX + 'order_complete', json);
}
this.order_cancel = function (obj) {
let json = JSON.stringify(obj);
if (isDebug) console.debug('SocketIoClient::order_cancel()::', json);
this.socket.emit(WHO.USER.PREFIX + 'order_cancel', json);
}
this.order_copy = function (order_id) {
if (isDebug) console.debug('SocketIoClient::order_copy()::', order_id, config.client_id);
this.socket.emit(WHO.USER.PREFIX + 'order_copy', order_id, config.client_id);
}
this.order_send_to_car = function (obj) {
let json = JSON.stringify(obj);
if (isDebug) console.debug('wsClient::order_send_to_car():: ', json);
this.socket.emit(WHO.USER.PREFIX + 'order_send_to_car', json);
}
this.order_send_to_offline_car = function (obj) {
let json = JSON.stringify(obj);
if (isDebug) console.debug('wsClient::order_send_to_offline_car():: ', json);
this.socket.emit(WHO.USER.PREFIX + 'order_send_to_offline_car', json);
}
this.order_send_online = function (obj) {
let json = JSON.stringify(obj);
if (isDebug) console.debug('wsClient::order_send_online()::', json);
this.socket.emit(WHO.USER.PREFIX + 'order_send_online', json);
}
this.order_send_manual = function (obj) {
let json = JSON.stringify(obj);
if (isDebug) console.debug('wsClient::order_send_manual()::', json);
this.socket.emit(WHO.USER.PREFIX + 'order_send_manual', json);
}
this.order_send_other_car = function (order_id) {
let data = {
order_id: order_id,
who: WHO.USER.KEY,
who_id: config.client_id,
cause: 4
}
this.socket.emit(WHO.USER.PREFIX + 'order_send_other_car', JSON.stringify(data));
}
this.call_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 (toolBarTop) toolBarTop.UpdatePhoneStatus(data.p, 'hungup');
}
/*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.call_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.call_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.call_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);
}
}
};
}