-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathcallbacks.cc
More file actions
486 lines (463 loc) · 17.2 KB
/
callbacks.cc
File metadata and controls
486 lines (463 loc) · 17.2 KB
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
* Copyright (c) 2013 Jonathan Perkin <jonathan@perkin.org.uk>
* Copyright (c) 2015-2017 Elias Karakoulakis <elias.karakoulakis@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "openzwave.hpp"
using namespace v8;
using namespace node;
using namespace OpenZWave;
namespace OZW {
//
uv_async_t async;
//
Nan::Callback *emit_cb;
// Message passing queue between OpenZWave callback and v8 async handler.
mutex zqueue_mutex;
std::queue<NotifInfo *> zqueue;
// Node state.
mutex znodes_mutex;
std::map<uint8_t, NodeInfo *> znodes;
mutex zscenes_mutex;
std::list<SceneInfo *> zscenes;
/*
* OpenZWave callback, registered in Driver::AddWatcher.
* Just push onto queue and trigger the handler in v8 land.
*/
// ===================================================================
void ozw_watcher_callback(OpenZWave::Notification const *cb, void *ctx)
// ===================================================================
{
NotifInfo *notif = new NotifInfo();
notif->type = cb->GetType();
notif->homeid = cb->GetHomeId();
notif->nodeid = cb->GetNodeId();
// only valueId-related callbacks carry an actual OZW ValueID
if (notif->type <= OpenZWave::Notification::Type_ValueRefreshed) {
notif->values.push_front(cb->GetValueID());
}
notif->help = getNotifHelpMsg(cb);
/*
* Some values are only set on particular notifications, and
* assertions in openzwave prevent us from trying to fetch them
* unconditionally.
*/
switch (notif->type) {
case OpenZWave::Notification::Type_Group:
notif->groupidx = cb->GetGroupIdx();
break;
case OpenZWave::Notification::Type_NodeEvent:
notif->event = cb->GetEvent();
break;
case OpenZWave::Notification::Type_CreateButton:
case OpenZWave::Notification::Type_DeleteButton:
case OpenZWave::Notification::Type_ButtonOn:
case OpenZWave::Notification::Type_ButtonOff:
notif->buttonid = cb->GetButtonId();
break;
case OpenZWave::Notification::Type_SceneEvent:
notif->sceneid = cb->GetSceneId();
break;
case OpenZWave::Notification::Type_Notification:
notif->notification = cb->GetNotification();
break;
#if OPENZWAVE_SECURITY == 1
case OpenZWave::Notification::Type_ControllerCommand:
notif->event = cb->GetEvent();
notif->notification = cb->GetNotification();
break;
#endif
}
{
mutex::scoped_lock sl(zqueue_mutex);
zqueue.push(notif);
}
uv_async_send(&async);
}
// ##### LEGACY MODE ###### //
#if OPENZWAVE_SECURITY != 1 //
// ######################## //
/*
* OpenZWave callback, registered in Manager::BeginControllerCommand.
* Just push onto queue and trigger the handler in v8 land.
*/
// ===================================================================
void ozw_ctrlcmd_callback(Driver::ControllerState _state,
Driver::ControllerError _err, void *context)
// ===================================================================
{
NotifInfo *notif = new NotifInfo();
notif->event = _err;
notif->notification = _state;
notif->homeid = 0; // use as guard value for legacy mode
notif->help = std::string("Controller State: ")
.append(getControllerStateAsStr(_state))
.append(", Error: ")
.append(getControllerErrorAsStr(_err));
{
mutex::scoped_lock sl(zqueue_mutex);
zqueue.push(notif);
}
uv_async_send(&async);
}
// ===================================================================
void handleControllerCommand(NotifInfo *notif)
// ===================================================================
{
Nan::HandleScope scope;
Local<v8::Value> info[16];
info[0] = Nan::New<String>("controller command").ToLocalChecked();
info[1] = Nan::New<Integer>(notif->nodeid);
info[2] = Nan::New<Integer>(notif->event); // Driver::ControllerCommand
info[3] = Nan::New<Integer>(notif->notification); // Driver::ControllerCommand
info[4] = Nan::New<String>(notif->help.c_str()).ToLocalChecked();
emit_cb->Call(5, info);
}
// ##### END OF LEGACY MODE ###### //
#endif
/*
* handle normal OpenZWave notifications
*/
// ===================================================================
void handleNotification(NotifInfo *notif)
// ===================================================================
{
Nan::HandleScope scope;
Local<v8::Value> emitinfo[16];
Local<Object> emitobj = Nan::New<Object>();
Local<Object> cbinfo = Nan::New<Object>();
AddIntegerProp(emitobj, homeid, notif->homeid);
//
NodeInfo *node;
//
switch (notif->type) {
// ################
case OpenZWave::Notification::Type_ValueAdded: {
// ################
OpenZWave::ValueID value = notif->values.front();
Local<Object> valobj = zwaveValue2v8Value(value);
if ((node = get_node_info(notif->nodeid))) {
mutex::scoped_lock sl(znodes_mutex);
node->values.push_back(value);
}
emitinfo[0] = Nan::New<String>("value added").ToLocalChecked();
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
emitinfo[2] = Nan::New<Integer>(value.GetCommandClassId());
emitinfo[3] = valobj;
emit_cb->Call(4, emitinfo);
break;
}
// ##################
case OpenZWave::Notification::Type_ValueRemoved: {
// ##################
OpenZWave::ValueID value = notif->values.front();
std::list<OpenZWave::ValueID>::iterator vit;
if ((node = get_node_info(notif->nodeid))) {
for (vit = node->values.begin(); vit != node->values.end(); ++vit) {
if ((*vit) == notif->values.front()) {
node->values.erase(vit);
break;
}
}
}
emitinfo[0] = Nan::New<String>("value removed").ToLocalChecked();
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
emitinfo[2] = Nan::New<Integer>(value.GetCommandClassId());
emitinfo[3] = Nan::New<Integer>(value.GetInstance());
emitinfo[4] = Nan::New<Integer>(value.GetIndex());
emit_cb->Call(5, emitinfo);
break;
}
// ##################
case OpenZWave::Notification::Type_ValueChanged: {
// ##################
OpenZWave::ValueID value = notif->values.front();
Local<Object> valobj = zwaveValue2v8Value(value);
emitinfo[0] = Nan::New<String>("value changed").ToLocalChecked();
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
emitinfo[2] = Nan::New<Integer>(value.GetCommandClassId());
emitinfo[3] = valobj;
emit_cb->Call(4, emitinfo);
break;
}
// ####################
case OpenZWave::Notification::Type_ValueRefreshed: {
// ####################
OpenZWave::ValueID value = notif->values.front();
Local<Object> valobj = zwaveValue2v8Value(value);
emitinfo[0] = Nan::New<String>("value refreshed").ToLocalChecked();
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
emitinfo[2] = Nan::New<Integer>(value.GetCommandClassId());
emitinfo[3] = valobj;
emit_cb->Call(4, emitinfo);
break;
}
// #############
case OpenZWave::Notification::Type_NodeNew:
// #############
/* NodeNew is triggered when a node is discovered which is not
* found in the OpenZWave XML file. As we do not use that file
* simply ignore those notifications for now.
*
* NodeAdded is when we actually have a new node to set up.
*/
break;
// ###############
case OpenZWave::Notification::Type_NodeAdded: {
// ###############
node = new NodeInfo();
node->homeid = notif->homeid;
node->nodeid = notif->nodeid;
node->polled = false;
{
mutex::scoped_lock sl(znodes_mutex);
znodes[notif->nodeid] = node;
}
AddIntegerProp(emitobj, nodeid, notif->nodeid);
emitinfo[0] = Nan::New<String>("node added").ToLocalChecked();
emitinfo[1] = emitobj;
emit_cb->Call(2, emitinfo);
break;
}
// #################
case OpenZWave::Notification::Type_NodeRemoved: {
// #################
{
mutex::scoped_lock sl(znodes_mutex);
znodes.erase(notif->nodeid);
}
emitinfo[0] = Nan::New<String>("node removed").ToLocalChecked();
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
emit_cb->Call(2, emitinfo);
break;
}
// ######################
case OpenZWave::Notification::Type_NodeProtocolInfo: {
// ######################
/*
* Ignore intermediate notifications about a node status, we
* wait until the node is ready before retrieving information.
*/
break;
}
// ################
case OpenZWave::Notification::Type_NodeNaming: {
// ################
populateNode(cbinfo, notif->homeid, notif->nodeid);
emitinfo[0] = Nan::New<String>("node naming").ToLocalChecked();
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
emitinfo[2] = cbinfo;
emit_cb->Call(3, emitinfo);
break;
}
// ###############
case OpenZWave::Notification::Type_NodeEvent: {
// ###############
emitinfo[0] = Nan::New<String>("node event").ToLocalChecked();
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
emitinfo[2] = Nan::New<Integer>(notif->event);
emitinfo[3] = Nan::New<String>(notif->help.c_str()).ToLocalChecked();
emit_cb->Call(4, emitinfo);
break;
}
// #####################
case OpenZWave::Notification::Type_PollingDisabled: {
// #####################
if ((node = get_node_info(notif->nodeid))) {
node->polled = false;
emitinfo[0] = Nan::New<String>("polling disabled").ToLocalChecked();
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
emit_cb->Call(2, emitinfo);
}
break;
}
// ####################
case OpenZWave::Notification::Type_PollingEnabled: {
// ####################
if ((node = get_node_info(notif->nodeid))) {
node->polled = true;
emitinfo[0] = Nan::New<String>("polling enabled").ToLocalChecked();
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
emit_cb->Call(2, emitinfo);
}
break;
}
// ################
case OpenZWave::Notification::Type_SceneEvent: {
// ################
emitinfo[0] = Nan::New<String>("scene event").ToLocalChecked();
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
emitinfo[2] = Nan::New<Integer>(notif->sceneid);
emit_cb->Call(3, emitinfo);
break;
}
// ##################
case OpenZWave::Notification::Type_CreateButton: {
// ##################
emitinfo[0] = Nan::New<String>("create button").ToLocalChecked();
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
emitinfo[2] = Nan::New<Integer>(notif->buttonid);
emit_cb->Call(3, emitinfo);
break;
}
// ##################
case OpenZWave::Notification::Type_DeleteButton: {
// ##################
emitinfo[0] = Nan::New<String>("delete button").ToLocalChecked();
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
emitinfo[2] = Nan::New<Integer>(notif->buttonid);
emit_cb->Call(3, emitinfo);
break;
}
// ##############
case OpenZWave::Notification::Type_ButtonOn: {
// ##############
emitinfo[0] = Nan::New<String>("button on").ToLocalChecked();
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
emitinfo[2] = Nan::New<Integer>(notif->buttonid);
emit_cb->Call(3, emitinfo);
break;
}
// ###############
case OpenZWave::Notification::Type_ButtonOff: {
// ###############
emitinfo[0] = Nan::New<String>("button off").ToLocalChecked();
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
emitinfo[2] = Nan::New<Integer>(notif->buttonid);
emit_cb->Call(3, emitinfo);
break;
}
// #################
case OpenZWave::Notification::Type_DriverReady: {
// #################
// the driver is ready, set our global homeid
homeid = notif->homeid;
emitinfo[0] = Nan::New<String>("driver ready").ToLocalChecked();
emitinfo[1] = Nan::New<Integer>(homeid);
emit_cb->Call(2, emitinfo);
break;
}
// ##################
case OpenZWave::Notification::Type_DriverFailed: {
// ##################
emitinfo[0] = Nan::New<String>("driver failed").ToLocalChecked();
emit_cb->Call(1, emitinfo);
break;
}
// ##################################
case OpenZWave::Notification::Type_EssentialNodeQueriesComplete: {
// ##################################
populateNode(cbinfo, notif->homeid, notif->nodeid);
emitinfo[0] = Nan::New<String>("node available").ToLocalChecked();
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
emitinfo[2] = cbinfo;
emit_cb->Call(3, emitinfo);
break;
}
/*
* The node is now fully ready for operation.
*/
// #########################
case OpenZWave::Notification::Type_NodeQueriesComplete: {
// #########################
populateNode(cbinfo, notif->homeid, notif->nodeid);
emitinfo[0] = Nan::New<String>("node ready").ToLocalChecked();
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
emitinfo[2] = cbinfo;
emit_cb->Call(3, emitinfo);
break;
}
/*
* The network scan has been completed. Currently we do not
* care about dead nodes - is there anything we can do anyway?
*/
// #########################
case OpenZWave::Notification::Type_AwakeNodesQueried:
case OpenZWave::Notification::Type_AllNodesQueried:
case OpenZWave::Notification::Type_AllNodesQueriedSomeDead: {
// #############################
emitinfo[0] = Nan::New<String>("scan complete").ToLocalChecked();
emit_cb->Call(1, emitinfo);
break;
}
// ##################
case OpenZWave::Notification::Type_Notification: {
// #########################
emitinfo[0] = Nan::New<String>("notification").ToLocalChecked();
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
emitinfo[2] = Nan::New<Integer>(notif->notification);
emitinfo[3] = Nan::New<String>(notif->help.c_str()).ToLocalChecked();
emit_cb->Call(4, emitinfo);
break;
}
case OpenZWave::Notification::Type_DriverRemoved:
case OpenZWave::Notification::Type_Group:
/* The associations for the node have changed. The
* application should rebuild any group information it
* holds about the node.
*/
// todo
break;
#if OPENZWAVE_SECURITY == 1
case OpenZWave::Notification::Type_ControllerCommand:
emitinfo[0] = Nan::New<String>("controller command").ToLocalChecked();
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
emitinfo[2] = Nan::New<Integer>(notif->event); // Driver::ControllerCommand
emitinfo[3] =
Nan::New<Integer>(notif->notification); // Driver::ControllerState
emitinfo[4] = Nan::New<String>(notif->help.c_str()).ToLocalChecked();
emit_cb->Call(5, emitinfo);
break;
case OpenZWave::Notification::Type_NodeReset:
emitinfo[0] = Nan::New<String>("node reset").ToLocalChecked();
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
emitinfo[2] = Nan::New<Integer>(notif->event); // Driver::ControllerCommand
emitinfo[3] =
Nan::New<Integer>(notif->notification); // Driver::ControllerState
emit_cb->Call(4, emitinfo);
break;
#endif
default:
fprintf(stderr, "Unhandled OpenZWave notification: %d\n", notif->type);
break;
} // end switch
}
/*
* Async handler, triggered by the OpenZWave callback.
*/
// ===================================================================
void async_cb_handler(uv_async_t *handle)
// ===================================================================
{
NotifInfo *notif;
mutex::scoped_lock sl(zqueue_mutex);
while (!zqueue.empty()) {
notif = zqueue.front();
#if OPENZWAVE_SECURITY != 1
if (notif->homeid == 0) {
handleControllerCommand(notif);
} else {
handleNotification(notif);
}
#else
handleNotification(notif);
#endif
delete notif;
zqueue.pop();
}
}
void async_cb_handler(uv_async_t *handle, int status) {
async_cb_handler(handle);
}
}