-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
telldus.cc
1140 lines (882 loc) · 41.8 KB
/
telldus.cc
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef BUILDING_NODE_EXTENSION
#define BUILDING_NODE_EXTENSION
#endif // BUILDING_NODE_EXTENSION
#include <cstdlib>
#include <string.h>
#include <list>
#include <node.h>
#include <v8.h>
#include <uv.h>
#include <time.h>
#include <telldus-core.h>
using namespace v8;
using namespace node;
using namespace std;
namespace telldus_v8 {
const int DATA_LENGTH = 20;
struct EventContext {
Persistent<Function, CopyablePersistentTraits<Function> > callback;
Isolate *isolate;
};
struct DeviceEventBaton {
EventContext *callback;
int deviceId;
int lastSentCommand;
int levelNum;
};
struct SensorEventBaton {
EventContext *callback;
int sensorId;
char *model;
char *protocol;
char *value;
int ts;
int dataType;
};
struct RawDeviceEventBaton {
EventContext *callback;
int controllerId;
char *data;
};
const int SUPPORTED_METHODS =
TELLSTICK_TURNON
| TELLSTICK_TURNOFF
| TELLSTICK_BELL
| TELLSTICK_TOGGLE
| TELLSTICK_DIM
| TELLSTICK_LEARN
| TELLSTICK_EXECUTE
| TELLSTICK_UP
| TELLSTICK_DOWN
| TELLSTICK_STOP;
struct sensorValue {
char *timeStamp;
char *value;
};
struct telldusDeviceInternals {
int supportedMethods;
int deviceType;
int lastSentCommand;
int level;
int id;
char *name;
char *model;
char *protocol;
};
struct telldusSensorInternals {
int sensorId;
int dataTypes;
char *model;
char *protocol;
};
uv_mutex_t radioMutex;
Local<Object> GetSupportedMethods(int id, int supportedMethods, Isolate* isolate){
Local<Array> methodsObj = Array::New(isolate);
int i = 0;
if (supportedMethods & TELLSTICK_TURNON) methodsObj->Set(isolate->GetCurrentContext(), i++, String::NewFromUtf8(isolate, "TURNON", v8::NewStringType::kNormal).ToLocalChecked());
if (supportedMethods & TELLSTICK_TURNOFF) methodsObj->Set(isolate->GetCurrentContext(), i++, String::NewFromUtf8(isolate, "TURNOFF", v8::NewStringType::kNormal).ToLocalChecked());
if (supportedMethods & TELLSTICK_BELL) methodsObj->Set(isolate->GetCurrentContext(), i++, String::NewFromUtf8(isolate, "BELL", v8::NewStringType::kNormal).ToLocalChecked());
if (supportedMethods & TELLSTICK_TOGGLE) methodsObj->Set(isolate->GetCurrentContext(), i++, String::NewFromUtf8(isolate, "TOGGLE", v8::NewStringType::kNormal).ToLocalChecked());
if (supportedMethods & TELLSTICK_DIM) methodsObj->Set(isolate->GetCurrentContext(), i++, String::NewFromUtf8(isolate, "DIM", v8::NewStringType::kNormal).ToLocalChecked());
if (supportedMethods & TELLSTICK_UP) methodsObj->Set(isolate->GetCurrentContext(), i++, String::NewFromUtf8(isolate, "UP", v8::NewStringType::kNormal).ToLocalChecked());
if (supportedMethods & TELLSTICK_DOWN) methodsObj->Set(isolate->GetCurrentContext(), i++, String::NewFromUtf8(isolate, "DOWN", v8::NewStringType::kNormal).ToLocalChecked());
if (supportedMethods & TELLSTICK_STOP) methodsObj->Set(isolate->GetCurrentContext(), i++, String::NewFromUtf8(isolate, "STOP", v8::NewStringType::kNormal).ToLocalChecked());
if (supportedMethods & TELLSTICK_LEARN) methodsObj->Set(isolate->GetCurrentContext(), i++, String::NewFromUtf8(isolate, "LEARN", v8::NewStringType::kNormal).ToLocalChecked());
return methodsObj;
}
Local<String> GetDeviceType(int id, int type, Isolate* isolate){
if(type & TELLSTICK_TYPE_DEVICE) return String::NewFromUtf8(isolate, "DEVICE", v8::NewStringType::kNormal).ToLocalChecked();
if(type & TELLSTICK_TYPE_GROUP) return String::NewFromUtf8(isolate, "GROUP", v8::NewStringType::kNormal).ToLocalChecked();
if(type & TELLSTICK_TYPE_SCENE) return String::NewFromUtf8(isolate, "SCENE", v8::NewStringType::kNormal).ToLocalChecked();
return String::NewFromUtf8(isolate, "UNKNOWN", v8::NewStringType::kNormal).ToLocalChecked();
}
Local<Object> GetDeviceStatus(int id, int lastSentCommand, int level, Isolate* isolate){
Local<Object> status = Object::New(isolate);
switch(lastSentCommand) {
case TELLSTICK_TURNON:
status->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "name", v8::NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, "ON", v8::NewStringType::kNormal).ToLocalChecked());
break;
case TELLSTICK_TURNOFF:
status->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "name", v8::NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, "OFF", v8::NewStringType::kNormal).ToLocalChecked());
break;
case TELLSTICK_DIM:
status->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "name", v8::NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, "DIM", v8::NewStringType::kNormal).ToLocalChecked());
status->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "level", v8::NewStringType::kNormal).ToLocalChecked(), Number::New(isolate, level));
break;
default:
status->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "name", v8::NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, "UNNKOWN", v8::NewStringType::kNormal).ToLocalChecked());
}
return status;
}
Local<Object> GetDevice( telldusDeviceInternals deviceInternals, Isolate* isolate ) {
Local<Object> obj = Object::New(isolate);
obj->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "name", v8::NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate,deviceInternals.name, v8::NewStringType::kNormal).ToLocalChecked());
obj->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "id", v8::NewStringType::kNormal).ToLocalChecked(), Number::New(isolate,deviceInternals.id));
obj->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "methods", v8::NewStringType::kNormal).ToLocalChecked(), GetSupportedMethods(deviceInternals.id, deviceInternals.supportedMethods, isolate));
obj->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "model", v8::NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate,deviceInternals.model, v8::NewStringType::kNormal).ToLocalChecked());
obj->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "protocol", v8::NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate,deviceInternals.protocol, v8::NewStringType::kNormal).ToLocalChecked());
obj->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "type", v8::NewStringType::kNormal).ToLocalChecked(), GetDeviceType(deviceInternals.id,deviceInternals.deviceType, isolate));
obj->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "status", v8::NewStringType::kNormal).ToLocalChecked(), GetDeviceStatus(deviceInternals.id,deviceInternals.lastSentCommand,deviceInternals.level, isolate));
// Cleanup
tdReleaseString(deviceInternals.name);
tdReleaseString(deviceInternals.model);
tdReleaseString(deviceInternals.protocol);
return obj;
}
Local<Value> getDevicesFromInternals( list<telldusDeviceInternals> t, Isolate* isolate ) {
// Destination array
Local<Array> devices = Array::New(isolate,t.size());
int i=0;
for (list<telldusDeviceInternals>::const_iterator iterator = t.begin(), end = t.end(); iterator != end; ++iterator) {
devices->Set(isolate->GetCurrentContext(), i, GetDevice(*iterator, isolate));
i++;
}
return devices;
}
telldusDeviceInternals getDeviceRaw(int idx) {
telldusDeviceInternals deviceInternals;
deviceInternals.id = tdGetDeviceId( idx );
deviceInternals.name = tdGetName( deviceInternals.id );
deviceInternals.model = tdGetModel( deviceInternals.id );
deviceInternals.protocol = tdGetProtocol( deviceInternals.id );
deviceInternals.supportedMethods = tdMethods( deviceInternals.id, SUPPORTED_METHODS );
deviceInternals.deviceType = tdGetDeviceType( deviceInternals.id );
deviceInternals.lastSentCommand = tdLastSentCommand( deviceInternals.id, SUPPORTED_METHODS );
if( deviceInternals.lastSentCommand == TELLSTICK_DIM ) {
char * levelStr = tdLastSentValue(deviceInternals.id);
// Convert to number and add to object
deviceInternals.level = atoi(levelStr);
// Clean up the mess
tdReleaseString(levelStr);
}
return deviceInternals;
}
sensorValue GetSensorValue(int currentType, telldusSensorInternals si) {
sensorValue sv;
char value[DATA_LENGTH];
char timeBuf[80];
time_t timestamp = 0;
tdSensorValue(si.protocol, si.model, si.sensorId, currentType, value, DATA_LENGTH, (int *)×tamp);
strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d %H:%M:%S", localtime(×tamp));
sv.timeStamp = strdup(timeBuf);
sv.value = strdup(value);
return sv;
}
Local<Object> GetSupportedSensorValues(telldusSensorInternals si, Isolate* isolate){
Local<Array> methodsObj = Array::New(isolate);
int i = 0;
if (si.dataTypes & TELLSTICK_TEMPERATURE) {
Local<Object> cur = Object::New(isolate);
sensorValue sv = GetSensorValue(TELLSTICK_TEMPERATURE, si);
cur->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "type", v8::NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, "TEMPERATURE", v8::NewStringType::kNormal).ToLocalChecked());
cur->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "value", v8::NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, sv.value, v8::NewStringType::kNormal).ToLocalChecked());
cur->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "timestamp", v8::NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, sv.timeStamp, v8::NewStringType::kNormal).ToLocalChecked());
methodsObj->Set(isolate->GetCurrentContext(), i++, cur);
}
if (si.dataTypes & TELLSTICK_HUMIDITY) {
Local<Object> cur = Object::New(isolate);
sensorValue sv = GetSensorValue(TELLSTICK_HUMIDITY, si);
cur->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "type", v8::NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, "HUMIDITY", v8::NewStringType::kNormal).ToLocalChecked());
cur->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "value", v8::NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, sv.value, v8::NewStringType::kNormal).ToLocalChecked());
cur->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "timestamp", v8::NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, sv.timeStamp, v8::NewStringType::kNormal).ToLocalChecked());
methodsObj->Set(isolate->GetCurrentContext(), i++, cur);
}
#ifdef TELLSTICK_RAINRATE
if (si.dataTypes & TELLSTICK_RAINRATE) {
Local<Object> cur = Object::New(isolate);
sensorValue sv = GetSensorValue(TELLSTICK_RAINRATE, si);
cur->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "type", v8::NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, "RAINRATE", v8::NewStringType::kNormal).ToLocalChecked());
cur->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "value", v8::NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, sv.value, v8::NewStringType::kNormal).ToLocalChecked());
cur->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "timestamp", v8::NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, sv.timeStamp, v8::NewStringType::kNormal).ToLocalChecked());
methodsObj->Set(isolate->GetCurrentContext(), i++, cur);
}
if (si.dataTypes & TELLSTICK_RAINTOTAL) {
Local<Object> cur = Object::New(isolate);
sensorValue sv = GetSensorValue(TELLSTICK_RAINTOTAL, si);
cur->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "type", v8::NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, "RAINTOTAL", v8::NewStringType::kNormal).ToLocalChecked());
cur->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "value", v8::NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, sv.value, v8::NewStringType::kNormal).ToLocalChecked());
cur->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "timestamp", v8::NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, sv.timeStamp, v8::NewStringType::kNormal).ToLocalChecked());
methodsObj->Set(isolate->GetCurrentContext(), i++, cur);
}
if (si.dataTypes & TELLSTICK_WINDDIRECTION) {
Local<Object> cur = Object::New(isolate);
sensorValue sv = GetSensorValue(TELLSTICK_WINDDIRECTION, si);
cur->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "type", v8::NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, "WINDDIRECTION", v8::NewStringType::kNormal).ToLocalChecked());
cur->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "value", v8::NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, sv.value, v8::NewStringType::kNormal).ToLocalChecked());
cur->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "timestamp", v8::NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, sv.timeStamp, v8::NewStringType::kNormal).ToLocalChecked());
methodsObj->Set(isolate->GetCurrentContext(), i++, cur);
}
if (si.dataTypes & TELLSTICK_WINDAVERAGE) {
Local<Object> cur = Object::New(isolate);
sensorValue sv = GetSensorValue(TELLSTICK_WINDAVERAGE, si);
cur->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "type", v8::NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, "WINDAVERAGE", v8::NewStringType::kNormal).ToLocalChecked());
cur->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "value", v8::NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, sv.value, v8::NewStringType::kNormal).ToLocalChecked());
cur->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "timestamp", v8::NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, sv.timeStamp, v8::NewStringType::kNormal).ToLocalChecked());
methodsObj->Set(isolate->GetCurrentContext(), i++, cur);
}
if (si.dataTypes & TELLSTICK_WINDGUST) {
Local<Object> cur = Object::New(isolate);
sensorValue sv = GetSensorValue(TELLSTICK_WINDGUST, si);
cur->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "type", v8::NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, "WINDGUST", v8::NewStringType::kNormal).ToLocalChecked());
cur->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "value", v8::NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, sv.value, v8::NewStringType::kNormal).ToLocalChecked());
cur->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "timestamp", v8::NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, sv.timeStamp, v8::NewStringType::kNormal).ToLocalChecked());
methodsObj->Set(isolate->GetCurrentContext(), i++, cur);
}
#endif
return methodsObj;
}
list<telldusDeviceInternals> getDevicesRaw() {
int intNumberOfDevices = tdGetNumberOfDevices();
list<telldusDeviceInternals> deviceList;
for ( int i = 0 ; i < intNumberOfDevices ; i++ ) {
deviceList.push_back(getDeviceRaw(i));
}
return deviceList;
}
list<telldusSensorInternals> getSensorsRaw() {
char protocol[DATA_LENGTH], model[DATA_LENGTH];
int sensorId = 0, dataTypes = 0;
list<telldusSensorInternals> sensorList;
while(tdSensor(protocol, DATA_LENGTH, model, DATA_LENGTH, &sensorId, &dataTypes) == TELLSTICK_SUCCESS) {
telldusSensorInternals t;
t.protocol = strdup(protocol);
t.model = strdup(model);
t.sensorId = sensorId;
t.dataTypes = dataTypes;
// TODO: Cleanup of char* ?
sensorList.push_back(t);
}
return sensorList;
}
Local<Object> GetSensor( telldusSensorInternals sensorInternals, Isolate* isolate ) {
Local<Object> obj = Object::New(isolate);
obj->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "model", v8::NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate,sensorInternals.model, v8::NewStringType::kNormal).ToLocalChecked());
obj->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "protocol", v8::NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate,sensorInternals.protocol, v8::NewStringType::kNormal).ToLocalChecked());
obj->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "id", v8::NewStringType::kNormal).ToLocalChecked(), Number::New(isolate, sensorInternals.sensorId));
obj->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "data", v8::NewStringType::kNormal).ToLocalChecked(), GetSupportedSensorValues(sensorInternals, isolate));
free(sensorInternals.model);
free(sensorInternals.protocol);
return obj;
}
Local<Value> getSensorsFromInternals( list<telldusSensorInternals> s, Isolate* isolate ) {
// Destination array
Local<Array> sensors = Array::New(isolate,s.size());
int i=0;
for (list<telldusSensorInternals>::const_iterator iterator = s.begin(), end = s.end(); iterator != end; ++iterator) {
sensors->Set(isolate->GetCurrentContext(), i, GetSensor(*iterator, isolate));
i++;
}
return sensors;
}
void DeviceHandleClosed(uv_handle_t* handle) {
DeviceEventBaton *baton = static_cast<DeviceEventBaton *>(handle->data);
delete baton;
delete handle;
}
void DeviceCallbackAfter(uv_async_t *handle) {
DeviceEventBaton *baton = static_cast<DeviceEventBaton *>(handle->data);
HandleScope handleScope(baton->callback->isolate);
Local<Function> func = Local<Function>::New(baton->callback->isolate, ((Persistent<Function, CopyablePersistentTraits<Function> >)baton->callback->callback));
Local<Value> args[] = {
Number::New(baton->callback->isolate,baton->deviceId),
GetDeviceStatus(baton->deviceId,baton->lastSentCommand,baton->levelNum, baton->callback->isolate),
};
func->Call(baton->callback->isolate->GetCurrentContext(), Null(baton->callback->isolate), 2, args);
uv_close((uv_handle_t*) handle, DeviceHandleClosed);
}
void DeviceCallback( int deviceId, int method, const char * data, int callbackId, void* callbackVoid ) {
DeviceEventBaton *baton = new DeviceEventBaton();
baton->callback = static_cast<EventContext *>(callbackVoid);
baton->deviceId = deviceId;
baton->lastSentCommand = tdLastSentCommand(baton->deviceId, SUPPORTED_METHODS);
baton->levelNum = 0;
char *level;
if(baton->lastSentCommand == TELLSTICK_DIM) {
// Get level, returned from telldus-core as char
level = tdLastSentValue(baton->deviceId);
// Convert to number and add to object
baton->levelNum = atoi(level);
// Clean up the mess
tdReleaseString(level);
}
uv_async_t *asyncDevice = new uv_async_t;
asyncDevice->data = baton;
// Start event loop and attach callbacks
uv_async_init(uv_default_loop(), asyncDevice, DeviceCallbackAfter);
uv_async_send(asyncDevice);
}
void addDeviceEventListener(const FunctionCallbackInfo<Value>& args){
Isolate* isolate = args.GetIsolate();
HandleScope handleScope(isolate);
if (!args[0]->IsFunction()) {
Local<Value> exception = Exception::TypeError(String::NewFromUtf8(isolate, "Expected 1 argument: (function callback)", v8::NewStringType::kNormal).ToLocalChecked());
isolate->ThrowException(exception);
}
Local<Function> cb = Local<Function>::Cast(args[0]);
Persistent<Function, CopyablePersistentTraits<Function> > value(isolate, cb);
EventContext *ctx = new EventContext();
ctx->callback = value;
ctx->isolate = isolate;
Local<Number> num = Number::New(isolate, tdRegisterDeviceEvent((TDDeviceEvent)&DeviceCallback, ctx));
args.GetReturnValue().Set(num);
}
void SensorHandleClosed(uv_handle_t* handle) {
SensorEventBaton *baton = static_cast<SensorEventBaton *>(handle->data);
// Is this ok?
free(baton->protocol);
free(baton->model);
free(baton->value);
delete baton;
delete handle;
}
void SensorCallbackAfter(uv_async_t* handle) {
SensorEventBaton *baton = static_cast<SensorEventBaton *>(handle->data);
HandleScope scope(baton->callback->isolate);
Local<Function> func = Local<Function>::New(baton->callback->isolate, ((Persistent<Function, CopyablePersistentTraits<Function> >)baton->callback->callback));
Local<Value> args[] = {
Number::New(baton->callback->isolate,baton->sensorId),
String::NewFromUtf8(baton->callback->isolate, baton->model, v8::NewStringType::kNormal).ToLocalChecked(),
String::NewFromUtf8(baton->callback->isolate, baton->protocol, v8::NewStringType::kNormal).ToLocalChecked(),
Number::New(baton->callback->isolate,baton->dataType),
String::NewFromUtf8(baton->callback->isolate, baton->value, v8::NewStringType::kNormal).ToLocalChecked(),
Number::New(baton->callback->isolate,baton->ts)
};
func->Call(baton->callback->isolate->GetCurrentContext(), Null(baton->callback->isolate), 6, args);
uv_close((uv_handle_t*) handle, SensorHandleClosed);
}
void SensorCallback( const char *protocol, const char *model, int sensorId, int dataType, const char *value, int ts, int callbackId, void *callbackVoid ) {
EventContext *ctx = static_cast<EventContext *>(callbackVoid);
SensorEventBaton *baton = new SensorEventBaton();
baton->callback = ctx;
baton->sensorId = sensorId;
baton->protocol = strdup(protocol);
baton->model = strdup(model);
baton->ts = ts;
baton->dataType = dataType;
baton->value = strdup(value);
uv_async_t *asyncSensor = new uv_async_t;
asyncSensor->data = baton;
// Start event loop and attach callbacks
uv_async_init(uv_default_loop(), asyncSensor, SensorCallbackAfter);
uv_async_send(asyncSensor);
}
void addSensorEventListener(const FunctionCallbackInfo<Value>& args){
Isolate* isolate = args.GetIsolate();
HandleScope handleScope(isolate);
Local<Function> cb = Local<Function>::Cast(args[0]);
Persistent<Function, CopyablePersistentTraits<Function> > value(isolate, cb);
EventContext *ctx = new EventContext();
ctx->callback = value;
ctx->isolate = isolate;
if (!args[0]->IsFunction()) {
Local<Value> exception = Exception::TypeError(String::NewFromUtf8(isolate, "Expected 1 argument: (function callback)", v8::NewStringType::kNormal).ToLocalChecked());
isolate->ThrowException(exception);
}
Local<Number> num = Number::New(isolate, tdRegisterSensorEvent((TDSensorEvent)&SensorCallback, ctx));
args.GetReturnValue().Set(num);
}
void RawDataHandleClosed(uv_handle_t* handle) {
RawDeviceEventBaton *baton = static_cast<RawDeviceEventBaton *>(handle->data);
// Is this ok?
free(baton->data);
delete baton;
delete handle;
}
void RawDataCallbackAfter(uv_async_t* handle) {
RawDeviceEventBaton *baton = static_cast<RawDeviceEventBaton *>(handle->data);
HandleScope scope(baton->callback->isolate);
Local<Function> func = Local<Function>::New(baton->callback->isolate, ((Persistent<Function, CopyablePersistentTraits<Function> >)baton->callback->callback));
Local<Value> args[] = {
Number::New(baton->callback->isolate, baton->controllerId),
String::NewFromUtf8(baton->callback->isolate, baton->data, v8::NewStringType::kNormal).ToLocalChecked(),
};
func->Call(baton->callback->isolate->GetCurrentContext(), Null(baton->callback->isolate), 2, args);
uv_close((uv_handle_t*) handle, RawDataHandleClosed);
}
void RawDataCallback(const char* data, int controllerId, int callbackId, void *callbackVoid) {
RawDeviceEventBaton *baton = new RawDeviceEventBaton();
baton->callback = static_cast<EventContext *>(callbackVoid);
baton->data = strdup(data);
baton->controllerId = controllerId;
uv_async_t *asyncRawData = new uv_async_t;
asyncRawData->data = baton;
// Start event loop and attach callbacks
uv_async_init(uv_default_loop(), asyncRawData, RawDataCallbackAfter);
uv_async_send(asyncRawData);
}
void addRawDeviceEventListener(const FunctionCallbackInfo<Value>& args){
Isolate* isolate = args.GetIsolate();
HandleScope handleScope(isolate);
Local<Function> cb = Local<Function>::Cast(args[0]);
Persistent<Function, CopyablePersistentTraits<Function> > value(isolate, cb);
if (!args[0]->IsFunction()) {
Local<Value> exception = Exception::TypeError(String::NewFromUtf8(isolate, "Expected 1 argument: (function callback)", v8::NewStringType::kNormal).ToLocalChecked());
isolate->ThrowException(exception);
}
EventContext *ctx = new EventContext();
ctx->callback = value;
ctx->isolate = isolate;
Local<Number> num = Number::New(isolate, tdRegisterRawDeviceEvent((TDRawDeviceEvent)&RawDataCallback, ctx));
args.GetReturnValue().Set(num);
}
struct js_work {
uv_work_t req;
EventContext *callback;
//char* data;
bool rb; // Return value, boolean
int rn; // Return value, number
char* rs; // Return value, string
int f; // Worktype
int devID; // Device ID
int v; // Arbitrary number value
char* s; // Arbitrary string value
char* s2; // Arbitrary string value
bool string_used;
Isolate* isolate; // Global V8 isolate
list<telldusDeviceInternals> l;
list<telldusSensorInternals> si;
};
void RunWork(uv_work_t* req) {
js_work* work = static_cast<js_work*>(req->data);
switch(work->f) {
case 0:
uv_mutex_lock (&radioMutex);
work->rn = tdTurnOn(work->devID);
uv_mutex_unlock (&radioMutex);
break;
case 1:
uv_mutex_lock (&radioMutex);
work->rn = tdTurnOff(work->devID);
uv_mutex_unlock (&radioMutex);
break;
case 2:
uv_mutex_lock (&radioMutex);
work->rn = tdDim(work->devID,(unsigned char)work->v);
uv_mutex_unlock (&radioMutex);
break;
case 3:
uv_mutex_lock (&radioMutex);
work->rn = tdLearn(work->devID);
uv_mutex_unlock (&radioMutex);
break;
case 4:
work->rn = tdAddDevice();
break;
case 5: // SetName
work->rb = tdSetName(work->devID,work->s);
break;
case 6: // GetName
work->rs = tdGetName(work->devID);
work->string_used = true;
break;
case 7: // SetProtocol
work->rb = tdSetProtocol(work->devID,work->s);
break;
case 8: // GetProtocol
work->rs = tdGetProtocol(work->devID);
work->string_used = true;
break;
case 9: // SetModel
work->rb = tdSetModel(work->devID,work->s);
break;
case 10: // GetModel
work->rs = tdGetModel(work->devID);
work->string_used = true;
break;
case 11: // GetDeviceType
work->rn = tdGetDeviceType(work->devID);
break;
case 12:
work->rb = tdRemoveDevice(work->devID);
break;
case 13:
work->rn = tdUnregisterCallback(work->devID);
break;
case 14: // GetModel
work->rs = tdGetErrorString(work->devID);
work->string_used = true;
break;
case 15: // tdInit();
uv_mutex_lock (&radioMutex);
tdInit();
uv_mutex_unlock (&radioMutex);
work->rb = true; // tdInit() has no return value, so we augment true for a return value
break;
case 16: // tdClose();
tdClose();
work->rb = true; // tdClose() has no return value, so we augment true for a return value
break;
case 17: // tdGetNumberOfDevices();
work->rn = tdGetNumberOfDevices();
break;
case 18: // tdStop
uv_mutex_lock (&radioMutex);
work->rn = tdStop(work->devID);
uv_mutex_unlock (&radioMutex);
break;
case 19: // tdBell
uv_mutex_lock (&radioMutex);
work->rn = tdBell(work->devID);
uv_mutex_unlock (&radioMutex);
break;
case 20: // tdGetDeviceId(deviceIndex)
work->rn = tdGetDeviceId(work->devID);
break;
case 21: // tdGetDeviceParameter(deviceId, name, val)
work->rs = tdGetDeviceParameter(work->devID, work->s, work->s2);
work->string_used = true;
break;
case 22: // tdSetDeviceParameter(deviceId, name, val)
work->rb = tdSetDeviceParameter(work->devID, work->s, work->s2);
break;
case 23: // tdExecute
uv_mutex_lock (&radioMutex);
work->rn = tdExecute(work->devID);
uv_mutex_unlock (&radioMutex);
break;
case 24: // tdUp
uv_mutex_lock (&radioMutex);
work->rn = tdUp(work->devID);
uv_mutex_unlock (&radioMutex);
break;
case 25: // tdDown
uv_mutex_lock (&radioMutex);
work->rn = tdDown(work->devID);
uv_mutex_unlock (&radioMutex);
break;
case 26: // getDevices
work->l = getDevicesRaw();
break;
case 27: // getSensors
work->si = getSensorsRaw();
break;
}
}
void RunCallback(uv_work_t* req, int status) {
js_work* work = static_cast<js_work*>(req->data);
work->string_used = false;
HandleScope handleScope(work->isolate);
Local<Value> argv[3];
// This makes it possible to catch
// the exception from JavaScript land using the
// process.on('uncaughtException') event.
TryCatch try_catch(work->isolate);
EventContext *ctx;
Local<Function> func;
// Reenter the js-world
switch(work->f) {
// Return Number
case 0:
case 1:
case 2:
case 3:
case 4:
case 11:
case 13:
case 17:
case 18:
case 19:
case 20:
case 23:
case 24:
case 25:
argv[0] = Integer::New(work->isolate, work->rn); // Return number value
argv[1] = Integer::New(work->isolate, work->f); // Return worktype
ctx = static_cast<EventContext *>(work->callback);
func = Local<Function>::New(work->isolate, ((Persistent<Function, CopyablePersistentTraits<Function> >)ctx->callback));
func->Call(work->isolate->GetCurrentContext(), work->isolate->GetCurrentContext()->Global(), 2, argv);
break;
// Return boolean
case 5:
case 7:
case 9:
case 12:
case 15:
case 16:
case 22:
argv[0] = Boolean::New(work->isolate, work->rb); // Return number value
argv[1] = Integer::New(work->isolate, work->f); // Return worktype
ctx = static_cast<EventContext *>(work->callback);
func = Local<Function>::New(work->isolate, ((Persistent<Function, CopyablePersistentTraits<Function> >)ctx->callback));
func->Call(work->isolate->GetCurrentContext(), work->isolate->GetCurrentContext()->Global(), 2, argv);
break;
// Return String
case 6:
case 8:
case 10:
case 14:
case 21:
argv[0] = String::NewFromUtf8(work->isolate, work->rs, v8::NewStringType::kNormal).ToLocalChecked(); // Return string value
argv[1] = Integer::New(work->isolate, work->f); // Return callback function
ctx = static_cast<EventContext *>(work->callback);
func = Local<Function>::New(work->isolate, ((Persistent<Function, CopyablePersistentTraits<Function> >)ctx->callback));
func->Call(work->isolate->GetCurrentContext(), work->isolate->GetCurrentContext()->Global(), 2, argv);
break;
// Return list<telldusDeviceInternals>
case 26:
argv[0] = getDevicesFromInternals(work->l, work->isolate); // Return Object
argv[1] = Integer::New(work->isolate, work->f); // Return callback function
ctx = static_cast<EventContext *>(work->callback);
func = Local<Function>::New(work->isolate, ((Persistent<Function, CopyablePersistentTraits<Function> >)ctx->callback));
func->Call(work->isolate->GetCurrentContext(), work->isolate->GetCurrentContext()->Global(), 2, argv);
break;
// Return list<telldusSensorInternals>
case 27:
argv[0] = getSensorsFromInternals(work->si, work->isolate); // Return Object
argv[1] = Integer::New(work->isolate, work->f); // Return callback functio
ctx = static_cast<EventContext *>(work->callback);
func = Local<Function>::New(work->isolate, ((Persistent<Function, CopyablePersistentTraits<Function> >)ctx->callback));
func->Call(work->isolate->GetCurrentContext(), work->isolate->GetCurrentContext()->Global(), 2, argv);
break;
}
// Handle any exceptions thrown inside the callback
if (try_catch.HasCaught()) {
node::FatalException(work->isolate, try_catch);
}
// Check if we have an allocated string from telldus
if( work->string_used ) {
tdReleaseString(work->rs);
}
// properly cleanup, or death by millions of tiny leaks
//work->callback.Dispose();
//work->callback.Clear();
free(work->s); // char* Created in AsyncCaller
free(work->s2); // char* Created in AsyncCaller
delete work;
}
void AsyncCaller(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
HandleScope handleScope(isolate);
// Make sure we don't get any funky data
if(!args[0]->IsNumber() || !args[1]->IsNumber() || !args[2]->IsNumber() || !args[3]->IsString() || !args[4]->IsString()) {
Local<Value> exception = Exception::TypeError(String::NewFromUtf8(isolate, "Wrong arguments", v8::NewStringType::kNormal).ToLocalChecked());
isolate->ThrowException(exception);
}
// Make a deep copy of the string argument as we don't want
// it memory managed by v8 in the worker thread
String::Utf8Value str(isolate, args[3]);
char * str_copy = strdup(*str); // Deleted at end of RunCallback
String::Utf8Value str2(isolate, args[4]);
char * str_copy2 = strdup(*str2); // Deleted at end of RunCallback
Local<Function> cb = Local<Function>::Cast(args[5]);
Persistent<Function, CopyablePersistentTraits<Function> > value(isolate, cb);
EventContext *ctx = new EventContext();
ctx->callback = value;
ctx->isolate = isolate;
js_work* work = new js_work;
work->f = args[0].As<Integer>()->Value(); // Worktype
work->devID = args[1].As<Integer>()->Value(); // Device ID
work->v = args[2].As<Integer>()->Value(); // Arbitrary number value
work->s = str_copy; // Arbitrary string value
work->s2 = str_copy2; // Arbitrary string value
work->isolate = isolate;
work->callback = ctx;
work->req.data = work;
uv_queue_work(uv_default_loop(), &work->req, (uv_work_cb)RunWork, (uv_after_work_cb)RunCallback);
args.GetReturnValue().Set(String::NewFromUtf8(isolate, "Running asynchronous process initializer", v8::NewStringType::kNormal).ToLocalChecked());
}
void SyncCaller(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
HandleScope handleScope(isolate);
// Make sure we don't get any funky data
if(!args[0]->IsNumber() || !args[1]->IsNumber() || !args[2]->IsNumber() || !args[3]->IsString() || !args[4]->IsString()) {
Local<Value> exception = Exception::TypeError(String::NewFromUtf8(isolate, "Wrong arguments", v8::NewStringType::kNormal).ToLocalChecked());
isolate->ThrowException(exception);
}
// Make a deep copy of the string argument
String::Utf8Value str(isolate, args[3]);
char * str_copy = strdup(*str); // Deleted at end of this function
String::Utf8Value str2(isolate, args[4]);
char * str_copy2 = strdup(*str2); // Deleted at end of this function
js_work* work = new js_work;
work->f = args[0].As<Integer>()->Value(); // Worktype
work->devID = args[1].As<Integer>()->Value(); // Device ID
work->v = args[2].As<Integer>()->Value(); // Arbitrary number value
work->s = str_copy; // Arbitrary string value
work->s2 = str_copy2; // Arbitrary string value
work->isolate = isolate;
work->string_used = false; // Used to keep track of used telldus strings
// Run requested operation
switch(work->f) {
case 0:
uv_mutex_lock (&radioMutex);
work->rn = tdTurnOn(work->devID);
uv_mutex_unlock (&radioMutex);
break;
case 1:
uv_mutex_lock (&radioMutex);
work->rn = tdTurnOff(work->devID);
uv_mutex_unlock (&radioMutex);
break;
case 2:
uv_mutex_lock (&radioMutex);
work->rn = tdDim(work->devID,(unsigned char)work->v);
uv_mutex_unlock (&radioMutex);
break;
case 3:
uv_mutex_lock (&radioMutex);
work->rn = tdLearn(work->devID);
uv_mutex_unlock (&radioMutex);
break;
case 4:
work->rn = tdAddDevice();
break;
case 5: // SetName
work->rb = tdSetName(work->devID,work->s);
break;
case 6: // GetName
work->rs = tdGetName(work->devID);
work->string_used = true;
break;
case 7: // SetProtocol
work->rb = tdSetProtocol(work->devID,work->s);
break;
case 8: // GetProtocol
work->rs = tdGetProtocol(work->devID);
work->string_used = true;
break;
case 9: // SetModel
work->rb = tdSetModel(work->devID,work->s);
break;
case 10: // GetModel
work->rs = tdGetModel(work->devID);
work->string_used = true;
break;
case 11: // GetDeviceType
work->rn = tdGetDeviceType(work->devID);
break;
case 12:
work->rb = tdRemoveDevice(work->devID);
break;
case 13:
work->rn = tdUnregisterCallback(work->devID);
break;
case 14: // GetModel
work->rs = tdGetErrorString(work->devID);
work->string_used = true;
break;
case 15: // tdInit();
uv_mutex_lock (&radioMutex);
tdInit();
uv_mutex_unlock (&radioMutex);
work->rb = true; // tdInit() has no return value, so we augment true for a return value
break;