forked from cccgoodboy/react-native-ugen-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.native.js
2796 lines (2587 loc) · 97.7 KB
/
index.native.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
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
const {
NativeModules,
DeviceEventEmitter,
NativeEventEmitter,
Platform,
} = require('react-native');
const NativeModule = NativeModules.UgenAliLiving;
const tinycolor = require("tinycolor2");
const Location = require('expo-location');
const NetInfo = require("@react-native-community/netinfo").default;
const TcpSocketCreateConnection = require("react-native-tcp-socket").createConnection;
// ref to our close source project device firmware ali-smartliving-device-alios-things/Products/example/lightstring/property_report.h
const property_msg = {
MSG_NONE : (0),
MSG_POWERSWITCH : (1 << 0),
MSG_SCENE_STATUS : (1 << 1),
MSG_LTSTR_INFO : (1 << 2),
MSG_DEVICE_INFO : (1 << 3),
};
function sliceArray (arr, size) {
var arr2 = [];
for(let i = 0; i < arr.length; i = i + size){
arr2.push(arr.slice(i, i + size));
}
return arr2;
}
class AliLiving {
static MESH_ADDRESS_MIN = 0x0001;
static MESH_ADDRESS_MAX = 0x00FF;
static GROUP_ADDRESS_MIN = 0xC001;
static GROUP_ADDRESS_MAX = 0xC0FF;
// static GROUP_ADDRESS_MAX = 0xFEFF;
static GROUP_ADDRESS_MASK = 0x00FF;
static HUE_MIN = 0;
static HUE_MAX = 360;
static SATURATION_MIN = 0;
static SATURATION_MAX = 100;
static BRIGHTNESS_MIN = 42; // 实测灯串不会随着亮度变化而改变颜色的最低亮度,比如 30 的话就代表 30%
static BRIGHTNESS_MAX = 100;
// 如果 LED 中绿色灯珠容易烧坏,此处记录着实际(也就是经过下面的 whiteBalance 之后)发给灯珠不会烧坏的最大值,比如 65 代表 65/255
// 纯绿色情况下的 g 值
// 100% 153
// 95% 135
// 90% 119
// 85% 103
// 80% 89
// 75% 76
// 70% 65
// 白色情况下的 r g b 值
// 100% 255, 153, 61
// 95% 225, 135, 54
// 90% 199, 119, 47
// 85% 173, 103, 41
// 80% 149, 89, 35
// 75% 127, 76, 30
// 70% 109, 65, 26
// static LED_GREEN_MAX = 103;
static LED_GREEN_MAX = 255;
static COLOR_TEMP_MIN = 5;
static COLOR_TEMP_MAX = 100;
static NODE_STATUS_OFF = 0;
static NODE_STATUS_ON = 1;
static onTime = {};
static sceneSyncTime = {};
static NODE_STATUS_INACTIVE = 0; // 未激活
static NODE_STATUS_ONLINE = 1; // 上线
static NODE_STATUS_OFFLINE = 3; // 离线
static NODE_STATUS_FORBIDDEN = 8; // 禁用
static onlineTime = {};
static RELAY_TIMES_MAX = 16;
// 向同一个设备连续发送命令之间的延时,这是为了避免比如连续两个 setAlarm 时第二个
// setAlarm 的 getProperties 在第一个 setAlarm 的 setProperties 之前执行
// 导致的逻辑混乱,虽然说根本的解决办法是将 setAlarm 自身设为 promise ,但为了
// 兼容其它比如 react-native-btsig-telink 组件,以及设备本身执行 setProperties
// 也需要时间来完成,所以还是使用延时了
static DELAY_MS_AFTER_UPDATE_MESH_COMPLETED = 500;
// 向不同设备连续发送命令之间的延时
// ali smartliving 的 SDK 中没有自带命令队列然后自动在命令间加入延时,不过也没
// 有关系,它发送的 WiFi 信号,是不用像蓝牙信号那样需要加入几百毫秒延时的, 1 即可
static DELAY_MS_COMMAND = 1;
static ALARM_CREATE = 0;
static ALARM_REMOVE = 1;
static ALARM_UPDATE = 2;
static ALARM_ENABLE = 3;
static ALARM_DISABLE = 4;
static ALARM_ACTION_TURN_OFF = 0;
static ALARM_ACTION_TURN_ON = 1;
static ALARM_ACTION_SCENE = 2;
static ALARM_TYPE_DAY = 0;
static ALARM_TYPE_WEEK = 1;
static passthroughMode = undefined; // 以前在 react-native-bt-oe 中用于表明通过串口或者说自定义发送数据来控制蓝牙节点,现在只是为了兼容而保留
static gamma = [ // gamma 2.4 ,normal color ,据说较暗时颜色经 gamma 校正后会比较准
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0
0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, // 16
2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, // 32
5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, // 48
9, 10, 10, 10, 11, 11, 11, 12, 12, 13, 13, 14, 14, 14, 15, 15, // 64
16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 22, 22, 23, 23, 24, // 80
24, 25, 26, 26, 27, 28, 28, 29, 30, 30, 31, 32, 32, 33, 34, 35, // 96
35, 36, 37, 38, 39, 39, 40, 41, 42, 43, 43, 44, 45, 46, 47, 48, // 112
49, 50, 51, 52, 53, 53, 54, 55, 56, 57, 58, 59, 60, 62, 63, 64, // 128
65, 66, 67, 68, 69, 70, 71, 73, 74, 75, 76, 77, 78, 80, 81, 82, // 144
83, 85, 86, 87, 88, 90, 91, 92, 94, 95, 96, 98, 99, 100, 102, 103, // 160
105, 106, 108, 109, 111, 112, 114, 115, 117, 118, 120, 121, 123, 124, 126, 127, // 176
129, 131, 132, 134, 136, 137, 139, 141, 142, 144, 146, 148, 149, 151, 153, 155, // 192
156, 158, 160, 162, 164, 166, 167, 169, 171, 173, 175, 177, 179, 181, 183, 185, // 208
187, 189, 191, 193, 195, 197, 199, 201, 203, 205, 207, 210, 212, 214, 216, 218, // 224
220, 223, 225, 227, 229, 232, 234, 236, 239, 241, 243, 246, 248, 250, 253, 255 // 240
];
// static gamma = [ // gamma 2.8 ,vivid color ,据说较明亮时颜色经 gamma 校正后会比较准
// 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0
// 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 16
// 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, // 32
// 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, // 48
// 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 10, 10, // 64
// 10, 11, 11, 12, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16, 17, // 80
// 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25, 25, // 96
// 26, 27, 27, 28, 29, 29, 30, 31, 31, 32, 33, 34, 34, 35, 36, 37, // 112
// 38, 38, 39, 40, 41, 42, 43, 43, 44, 45, 46, 47, 48, 49, 50, 51, // 128
// 52, 53, 54, 55, 56, 57, 58, 59, 60, 62, 63, 64, 65, 66, 67, 68, // 144
// 70, 71, 72, 73, 75, 76, 77, 78, 80, 81, 82, 84, 85, 87, 88, 89, // 160
// 91, 92, 94, 95, 97, 98, 100, 101, 103, 104, 106, 108, 109, 111, 112, 114, // 176
// 116, 117, 119, 121, 123, 124, 126, 128, 130, 131, 133, 135, 137, 139, 141, 143, // 192
// 145, 147, 149, 151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 171, 173, 176, // 208
// 178, 180, 182, 185, 187, 189, 192, 194, 196, 199, 201, 203, 206, 208, 211, 213, // 224
// 216, 218, 221, 223, 226, 228, 231, 234, 236, 239, 242, 244, 247, 250, 253, 255 // 240
// ];
static whiteBalance = { // 为了让灯串能够在低电压下(就可以低温了)保持足够亮度,需要不再按照 1:6:3 的比例来设置 rgb 的白平衡
r: 1,
g: 0.85,
b: 0.4,
};
// static whiteBalance = { // cooler
// r: 1,
// g: 0.6,
// b: 0.24,
// };
// static whiteBalance = { // warmer
// r: 1,
// g: 0.5,
// b: 0.18,
// };
static longCommandParams = true; // 可以在一条命令中发送大量数据,无需拆分成许多小命令
static MESH_CMD_ACCESS_LEN_MAX = 380;
// 逻辑上能否通过蓝牙模块返回的在线状态或者开关灯等状态推理出在线状态
static hasOnlineStatusNotify = true;
// 物理上蓝牙模块是否支持返回在线状态
static hasOnlineStatusNotifyRaw = true;
// WiFi 设备断电后需要 3 分钟才由阿里生活物联网平台向 APP 发送离线事件。
// 在设备端开启 debug 后,每隔 2 秒会从串口打印一下
// [010728]<I> topic:/sys/device/info/notify
// [010730]<D> payload:{"id":"5","version":"1.0","method":"device.info.notify","params":...
// [010768]<D> Cancel message id 6 from list, cur count 2
// [010780]<D> The uri is /sys/device/info/notify
// [010782]<D> The message id 7 len 519 send success, add to the list
// [010784]<D> The message 7 need keep
// [010796]<I> coap send notify success
// 曾问阿里人员, APP 能否收到该消息,还是说只有云端能够收到?因为云端目前
// 只能在设备离线后 3 分钟才通知 APP 设备离线了,而我们需求在几秒内获知设备
// 离线,所以如果 APP 能收到该消息的话,就能达成我们的需求了,否则我们还得
// 想办法实现自己的心跳机制
// 阿里人员的回复是:
// 1. APP 能收到,但是不是作为维持心跳用的,也不会一直发,可以不用关心这个 log 打印;
// 2. 我们跟云端通信基于 mqtt 协议, mqtt 是依靠心跳来维持连接的,所以 3 分钟离线是正常行为,这样会对网络不好的环境有一定的兼容性;
// 3. 根据云端的规则,心跳间隔时间是不支持设置成几秒的;
// 4. 心跳频率过高会导致在网络不稳定的情况下,设备频繁地上下线。
static isOfflineStatusNotifyVeryLate = true;
// Let APP to determine if connected by online devices number > 0
// To compatible with react-native-bt-telink which connected is determined by sdk react-native-bt-telink itself
static isConnectedDeterminedByApp = true;
static needRefreshMeshNodesBeforeConfig = true;
static canConfigEvenDisconnected = true;
static needClaimedBeforeConnect = true;
static isClaiming = false;
static del4GroupStillSendOriginGroupAddress = true;
static isSetNodeGroupAddrReturnAddresses = false;
// 需要如下两个变量来保证韵律期间频发音量转亮度 changeBrightness() 的情况下也能正常 changeScene() ,
// 否则在两串灯时, All 组,开启韵律后,切换效果很多时候只有一盏灯能切换效果,另外一盏没变
static isSceneCadenceBusy = false;
static allowSceneCadence = true;
static shareType = 'aliSmartlivingShare';
static devices = [];
static tempLocalTimer = [];
static dummyMeshAddress = 'z';
static dummyMacAddress = 'f00baa';
static directWifiPort = 8821;
static directWifiIpv4Byte3 = '192.88.21.';
static otaFileVersionOffset = 4; // 把二进制固件作为一个字节数组看待的话,描述着版本号的第一个字节的数组地址
static otaFileVersionLength = 2; // 二进制固件中描述版本号用了几个字节
static lastSceneSyncMeshAddress = undefined;
static emitter = Platform.OS === 'ios' ? (NativeModule ? new NativeEventEmitter(NativeModule) : {}) : DeviceEventEmitter;
static unsubscribeNetInfo = undefined;
static directWiFiSocket = undefined;
static lastNetType = 'none';
static useWebSocket = true; // need firmware add [设备端添加 WebSocket Server 支持](https://github.com/flyskywhy/g/blob/master/i%E4%B8%BB%E8%A7%82%E7%9A%84%E4%BD%93%E9%AA%8C%E6%96%B9%E5%BC%8F/t%E5%BF%AB%E4%B9%90%E7%9A%84%E4%BD%93%E9%AA%8C/%E7%94%B5%E4%BF%A1/Net/%E7%89%A9%E8%81%94%E7%BD%91/SmartLiving/AliSmartLiving%E4%BD%BF%E7%94%A8%E8%AF%A6%E8%A7%A3.md#%E8%AE%BE%E5%A4%87%E7%AB%AF%E6%B7%BB%E5%8A%A0-websocket-server-%E6%94%AF%E6%8C%81)
static isNativeModuleInited = false;
static async connectThenDoInit() {
try {
let result = await fetch('https://eu-central-1.api-iot.aliyuncs.com');
// indirect WiFi mode need this to controll device of e.g. `ble_awss start` (蓝牙配网)
NativeModule.doInit();
this.isNativeModuleInited = true;
} catch (err) {
// 如果当初用户是用德国(默认)服务器注册的帐号,则当处于登录状态,此时杀死 APP ,将手机连接到不能
// 上网的路由器或是直连设备的 WiFi,再打开 APP ,则 ali smartliving 的 Android SDK 会在
// NativeModule.doInit() 中让 APP 卡死 60 秒,所以这里使用 await fetch 尝试测试网络以免卡死
this.connectThenDoInit();
}
}
static async doInit() {
this.connectThenDoInit();
// to [get ssid on ios](https://github.com/react-native-netinfo/react-native-netinfo/issues/263#issuecomment-808727622)
// in this.getCurrentSsid() later
await Location.requestPermissionsAsync();
// direct WiFi mode need this to controll device of `soft_ap start` (WiFi 直连)
// need firmware add code from your module vendor
this.unsubscribeNetInfo = NetInfo.addEventListener(state => {
if (
state.isWifiEnabled &&
state.isConnected
&& this.lastNetType !== state.type
&& state.type === 'wifi'
&& state.details.ipAddress
&& state.details.ipAddress.match(this.directWifiIpv4Byte3) // to match unique not usual 192.168, otherwise socket 30s connect timeout will interfere indirect WiFi device
) {
if (this.directWiFiSocket === undefined) {
let host = this.directWifiIpv4Byte3 + '1';
let socketOptions = {
port: this.directWifiPort,
host,
}
if (this.useWebSocket) {
this.directWiFiSocket = new WebSocket(`ws://${socketOptions.host}:${socketOptions.port}`,
'local-ali-smartliving');
this.emitter.emit('serviceConnected', {});
this.directWiFiSocket.onopen = () => this.onDirectWiFiSocketOpen();
this.directWiFiSocket.onmessage = (message) => {
try {
const json = JSON.parse(message.data);
this.onDirectWiFiSocketReceive(json);
} catch (err) {
console.warn('directWiFiSocket received->', err);
}
};
this.directWiFiSocket.onerror = (error) => {
console.warn('directWiFiSocket error->' + JSON.stringify(error));
};
this.directWiFiSocket.onclose = () => {
// APP 刚开启时 this.emitter 还没有来得及初始化好,所以需要 &&
this.emitter && this.emitter.emit('notificationVendorResponse', {
meshAddress: this.dummyMeshAddress,
opcode: 'BoneThingLocalConnectionChange',
params: JSON.stringify({
localConnectionState: this.NODE_STATUS_OFFLINE,
}),
});
this.directWiFiSocket = undefined;
console.warn('directWiFiSocket connection closed');
};
} else {
this.directWiFiSocket = TcpSocketCreateConnection(socketOptions, () => this.onDirectWiFiSocketOpen());
this.directWiFiSocket.setEncoding('utf8');
this.directWiFiSocket.on('data', (data) => {
try {
const json = JSON.parse(data);
this.onDirectWiFiSocketReceive(json);
} catch (err) {
console.warn('directWiFiSocket received->', err);
}
});
this.directWiFiSocket.on('error', (error) => {
console.warn('directWiFiSocket error->' + error);
});
this.directWiFiSocket.on('close', () => {
// APP 刚开启时 this.emitter 还没有来得及初始化好,所以需要 &&
this.emitter && this.emitter.emit('notificationVendorResponse', {
meshAddress: this.dummyMeshAddress,
opcode: 'BoneThingLocalConnectionChange',
params: JSON.stringify({
localConnectionState: this.NODE_STATUS_OFFLINE,
}),
});
this.directWiFiSocket.destroy();
this.directWiFiSocket = undefined;
console.warn('directWiFiSocket connection closed');
});
}
}
}
this.lastNetType = state.type;
});
}
static doDestroy() {
// NativeModule.doDestroy();
this.unsubscribeNetInfo();
if (this.directWiFiSocket) {
if (this.useWebSocket) {
} else {
this.directWiFiSocket.destroy();
}
}
}
static addListener(eventName, handler) {
if (Platform.OS === 'ios') {
const nativeEventEmitter = new NativeEventEmitter(NativeModule);
nativeEventEmitter.addListener(eventName, handler);
} else {
DeviceEventEmitter.addListener(eventName, handler);
}
}
static removeListener(eventName, handler) {
if (Platform.OS === 'ios') {
const nativeEventEmitter = new NativeEventEmitter(NativeModule);
nativeEventEmitter.removeListener(eventName, handler);
} else {
DeviceEventEmitter.removeListener(eventName, handler);
}
}
static enableBluetooth() {
NativeModule.enableBluetooth();
}
static enableSystemLocation() {
// NativeModule.enableSystemLocation();
}
static notModeAutoConnectMesh() {
return new Promise((resolve, reject) => {
reject();
});
}
static autoConnect({
userMeshPwd,
}) {}
static async postConnected({
meshAddress,
type,
immediate = false,
}) {
// 需要此处,否则在 1 个设备的情况下,走过
// 设置效果、删除设备、认领设备、设置效果
// 这 4 个步骤时,除非重启 APP ,否则因为
// 无法符合 selectNodeToResponseSceneId()
// 中 sceneSyncMeshAddress !== this.lastSceneSyncMeshAddress
// 的条件而导致 APP 一直没有发出设置同步的命令
this.lastSceneSyncMeshAddress = undefined;
this.remind({
meshAddress,
})
}
static autoRefreshNotify({
repeatCount,
Interval
}) {}
static idleMode({
disconnect
}) {
// return NativeModule.idleMode(disconnect);
}
// 登录
static async login() {
if (this.isNativeModuleInited) {
return NativeModule.login();
} else {
return new Promise((resolve, reject) => {
reject(new TypeError('NativeModule is not inited'));
});
}
}
// 退出登录
static async logout() {
return NativeModule.logout();
}
// 获取登录状态
static async isLogin() {
if (this.isNativeModuleInited) {
return NativeModule.isLogin();
} else {
return new Promise((resolve, reject) => {
reject(new TypeError('NativeModule is not inited'));
});
}
}
static scanTimer = undefined;
static startScan({
meshName, // to compatible with react-native-bt-telink which use meshName
timeoutSeconds,
isSingleNode,
}) {
if (this.directWiFiSocket) {
} else {
this.isClaiming = false;
if (this.scanTimer) {
clearTimeout(this.scanTimer);
this.scanTimer = undefined;
NativeModule.stopScanLocalDevice();
}
// in my use case, I use `unclaimed = meshName === 'sysin_mesh'`, maybe you need change it
let unclaimed = meshName === 'sysin_mesh';
NativeModule.startScanLocalDevice(unclaimed);
this.scanTimer = setTimeout(() => {
this.scanTimer = undefined;
NativeModule.stopScanLocalDevice();
}, timeoutSeconds * 1000);
}
}
// 获取设备token
static async getDeviceToken(pk, dn, timeout) {
return NativeModule.getDeviceToken(pk, dn, timeout);
}
// 开启飞燕长连接
static async startAliSocketListener() {
return NativeModule.startAliSocketListener();
}
// 关闭飞燕长连接
static async stopAliSocketListener() {
return NativeModule.stopAliSocketListener();
}
// 获取长连接状态
static async getAliSocketListenerState() {
return NativeModule.getAliSocketListenerState();
}
// 获取账号信息
static async getCurrentAccountMessage() {
if (this.isNativeModuleInited) {
return NativeModule.getCurrentAccountMessage();
} else {
return new Promise((resolve, reject) => {
reject(new TypeError('NativeModule is not inited'));
});
}
}
// 对 topic 进行订阅
static subscribe(topic) {
topic && NativeModule.subscribe(topic);
}
// 对 topic 取消订阅
static unsubscribe(topic) {
topic && NativeModule.unsubscribe(topic);
}
// 在 topic 上发布消息
static async ayncSendPublishRequest(topic, jsonParams) {
if (topic) {
// if (topic.includes('#')) {
// reject('send publish error: The topic name MUST NOT contain any wildcard characters (#+)');
// } else {
return NativeModule.ayncSendPublishRequest(topic, json);
// }
} else {
reject('ayncSendPublishRequest The topic name can not be empty');
}
}
// 发送客户端 API
static async send(path, params, version, iotAuth) {
return NativeModule.send(path, params, version, iotAuth);
}
static async setNodeName({
meshAddress,
name,
}) {
try {
let res = await NativeModule.send(
'/uc/setDeviceNickName',
JSON.stringify({
iotId: meshAddress,
nickName: name,
}),
'1.0.6',
true,
);
let resJson = JSON.parse(res);
return resJson.data;
} catch (error) {
console.warn(error);
}
}
static async loadUserDeviceList() {
try {
let res = await NativeModule.send(
'/uc/listBindingByAccount',
JSON.stringify({pageNo: 1, pageSize: 100}), // TODO: more than 100?
'1.0.8',
true,
);
let resJson = JSON.parse(res);
return resJson.data.data;
} catch (error) {
console.warn(error);
}
}
// Load devices info from `https://living.aliyun.com` and convert them
// into our nodes format, then init them in native code and return nodes.
// Some use case: Old user install new APP and want get old devices;
// New user shared by others and want get shared devices.
// This function can be invoked before connected any device node.
static async loadNodes({
oldNodes = [],
forceRefresh = false,
}) {
let devices = await this.loadUserDeviceList();
let nodes = oldNodes.filter((node) => {
return node.a === this.dummyMeshAddress;
});
if (devices) {
for (let device of devices) {
let oldNode = oldNodes.find((node) => node.a === device.iotId);
if (oldNode === undefined || oldNode.n === device.deviceName || forceRefresh) {
try {
let detailRes = await NativeModule.send(
'/thing/info/get',
JSON.stringify({iotId: device.iotId}),
'1.0.4',
true,
);
let detailJson = JSON.parse(detailRes);
let deviceDetail = detailJson.data;
nodes.push({
n: device.nickName || deviceDetail.name,
m: deviceDetail.mac,
a: deviceDetail.iotId,
i: {
v: deviceDetail.firmwareVersion,
},
t: {
d: 0, // discoveryType 0 means LOCAL_ONLINE_DEVICE
p: deviceDetail.productKey,
},
})
} catch (error) {
console.warn(error);
if (oldNode) {
nodes.push(oldNode);
}
}
} else {
nodes.push(oldNode);
}
}
} else {
nodes = oldNodes;
}
if (nodes.length) {
this.devices = nodes.map((node) => {
return {
meshAddress: node.a,
macAddress: node.m,
};
})
NativeModule.setDevices(this.devices);
}
if (devices && nodes.length) {
return nodes;
}
}
// This function should be invoked after connected any device node.
// The properties will be emitted later.
static getNodesProperties({
meshAddresses = [],
}) {
let addresses = [];
if (this.directWiFiSocket) {
addresses = meshAddresses.filter((meshAddress) => {
return meshAddress === this.dummyMeshAddress;
});
} else {
addresses = meshAddresses.filter((meshAddress) => {
return meshAddress !== this.dummyMeshAddress;
});
}
addresses.map((meshAddress) => {
let params = {
iotId: meshAddress,
items: {
// here MSG_POWERSWITCH is for whitelist described in react-native-ali-smartliving/android/src/main/java/ugen/fy/plugin/AliLiving.java
property_report: property_msg.MSG_POWERSWITCH | property_msg.MSG_LTSTR_INFO | property_msg.MSG_DEVICE_INFO,
},
};
if (this.directWiFiSocket) {
if (this.useWebSocket) {
this.directWiFiSocket.send(JSON.stringify(params.items));
} else {
this.directWiFiSocket.write(JSON.stringify(params.items));
}
} else {
// because NativeModule.getProperties() is for get all properties
// and sometimes actually not return "all" properties, so I have
// to set custom "property_report" property to device firmware to
// let it return some other individual property
//
// because NativeModule.getProperties() will get offline device's
// properties from cloud, that will confuse this.isOnline(), so
// getProperties() in insertDevice() in AliLiving.java and AliLiving.m
// is disabled, just use setProperties "property_report" here instead
NativeModule.setProperties(meshAddress, JSON.stringify(params));
}
});
}
static onDirectWiFiSocketReceive(data) {
// console.warn('directWiFiSocket received->', data);
if (data.hasOwnProperty('sceneSyncId')) {
// {
// "PowerSwitch": 1,
// "sceneSyncId": 27
// }
this.emitter.emit('notificationVendorResponse', {
opcode: 'SCENE_SYNC',
meshAddress: this.dummyMeshAddress,
isOnline: true,
isOn: data.hasOwnProperty('PowerSwitch') ? (data.PowerSwitch !== 0) : true,
sceneID: data.sceneSyncId,
sceneSyncTime: new Date().getTime(),
});
}
if (data.ltstr_info || data.device_info) {
// {
// "CommonServiceResponse": {
// "seq": "\ufffd"
// },
// "PowerSwitch": 1,
// "device_info": {
// "addr_bits_length": 8,
// "color_mode": 3,
// "ltstr_scene": 0,
// "rev": 0,
// "vendor_id": 28
// },
// "ltstr_info": {
// "crash_point": 40,
// "flag_gain_ratio": 100,
// "gamma_enable": 0,
// "ltstr_length": 200,
// "timing_sequence": 1
// }
// }
let params = {
data: {
PowerSwitch: {
time: 0,
value: 1,
},
},
};
if (data.ltstr_info) {
params.data.ltstr_info = {
time: 0,
value: data.ltstr_info,
}
}
if (data.device_info) {
params.data.device_info = {
time: 0,
value: data.device_info,
}
}
this.emitter.emit('notificationVendorResponse', {
meshAddress: this.dummyMeshAddress,
opcode: 'getProperties',
params: JSON.stringify(params),
});
}
}
static onDirectWiFiSocketOpen() {
// To use LocalTimer in direct WiFi mode, need modify firmware code to add
// timer_service_ntp_update(str);
// in ntp_timer_update(const char *str) of e.g.
// ali-smartliving-device-alios-things/Products/example/smart_outlet/property_report.c
// and make sure ntp_timer_update() will be invoked when `this.setTime()` here,
// and modify timer_service_init() of
// ali-smartliving-device-alios-things/Living_SDK/framework/protocol/linkkit/sdk/iotx-sdk-c_clone/components/timer_service/timer_service.c
// to move below code
// if (ret != 0) {
// // EXAMPLE_TRACE("ERR:ntp_time_request failed=%d!", ret);
// // return -1;
// ret = 2;
// goto err;
// }
// down to just before
// return 0;
this.setTime({
meshAddress: this.dummyMeshAddress,
});
// 如果将直连 WiFi 设备也按照扫描、认领的方式进行,则无法简单做到
// 让多人简单通过 WiFi 直连共享同一个设备的目的,而且用户在连接 WiFi
// 时已经输入了一次密码,相当于已经认证了一次,再需要认领操作的话
// 就显得罗嗦了,所以还是像这里这样连接上 WiFi 就自动认领并上线。
this.emitter.emit('deviceStatusUpdateMeshCompleted', {
meshAddress: this.dummyMeshAddress,
macAddress: this.dummyMacAddress,
name: "(((<----->)))",
type: {p: 'directWifi'},
});
this.emitter.emit('notificationVendorResponse', {
meshAddress: this.dummyMeshAddress,
opcode: 'BoneThingLocalConnectionChange',
params: JSON.stringify({
localConnectionState: this.NODE_STATUS_ONLINE,
}),
});
}
static parseVendorResponse(resRaw) {
let res = resRaw;
switch (resRaw.opcode) {
case '/app/down/thing/properties': {
let params = JSON.parse(resRaw.params);
let sceneSyncId;
if (params.items) {
if (params.items.sceneSyncId) {
sceneSyncId = params.items.sceneSyncId;
} else if (params.checkFailedData && params.checkFailedData.sceneSyncId) {
sceneSyncId = params.checkFailedData.sceneSyncId;
}
}
if (sceneSyncId) {
let oldTime = this.sceneSyncTime[resRaw.meshAddress];
let newTime = sceneSyncId.time;
this.sceneSyncTime[resRaw.meshAddress] = newTime;
// [Mon Sep 27 2021 16:44:58.801] WARN {"meshAddress": "vvssa4DEzVglIGxDl777000000", "opcode": "/app/down/thing/properties", "params": "{\"iotId\":\"vvssa4DEzVglIGxDl777000000\",\"productKey\":\"a17hN4W4777\",\"deviceName\":\"testDeviceName1\",\"items\":{\"PowerSwitch\":{\"time\":1632732300192,\"value\":1},\"sceneSyncId\":{\"time\":1632732300192,\"value\":11}}}"}
// [Mon Sep 27 2021 16:44:58.930] WARN {"meshAddress": "vvssa4DEzVglIGxDl777000000", "opcode": "/app/down/thing/properties", "params": "{\"iotId\":\"vvssa4DEzVglIGxDl777000000\",\"productKey\":\"a17hN4W4777\",\"deviceName\":\"testDeviceName1\",\"items\":{\"PowerSwitch\":{\"time\":1632732300207,\"value\":1},\"sceneSyncId\":{\"time\":1632732300207,\"value\":11}}}"}
// [Mon Sep 27 2021 16:44:58.993] WARN {"meshAddress": "vvssa4DEzVglIGxDl777000000", "opcode": "/app/down/thing/properties", "params": "{\"iotId\":\"vvssa4DEzVglIGxDl777000000\",\"productKey\":\"a17hN4W4777\",\"deviceName\":\"testDeviceName1\",\"items\":{\"PowerSwitch\":{\"time\":1632732300218,\"value\":1},\"sceneSyncId\":{\"time\":1632732300218,\"value\":11}}}"
// LAN will cause above and below, WAN only will cause below
// [Mon Sep 27 2021 16:44:59.650] WARN {"meshAddress": "vvssa4DEzVglIGxDl777000000", "opcode": "/app/down/thing/properties", "params": "{\"checkFailedData\":{\"sceneSyncId\":{\"code\":5092,\"time\":1632732298504,\"message\":\"property not found\",\"value\":11}},\"groupIdList\":[\"a103ToHAxVJe1777\"],\"groupId\":\"a103ToHAxVJe1777\",\"categoryKey\":\"light\",\"batchId\":\"e80a059fd1d9426d924187f290e0a777\",\"gmtCreate\":1632732298510,\"productKey\":\"a17hN4W4777\",\"deviceName\":\"testDeviceName1\",\"iotId\":\"vvssa4DEzVglIGxDl777000000\",\"checkLevel\":0,\"namespace\":\"TmallGenie\",\"tenantId\":\"19A31790C888469BAFE4C384FFD60777\",\"thingType\":\"DEVICE\",\"items\":{\"PowerSwitch\":{\"time\":1632732300218,\"value\":1}},\"tenantInstanceId\":\"iotx-oxssharez200\"}"}
let debounceTime = 2000; // bigger than sceneSyncId.time gap between LAN and WAN
if (oldTime === undefined || newTime - oldTime > debounceTime) {
res = {
opcode: 'SCENE_SYNC',
meshAddress: resRaw.meshAddress,
isOnline: true,
isOn: params.items.PowerSwitch ? (params.items.PowerSwitch.value !== 0) : true,
sceneID: sceneSyncId.value,
sceneSyncTime: newTime,
};
}
}
}
break;
default:
break;
}
return res;
}
static sendCommand({
opcode,
meshAddress,
valueArray,
immediate = false,
}) {
// NativeModule.sendCommand(opcode, meshAddress, valueArray, immediate);
}
static remind({
meshAddress,
immediate = false,
}) {
let params = {
iotId: meshAddress,
items: {
flicker: 1,
},
};
if (this.directWiFiSocket) {
if (this.useWebSocket) {
this.directWiFiSocket.send(JSON.stringify(params.items));
} else {
this.directWiFiSocket.write(JSON.stringify(params.items));
}
} else {
NativeModule.setProperties(meshAddress, JSON.stringify(params));
}
}
// get the real address of the data.params
//
// because when there are 2 device, bind or unbind device A,
// the success event will be sent by device B ...
// see what happened in '/app/down/_thing/event/notify'
static targetAddress(data) {
let opcode = data.opcode;
let params = JSON.parse(data.params);
if (opcode === '/app/down/thing/status') {
return params.iotId;
} else if (opcode === '/app/down/_thing/event/notify') {
// data: {
// "meshAddress": "z0TeBIF5sGUKJB0K2tvu000000",
// "opcode": "/app/down/_thing/event/notify",
// "params": "{\"identifier\":\"awss.BindNotify\",\"value\":{\"iotId\":\"yCO6Qjp6medLPB9QVDXT000000\",\"identityId\":\"5022op8fb006f88b482b3f54d8541e0c1dfd0254\",\"owned\":1,\"productKey\":\"a17hN4W4777\",\"deviceName\":\"testDeviceName2\",\"operation\":\"Bind\"}}"
// }
// data: {
// "meshAddress": "z0TeBIF5sGUKJB0K2tvu000000",
// "opcode": "/app/down/_thing/event/notify",
// "params": "{\"identifier\":\"awss.BindNotify\",\"value\":{\"iotId\":\"yCO6Qjp6medLPB9QVDXT000000\",\"identityId\":\"5022op8fb006f88b482b3f54d8541e0c1dfd0254\",\"owned\":1,\"productKey\":\"a17hN4W4777\",\"deviceName\":\"testDeviceName2\",\"operation\":\"Unbind\"}}"
// }
return params.value.iotId;
} else if (opcode === '/app/down/thing/properties') {
return params.iotId;
} else {
return data.meshAddress;
}
}
static getBulbsNumber(data) {
let opcode = data.opcode;
let params = JSON.parse(data.params);
let realData = opcode === 'getProperties' ? params.data : params.items;
if (realData) {
if (realData.ltstr_info) {
return realData.ltstr_info.value.ltstr_length;
}
}
}
static getBulbAddrBitsLength(data) {
let opcode = data.opcode;
let params = JSON.parse(data.params);
let realData = opcode === 'getProperties' ? params.data : params.items;
if (realData) {
if (realData.device_info) {
return realData.device_info.value.addr_bits_length;
}
}
}
static getBulbColorBitsLength(data) {
let opcode = data.opcode;
let params = JSON.parse(data.params);
let realData = opcode === 'getProperties' ? params.data : params.items;
if (realData) {
if (realData.device_info) {
switch (realData.device_info.value.color_mode) {
case 3: // our project use 3 as 24 bits (RGB) color
return 24;
case 2: // our project use 2 as 32 bits (RGBW) color
return 32;
default:
return 24;
}
}
}
}
static isOnline(data) {
let opcode = data.opcode;
let params = JSON.parse(data.params);
if (opcode === '/app/down/thing/status') {
let time = this.onlineTime[data.meshAddress];
if (time === undefined || params.status.time > time) { // sometimes the seq of '/app/down/thing/status' is not ordered by status.time, so need this
this.onlineTime[data.meshAddress] = params.status.time;
return params.status.value === this.NODE_STATUS_ONLINE;
}
}
if (opcode === 'getStatus') {
let time = this.onlineTime[data.meshAddress];
if (time === undefined || params.data.time > time) {
this.onlineTime[data.meshAddress] = params.data.time;
return params.data.status === this.NODE_STATUS_ONLINE;
}
}
if (opcode === 'BoneThingLocalConnectionChange') {
// 好像只在设备与 APP 之前已经通过在线路由器互相连接然后让路由器离线的情况下才有用,
// 否则只能使用下面的 time === 0
return params.localConnectionState === this.NODE_STATUS_ONLINE;
}
if (opcode === 'getProperties') {
if (params.data.PowerSwitch) {
// 手机与设备连着同一路由器,且该路由器离线时,启动 APP 会得到
// 当前值比如 PowerSwitch.value 的开始时间 time === 0
// if (params.data.PowerSwitch.time === 0) {
return true;
// }
}
}
if (opcode === '/app/down/_thing/event/notify') {
if (params.identifier === 'awss.BindNotify') {
if (params.value.operation === 'Bind') {
return true;
}
if (params.value.operation === 'Unbind') {
return false;
}
}
}
}
static isOn(data) {
let opcode = data.opcode;
let params = JSON.parse(data.params);
if (opcode === '/app/down/thing/properties') {
if (params.items && params.items.PowerSwitch) {
let time = this.onTime[data.meshAddress];
if (time === undefined || params.items.PowerSwitch.time > time) {
this.onTime[data.meshAddress] = params.items.PowerSwitch.time;
return params.items.PowerSwitch.value === this.NODE_STATUS_ON;
}
}
}
if (opcode === 'getProperties') {
if (params.data.PowerSwitch) {
let time = this.onTime[data.meshAddress];
if (time === undefined || params.data.PowerSwitch.time > time) {
this.onTime[data.meshAddress] = params.data.PowerSwitch.time;
return params.data.PowerSwitch.value === this.NODE_STATUS_ON;
} else if (params.data.PowerSwitch.time === 0) {
// 刚认领设备的那一瞬间获取属性时得到的 time === 0
return params.data.PowerSwitch.value === this.NODE_STATUS_ON;
}
}
}