mini_program_gizwits_sdk 3.4.6-beta → 3.4.7-juhua
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +3 -3
- package/dist/src/handler/socket.d.ts +13 -2
- package/dist/src/productConfigFileManage.d.ts +1 -0
- package/dist/src/sdk.d.ts +14 -0
- package/dist/src/utils.d.ts +1 -0
- package/package.json +2 -2
- package/src/handler/socket.ts +79 -14
- package/src/productConfigFileManage.ts +33 -17
- package/src/sdk.ts +124 -28
- package/src/utils.ts +21 -17
- package/src/wifiConfig/ConfigBase.ts +2 -1
|
@@ -43,9 +43,19 @@ export default class GizwitsWS {
|
|
|
43
43
|
success: boolean;
|
|
44
44
|
errorCode?: undefined;
|
|
45
45
|
};
|
|
46
|
+
connectDevices: (devices: IDevice[]) => {
|
|
47
|
+
success: boolean;
|
|
48
|
+
errorCode: IError;
|
|
49
|
+
} | {
|
|
50
|
+
success: boolean;
|
|
51
|
+
errorCode?: undefined;
|
|
52
|
+
};
|
|
46
53
|
disConnectDevice: (device: IDevice) => {
|
|
47
54
|
success: boolean;
|
|
48
55
|
};
|
|
56
|
+
disConnectDevices: (devices: IDevice[]) => {
|
|
57
|
+
success: boolean;
|
|
58
|
+
};
|
|
49
59
|
connect: (did: string) => IError;
|
|
50
60
|
send: (did: string, raw: number[]) => IError | Promise<IWriteRes>;
|
|
51
61
|
writeData: (device: IDevice, attrs: ICommonObj) => Promise<IWriteRes> | {
|
|
@@ -99,8 +109,8 @@ export declare class Connection {
|
|
|
99
109
|
destory: () => void;
|
|
100
110
|
_networkChange: (res: WechatMiniprogram.OnNetworkStatusChangeListenerResult) => Promise<void>;
|
|
101
111
|
_appShow: () => Promise<void>;
|
|
102
|
-
_addSubDid: (
|
|
103
|
-
_removeSubDid: (
|
|
112
|
+
_addSubDid: (...dids: any[]) => void;
|
|
113
|
+
_removeSubDid: (...dids: any[]) => void;
|
|
104
114
|
_connectWS: () => void;
|
|
105
115
|
_subDevices: (dids: string[]) => void;
|
|
106
116
|
_unSubDevices: (dids: string[]) => void;
|
|
@@ -122,6 +132,7 @@ export declare class Connection {
|
|
|
122
132
|
_startPing: () => void;
|
|
123
133
|
_stopPing: () => void;
|
|
124
134
|
pongResp: () => void;
|
|
135
|
+
_currentSocketStatus: boolean;
|
|
125
136
|
_loginResp: (data: any) => void;
|
|
126
137
|
_consumeWaitSends: () => void;
|
|
127
138
|
_handleSocketStatusChanged: (enabled: boolean) => void;
|
|
@@ -6,6 +6,7 @@ declare class ProductConfigFileManage {
|
|
|
6
6
|
data: IData;
|
|
7
7
|
key: string;
|
|
8
8
|
init: () => Promise<void>;
|
|
9
|
+
getConfigFileMap: Record<string, Promise<IDataPointConfig | null>>;
|
|
9
10
|
getConfigFile: (pk: string) => Promise<IDataPointConfig | null>;
|
|
10
11
|
getAttrItem: (pk: string, key: string) => Promise<import("./protocol/DataPoint").IDataPointAttr>;
|
|
11
12
|
}
|
package/dist/src/sdk.d.ts
CHANGED
|
@@ -141,6 +141,7 @@ declare class GizwitsMiniSDK {
|
|
|
141
141
|
startAutoScan: (services?: string[]) => void;
|
|
142
142
|
stopAutoScan: () => void;
|
|
143
143
|
setDeviceMeta: <K extends keyof IDevice>(curDev: IDevice, key: K, value: IDevice[K], force?: boolean) => void;
|
|
144
|
+
setDevicesMeta: <K extends keyof IDevice>(devices: IDevice[], key: K, value: IDevice[K], force?: boolean) => void;
|
|
144
145
|
static getVersion: () => {
|
|
145
146
|
success: boolean;
|
|
146
147
|
data: string;
|
|
@@ -168,6 +169,19 @@ declare class GizwitsMiniSDK {
|
|
|
168
169
|
success: boolean;
|
|
169
170
|
message: string;
|
|
170
171
|
}>;
|
|
172
|
+
batchSubscribe: (devices: IDevice[], connectType?: TConnectType) => Promise<{
|
|
173
|
+
success: boolean;
|
|
174
|
+
errorCode: IError;
|
|
175
|
+
} | {
|
|
176
|
+
success: boolean;
|
|
177
|
+
errorCode?: undefined;
|
|
178
|
+
} | {
|
|
179
|
+
success: boolean;
|
|
180
|
+
message: string;
|
|
181
|
+
}>;
|
|
182
|
+
batchWanUnSubscribe: (devices: IDevice[]) => Promise<{
|
|
183
|
+
success: boolean;
|
|
184
|
+
}>;
|
|
171
185
|
unSubscribe: (device: IDevice) => Promise<{
|
|
172
186
|
success: boolean;
|
|
173
187
|
}>;
|
package/dist/src/utils.d.ts
CHANGED
|
@@ -23,6 +23,7 @@ export declare const advertisData2PkAndMac: (advertisData: ArrayBuffer, pks: str
|
|
|
23
23
|
mac: any;
|
|
24
24
|
};
|
|
25
25
|
export declare const isSameDevice: (deviceA: IDevice, deviceB: IDevice) => boolean;
|
|
26
|
+
export declare const getDeviceId: (deivce: IDevice) => string;
|
|
26
27
|
export declare const merageBleLocalDevices: (mainDevice: IDevice[], localDevice: IDevice[]) => IDevice[];
|
|
27
28
|
export declare const merageLanLocalDevices: (mainDevice: IDevice[], localDevice: IDevice[]) => IDevice[];
|
|
28
29
|
export declare const BTDevice2GDevice: (BTDevice: GizwitsWxBlueToothDevice, pks: string[]) => IDevice;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mini_program_gizwits_sdk",
|
|
3
|
-
"version": "3.4.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "3.4.7-juhua",
|
|
4
|
+
"description": "订阅成功不查询",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"test": "node_modules/mocha/bin/_mocha -r ts-node/register tests/**/*.spec.ts -file ./tests/setup-wx.js -timeout 60s",
|
package/src/handler/socket.ts
CHANGED
|
@@ -70,7 +70,7 @@ export default class GizwitsWS {
|
|
|
70
70
|
}
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
updateToken = ({token, uid}) => {
|
|
73
|
+
updateToken = ({ token, uid }) => {
|
|
74
74
|
this.token = token;
|
|
75
75
|
this.uid = uid;
|
|
76
76
|
}
|
|
@@ -132,13 +132,72 @@ export default class GizwitsWS {
|
|
|
132
132
|
return { success: true };
|
|
133
133
|
}
|
|
134
134
|
|
|
135
|
+
connectDevices = (devices: IDevice[]) => {
|
|
136
|
+
const wsInfoMap: Record<string, string[]> = {};
|
|
137
|
+
devices.forEach(device => {
|
|
138
|
+
const wsInfo = this._getWebsocketConnInfo(device);
|
|
139
|
+
if (!wsInfoMap[wsInfo]) {
|
|
140
|
+
wsInfoMap[wsInfo] = [];
|
|
141
|
+
}
|
|
142
|
+
wsInfoMap[wsInfo].push(device.did);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
const notConnNum = Object.keys(wsInfoMap).filter(wsInfo => this._connections[wsInfo] == null).length;
|
|
146
|
+
const connNum = Object.keys(this._connections).length;
|
|
147
|
+
if (this._maxSocketNum !== 0 && connNum + notConnNum > this._maxSocketNum) {
|
|
148
|
+
return { success: false, errorCode: errorCode.MAX_CONNECT };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
Object.entries(wsInfoMap).forEach(([wsInfo, dids]) => {
|
|
152
|
+
let conn = this._connections[wsInfo];
|
|
153
|
+
if (conn == null) {
|
|
154
|
+
conn = new Connection({
|
|
155
|
+
appID: this.appID,
|
|
156
|
+
token: this.token,
|
|
157
|
+
uid: this.uid,
|
|
158
|
+
wsInfo,
|
|
159
|
+
onDeviceStatusChanged: this._handleDeviceStatusChanged,
|
|
160
|
+
onBindingChanged: this._handleBindingChanged,
|
|
161
|
+
onSocketStatusChanged: this._handleSocketStatusChanged,
|
|
162
|
+
});
|
|
163
|
+
this._connections[wsInfo] = conn;
|
|
164
|
+
}
|
|
165
|
+
conn._addSubDid(...dids);
|
|
166
|
+
if (conn._websocket == null) {
|
|
167
|
+
conn._connectWS();
|
|
168
|
+
} else if (conn.ready) {
|
|
169
|
+
conn._subDevices(dids);
|
|
170
|
+
}
|
|
171
|
+
})
|
|
172
|
+
return { success: true };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
|
|
135
176
|
disConnectDevice = (device: IDevice) => {
|
|
136
177
|
const wsInfo = this._getWebsocketConnInfo(device);
|
|
137
178
|
let conn = this._connections[wsInfo];
|
|
138
179
|
if (conn) {
|
|
139
180
|
conn._unSubDevices([device.did])
|
|
140
181
|
}
|
|
141
|
-
return {success: true}
|
|
182
|
+
return { success: true }
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
disConnectDevices = (devices: IDevice[]) => {
|
|
186
|
+
const wsInfoMap: Record<string, string[]> = {};
|
|
187
|
+
devices.forEach(device => {
|
|
188
|
+
const wsInfo = this._getWebsocketConnInfo(device);
|
|
189
|
+
if (!wsInfoMap[wsInfo]) {
|
|
190
|
+
wsInfoMap[wsInfo] = [];
|
|
191
|
+
}
|
|
192
|
+
wsInfoMap[wsInfo].push(device.did);
|
|
193
|
+
});
|
|
194
|
+
Object.entries(wsInfoMap).forEach(([wsInfo, dids]) => {
|
|
195
|
+
const conn = this._connections[wsInfo];
|
|
196
|
+
if (conn) {
|
|
197
|
+
conn._unSubDevices(dids)
|
|
198
|
+
}
|
|
199
|
+
})
|
|
200
|
+
return { success: true }
|
|
142
201
|
}
|
|
143
202
|
|
|
144
203
|
connect = (did: string) => {
|
|
@@ -365,12 +424,12 @@ export class Connection {
|
|
|
365
424
|
// }
|
|
366
425
|
}
|
|
367
426
|
|
|
368
|
-
_addSubDid = (
|
|
369
|
-
this._subDids.add(did);
|
|
427
|
+
_addSubDid = (...dids) => {
|
|
428
|
+
dids.forEach(did => this._subDids.add(did));
|
|
370
429
|
}
|
|
371
430
|
|
|
372
|
-
_removeSubDid = (
|
|
373
|
-
this._subDids.delete(did);
|
|
431
|
+
_removeSubDid = (...dids) => {
|
|
432
|
+
dids.forEach(did => this._subDids.delete(did));
|
|
374
433
|
}
|
|
375
434
|
|
|
376
435
|
_connectWS = () => {
|
|
@@ -471,7 +530,10 @@ export class Connection {
|
|
|
471
530
|
this._websocket.close({});
|
|
472
531
|
this._websocket = null;
|
|
473
532
|
}
|
|
474
|
-
this.
|
|
533
|
+
if (this._currentSocketStatus) {
|
|
534
|
+
this._currentSocketStatus = false;
|
|
535
|
+
this._handleSocketStatusChanged(false);
|
|
536
|
+
}
|
|
475
537
|
}
|
|
476
538
|
|
|
477
539
|
handleClose = (res: { code: number, reason: string }) => {
|
|
@@ -536,13 +598,16 @@ export class Connection {
|
|
|
536
598
|
// 不处理
|
|
537
599
|
}
|
|
538
600
|
|
|
601
|
+
_currentSocketStatus = false;
|
|
602
|
+
|
|
539
603
|
_loginResp = (data) => {
|
|
540
604
|
if (data.success == true) {
|
|
541
605
|
this._loginFailedTimes = 0;
|
|
542
606
|
this.ready = true;
|
|
543
607
|
this._startPing();
|
|
544
608
|
this._subDevices([...this._subDids]);
|
|
545
|
-
this.
|
|
609
|
+
this._currentSocketStatus = true;
|
|
610
|
+
this._handleSocketStatusChanged(this._currentSocketStatus);
|
|
546
611
|
this._consumeWaitSends();
|
|
547
612
|
} else if (this._loginFailedTimes < 3) {
|
|
548
613
|
GizLog.log('GIZ_SDK: Login failed, will try again, please wait...');
|
|
@@ -569,12 +634,12 @@ export class Connection {
|
|
|
569
634
|
GizLog.log('GIZ_SDK: subscribe_res', data);
|
|
570
635
|
if (data && data.success) {
|
|
571
636
|
// 订阅成功设备获取设备状态
|
|
572
|
-
data.success.forEach(d => this._send({
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
}))
|
|
637
|
+
// data.success.forEach(d => this._send({
|
|
638
|
+
// cmd: "c2s_read",
|
|
639
|
+
// data: {
|
|
640
|
+
// did: d.did,
|
|
641
|
+
// }
|
|
642
|
+
// }))
|
|
578
643
|
}
|
|
579
644
|
GizLog.debug('GIZ_SDK: subscribe_res', data);
|
|
580
645
|
}
|
|
@@ -9,35 +9,51 @@ class ProductConfigFileManage {
|
|
|
9
9
|
data: IData = {};
|
|
10
10
|
key = 'dataPointConfig';
|
|
11
11
|
|
|
12
|
-
init = async () =>
|
|
12
|
+
init = async () => {
|
|
13
13
|
const data = await wx.getStorageSync(this.key) || '{}';
|
|
14
14
|
let dataJson = {};
|
|
15
15
|
try {
|
|
16
16
|
dataJson = JSON.parse(data);
|
|
17
17
|
} catch (error) {
|
|
18
|
-
|
|
18
|
+
|
|
19
19
|
}
|
|
20
|
-
this.data =dataJson;
|
|
20
|
+
this.data = dataJson;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
getConfigFileMap: Record<string, Promise<IDataPointConfig | null>> = {};
|
|
24
|
+
|
|
25
|
+
public getConfigFile = (pk: string): Promise<IDataPointConfig | null> => {
|
|
24
26
|
if (!this.data[pk]) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
this.data[pk] = data.data as IDataPointConfig;
|
|
30
|
-
// 更新缓存
|
|
31
|
-
wx.setStorageSync(this.key, JSON.stringify(this.data))
|
|
32
|
-
} else {
|
|
33
|
-
// 请求失败
|
|
34
|
-
return null;
|
|
27
|
+
|
|
28
|
+
if (this.getConfigFileMap[pk]) {
|
|
29
|
+
// 正在请求中
|
|
30
|
+
return this.getConfigFileMap[pk];
|
|
35
31
|
}
|
|
32
|
+
|
|
33
|
+
const promise = new Promise<IDataPointConfig | null>(async (resolve) => {
|
|
34
|
+
// 不在缓存里,去云端获取
|
|
35
|
+
const url = `/app/datapoint?product_key=${pk}`;
|
|
36
|
+
const data = await openApiRequest<IDataPointConfig>(`${url}`, { method: 'GET' });
|
|
37
|
+
if (data.success && data.data?.entities) {
|
|
38
|
+
this.data[pk] = data.data as IDataPointConfig;
|
|
39
|
+
resolve(this.data[pk]);
|
|
40
|
+
// 更新缓存
|
|
41
|
+
wx.setStorageSync(this.key, JSON.stringify(this.data))
|
|
42
|
+
} else {
|
|
43
|
+
// 请求失败
|
|
44
|
+
resolve(null);
|
|
45
|
+
}
|
|
46
|
+
this.getConfigFileMap[pk] = null;
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
this.getConfigFileMap[pk] = promise;
|
|
50
|
+
return promise;
|
|
51
|
+
|
|
36
52
|
}
|
|
37
|
-
return this.data[pk];
|
|
53
|
+
return Promise.resolve(this.data[pk]);
|
|
38
54
|
}
|
|
39
55
|
|
|
40
|
-
public getAttrItem = async (pk:string, key: string) => {
|
|
56
|
+
public getAttrItem = async (pk: string, key: string) => {
|
|
41
57
|
if (!this.data[pk]) {
|
|
42
58
|
await this.getConfigFile(pk)
|
|
43
59
|
}
|
|
@@ -55,4 +71,4 @@ class ProductConfigFileManage {
|
|
|
55
71
|
const productConfigFileManage = new ProductConfigFileManage();
|
|
56
72
|
productConfigFileManage.init();
|
|
57
73
|
|
|
58
|
-
export default productConfigFileManage;
|
|
74
|
+
export default productConfigFileManage;
|
package/src/sdk.ts
CHANGED
|
@@ -30,6 +30,7 @@ import { AnonymousLogin, ILoginRes } from './services/login';
|
|
|
30
30
|
import { uploadP0 } from './services/uploadP0';
|
|
31
31
|
import sleep from './sleep';
|
|
32
32
|
import {
|
|
33
|
+
getDeviceId,
|
|
33
34
|
getFirstConnectType,
|
|
34
35
|
getProductInfoThroughPK,
|
|
35
36
|
isSameDevice,
|
|
@@ -352,12 +353,12 @@ class GizwitsMiniSDK {
|
|
|
352
353
|
this.bleHandle.pks = this.specialProductKeys;
|
|
353
354
|
this.bleHandle.offlineThreshold = props.offlineThreshold;
|
|
354
355
|
|
|
355
|
-
const { appID, token, cloudServiceInfo = {openAPIInfo: ''} } = props;
|
|
356
|
+
const { appID, token, cloudServiceInfo = { openAPIInfo: '' } } = props;
|
|
356
357
|
const openAPIInfo = (cloudServiceInfo && cloudServiceInfo.openAPIInfo) || 'api.gizwits.com';
|
|
357
358
|
if (appID !== getGlobalData('appID') ||
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
359
|
+
token !== getGlobalData('token') ||
|
|
360
|
+
openAPIInfo !== getGlobalData('cloudServiceInfo').openAPIInfo
|
|
361
|
+
) {
|
|
361
362
|
// 需要重启
|
|
362
363
|
this.socketHandle.destory();
|
|
363
364
|
this._gizSocket = null;
|
|
@@ -487,9 +488,9 @@ class GizwitsMiniSDK {
|
|
|
487
488
|
case '0072': {
|
|
488
489
|
// 设备回复是否允许同步数据
|
|
489
490
|
const requestSyncData = new RequestSync(bleBytesData)
|
|
490
|
-
GizLog.debug("设备回复是否允许同步, state:"
|
|
491
|
+
GizLog.debug("设备回复是否允许同步, state:", requestSyncData.state)
|
|
491
492
|
|
|
492
|
-
this.syncDataLengthCallback && this.syncDataLengthCallback({success: requestSyncData.state == 0, data: requestSyncData.len})
|
|
493
|
+
this.syncDataLengthCallback && this.syncDataLengthCallback({ success: requestSyncData.state == 0, data: requestSyncData.len })
|
|
493
494
|
|
|
494
495
|
if (!this.syncDataCallBack) break;
|
|
495
496
|
if (requestSyncData.state === 0) {
|
|
@@ -529,7 +530,7 @@ class GizwitsMiniSDK {
|
|
|
529
530
|
}
|
|
530
531
|
|
|
531
532
|
const productConfig = this.productInfo.find(item => item.productKey === this.syncDevice.productKey)
|
|
532
|
-
if(productConfig) {
|
|
533
|
+
if (productConfig) {
|
|
533
534
|
const uploadRes = await uploadP0({
|
|
534
535
|
device: this.syncDevice,
|
|
535
536
|
data: [
|
|
@@ -575,7 +576,7 @@ class GizwitsMiniSDK {
|
|
|
575
576
|
// 设备上报停止同步
|
|
576
577
|
|
|
577
578
|
const cancelData = new CancelSync(bleBytesData)
|
|
578
|
-
GizLog.debug("设备上报停止同步 state:", cancelData.state
|
|
579
|
+
GizLog.debug("设备上报停止同步 state:", cancelData.state)
|
|
579
580
|
|
|
580
581
|
if (cancelData.state === 0) {
|
|
581
582
|
// 同步完成
|
|
@@ -714,6 +715,32 @@ class GizwitsMiniSDK {
|
|
|
714
715
|
}
|
|
715
716
|
};
|
|
716
717
|
|
|
718
|
+
|
|
719
|
+
/**
|
|
720
|
+
* @description 更改设备列表中的元信息,包括设备mac,设备在线状态等
|
|
721
|
+
*/
|
|
722
|
+
public setDevicesMeta = <K extends keyof IDevice>(
|
|
723
|
+
devices: IDevice[],
|
|
724
|
+
key: K,
|
|
725
|
+
value: IDevice[K],
|
|
726
|
+
force: boolean = false
|
|
727
|
+
) => {
|
|
728
|
+
let hasChange = false;
|
|
729
|
+
const targets = devices.map((dev) => this.allDevices.find((item) => item.mac === dev.mac));
|
|
730
|
+
for (let i = 0; i < targets.length; i++) {
|
|
731
|
+
const target = targets[i];
|
|
732
|
+
if (target) {
|
|
733
|
+
if (target[key] !== value || force) {
|
|
734
|
+
target[key] = value;
|
|
735
|
+
hasChange = true;
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
hasChange && this.notiDeviceList();
|
|
740
|
+
};
|
|
741
|
+
|
|
742
|
+
|
|
743
|
+
|
|
717
744
|
static getVersion = () => {
|
|
718
745
|
return {
|
|
719
746
|
success: true,
|
|
@@ -744,7 +771,7 @@ class GizwitsMiniSDK {
|
|
|
744
771
|
GizLog.debug('init BLE succcess', permissionData);
|
|
745
772
|
} catch (error) {
|
|
746
773
|
GizLog.error('init BLE error', error);
|
|
747
|
-
return {success: false, err: errorCode.GIZ_SDK_BLE_BLUETOOTH_FUNCTION_NOT_TURNED_ON}
|
|
774
|
+
return { success: false, err: errorCode.GIZ_SDK_BLE_BLUETOOTH_FUNCTION_NOT_TURNED_ON }
|
|
748
775
|
}
|
|
749
776
|
return { success: true };
|
|
750
777
|
};
|
|
@@ -872,7 +899,7 @@ class GizwitsMiniSDK {
|
|
|
872
899
|
}
|
|
873
900
|
|
|
874
901
|
// 同步蓝牙设备的离线数据
|
|
875
|
-
syncDeviceData =async (device: IDevice, callback: SyncCallBack = () => {}) => {
|
|
902
|
+
syncDeviceData = async (device: IDevice, callback: SyncCallBack = () => { }) => {
|
|
876
903
|
const target = this.allDevices.find((item) => isSameDevice(item, device));
|
|
877
904
|
if (!target) {
|
|
878
905
|
return { success: false, message: 'target is undefind' };
|
|
@@ -888,7 +915,7 @@ class GizwitsMiniSDK {
|
|
|
888
915
|
}
|
|
889
916
|
}
|
|
890
917
|
|
|
891
|
-
this.syncDevice = {...target}
|
|
918
|
+
this.syncDevice = { ...target }
|
|
892
919
|
|
|
893
920
|
switch (target.connectType) {
|
|
894
921
|
case 'BLE': {
|
|
@@ -915,7 +942,7 @@ class GizwitsMiniSDK {
|
|
|
915
942
|
}
|
|
916
943
|
|
|
917
944
|
// 查询设备里面还有多少数据需要同步
|
|
918
|
-
queryNeedSyncDataLength = async(device: IDevice): Promise<ISDKResult<number>> => {
|
|
945
|
+
queryNeedSyncDataLength = async (device: IDevice): Promise<ISDKResult<number>> => {
|
|
919
946
|
const target = this.allDevices.find((item) => isSameDevice(item, device));
|
|
920
947
|
if (!target) {
|
|
921
948
|
return { success: false, message: 'target is undefind' };
|
|
@@ -927,14 +954,14 @@ class GizwitsMiniSDK {
|
|
|
927
954
|
case 'BLE': {
|
|
928
955
|
const data = RequestSync.pack();
|
|
929
956
|
// 发送开始同步的指令
|
|
930
|
-
this.syncDevice = {...target}
|
|
957
|
+
this.syncDevice = { ...target }
|
|
931
958
|
GizLog.debug("sent RequestSync data len");
|
|
932
959
|
this.bleHandle.write(device.bleDeviceID, numberArray2Uint8Array(data).buffer)
|
|
933
960
|
return new Promise((res) => {
|
|
934
961
|
this.syncDataLengthCallback = res;
|
|
935
962
|
setTimeout(() => {
|
|
936
963
|
try {
|
|
937
|
-
res({success: false, message: 'timeout'})
|
|
964
|
+
res({ success: false, message: 'timeout' })
|
|
938
965
|
} catch (error) {
|
|
939
966
|
|
|
940
967
|
}
|
|
@@ -948,7 +975,7 @@ class GizwitsMiniSDK {
|
|
|
948
975
|
}
|
|
949
976
|
}
|
|
950
977
|
|
|
951
|
-
public cancelSyncDeviceData =async () => {
|
|
978
|
+
public cancelSyncDeviceData = async () => {
|
|
952
979
|
this.cleanSyncDeviceData();
|
|
953
980
|
}
|
|
954
981
|
|
|
@@ -990,7 +1017,7 @@ class GizwitsMiniSDK {
|
|
|
990
1017
|
if (data.success) {
|
|
991
1018
|
setGlobalData('token', data.data?.token);
|
|
992
1019
|
setGlobalData('uid', data.data?.uid);
|
|
993
|
-
this.socketHandle.updateToken({token: data.data.token, uid: data.data.uid})
|
|
1020
|
+
this.socketHandle.updateToken({ token: data.data.token, uid: data.data.uid })
|
|
994
1021
|
}
|
|
995
1022
|
return data;
|
|
996
1023
|
};
|
|
@@ -1099,6 +1126,75 @@ class GizwitsMiniSDK {
|
|
|
1099
1126
|
};
|
|
1100
1127
|
};
|
|
1101
1128
|
|
|
1129
|
+
batchSubscribe = async (devices: IDevice[], connectType?: TConnectType) => {
|
|
1130
|
+
GizLog.debug("subscribe devices =>", devices);
|
|
1131
|
+
const sdkDevicesMap = this.allDevices.reduce((acc, cur) => ({
|
|
1132
|
+
...acc,
|
|
1133
|
+
[getDeviceId(cur)]: cur
|
|
1134
|
+
}), {});
|
|
1135
|
+
const foundDevices = devices.filter((device) => sdkDevicesMap[getDeviceId(device)]);
|
|
1136
|
+
if (foundDevices.length === 0) {
|
|
1137
|
+
GizLog.warn('Device Target Not Found')
|
|
1138
|
+
GizLog.warn('current devices ', this.allDevices)
|
|
1139
|
+
return { success: false, message: "device not found" };
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
const connectTypeMap: Record<string, IDevice[]> = foundDevices.reduce((acc, cur) => {
|
|
1143
|
+
const type = cur.connectType === 'BLE_BROADCAST' ? 'BLE_BROADCAST' : getFirstConnectType(cur, connectType);
|
|
1144
|
+
if (!acc[type]) {
|
|
1145
|
+
acc[type] = [];
|
|
1146
|
+
}
|
|
1147
|
+
acc[type].push(cur);
|
|
1148
|
+
return acc;
|
|
1149
|
+
}, {});
|
|
1150
|
+
|
|
1151
|
+
const types = Object.keys(connectTypeMap);
|
|
1152
|
+
// 广播模式不需要连接,所以默认是连接状态
|
|
1153
|
+
if (types.length === 1 && types[0] === 'BLE_BROADCAST') {
|
|
1154
|
+
return {
|
|
1155
|
+
success: true,
|
|
1156
|
+
message: '',
|
|
1157
|
+
};
|
|
1158
|
+
}
|
|
1159
|
+
|
|
1160
|
+
if ((connectTypeMap['BLE'] ?? []).length > 0 || (connectTypeMap['LAN'] ?? []).length > 0) {
|
|
1161
|
+
return {
|
|
1162
|
+
success: false,
|
|
1163
|
+
message: 'BLE and LAN can not subscribe together'
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
const wanDevices = connectTypeMap['WAN'] ?? [];
|
|
1168
|
+
if (wanDevices.length === 0) {
|
|
1169
|
+
return {
|
|
1170
|
+
success: false,
|
|
1171
|
+
message: 'no WAN device'
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
|
|
1175
|
+
const wanUnsubscribeDevices = wanDevices.filter((device) => {
|
|
1176
|
+
return device.connectType !== 'NONE' && device.connectType !== 'WAN';
|
|
1177
|
+
});
|
|
1178
|
+
|
|
1179
|
+
if (wanUnsubscribeDevices.length > 0) {
|
|
1180
|
+
await this.batchWanUnSubscribe(wanUnsubscribeDevices);
|
|
1181
|
+
}
|
|
1182
|
+
|
|
1183
|
+
const data = this.socketHandle.connectDevices(wanDevices);
|
|
1184
|
+
if (data.success) {
|
|
1185
|
+
this.setDevicesMeta(wanDevices, 'connectType', 'WAN', true);
|
|
1186
|
+
}
|
|
1187
|
+
return data;
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
batchWanUnSubscribe = async (devices: IDevice[]) => {
|
|
1191
|
+
const res = await this.socketHandle.disConnectDevices(devices);
|
|
1192
|
+
if (res.success) {
|
|
1193
|
+
this.setDevicesMeta(devices, 'connectType', 'NONE', true);
|
|
1194
|
+
}
|
|
1195
|
+
return res;
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1102
1198
|
/**
|
|
1103
1199
|
*
|
|
1104
1200
|
* @param device 待取消订阅设备
|
|
@@ -1169,7 +1265,7 @@ class GizwitsMiniSDK {
|
|
|
1169
1265
|
|
|
1170
1266
|
checkUpdate = async ({
|
|
1171
1267
|
device, type
|
|
1172
|
-
}: {device: IDevice, type: IOTAType}) => {
|
|
1268
|
+
}: { device: IDevice, type: IOTAType }) => {
|
|
1173
1269
|
const target = this.allDevices.find((item) => isSameDevice(item, device));
|
|
1174
1270
|
if (target) {
|
|
1175
1271
|
if (!target.did) {
|
|
@@ -1179,9 +1275,9 @@ class GizwitsMiniSDK {
|
|
|
1179
1275
|
err: errorCode.GIZ_OPENAPI_DEVICE_NOT_BOUND,
|
|
1180
1276
|
}
|
|
1181
1277
|
}
|
|
1182
|
-
switch(target.connectType) {
|
|
1278
|
+
switch (target.connectType) {
|
|
1183
1279
|
case 'BLE': {
|
|
1184
|
-
return this.bleHandle.checkUpdate({device,type})
|
|
1280
|
+
return this.bleHandle.checkUpdate({ device, type })
|
|
1185
1281
|
}
|
|
1186
1282
|
default: {
|
|
1187
1283
|
return {
|
|
@@ -1199,7 +1295,7 @@ class GizwitsMiniSDK {
|
|
|
1199
1295
|
|
|
1200
1296
|
getDeviceInfo = async ({
|
|
1201
1297
|
device
|
|
1202
|
-
}: {device: IDevice}) => {
|
|
1298
|
+
}: { device: IDevice }) => {
|
|
1203
1299
|
const target = this.allDevices.find((item) => isSameDevice(item, device));
|
|
1204
1300
|
if (target) {
|
|
1205
1301
|
if (!target.did) {
|
|
@@ -1209,9 +1305,9 @@ class GizwitsMiniSDK {
|
|
|
1209
1305
|
err: errorCode.GIZ_OPENAPI_DEVICE_NOT_BOUND,
|
|
1210
1306
|
}
|
|
1211
1307
|
}
|
|
1212
|
-
switch(target.connectType) {
|
|
1308
|
+
switch (target.connectType) {
|
|
1213
1309
|
case 'BLE': {
|
|
1214
|
-
return this.bleHandle.getDeviceInfo({deviceId: device.bleDeviceID})
|
|
1310
|
+
return this.bleHandle.getDeviceInfo({ deviceId: device.bleDeviceID })
|
|
1215
1311
|
}
|
|
1216
1312
|
default: {
|
|
1217
1313
|
return {
|
|
@@ -1226,10 +1322,10 @@ class GizwitsMiniSDK {
|
|
|
1226
1322
|
err: errorCode.GIZ_SDK_DEVICE_NOT_SUBSCRIBED,
|
|
1227
1323
|
};
|
|
1228
1324
|
}
|
|
1229
|
-
|
|
1325
|
+
|
|
1230
1326
|
startUpgrade = async ({
|
|
1231
1327
|
device, type, onProgress, onEvent
|
|
1232
|
-
}: {device: IDevice, type: IOTAType, onProgress: OTAProgressEventCallback, onEvent: OTAEventCallback}) => {
|
|
1328
|
+
}: { device: IDevice, type: IOTAType, onProgress: OTAProgressEventCallback, onEvent: OTAEventCallback }) => {
|
|
1233
1329
|
const target = this.allDevices.find((item) => isSameDevice(item, device));
|
|
1234
1330
|
if (target) {
|
|
1235
1331
|
if (!target.did) {
|
|
@@ -1239,9 +1335,9 @@ class GizwitsMiniSDK {
|
|
|
1239
1335
|
err: errorCode.GIZ_OPENAPI_DEVICE_NOT_BOUND,
|
|
1240
1336
|
}
|
|
1241
1337
|
}
|
|
1242
|
-
switch(target.connectType) {
|
|
1338
|
+
switch (target.connectType) {
|
|
1243
1339
|
case 'BLE': {
|
|
1244
|
-
return this.bleHandle.startUpgrade({device,type, productKey: device.productKey, onProgress, onEvent})
|
|
1340
|
+
return this.bleHandle.startUpgrade({ device, type, productKey: device.productKey, onProgress, onEvent })
|
|
1245
1341
|
}
|
|
1246
1342
|
default: {
|
|
1247
1343
|
return {
|
|
@@ -1360,7 +1456,7 @@ class GizwitsMiniSDK {
|
|
|
1360
1456
|
const cloudServiceInfo = getGlobalData('cloudServiceInfo');
|
|
1361
1457
|
|
|
1362
1458
|
console.log('cloudServiceInfo', cloudServiceInfo)
|
|
1363
|
-
|
|
1459
|
+
|
|
1364
1460
|
this.currentWifiConfigHandle = new NFCConfig(
|
|
1365
1461
|
ssid,
|
|
1366
1462
|
bssid,
|
|
@@ -1471,7 +1567,7 @@ class GizwitsMiniSDK {
|
|
|
1471
1567
|
return GizLog.setLogLevel(level);
|
|
1472
1568
|
}
|
|
1473
1569
|
|
|
1474
|
-
public renameDevice =async (device:IDevice, name: string) => {
|
|
1570
|
+
public renameDevice = async (device: IDevice, name: string) => {
|
|
1475
1571
|
if (!name) {
|
|
1476
1572
|
return {
|
|
1477
1573
|
success: false,
|