-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
280 lines (243 loc) · 6.51 KB
/
index.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
/**
* Z-Way WebSocket Bridge
* (c) 2020 by Arjan Haverkamp ([email protected])
* Inspired by https://github.com/blakeblackshear/zway-websocket
*/
function WebSocketBridge(id, controller) {
WebSocketBridge.super_.call(this, id, controller);
}
inherits(WebSocketBridge, BaseModule);
_module = WebSocketBridge;
/**
* Initialize this module.
* Start a listener to listen for changes in metrics on all devices.
*/
WebSocketBridge.prototype.init = function(config) {
WebSocketBridge.super_.prototype.init.call(this, config);
var self = this;
this.verbose = false; // Set to true for more debug info in z-way-server.log
this.wsServer = config.wsServer;
this.sock = null;
this.connecting = false;
this.connected = false;
// Device-change handler:
self.handlerModify = function(vDev) {
self.sendMessage('deviceChange', self._getData(vDev));
};
self.handlerChange = function(vDev) {
if (!self.connected) {
self.connectToServer();
}
}
self.connectToServer(); // Initial socket connect
self.controller.devices.on('modify:metrics:level', self.handlerModify);
self.controller.devices.on('change:metrics:level', self.handlerChange);
};
/**
* Connect to the socket server.
* Prevent race conditions via `self.connecting`.
*/
WebSocketBridge.prototype.connectToServer = function() {
var self = this;
if (self.connecting) {
return;
}
self.connecting = true;
try {
self.sock = new sockets.websocket(self.wsServer);
self.sock.onopen = function() {
self.connected = true;
self.verbose && self.log('Socket: Connected');
// Send status of all devices when connected
self.sendMessage('allDevices', self.getAllDevices());
setTimeout(function() {
self.connecting = false;
}, 100)
}
self.sock.onmessage = function(ev) {
self.verbose && self.log('Socket: Received Message: ' + ev.data);
self.processMessage(ev.data);
}
self.sock.onclose = function(ev) {
self.verbose && self.log('Socket: Connection closed');
self.connected = false;
}
self.sock.onerror = function(ev) {
self.verbose && self.log('Socket: Error: ' + ev.data);
self.connected = false;
self.connecting = false;
}
} catch(err) {
self.error(err);
setTimeout(function() {
self.connecting = false;
}, 100)
}
}
/**
* Send a message to the websocket server.
* If not connected, connect first.
*/
WebSocketBridge.prototype.sendMessage = function(msgType, data) {
if (!this.connected) {
// (Re)Connect:
this.connectToServer();
}
var json = {
type: msgType,
data: data
};
this.connected && this.sock.send(JSON.stringify(json));
}
/**
* Process an incoming message.
* The message should be in JSON format, otherwise we don't accept.
* Example message:
* {
* 'socketCommand': 'setDevice',
* 'vDevId': 'ZWayVDev_zway_30-0-38',
* 'onoff': 'on', // 'on', 'off', ...
* 'level' 55
* }
* - or -
* {
* 'socketCommand': 'getAll'
* }
*/
WebSocketBridge.prototype.processMessage = function(message) {
var self = this, json;
try {
json = JSON.parse(message);
} catch(err) {
self.log('Unable to parse message: ' + err);
return;
}
switch(json.socketCommand) {
case 'setDevice':
self.setDevice(json.vDevId, json.command, json.extra || {});
break;
case 'getAll':
self.sendMessage('allDevices', self.getAllDevices());
break;
}
}
/**
* Change the level of a device.
*
* @param id(string): vDevID (f.e: ZWayVDev_zway_30-0-38)
* @param command(string): Either 'on', 'off'
* @param extra(object): {
* level: 50,
* r: 10,
* g: 20,
* b: 30
* }
*/
WebSocketBridge.prototype.setDevice = function(id, command, extra) {
var vDev = this.controller.devices.get(id);
if (!vDev) {
this.error('Device ' + id + ' does not exist.');
return;
}
var deviceType = vDev.get('deviceType');
if (/sensor/i.test(deviceType)) {
this.error('Can\'t perform action on sensor ' + device.get('metrics:title'));
return;
}
switch(deviceType) {
case 'switchMultilevel':
case 'thermostat':
if ('level' in extra) {
vDev.performCommand('exact', { level: extra.level });
}
else {
vDev.performCommand(command);
}
break;
case 'switchRGBW':
if ('r' in extra && 'g' in extra && 'b' in extra) {
vDev.performCommand('exact', {
red: extra.r,
green: extra.g,
blue: extra.b,
});
}
else {
vDev.performCommand(command);
}
break;
case 'switchControl':
if ('level' in extra) {
if (/(upstart|upstop|downstart|downstop)/.test(extra.level)) {
vDev.performCommand('exact', { change: extra.level });
}
else {
vDev.performCommand('exact', { level: extra.level });
}
}
else {
vDev.performCommand(command);
}
break;
case 'toggleButton':
vDev.performCommand('on');
break;
default:
// Includes: switchBinary, doorlock
vDev.performCommand(command);
break;
}
}
/**
* Retrieve the on/off/level states of all available devices.
*/
WebSocketBridge.prototype.getAllDevices = function() {
var self = this, json = {};
self.controller.devices.each(function(vDev) {
var id = vDev.get('id');
if (/^ZWayVDev/.test(id)) {
json[id] = self._getData(vDev);
}
});
return json;
}
/**
* Construct a JSON object containing device info.
*/
WebSocketBridge.prototype._getData = function(vDev) {
function convertLevel() {
var metric = vDev.get('metrics:level'), onoff = 'off', level;
var lastLevel = vDev.get('metrics:lastLevel');
if (/^\-?\d+(\.\d+)?$/.test(metric)) {
// metric is an integer
level = +metric;
onoff = (metric == 0) ? 'off' : 'on';
}
else {
// metric == 'on', 'off', ...
level = (/off|close/.test(metric)) ? 0 : 100;
onoff = metric;
}
return {onoff:onoff, level:level};
}
var metric = convertLevel();
return {
vDevId: vDev.get('id'),
onoff: metric.onoff,
level: metric.level,
lastLevel: vDev.get('metrics:lastLevel'),
name: vDev.get('name'),
title: vDev.get('metrics:title'),
type: vDev.get('deviceType'),
modificationTime: vDev.get('metrics:modificationTime')
};
}
/**
* Stop this module
*/
WebSocketBridge.prototype.stop = function() {
this.sock && this.sock.close();
this.controller.devices.off('modify:metrics:level', this.handlerModify);
this.controller.devices.off('change:metrics:level', this.handlerChange);
WebSocketBridge.super_.prototype.stop.call(this);
};