/
var
/
www
/
myprog.com.ua
/
server
/
node
/
ws
/
/var/www/myprog.com.ua/server/node/ws
mkdir
upload
Name
Size
Mode
Actions
hosts.js
23377
0644
edit
dl
rm
system.js
5215
0644
edit
dl
rm
users.js
23704
0644
edit
dl
rm
ws_server.js
1937
0644
edit
dl
rm
Edit:
/var/www/myprog.com.ua/server/node/ws/hosts.js
(23377B)
const Session = require('../objects/session'); const User = require('../objects/user'); const Device = require('../objects/device'); const Value = require('../objects/value'); const Event = require('../objects/event'); const Command = require('../objects/command'); const HostConfig = require('../objects/host_config'); const ValueHistory = require('../objects/value_history'); const EventHistory = require('../objects/event_history'); const CommandHistory = require('../objects/command_history'); const Host = require('../objects/host'); const Core = require('../core'); const log = require('../helpers/log'); const nodemailer = require("nodemailer"); module.exports = (WsServer) => { WsServer.prototype.accept_host = function (socket) { let _self = this; socket.on(WHO.HOST.PREFIX + 'join', (json) => { _self.core.log('--- JOIN HOST '+json.sn+' ---'); log.debug('WsServer', 'join_host()', `host = ${json.device}`); this.join_host(socket, json); }); socket.on(WHO.HOST.PREFIX + 'values', (json) => { //console.log('host_values() json ='+ json); //var host_name = 'no session'; var session = _self.core.sessions_manager.get_session_by_socket(socket.client.id); if(session!=null){ json.forEach(value => { value.host_id = session.host.id; this.set_value(socket,value); }); // host_name = session.host.name; } // console.debug(json); // if(json[1]!=undefined){ // _self.core.log(host_name+' Power: '+json[0].v+' Temperature: '+json[1].v+' Humidity: '+json[2].v); // } }); socket.on(WHO.HOST.PREFIX + 'config', (json) => { //console.log('host_config() json ='+ json); //var host_name = 'no session'; var session = _self.core.sessions_manager.get_session_by_socket(socket.client.id); if(session!=null){ this.set_host_config(socket,session.host.id,json); // json.forEach(cfg => { // cfg.host_id = session.host.id; // this.set_host_config(socket,cfg); // }); // host_name = session.host.name; } // console.debug(json); // if(json[1]!=undefined){ // _self.core.log(host_name+' Power: '+json[0].v+' Temperature: '+json[1].v+' Humidity: '+json[2].v); // } }); socket.on(WHO.HOST.PREFIX + 'event', (json) => { var session = _self.core.sessions_manager.get_session_by_socket(socket.client.id); if(session!=null){ // POLIV if (session.host.id == 17) { // MA DVOR // print(json) if (json.type == "relay" && json.n == 5) { if (json.event == 'on') { console.log('------- START POLIV') _self.core.start_poliv(); } else { console.log('------- FINISH POLIV') _self.core.stop_poliv(); } } } // if (session.host.id == 18) { // NEW CODE Sofo // socket.emit("set_code1", "6666"); // socket.emit("write_config", "1"); // } if (json.type=="security") { let transporter = nodemailer.createTransport({ service: 'gmail', auth: { user: 'myprog.com.ua@gmail.com', pass: 'Zedqpl0=77' } }); var sec_text = ""; var full_date = '<b style="font-size:26;">'+ new Date().toMysqlString()+'</b>'; var full_name = '<b style="font-size:36;">'+ session.host.name+'</b>'; var full_ver = '<b style="font-size:20;">'+ "SN:"+session.host.sn+" - v"+session.host.sw+'</b>'; var full_event = '<b style="font-size:76;">'+ json.event+'</b>'; if (full_event=="lock") { full_event = '<b style="font-size:56;color:#7f7;">'+ 'Security ON'+'</b>'; } if (full_event=="unlock") { full_event = '<b style="font-size:56;color:#77f;">'+ 'Security OFF'+'</b>'; } if (full_event=="alert") { full_event = '<b style="font-size:76;color:#f73;">'+ 'Security ALERT!!!'+'</b>'; } if (full_event=="panic") { full_event = '<b style="font-size:76;color:#f33;">'+ 'Security PANIC!!!'+'</b>'; } sec_text = "<html><body><br>"+ full_name+"<br>" + full_event + "<br>" + full_date + "<br>" + full_ver + "</body></html>"; var recepient = 'g0502212339@gmail.com'; if (session.host.id!=14&&session.host.id!=11) { recepient = 'g0502212339@gmail.com, odissey123@gmail.com'; } var mailOptions = { from: 'myprog.com.ua@gmail.com', to: recepient, //alinaiiegovska@gmail.com subject: session.host.name+' - Security '+json.n+' '+json.event, text: "html body", html: sec_text }; transporter.sendMail(mailOptions, function(error, info){ if (error) { console.log(error); } else { console.log('Email sent: ' + info.response); } }); } // ACCEPT SECTION console.log('host_event() session.host.id='+session.host.id); console.log(json); //json.last_date = new Date().toMysqlString(); let ev = []; if(json.button!=undefined){ ev["type"] = "button"; ev["n"] = json.button; } if(json.relay!=undefined){ ev["type"] = "relay"; ev["n"] = json.relay; } if(json.door!=undefined){ ev["type"] = "door"; ev["n"] = json.door; } if(json.lock!=undefined){ ev["type"] = "lock"; ev["n"] = json.lock; } // NEW if(json.type!=undefined){ ev["type"] = json.type; ev["n"] = json.n; } ev["event"] = json.event; //if(session!=undefined&&session.host.id==7&&json.door==2){ // Поломан сенсор, не сохраняем в базу //}else{ this.set_event(socket,session.host.id,ev); //} } // >> HARD CODE if(json.button!=undefined){ if(json.event=='click'){ _self.core.log(session.host.name+' Event: button '+json.button+' '+json.event); if(json.button==1){ socket.emit("relay_on", "1"); } } } if(json.relay!=undefined){ _self.core.log(session.host.name+' Event: relay '+json.relay+' '+json.event); } if(json.door!=undefined){ //_self.core.log(session.host.name+' Event: door '+json.door+' '+json.event); if(session!=undefined&&session.host.id==5){ if(json.door==1 && json.event=="open"){ // _self.core.sessions_manager.send_to_host(2, "relay_off", "4"); } if(json.door==1 && json.event=="close"){ _self.core.sessions_manager.send_to_host(2, "relay_on", "4"); } if(json.door==2 && json.event=="open"){ // _self.core.sessions_manager.send_to_host(2, "relay_off", "3"); } if(json.door==2 && json.event=="close"){ _self.core.sessions_manager.send_to_host(2, "relay_on", "3"); } } if(session!=undefined&&session.host.id==7){ // KALIFEA SENSOR if(json.door==1 && json.event=="open"){ return; // _self.core.sessions_manager.send_to_host(1, "relay_off", "3"); //_self.core.sessions_manager.send_to_host(7, "led_on", "red"); } if(json.door==1 && json.event=="close"){ return; //_self.core.sessions_manager.send_to_host(7, "relay_on", "3"); //_self.core.sessions_manager.send_to_host(7, "led_off", ""); } if(json.door==2 && json.event=="open"){ // _self.core.sessions_manager.send_to_host(1, "relay_off", "4"); //_self.core.sessions_manager.send_to_host(7, "led_on", "red"); } if(json.door==2 && json.event=="close"){ _self.core.sessions_manager.send_to_host(7, "relay_on", "4"); // _self.core.sessions_manager.send_to_host(7, "led_off", ""); } } if(session!=undefined && session.host.id == 18){ // KALIFEA SENSOR if(json.door==2 && json.event=="open"){ // _self.core.sessions_manager.send_to_host(1, "relay_off", "3"); _self.core.sessions_manager.send_to_host(1, "relay_off", "1"); } if(json.door==2 && json.event=="close"){ _self.core.sessions_manager.send_to_host(1, "relay_on", "1"); } } } // <<< HARD CODE //this.join_host(socket, json); }); // socket.on('host_values', (json) => { // console.log("another cmd......"); //this.join_host(socket, json); // }); } WsServer.prototype.join_host = function (socket, json) { let _self = this; _self.core.log('--- JOIN HOST '+json.sn+' ---'); log.debug('WsServer', 'join_host()', `host = ${json.device}`); if (!json) { log.error('WsServer', 'join_host()', global.error.e103); socket.emit(WHO.HOST.PREFIX + 'error', global.error.e103); return; } let session = new Session(); session.key = json.key; session.socket = socket; session.ver = json.version; _self.core.sessions_manager.add_socket(socket.client.id, json.key); _self.core.service_hosts.get_host_by_key(session.key).then((host) => { if (host instanceof Host) { if (host.is_activated()) { //console.debug(json); session.socket.emit("set_time", Math.round(new Date().microtime(true))+60*60*2); // + 2 hour // >>> HARD CODE !!!!!!!!!!!!!!!!!!!!!!!!!!!! if(host.sn=="MPH0001"){ session.socket.emit("relay_timeout", "1500"); session.socket.emit("led_on", "green"); } if(host.sn=="MPH0002"){ session.socket.emit("relay_timeout", "1500"); session.socket.emit("led_on", "green"); } if(host.sn=="HH00013"){ session.socket.emit("relay_off", "1"); session.socket.emit("led_on", "red"); } // if(host.sn=="HH00007"){ // session.socket.emit("security_off", "1"); // session.socket.emit("led_on", "pink"); // } // if(host.sn=="HH00011"){ // session.socket.emit("security_off", "1"); // session.socket.emit("led_on", "pink"); // } //console.debug(json.devices); // <<< HARD CODE !!!!!!!!!!!!!!!!!!!!!!!!!!!! host.socket_id = socket.client.id; host.online = 1; host.sw = json.version; host.hw = json.type; host.sn = json.sn; host.devices = json.devices; host.events = json.events; host.commands = json.commands; host.values = json.values; session.set_host(host); _self.core.service_hosts.update_host(host); _self.core.sessions_manager.add_host(session); _self.core.sessions_manager.add_to_room(WHO.HOST.VALUE, session); session.socket.emit("join_ok"); //session.socket.emit("set_time",new Date().microtime(true)); console.debug(Math.round(new Date().getTime()/1000)); if(json.devices!=undefined){ this.set_devices(socket,host.id,json.devices); } if(json.events!=undefined){ //this.set_events(socket,host.id,json.events); } if(json.commands!=undefined){ this.set_commands(socket,host.id,json.commands); } if(json.values!=undefined){ this.set_values(socket,host.id,json.values); } // >>> HARD CODE !!!!!!!!!!!!!!!!!!!!!!!!!!!! // if(host.id == 18){ // SOFOKLEUS 59 // _self.core.sessions_manager.send_to_host(18, "security_off", "1"); // } // if(host.id == 14){ // SOFOKLEUS 59 // session.socket.emit("security_off", "1"); // } // if(host.id == 17){ // Patision 141 / 1 // _self.core.sessions_manager.send_to_host(17, "security_off", "1"); // } // if(host.id == 12){ // Patision 141 / 2 // _self.core.sessions_manager.send_to_host(12, "security_off", "1"); // } //console.debug(json.devices); // <<< HARD CODE !!!!!!!!!!!!!!!!!!!!!!!!!!!! } else { session.socket.emit("need_activation", "Authorization error"); log.debug('WsServer', 'join_host()',`need activation: ${session.key}`); } } else { session.socket.emit("need_login", "Authorization error"); log.debug('WsServer', 'join_host()',`need login: ${session.key}`); } }); } WsServer.prototype.set_devices = function (socket, host_id, devices) { let _self = this; _self.core.log('--- SET DEVICES ---'); //log.debug('WsServer', 'set_devices()'); devices.forEach(device => { device = new Device(device); device.host_id = host_id; _self.core.service_devices.get_device(device.host_id,device.type,device.n).then((dev) => { if (dev instanceof Device) { device.id = dev.id; device.name = dev.name; device.event_id = dev.event_id; } _self.core.service_devices.update_device(device); }); }); } WsServer.prototype.set_values = function (socket, host_id, values) { let _self = this; _self.core.log('--- SET VALUES ---'); //log.debug('WsServer', 'set_values()'); values.forEach(value => { value = new Value(value); value.host_id = host_id; _self.core.service_values.get_value(value.host_id,value.n).then((val) => { if (val instanceof Value) { value.id = val.id; value.name = val.name; } _self.core.service_values.update_value(value); }); // console.log(device); // if (device instanceof Device) { // } }); } WsServer.prototype.set_host_config = function (socket, host_id, config) { let _self = this; _self.core.log('--- SET CONFIG ---'); //log.debug('WsServer', 'set_host_config()'); try{ console.log("config*************"); console.log(config); config.forEach(cfg => { //console.log(cfg); cfg = new HostConfig(cfg); cfg.host_id = host_id; _self.core.service_host_config.get_config(cfg.host_id,cfg.key).then((val) => { if (val instanceof HostConfig) { cfg.id = val.id; } // console.log(cfg.id); _self.core.service_host_config.update_config(cfg); }); // console.log(device); // if (device instanceof Device) { // } }); }catch(e){ console.log(e); } } WsServer.prototype.set_events = function (socket, host_id, events) { let _self = this; _self.core.log('--- SET EVENTS ---'); //log.debug('WsServer', 'set_events()'); events.forEach(event => { event = new Event(event); event.host_id = host_id; _self.core.service_events.get_event(event.host_id,event.type, event.n, event.event).then((val) => { if (val instanceof Event) { event.id = val.id; } _self.core.service_events.update_event(event); }); // console.log(device); // if (device instanceof Device) { // } }); } WsServer.prototype.set_commands = function (socket, host_id, commands) { let _self = this; _self.core.log('--- SET COMMANDS ---'); //log.debug('WsServer', 'set_commands()'); commands.forEach(command => { command = new Command(command); command.host_id = host_id; _self.core.service_commands.get_command(command.host_id,command.c, command.v).then((val) => { if (val instanceof Command) { command.id = val.id; } _self.core.service_commands.update_command(command); }); // console.log(device); // if (device instanceof Device) { // } }); } WsServer.prototype.set_value = function (socket, value) { let _self = this; //_self.core.log('--- SET VALUE ---'); //log.debug('WsServer', 'set_value()'); value = new Value(value); _self.core.service_values.get_value(value.host_id,value.n).then((val) => { if (val instanceof Value) { val.v = value.v; val.last_date = new Date().toMysqlString(); _self.core.service_values.update_value(val); _self.core.sessions_manager.send_to_all_users(WHO.USER.PREFIX + 'value', JSON.stringify(val)); val.value_id = val.id; val.id = 0; if (val.store=='1') { //console.info("Add value history for val.store="+val.store) _self.core.service_values.update_value_history(new ValueHistory(val)); } } }); } WsServer.prototype.set_event = function (socket, host_id, event) { // Событие let _self = this; //let device_id = 0; event = new Event(event); //event.host_id = host_id; _self.core.service_events.get_event(event.type,event.n,event.event).then((val) => { if (val instanceof Event) { event.id = val.id; _self.core.service_devices.get_device(host_id,event.type,event.n).then((dev) => { if (dev instanceof Device) { dev.event_id = event.id; val.device_id = dev.id; val.last_date = dev.last_date = new Date().toMysqlString(); _self.core.service_devices.update_device(dev); _self.core.service_events.update_event(val); _self.core.sessions_manager.send_to_all_users(WHO.USER.PREFIX + 'event', JSON.stringify(val)); // And into history val.event_id = val.id; val.id = 0; if (val.store=='1') { //console.info("Add event history for val.store="+val.store) _self.core.service_events.update_event_history(new EventHistory(val)); } } }); }else{ // New event //console.info("Add new event for host id="+host_id) _self.core.service_devices.get_device(host_id,event.type,event.n).then((dev) => { if (dev instanceof Device) { event.device_id = dev.id; event.last_date = new Date().toMysqlString(); console.info("Add new event for device id="+event.device_id) _self.core.service_events.update_event(event); } }); } }); } WsServer.prototype.set_command = function (socket, host_id, command) { let _self = this; command = new Command(command); command.host_id = host_id; _self.core.service_commands.get_command(command.host_id,command.c,command.v).then((val) => { if (val instanceof Command) { val.last_date = new Date().toMysqlString(); val.command_id = val.id; val.id = 0; _self.core.service_commands.update_command_history(new CommandHistory(val)); } }); } }
Save
cmd:
run