-
Notifications
You must be signed in to change notification settings - Fork 6
/
switch.js
55 lines (45 loc) · 1.57 KB
/
switch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* Use this to switch socket from command line like:
* node switch.js IP PIN 0/1 [Index]
*/
const util = require('util');
const WebSocketClient = require('./index');
if (process.argv.length < 5) {
console.log('Too few parameters: ' + JSON.stringify(process.argv));
console.log('Supply TELNET as PIN to get token from telnet programatically.');
console.log('Usage: node switch.js IP PIN 0/1 [index] - index defaults to 0');
process.exit(-1);
}
const ip = process.argv[2];
const pin = process.argv[3];
const value = Number.parseInt(process.argv[4], 10);
const index = Number.parseInt(process.argv[5], 10) || 0;
async function main() {
const client = new WebSocketClient({
ip: ip,
pin: pin,
useTelnetForToken: pin === 'TELNET',
log: console.log
});
client.on('switched', (newState, socket) => {
console.log(`Socket ${socket} switched to ${newState}`);
});
await client.login();
console.log('Signed in!');
console.log('Getting state');
let state = await client.state(index);
if (state instanceof Array) {
console.log('Socket states: ', state);
} else {
console.log('Socket is ' + (state ? 'on' : 'off'));
}
//await client.switchLED(value === 1, index);
//console.log(util.inspect(result, {showHidden: false, depth: null, colors: true}));
const newValue = await client.switch(value === 1, index);
console.log('Socket now is ' + (newValue ? 'on' : 'off'));
process.exit(0);
}
main().catch((e) => {
console.error('Had error:', e);
process.exit(-1);
});