-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmyhome-utils.js
53 lines (44 loc) · 1.1 KB
/
myhome-utils.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
var exports = module.exports = {}
exports.execute_command = function(command, config, success, error) {
let net = require('net');
const ACK = '*#*1##'
const NACK = '*#*0##'
const START_COMMAND = '*99*9##'
var client = new net.Socket(),
state = 'disconnected'
client.on('error', function() {
console.error("Command socket error")
state = 'disconnected'
client.destroy();
error(command);
})
client.connect(config.port, config.host, function() {
// opening command session
})
client.on('data', function(data) {
var sdata = data.toString()
if(sdata === NACK) {
// calling callback
error(sdata, command)
} else {
if(state == 'disconnected') {
state = 'logged-in'
client.write(START_COMMAND)
return
}
if(state == 'logged-in') {
state = 'open'
client.write(command)
return
}
// calling callback
success(sdata, command)
}
client.destroy()
return
})
client.on('close', function() {
// to verify that no connections are left open
return
})
}