Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // console.log(process.argv);
- var port = parseInt(process.argv[2]);
- if ( !port || isNaN(port) ) {
- console.log('\nPort arg must be int.\n\n');
- process.exit();
- }
- var status = [0, 0, 0, 0, 0, 0, 0, 0];
- require('net').createServer(function(socket) {
- console.log('\nNew connection...');
- socket.setEncoding('utf8');
- function writeStatus() {
- var dec = 0;
- status.forEach(function(on, i) {
- dec += on * Math.pow(2, i);
- });
- socket.write(String.fromCharCode(dec));
- }
- socket.on('data', function(data) {
- var cmd = data.charCodeAt(0);
- console.log('Incoming data...', cmd);
- if ( cmd == 91 ) {
- // get status
- writeStatus();
- }
- else if ( cmd == 100 ) {
- // all on
- status.forEach(function(on, i) {
- status[i] = 1;
- });
- writeStatus();
- }
- else if ( cmd >= 101 && cmd <= 108 ) {
- // one on
- var relayIndex = cmd - 100 - 1;
- status[relayIndex] = 1;
- writeStatus();
- }
- else if ( cmd == 110 ) {
- // all off
- status.forEach(function(on, i) {
- status[i] = 0;
- });
- writeStatus();
- }
- else if ( cmd >= 111 && cmd <= 118 ) {
- // one off
- var relayIndex = cmd - 110 - 1;
- status[relayIndex] = 0;
- writeStatus();
- }
- console.log('New status:', status.join(''));
- });
- }).listen(port);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement