-
Notifications
You must be signed in to change notification settings - Fork 7
/
sonoff_server.js
executable file
·401 lines (331 loc) · 15.9 KB
/
sonoff_server.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
module.exports.createServer = function (config) {
const ws = require("nodejs-websocket");
const log = require('./helpers/logger');
//set initialized parameters
var state = {
knownDevices: [],
connections: [],
listeners: {
onDeviceConnectedListeners: [],
onDeviceDisconnectedListeners: [],
onDeviceUpdatedListeners: []
}
};
// Find the device in known devices.
state.getDeviceById = (deviceId) => {
return state.knownDevices.find(d => d.id == deviceId);
};
state.getDeviceByParentId = (deviceId) => {
return state.knownDevices.find(d => d.parentId == deviceId);
};
state.updateKnownDevice = (device) => {
var updated = false;
for (var i = 0; i < state.knownDevices.length; i++) {
if (state.knownDevices[i].id == device.id) {
state.knownDevices[i] = device;
updated = true;
callDeviceListeners(state.listeners.onDeviceUpdatedListeners, device);
}
}
if (!updated) {
state.knownDevices.push(device);
callDeviceListeners(state.listeners.onDeviceConnectedListeners, device);
}
};
function callDeviceListeners(listeners, device) {
const deviceListeners = listeners[device.id];
if (!deviceListeners)
return;
deviceListeners.forEach(listener => listener(device.state));
}
function addDeviceListener(listeners, deviceId, listener) {
if (!listeners[deviceId]) {
listeners[deviceId] = [listener];
} else {
listeners[deviceId].push(listener);
}
}
state.pushMessage = a => {
var request = {
"apikey": "111111111-1111-1111-1111-111111111111",
"action": a.action,
"deviceid": a.target,
"params": a.value,
"userAgent": "app",
"sequence": Date.now().toString(),
"ts": 0,
"from": "app"
};
var r = JSON.stringify(request);
log.log('REQ | WS | APP | ' + r);
var device = state.getDeviceById(a.device);
if (!device.messages) device.messages = [];
device.messages.push(request);
device.connection.conn.sendText(r);
};
function addConnection(connection) {
var conn = connection.conn;
var connId = conn.socket.remoteAddress + ':' + conn.socket.remotePort;
connection.isAlive = true;
state.connections[connId] = connection;
log.log(`WS | addConnection |`);
console.log(config.IS_ALIVE_CHECK_ENABLED);
if (config.IS_ALIVE_CHECK_ENABLED === "ON") {
connection.isAliveIntervalId = setInterval(() => {
if (connection.conn.readyState == connection.conn.CONNECTING) return;
if (!connection.isAlive) {
clearInterval(connection.isAliveIntervalId);
return connection.conn.close(408, "connection timed out");
}
connection.isAlive = false;
conn.sendPing();
log.log(`WS | sendPing |`);
}, config.IS_ALIVE_CHECK_INTERVAL);
connection.conn.on("pong", () => {
connection.isAlive = true;
log.log(`WS | pong |`);
});
}
}
// ----------- sonoff server ------------------------
// setup a server, that will respond to the SONOFF requests
// this is the replacement for the SONOFF cloud!
var wsOptions = {
secure: true,
key: config.ssl_key,
cert: config.ssl_cert,
};
const wsServer = ws.createServer(wsOptions, function (conn) {
log.log("WS | Server is up %s:%s to %s:%s", config.SERVER_IP, config.WEBSOCKET_PORT, conn.socket.remoteAddress, conn.socket.remotePort);
conn.on("text", function (str) {
var data = JSON.parse(str);
log.log('REQ | WS | DEV | %s', JSON.stringify(data));
res = {
"error": 0,
"deviceid": data.deviceid,
// "apikey": "123456789-1234-1234-1234-123456789123"
"apikey": "111111111-1111-1111-1111-111111111111"
};
if (data.action) {
switch (data.action) {
case 'date':
res.date = new Date().toISOString();
break;
case 'query':
//device wants information
var device = state.getDeviceById(data.deviceid);
if (!device) {
device = state.getDeviceByParentId(data.deviceid);
if (!device) {
log.error('ERR | WS | Unknown device ', data.deviceid);
break;
}
}
/*if(data.params.includes('timers')){
log.log('INFO | WS | Device %s asks for timers',device.id);
if(device.timers){
res.params = [{timers : device.timers}];
}
}*/
res.params = {};
data.params.forEach(p => {
res.params[p] = device[p];
});
break;
case 'update':
// device wants to update its state
if (typeof data.params.switches == 'undefined') {
// Single switch
var device = state.getDeviceById(data.deviceid);
if (!device) {
log.error('ERR | WS | Unknown device ', data.deviceid);
} else {
device.state = data.params.switch;
device.rawMessageLastUpdate = data;
device.rawMessageLastUpdate.timestamp = Date.now();
log.log('WS | DeviceUpdated device ', data);
state.updateKnownDevice(device);
}
} else {
// Multiple switches, look for parent
var device = state.getDeviceByParentId(data.deviceid);
if (!device) {
log.error('ERR | WS | Unknown device ', data.deviceid);
} else {
for (i = 0; i < data.params.switches.length; i++) {
var device = state.getDeviceById(data.deviceid + '-' + i);
device.state = data.params.switches[i].switch;
device.rawMessageLastUpdate = data;
device.rawMessageLastUpdate.timestamp = Date.now();
state.updateKnownDevice(device);
}
}
}
break;
case 'register':
var connection = {
conn: conn,
devices: []
}
if (data.model == 'PSF-B04-GL') {
//register for devices appending the outlet to the deviceId
for (i = 0; i < 4; i++) {
var device = {
id: data.deviceid + '-' + i,
parentId: data.deviceid,
outlet: i
};
device.version = data.romVersion;
device.model = data.model;
device.connection = connection;
device.rawMessageRegister = data;
device.rawMessageRegister.timestamp = Date.now();
connection.devices.push(device);
state.updateKnownDevice(device);
log.log('INFO | WS | Device %s registered', device.id);
log.log(`WS | deviceInfo | ${data}`);
log.log(data);
}
//All devices share connection
addConnection(connection);
} else {
var device = {
id: data.deviceid
};
device.version = data.romVersion;
device.model = data.model;
device.connection = connection;
device.rawMessageRegister = data;
device.rawMessageRegister.timestamp = Date.now();
connection.devices.push(device);
addConnection(connection);
state.updateKnownDevice(device);
log.log('INFO | WS | Device %s registered', device.id);
}
break;
default: log.error('TODO | Unknown action "%s"', data.action); break;
}
} else {
if (data.sequence && data.deviceid) {
var device = state.getDeviceById(data.deviceid);
if (!device) {
// Look for parent
device = state.getDeviceByParentId(data.deviceid);
if (!device) {
log.error('ERR | WS | Unknown device ', data.deviceid);
} else {
// Look for message
for (i = 0; i < 4; i++) {
device = state.getDeviceById(data.deviceid + '-' + i);
if (device.messages) {
var message = device.messages.find(item => item.sequence == data.sequence);
if (message) {
device.messages = device.messages.filter(function (item) {
return item !== message;
})
device.state = message.params.switches[0].switch;
state.updateKnownDevice(device);
log.log('INFO | WS | APP | action has been accnowlaged by the device ' + JSON.stringify(data));
break;;
}
}
}
}
} else {
if (device.messages) {
var message = device.messages.find(item => item.sequence == data.sequence);
if (message) {
device.messages = device.messages.filter(function (item) {
return item !== message;
})
device.state = message.params.switch;
state.updateKnownDevice(device);
log.log('INFO | WS | APP | action has been acknowledged by the device ' + JSON.stringify(data));
} else {
log.error('ERR | WS | No message send, but received an anser', JSON.stringify(data));
}
} else {
log.error('ERR | WS | No message send, but received an anser', JSON.stringify(data));
}
}
} else {
log.error('TODO | WS | Not data action frame\n' + JSON.stringify(data));
}
}
var r = JSON.stringify(res);
log.log('RES | WS | DEV | ' + r);
conn.sendText(r);
});
conn.on("close", function (code, reason) {
var connId = conn.socket.remoteAddress + ':' + conn.socket.remotePort;
state.connections[connId].devices.forEach((device, index) => {
log.log("Device %s disconnected", device.id);
callDeviceListeners(state.listeners.onDeviceDisconnectedListeners, device);
device.connnection = undefined;
});
clearInterval(state.connections[connId].isAliveIntervalId);
delete state.connections[connId];
});
conn.on("error", function (error) {
log.error("Connection error: ", error);
});
}).listen(config.WEBSOCKET_PORT);
return {
//currently all known devices are returned with a hint if they are currently connected
getConnectedDevices: () => {
return state.knownDevices.map(x => {
return { id: x.id, state: x.state, parentId: x.parentId, outlet: x.outlet, model: x.model, kind: x.kind, version: x.version, isConnected: (typeof x.connection !== 'undefined'), isAlive: x.connection.isAlive, rawMessageRegister: x.rawMessageRegister, rawMessageLastUpdate: x.rawMessageLastUpdate }
});
},
getDeviceState: (deviceId) => {
var d = state.getDeviceById(deviceId);
if (!d || (typeof d.connection == 'undefined')) return "disconnected";
return d.state;
},
turnOnDevice: (deviceId) => {
var d = state.getDeviceById(deviceId);
if (!d || (typeof d.connection == 'undefined')) return "disconnected";
if (typeof d.outlet == 'undefined') {
state.pushMessage({ action: 'update', value: { switch: "on" }, target: deviceId, device: deviceId });
} else {
state.pushMessage({ action: 'update', value: { switches: [{ switch: "on", outlet: Number(d.outlet) }]}, target: d.parentId, device: deviceId });
}
return "on";
},
turnOffDevice: (deviceId) => {
var d = state.getDeviceById(deviceId);
if (!d || (typeof d.connection == 'undefined')) return "disconnected";
if (typeof d.outlet == 'undefined') {
state.pushMessage({ action: 'update', value: { switch: "off" }, target: deviceId, device: deviceId });
} else {
state.pushMessage({ action: 'update', value: { switches: [{ switch: "off", outlet: Number(d.outlet) }]}, target: d.parentId, device: deviceId });
}
return "off";
},
registerOnDeviceConnectedListener: (deviceId, listener) => {
addDeviceListener(state.listeners.onDeviceConnectedListeners, deviceId, listener);
},
registerOnDeviceDisconnectedListener: (deviceId, listener) => {
addDeviceListener(state.listeners.onDeviceDisconnectedListeners, deviceId, listener);
},
registerOnDeviceUpdatedListener: (deviceId, listener) => {
addDeviceListener(state.listeners.onDeviceUpdatedListeners, deviceId, listener);
},
close: () => {
log.log("Stopping server");
for(key in state.connections) {
var connection = state.connections[key];
connection.conn.socket.setTimeout(100, function() {
if(connection) {
connection.conn.socket.destroy();
}
});
connection.conn.close();
}
wsServer.close(function () {
log.log('WS Server stopped');
});
log.log("Stopped server");
}
}
}