Exemplo n.º 1
0
process.stdin.on('keypress', function (chunk, key) {
    if (key && key.name === 'u') {
        // kick for update
        Client.update();
        wsServer.shutDown();
        console.log('Gracefully shut down server with 5s update-refresh message sent. Exiting.');
        process.exit();
    } else if (key && key.ctrl && key.name === 'c') {
        process.exit();
    }
});
Exemplo n.º 2
0
process.stdin.on('keypress', function (chunk, key) {
    if (key && key.name === 'u') {
        User.forEach(function (iterUser) {
            // kick for update
            iterUser.kick('update');
            console.log('Update-kicked ' + iterUser.nick);
        });
        wsServer.shutDown();
        console.log('Gracefully shut down server. Exiting.');
        process.exit();
    } else if (key && key.ctrl && key.name === 'c') {
        process.exit();
    }
});
Exemplo n.º 3
0
	connection.on('message', function(dgram){

		var msg = dgram.utf8Data.split(':');
		var cmd = msg[0];
		var uid = parseInt(msg[1]);
		var value = parseInt(msg[2]);

		var uid_high = (uid & 0xFF00) >> 8;
		var uid_low = (uid & 0x00FF);
		var value_high = (value & 0xFF00) >> 8;
		var value_low = (value & 0x00FF);

		if (cmd == 'stop') {
      stop();
      
		} else if (cmd == 'go') {
      go();
      
		} else if (cmd == 'setSpeed') {
      sendDatagram([0, LOCO_SPEED, 3, 0, 6, 0, 0, uid_high, uid_low, value_high, value_low, 0, 0]);
      
		} else if (cmd == 'getSpeed') {
			sendDatagram([0, LOCO_SPEED, 3, 0, 4, 0, 0, uid_high, uid_low, 0, 0, 0, 0]);
    
    } else if (cmd == 'lokNothalt') {
			sendDatagram([0, SYSTEM_CMD, 3, 0, 5, 0, 0, uid_high, uid_low, SYS_LOCO_EMERGENCY_STOP, 0, 0, 0]);
    
    } else if (cmd == 'setFn') {
			sendDatagram([0, LOCO_FN, 3, 0, 6, 0, 0, uid_high, uid_low, value_high, value_low, 0, 0]);
    
    } else if (cmd == 'getFn') {
			sendDatagram([0, LOCO_FN, 3, 0, 5, 0, 0, uid_high, uid_low, value, 0, 0, 0]);
    
    } else if (cmd == 'toggleDir') {
			sendDatagram([0, LOCO_DIR, 3, 0, 5, 0, 0, uid_high, uid_low, 3, 0, 0, 0]);
			sendDatagram([0, LOCO_DIR, 3, 0, 4, 0, 0, uid_high, uid_low, 0, 0, 0, 0]);
    
    } else if (cmd == 'getDir') {
			sendDatagram([0, LOCO_DIR, 3, 0, 4, 0, 0, uid_high, uid_low, 0, 0, 0, 0]);
    
    } else if (cmd == 'getStatus') {
      getStatus(msg[1], msg[2]);
    
    } else if (cmd == 'setProtocol') {
      setProtocol(msg[1]);
    
    } else if (cmd == 'getProtocol') {
      let config = require('./config.json');
      for (let i in clients){
        clients[i].sendUTF(`updateProtocol:${config.protocol}`);
      }
    
    } else if (cmd == 'restart') {
      stop();
      wsServer.shutDown();
      exec('reboot -f -d 1');
    
    } else if (cmd == 'shutdown') {
      stop();
      wsServer.shutDown();
      exec('poweroff -f -d 1');
    
    } else if (cmd == 'downloadIcon') {
      let link = "";
      for (let i = 1; i < msg.length; i++){
        if(i > 1) link += ':';
        link += msg[i];
      }
      exec('wget -c -P ' + local_config.server_path + '/html/loco_icons/ ' + link, console.log('Downloading ' + link));

    } else if (cmd == "addLoco" || cmd == "updateLoco") {
      addLocoFromMsg(dgram.utf8Data);
    
    } else if (cmd == "mfxDiscovery") {
      let last_power = power;
      stop();
      mfxDiscovery();
      if (last_power){
        setTimeout(function(){
          go();
        }, 500)
      }
    
    } else if (cmd == 'deleteLoco') {
      deleteLoco(msg[1]);
    
    } else if (cmd == 'clearDeviceList') {
      delete require.cache[require.resolve('../html/config/devices.json')];
      clearDeviceList();

    } else if (cmd == 'getVersion') {
      for (let i in clients){
        clients[i].sendUTF(`updateVersion:${version}`);
      }
    
    } else if (cmd == 'setConfigValue') {
      sendDatagram([0x00, SYSTEM_CMD, 0x03, 0x00, 0x08, (msg[1] & 0xff000000)>> 24, (msg[1] & 0x00ff0000)>> 16, (msg[1] & 0x0000ff00) >> 8, msg[1] & 0x000000ff, SYS_STATUS, msg[2], (msg[3] & 0xff00) >> 8, msg[3] & 0x00ff]);
      let devices = JSON.parse(fs.readFileSync('../html/config/devices.json'));

      for (let i = 0; i < devices.length; i++) {
        if (msg[1] == devices[i].uid) {
          if (devices[i].config_chanels_info[msg[2]-1].type == 1) {
            devices[i].config_chanels_info[msg[2]-1].def_option = msg[3];
          } else {
            devices[i].config_chanels_info[msg[2]-1].def_value = msg[3];
          }
          break;
        }
      }

      fs.writeFile('../html/config/devices.json', JSON.stringify(devices, null, 2), console.log("updating devices entry."))
    }
	});
Exemplo n.º 4
0
process.on('exit', () => {
  ws.shutDown();
});