-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwmp.js
executable file
·192 lines (162 loc) · 5.44 KB
/
wmp.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const net = require('net');
const debug = require('debug')('wmp')
function parseResponseLines(wmpString) {
let lines = wmpString.split('\r\n');
while (lines[lines.length - 1].length == 0) {
lines.pop();
}
let rv = [];
for (let i = 0; i < lines.length; i++) {
rv.push(parseResponseLine(lines[i]))
}
return rv;
}
function parseResponseLine(wmpLine) {
let segments = wmpLine.split(":");
let type = segments[0].split(",")[0];
let rv = {
type: type
};
switch (type) {
case "ACK":
break;
case "ERR":
break;
case "ID":
var parts = segments[1].split(",");
Object.assign(rv, {
"model": parts[0],
"mac": parts[1],
"ip": parts[2],
"protocol": parts[3],
"version": parts[4],
"rssi": parts[5]
});
break;
default:
var parts = segments[1].split(",");
Object.assign(rv, {
"feature": parts[0],
"value": parts[1]
})
break;
}
if (rv.type === "CHN")
if (rv.feature === "AMBTEMP" || rv.feature === "SETPTEMP")
rv.value = rv.value / 10;
return rv;
}
const DISCOVER_PREFIX = "DISCOVER:"
module.exports = {
discover: function (timeout, callback, timedOutCallback) {
let dgram = require('dgram');
let message = Buffer.from("DISCOVER\r\n");
let client = dgram.createSocket("udp4");
client.bind(3310);
client.on("message", function (msg, rinfo) {
debug("server got: " + msg + " from " + rinfo.address + ":" + rinfo.port);
if (msg.indexOf(DISCOVER_PREFIX) === 0) {
let parts = msg.toString().substr(DISCOVER_PREFIX.length).split(",");
callback({
model: parts[0],
mac: parts[1],
ip: parts[2],
protocol: parts[3],
version: parts[4],
rssi: parts[5]
});
}
});
client.on("listening", function () {
let address = client.address();
debug("server listening " + address.address + ":" + address.port);
client.setBroadcast(true);
client.send(message, 0, message.length, 3310, "255.255.255.255");
});
setTimeout(function () {
client.close();
if(timedOutCallback) {
timedOutCallback();
}
}, timeout);
},
connect: function (ip) /* Promise(mac) */ {
let nextCallback = null;
let client = new net.Socket();
let mac;
client.on('data', function (data) {
let wmpdata = parseResponseLines(data.toString())
for (let i = 0; i < wmpdata.length; i++) {
if (wmpdata[i].type != "CHN") {
if (nextCallback === null) {
console.error("Received message without callback: " + wmpdata[i])
} else {
nextCallback(wmpdata[i]);
nextCallback = null;
}
}
}
});
let on = function (event, callback) {
if (event == "update") {
client.on('data', function (data) {
var wmpdata = parseResponseLines(data.toString())
for (let i = 0; i < wmpdata.length; i++) {
if (wmpdata[i].type == "CHN") {
callback(wmpdata[i]);
}
}
});
} else if (event == 'close') {
client.on('close', callback);
}
};
let id = function () {
return sendCmd('ID')
};
let info = function () {
return sendCmd('INFO')
};
let get = function (feature) {
//todo: sanitise feature param
sendCmd("GET,1:" + feature);
};
let set = function (feature, value) {
//todo: sanitise feature & value params
//convert decimal to 10x temp numbers
if (feature.toUpperCase() == "SETPTEMP")
value = value * 10;
sendCmd("SET,1:" + feature + "," + value).then(function (data) {
if (data.type != "ACK")
console.error("Received non-ack message from set command: " + JSON.stringify(data))
});
};
let sendCmd = function (cmd) {
return new Promise(function (resolve, reject) {
nextCallback = resolve;
client.write(cmd + '\n');
})
};
//reconnect on close
client.on('close', function (e) {
client.setTimeout(5000, function () {
client.connect(3310, ip);
})
});
return new Promise(function (resolve, reject) {
client.connect(3310, ip, function () {
id().then(function (data) {
mac = data.mac;
resolve({
on: on,
id: id,
info: info,
get: get,
set: set,
mac: mac
});
})
});
});
}
}