mini_program_gizwits_sdk 3.6.4 → 3.6.5
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/package.json +1 -1
- package/yarn-error.log +6901 -0
- package/dist/src/GizLog.d.ts +0 -18
- package/dist/src/crc/calculators/crc16modbus.d.ts +0 -2
- package/dist/src/crc/calculators/crc32.d.ts +0 -2
- package/dist/src/crc/crc16modbus.d.ts +0 -7
- package/dist/src/crc/crc32.d.ts +0 -7
- package/dist/src/crc/create_buffer.d.ts +0 -3
- package/dist/src/crc/define_crc.d.ts +0 -7
- package/dist/src/crc/index.d.ts +0 -19
- package/dist/src/crc/types.d.ts +0 -11
- package/dist/src/errorCode.d.ts +0 -3
- package/dist/src/globalData.d.ts +0 -2
- package/dist/src/handler/EventListener.d.ts +0 -9
- package/dist/src/handler/ble.d.ts +0 -105
- package/dist/src/handler/lan.d.ts +0 -78
- package/dist/src/handler/socket.d.ts +0 -151
- package/dist/src/openApiRequest.d.ts +0 -3
- package/dist/src/productConfigFileManage.d.ts +0 -11
- package/dist/src/protocol/Bind.d.ts +0 -7
- package/dist/src/protocol/DeviceInfo.d.ts +0 -11
- package/dist/src/protocol/DiscoverUDP.d.ts +0 -17
- package/dist/src/protocol/GetDeviceStatus.d.ts +0 -5
- package/dist/src/protocol/Login.d.ts +0 -12
- package/dist/src/protocol/Ntp.d.ts +0 -5
- package/dist/src/protocol/OTA/AwaitNoti.d.ts +0 -7
- package/dist/src/protocol/OTA/CheckCanOTA.d.ts +0 -10
- package/dist/src/protocol/OTA/OTAPackPackage.d.ts +0 -8
- package/dist/src/protocol/OTA/OtaComplete.d.ts +0 -8
- package/dist/src/protocol/OTA/PreOTA.d.ts +0 -11
- package/dist/src/protocol/OTA/ResetPosition.d.ts +0 -6
- package/dist/src/protocol/OffLineData.d.ts +0 -23
- package/dist/src/protocol/ProtocolBase.d.ts +0 -11
- package/dist/src/protocol/Reset.d.ts +0 -8
- package/dist/src/protocol/SetReset.d.ts +0 -7
- package/dist/src/protocol/WifiConfig.d.ts +0 -6
- package/dist/src/protocol/dataPoint.d.ts +0 -34
- package/dist/src/protocol/tool.d.ts +0 -14
- package/dist/src/randomCode.d.ts +0 -3
- package/dist/src/request.d.ts +0 -1
- package/dist/src/sdk.d.ts +0 -136
- package/dist/src/sentry.d.ts +0 -2
- package/dist/src/services/devices.d.ts +0 -47
- package/dist/src/services/login.d.ts +0 -4
- package/dist/src/services/monitor.d.ts +0 -12
- package/dist/src/services/ota.d.ts +0 -7
- package/dist/src/services/tool.d.ts +0 -4
- package/dist/src/services/uploadP0.d.ts +0 -14
- package/dist/src/sleep.d.ts +0 -2
- package/dist/src/types/index.d.ts +0 -361
- package/dist/src/utils.d.ts +0 -46
- package/dist/src/wechatApi.d.ts +0 -19
- package/dist/src/wifiConfig/ConfigBase.d.ts +0 -27
- package/dist/src/wifiConfig/ap.d.ts +0 -11
- package/dist/src/wifiConfig/ble.d.ts +0 -41
- package/dist/src/wifiConfig/nfc/disposeData.d.ts +0 -2
- package/dist/src/wifiConfig/nfc/formatData.d.ts +0 -3
- package/dist/src/wifiConfig/nfc/formatSSID.d.ts +0 -6
- package/dist/src/wifiConfig/nfc/index.d.ts +0 -22
- package/dist/src/wifiConfig/nfc/isIncludedCH.d.ts +0 -1
- package/dist/src/wifiConfig/nfc/nfcAConnect.d.ts +0 -6
- package/dist/src/wifiConfig/nfc/nfcATransceive.d.ts +0 -5
- package/dist/src/wifiConfig/nfc/ssIDToASC.d.ts +0 -1
- package/dist/src/wifiConfig/nfc/stringToGbk.d.ts +0 -1
- package/dist/src/wifiConfig/nfc/transceive.wx.d.ts +0 -2
- package/dist/src/wifiConfig/nfc/types.d.ts +0 -15
- package/index.ts +0 -8
- package/src/GizLog.ts +0 -51
- package/src/crc/calculators/crc16modbus.ts +0 -36
- package/src/crc/calculators/crc32.ts +0 -47
- package/src/crc/crc16modbus.ts +0 -3
- package/src/crc/crc32.ts +0 -4
- package/src/crc/create_buffer.ts +0 -7
- package/src/crc/define_crc.ts +0 -9
- package/src/crc/index.ts +0 -10
- package/src/crc/package.json +0 -3
- package/src/crc/types.ts +0 -10
- package/src/errorCode.ts +0 -83
- package/src/globalData.ts +0 -9
- package/src/handler/EventListener.ts +0 -44
- package/src/handler/ble.ts +0 -1369
- package/src/handler/lan.ts +0 -389
- package/src/handler/socket.ts +0 -686
- package/src/openApiRequest.ts +0 -50
- package/src/productConfigFileManage.ts +0 -72
- package/src/protocol/Bind.ts +0 -22
- package/src/protocol/DeviceInfo.ts +0 -37
- package/src/protocol/DiscoverUDP.ts +0 -80
- package/src/protocol/GetDeviceStatus.ts +0 -61
- package/src/protocol/Login.ts +0 -43
- package/src/protocol/Ntp.ts +0 -13
- package/src/protocol/OTA/AwaitNoti.ts +0 -14
- package/src/protocol/OTA/CheckCanOTA.ts +0 -23
- package/src/protocol/OTA/OTAPackPackage.ts +0 -37
- package/src/protocol/OTA/OtaComplete.ts +0 -17
- package/src/protocol/OTA/PreOTA.ts +0 -48
- package/src/protocol/OTA/ResetPosition.ts +0 -14
- package/src/protocol/OffLineData.ts +0 -81
- package/src/protocol/ProtocolBase.ts +0 -54
- package/src/protocol/Reset.ts +0 -16
- package/src/protocol/SetReset.ts +0 -13
- package/src/protocol/WifiConfig.ts +0 -42
- package/src/protocol/dataPoint.ts +0 -687
- package/src/protocol/tool.ts +0 -140
- package/src/randomCode.ts +0 -25
- package/src/request.ts +0 -22
- package/src/sdk.ts +0 -1536
- package/src/sentry.ts +0 -55
- package/src/services/devices.ts +0 -313
- package/src/services/login.ts +0 -11
- package/src/services/monitor.ts +0 -117
- package/src/services/ota.ts +0 -17
- package/src/services/tool.ts +0 -10
- package/src/services/uploadP0.ts +0 -57
- package/src/sleep.ts +0 -2
- package/src/types/index.ts +0 -456
- package/src/utils.ts +0 -449
- package/src/wechatApi.ts +0 -211
- package/src/wifiConfig/ConfigBase.ts +0 -235
- package/src/wifiConfig/ap.ts +0 -185
- package/src/wifiConfig/ble.ts +0 -460
- package/src/wifiConfig/nfc/disposeData.ts +0 -63
- package/src/wifiConfig/nfc/formatData.ts +0 -46
- package/src/wifiConfig/nfc/formatSSID.ts +0 -59
- package/src/wifiConfig/nfc/index.ts +0 -134
- package/src/wifiConfig/nfc/isIncludedCH.ts +0 -8
- package/src/wifiConfig/nfc/nfcAConnect.ts +0 -30
- package/src/wifiConfig/nfc/nfcATransceive.ts +0 -29
- package/src/wifiConfig/nfc/ssIDToASC.ts +0 -12
- package/src/wifiConfig/nfc/stringToGbk.ts +0 -51
- package/src/wifiConfig/nfc/transceive.wx.ts +0 -124
- package/src/wifiConfig/nfc/types.ts +0 -18
- package/src/wifiConfig/timeZoneName.json +0 -1
package/src/sdk.ts
DELETED
|
@@ -1,1536 +0,0 @@
|
|
|
1
|
-
import { getGlobalData, setGlobalData } from './globalData';
|
|
2
|
-
|
|
3
|
-
import sdkConfig from '../package.json';
|
|
4
|
-
import { LanHandle } from './handler/lan';
|
|
5
|
-
import productConfigFileManage from './productConfigFileManage';
|
|
6
|
-
import { formatEnum, pack, unpack } from './protocol/dataPoint';
|
|
7
|
-
import Ntp from './protocol/Ntp';
|
|
8
|
-
import { CancelSync, DeletePackage, RequestSync, SyncPackage } from './protocol/OffLineData';
|
|
9
|
-
import ProtocolBase from './protocol/ProtocolBase';
|
|
10
|
-
import { arrayToString, hexStrint2byte } from './protocol/tool';
|
|
11
|
-
import { BleHandle } from './handler/ble';
|
|
12
|
-
import GizwitsWS from './handler/socket';
|
|
13
|
-
import Reset from './protocol/Reset';
|
|
14
|
-
import SetReset from './protocol/SetReset';
|
|
15
|
-
|
|
16
|
-
import GizLog, { TLogType } from './GizLog';
|
|
17
|
-
import errorCode from './errorCode';
|
|
18
|
-
|
|
19
|
-
import {
|
|
20
|
-
bindMac,
|
|
21
|
-
editBindInfo,
|
|
22
|
-
getBindingList,
|
|
23
|
-
ISafeRegisterReturn,
|
|
24
|
-
IUnbindReturn,
|
|
25
|
-
safeRegister,
|
|
26
|
-
unbindDevice
|
|
27
|
-
} from './services/devices';
|
|
28
|
-
import { AnonymousLogin } from './services/login';
|
|
29
|
-
import { uploadP0 } from './services/uploadP0';
|
|
30
|
-
import sleep from './sleep';
|
|
31
|
-
import {
|
|
32
|
-
getDeviceId,
|
|
33
|
-
getFirstConnectType,
|
|
34
|
-
getProductInfoThroughPK,
|
|
35
|
-
isSameDevice,
|
|
36
|
-
merageBleLocalDevices,
|
|
37
|
-
merageLanLocalDevices,
|
|
38
|
-
numberArray2Uint8Array
|
|
39
|
-
} from './utils';
|
|
40
|
-
import ApConfig from './wifiConfig/ap';
|
|
41
|
-
import BLEConfig from './wifiConfig/ble';
|
|
42
|
-
import NFCConfig from './wifiConfig/nfc';
|
|
43
|
-
import { IDevice, TConnectType, IOnDeviceStatusChanged,
|
|
44
|
-
IOpenApiDevice,
|
|
45
|
-
IDeviceStatusChangedProps, IOnSocketStatusChanged,
|
|
46
|
-
IProductInfo, GizwitsSdkOption, SyncCallBack, ISDKResult,
|
|
47
|
-
IResult, IOTAType, IBindRemoteDeviceParams, ICloudServiceInfo,
|
|
48
|
-
TListenerType, IDeviceSafetyParams, ISetDeviceOnboardingDeployProps,
|
|
49
|
-
IDataPointConfig, OTAEventCallback, OTAProgressEventCallback, ILoginRes,
|
|
50
|
-
IDeviceSafetyRegisterParams } from './types';
|
|
51
|
-
|
|
52
|
-
const version = sdkConfig.version;
|
|
53
|
-
class GizwitsMiniSDK {
|
|
54
|
-
private listenerMap: any = {};
|
|
55
|
-
private keepScanTimer: any = null;
|
|
56
|
-
|
|
57
|
-
// 云端获取的设备列表
|
|
58
|
-
private _deviceList: IDevice[] = [];
|
|
59
|
-
private get deviceList() {
|
|
60
|
-
return this._deviceList;
|
|
61
|
-
}
|
|
62
|
-
private set deviceList(data: IDevice[]) {
|
|
63
|
-
// TODO 覆盖的时候 需要保留netStatus
|
|
64
|
-
this._deviceList = data;
|
|
65
|
-
|
|
66
|
-
// TODO 判断是否发生改变
|
|
67
|
-
this.notiDeviceList();
|
|
68
|
-
}
|
|
69
|
-
// 云端获取的设备列表
|
|
70
|
-
|
|
71
|
-
// ble device
|
|
72
|
-
private _bleDevices: IDevice[] = [];
|
|
73
|
-
public get bleDevices() {
|
|
74
|
-
return this._bleDevices;
|
|
75
|
-
}
|
|
76
|
-
public set bleDevices(data: IDevice[]) {
|
|
77
|
-
this._bleDevices = data;
|
|
78
|
-
|
|
79
|
-
this.notiDeviceList();
|
|
80
|
-
}
|
|
81
|
-
// ble device
|
|
82
|
-
|
|
83
|
-
// udp device
|
|
84
|
-
private _lanDevices: IDevice[] = [];
|
|
85
|
-
private get lanDevices() {
|
|
86
|
-
return this._lanDevices;
|
|
87
|
-
}
|
|
88
|
-
private set lanDevices(data: IDevice[]) {
|
|
89
|
-
this._lanDevices = data;
|
|
90
|
-
this.notiDeviceList();
|
|
91
|
-
}
|
|
92
|
-
// udp device
|
|
93
|
-
|
|
94
|
-
private get allDevices() {
|
|
95
|
-
let newDevices = merageBleLocalDevices(
|
|
96
|
-
[...this.deviceList],
|
|
97
|
-
this.bleDevices
|
|
98
|
-
);
|
|
99
|
-
newDevices = merageLanLocalDevices(newDevices, this.lanDevices);
|
|
100
|
-
/**
|
|
101
|
-
* 只能有一种通讯方式
|
|
102
|
-
* 依次LAN BLE WAN
|
|
103
|
-
* 现在没有局域网,先跳过
|
|
104
|
-
*/
|
|
105
|
-
this.bleHandle.connectedList.map((item) => {
|
|
106
|
-
const index = newDevices.findIndex(
|
|
107
|
-
(device) => device.bleDeviceID === item.deviceId
|
|
108
|
-
);
|
|
109
|
-
if (index !== -1) {
|
|
110
|
-
newDevices[index].connectType = 'BLE';
|
|
111
|
-
}
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
const connectionMap = this.socketHandle._connections;
|
|
115
|
-
|
|
116
|
-
Object.keys(connectionMap).forEach((wsInfo) => {
|
|
117
|
-
const conn = connectionMap[wsInfo];
|
|
118
|
-
if (conn) {
|
|
119
|
-
conn._subDids.forEach((did) => {
|
|
120
|
-
const index = newDevices.findIndex((device) => device.did === did);
|
|
121
|
-
if (index !== -1) {
|
|
122
|
-
newDevices[index].connectType = 'WAN';
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
const lanConnected = this.lanHandle.connectedList;
|
|
129
|
-
lanConnected.map(item => {
|
|
130
|
-
const index = newDevices.findIndex(
|
|
131
|
-
(device) => device.mac === item.mac && device.productKey === item.productKey
|
|
132
|
-
);
|
|
133
|
-
if (index !== -1) {
|
|
134
|
-
newDevices[index].connectType = 'LAN';
|
|
135
|
-
}
|
|
136
|
-
})
|
|
137
|
-
return newDevices;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// private set allDevice(data: IDevice[]) {
|
|
141
|
-
// this.allDevice = data;
|
|
142
|
-
// this.notiDeviceList();
|
|
143
|
-
// }
|
|
144
|
-
|
|
145
|
-
private get bleScanDevice() {
|
|
146
|
-
return this.bleDevices.filter((item) => item.isBleOnline === true);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// private set bleScanDevice(data: IDevice[]){
|
|
150
|
-
// this.bleDevices = data;
|
|
151
|
-
// }
|
|
152
|
-
|
|
153
|
-
private productInfo: IProductInfo[] = [];
|
|
154
|
-
// private cloudServiceInfo: any;
|
|
155
|
-
// 保存当前执行中的配网handle
|
|
156
|
-
|
|
157
|
-
private currentWifiConfigHandle: any;
|
|
158
|
-
|
|
159
|
-
private _bleHandle: BleHandle;
|
|
160
|
-
private _lanHandle: LanHandle;
|
|
161
|
-
|
|
162
|
-
// private autoScanInterval: IntervalHandle = null;
|
|
163
|
-
|
|
164
|
-
private _gizSocket: GizwitsWS;
|
|
165
|
-
private offlineThreshold?: number;
|
|
166
|
-
|
|
167
|
-
// 离线同步
|
|
168
|
-
private syncDataCallBack?: SyncCallBack = null
|
|
169
|
-
private syncDataLengthCallback?: any;
|
|
170
|
-
private syncTotalNum: number = 0
|
|
171
|
-
private syncCurrnetNum: number = 0
|
|
172
|
-
private syncDataTimoutTimer: any = null;
|
|
173
|
-
private SYNC_TIMEOUT: number = 4 * 60 * 1000
|
|
174
|
-
private syncDevice?: IDevice;
|
|
175
|
-
|
|
176
|
-
constructor(props: GizwitsSdkOption) {
|
|
177
|
-
this.init(props);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
private get bleHandle() {
|
|
181
|
-
// 初始化蓝牙能力
|
|
182
|
-
if (!this._bleHandle) {
|
|
183
|
-
this._bleHandle = new BleHandle({
|
|
184
|
-
pks: this.specialProductKeys,
|
|
185
|
-
offlineThreshold: this.offlineThreshold,
|
|
186
|
-
});
|
|
187
|
-
}
|
|
188
|
-
return this._bleHandle;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
private get lanHandle() {
|
|
192
|
-
if (!this._lanHandle) {
|
|
193
|
-
this._lanHandle = new LanHandle({
|
|
194
|
-
pks: this.specialProductKeys,
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
return this._lanHandle;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
private get socketHandle() {
|
|
201
|
-
if (!this._gizSocket) {
|
|
202
|
-
this._gizSocket = new GizwitsWS({
|
|
203
|
-
appID: getGlobalData('appID'),
|
|
204
|
-
token: getGlobalData('token'),
|
|
205
|
-
uid: getGlobalData('uid'),
|
|
206
|
-
});
|
|
207
|
-
this._gizSocket.subscribeDeviceStatus(this.handleSocketDeviceData);
|
|
208
|
-
this._gizSocket.subscribeBindingChanged(this.getBindingList);
|
|
209
|
-
this._gizSocket.subscribeSocketStatus(this.handleSocketStatus);
|
|
210
|
-
}
|
|
211
|
-
return this._gizSocket;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
private init = ({
|
|
216
|
-
appID,
|
|
217
|
-
appSecret,
|
|
218
|
-
productInfo,
|
|
219
|
-
cloudServiceInfo,
|
|
220
|
-
token,
|
|
221
|
-
uid,
|
|
222
|
-
offlineThreshold,
|
|
223
|
-
}: GizwitsSdkOption) => {
|
|
224
|
-
this.productInfo = productInfo;
|
|
225
|
-
// 保存相关信息
|
|
226
|
-
setGlobalData('appID', appID);
|
|
227
|
-
setGlobalData('appSecret', appSecret);
|
|
228
|
-
setGlobalData('productInfo', productInfo);
|
|
229
|
-
setGlobalData('token', token);
|
|
230
|
-
setGlobalData('uid', uid);
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
* 同时也设置域名信息
|
|
234
|
-
*/
|
|
235
|
-
this.setDomain(cloudServiceInfo);
|
|
236
|
-
|
|
237
|
-
this.offlineThreshold = offlineThreshold;
|
|
238
|
-
|
|
239
|
-
GizLog.debug(`init sdk success, current version: ${GizwitsMiniSDK.getVersion().data}`);
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* lan 比较特殊
|
|
243
|
-
* 初始化sdk的时候就要开始扫描设备
|
|
244
|
-
*/
|
|
245
|
-
this.initLan();
|
|
246
|
-
}
|
|
247
|
-
public reInit(props: GizwitsSdkOption) {
|
|
248
|
-
this.init(props);
|
|
249
|
-
|
|
250
|
-
// 更新handler 参数
|
|
251
|
-
this.lanHandle.pks = this.specialProductKeys;
|
|
252
|
-
this.bleHandle.pks = this.specialProductKeys;
|
|
253
|
-
this.bleHandle.offlineThreshold = props.offlineThreshold;
|
|
254
|
-
|
|
255
|
-
const { appID, token, cloudServiceInfo = { openAPIInfo: '' } } = props;
|
|
256
|
-
const openAPIInfo = (cloudServiceInfo && cloudServiceInfo.openAPIInfo) || 'api.gizwits.com';
|
|
257
|
-
if (appID !== getGlobalData('appID') ||
|
|
258
|
-
token !== getGlobalData('token') ||
|
|
259
|
-
openAPIInfo !== getGlobalData('cloudServiceInfo').openAPIInfo
|
|
260
|
-
) {
|
|
261
|
-
// 需要重启
|
|
262
|
-
this.socketHandle.destory();
|
|
263
|
-
this._gizSocket = null;
|
|
264
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
|
265
|
-
this.socketHandle;
|
|
266
|
-
|
|
267
|
-
this.getBindingList();
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
get specialProductKeys() {
|
|
272
|
-
return this.productInfo.map((item) => item.productKey ?? '');
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
get specialProductKeySecrets() {
|
|
276
|
-
return this.productInfo.map((item) => item.productSecret ?? '');
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
private handleBleError = (e) => {
|
|
280
|
-
if (this.listenerMap['onBleHandleError']) {
|
|
281
|
-
this.listenerMap['onBleHandleError'].map((item) => {
|
|
282
|
-
item(e);
|
|
283
|
-
});
|
|
284
|
-
}
|
|
285
|
-
};
|
|
286
|
-
|
|
287
|
-
private handleBleDevices = (devices: IDevice[]) => {
|
|
288
|
-
this.bleDevices = devices;
|
|
289
|
-
// this.notiDeviceList();
|
|
290
|
-
};
|
|
291
|
-
|
|
292
|
-
handleLanDevices = (devices: IDevice[]) => {
|
|
293
|
-
this.lanDevices = devices;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
private handleLanDeviceData = async (mac: string, hexString: string) => {
|
|
297
|
-
const target = this.allDevices.find(
|
|
298
|
-
(item) => item.mac === mac
|
|
299
|
-
);
|
|
300
|
-
if (target) {
|
|
301
|
-
const data = await unpack(hexStrint2byte(hexString), target.productKey);
|
|
302
|
-
// 如果有kydata 则上报
|
|
303
|
-
if (data?.kvData && this.listenerMap['GizDeviceAttrsNotifications']) {
|
|
304
|
-
this.listenerMap['GizDeviceAttrsNotifications'].map((item) => {
|
|
305
|
-
item({
|
|
306
|
-
device: target,
|
|
307
|
-
data: data?.kvData,
|
|
308
|
-
});
|
|
309
|
-
});
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
private handleBleDeviceData = async (
|
|
314
|
-
curDevice: WechatMiniprogram.OnBLECharacteristicValueChangeListenerResult,
|
|
315
|
-
hexString: string
|
|
316
|
-
) => {
|
|
317
|
-
const target = this.allDevices.find(
|
|
318
|
-
(item) => item.bleDeviceID === curDevice.deviceId
|
|
319
|
-
);
|
|
320
|
-
if (target) {
|
|
321
|
-
const bleBytesData = hexStrint2byte(hexString)
|
|
322
|
-
const parseData = new ProtocolBase(bleBytesData);
|
|
323
|
-
switch (parseData.cmd) {
|
|
324
|
-
case '0093':
|
|
325
|
-
case '0094':
|
|
326
|
-
case '0090':
|
|
327
|
-
case '0091': {
|
|
328
|
-
// 设备上报的指令,上传给后台
|
|
329
|
-
console.debug('handleBleDeviceData', hexString, target)
|
|
330
|
-
if (target.passcode) {
|
|
331
|
-
const productConfig = this.productInfo.find(item => item.productKey === target.productKey)
|
|
332
|
-
if (productConfig) {
|
|
333
|
-
const uploadTime = parseInt(`${Date.now() / 1000}`, 10)
|
|
334
|
-
uploadP0({
|
|
335
|
-
device: target,
|
|
336
|
-
data: [
|
|
337
|
-
{
|
|
338
|
-
raw: hexString,
|
|
339
|
-
created_at: uploadTime
|
|
340
|
-
}
|
|
341
|
-
],
|
|
342
|
-
productSecret: productConfig.productSecret,
|
|
343
|
-
}).then(data => {
|
|
344
|
-
GizLog.debug("设备上报数据:timestemp", new Date(uploadTime * 1000).toLocaleString(), data)
|
|
345
|
-
})
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
}
|
|
349
|
-
const data = await unpack(hexStrint2byte(hexString), target.productKey);
|
|
350
|
-
// 如果有kydata 则上报
|
|
351
|
-
if (data?.kvData && this.listenerMap['GizDeviceAttrsNotifications']) {
|
|
352
|
-
this.listenerMap['GizDeviceAttrsNotifications'].map((item) => {
|
|
353
|
-
item({
|
|
354
|
-
device: target,
|
|
355
|
-
data: data?.kvData,
|
|
356
|
-
});
|
|
357
|
-
});
|
|
358
|
-
}
|
|
359
|
-
break
|
|
360
|
-
}
|
|
361
|
-
case '0062': {
|
|
362
|
-
// 蓝牙重置命令,app代理设备进行重置
|
|
363
|
-
const resetInfo = new Reset(bleBytesData)
|
|
364
|
-
GizLog.debug("收到代理重置事件", resetInfo)
|
|
365
|
-
|
|
366
|
-
if (resetInfo.status === 1) {
|
|
367
|
-
// 需要重置
|
|
368
|
-
this.deviceSafetyApi({
|
|
369
|
-
mac: target.mac,
|
|
370
|
-
productKey: target.productKey,
|
|
371
|
-
isReset: true
|
|
372
|
-
}).then(async data => {
|
|
373
|
-
GizLog.debug("代理重置结果", data)
|
|
374
|
-
if (data.success && data.data.successDevices.length > 0) {
|
|
375
|
-
// 回写数据
|
|
376
|
-
GizLog.debug("重置成功,回写数据")
|
|
377
|
-
await this.bleHandle.write(curDevice.deviceId, numberArray2Uint8Array(SetReset.pack(0)).buffer)
|
|
378
|
-
}
|
|
379
|
-
})
|
|
380
|
-
}
|
|
381
|
-
break;
|
|
382
|
-
}
|
|
383
|
-
case '0064': {
|
|
384
|
-
// 回写重置结果ack,暂时先忽略
|
|
385
|
-
break;
|
|
386
|
-
}
|
|
387
|
-
case '0072': {
|
|
388
|
-
// 设备回复是否允许同步数据
|
|
389
|
-
const requestSyncData = new RequestSync(bleBytesData)
|
|
390
|
-
GizLog.debug("设备回复是否允许同步, state:", requestSyncData.state)
|
|
391
|
-
|
|
392
|
-
this.syncDataLengthCallback && this.syncDataLengthCallback({ success: requestSyncData.state == 0, data: requestSyncData.len })
|
|
393
|
-
|
|
394
|
-
if (!this.syncDataCallBack) break;
|
|
395
|
-
if (requestSyncData.state === 0) {
|
|
396
|
-
// 开始同步
|
|
397
|
-
this.syncCurrnetNum = 0;
|
|
398
|
-
this.syncTotalNum = requestSyncData.len;
|
|
399
|
-
this.syncDataCallBack({
|
|
400
|
-
event: 'SYNC_START',
|
|
401
|
-
totalNum: this.syncTotalNum,
|
|
402
|
-
currentNum: this.syncCurrnetNum
|
|
403
|
-
})
|
|
404
|
-
// 读取第0条
|
|
405
|
-
GizLog.debug("查询第一条离线数据")
|
|
406
|
-
await this.bleHandle.write(curDevice.deviceId, numberArray2Uint8Array(SyncPackage.pack(0)).buffer)
|
|
407
|
-
} else {
|
|
408
|
-
GizLog.debug("设备返回状态为1,同步失败")
|
|
409
|
-
|
|
410
|
-
// 同步失败
|
|
411
|
-
this.syncDataCallBack({
|
|
412
|
-
event: 'SYNC_FAIL',
|
|
413
|
-
})
|
|
414
|
-
}
|
|
415
|
-
break
|
|
416
|
-
}
|
|
417
|
-
case '0074': {
|
|
418
|
-
if (!this.syncDataCallBack) break;
|
|
419
|
-
// 设备上报需要同步的数据
|
|
420
|
-
GizLog.debug("设备上报离线数据")
|
|
421
|
-
|
|
422
|
-
const syncPackageData = new SyncPackage(bleBytesData)
|
|
423
|
-
const deletePackageCmd = DeletePackage.pack(syncPackageData.id)
|
|
424
|
-
GizLog.debug("设备上报离线数据:payload", syncPackageData.payload)
|
|
425
|
-
try {
|
|
426
|
-
GizLog.debug("设备上报离线数据:timestemp", new Date(syncPackageData.timestemp * 1000).toLocaleString())
|
|
427
|
-
} catch (error) {
|
|
428
|
-
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
const productConfig = this.productInfo.find(item => item.productKey === this.syncDevice.productKey)
|
|
432
|
-
if (productConfig) {
|
|
433
|
-
const uploadRes = await uploadP0({
|
|
434
|
-
device: this.syncDevice,
|
|
435
|
-
data: [
|
|
436
|
-
{
|
|
437
|
-
raw: arrayToString(syncPackageData.payload),
|
|
438
|
-
created_at: syncPackageData.timestemp
|
|
439
|
-
}
|
|
440
|
-
],
|
|
441
|
-
productSecret: productConfig.productSecret
|
|
442
|
-
})
|
|
443
|
-
GizLog.debug("上传离线数据结果", uploadRes)
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
this.syncCurrnetNum += 1;
|
|
447
|
-
// 通知设备删除
|
|
448
|
-
this.syncDataCallBack({
|
|
449
|
-
event: "SYNC_PROGRESS",
|
|
450
|
-
totalNum: this.syncTotalNum,
|
|
451
|
-
currentNum: this.syncCurrnetNum
|
|
452
|
-
})
|
|
453
|
-
|
|
454
|
-
GizLog.debug("通知设备删除:", syncPackageData.id)
|
|
455
|
-
await this.bleHandle.write(curDevice.deviceId, numberArray2Uint8Array(deletePackageCmd).buffer)
|
|
456
|
-
await sleep(100)
|
|
457
|
-
// 查询新数据
|
|
458
|
-
if (syncPackageData.id < this.syncTotalNum - 1) {
|
|
459
|
-
GizLog.debug("查询新数据:", syncPackageData.id + 1)
|
|
460
|
-
await this.bleHandle.write(curDevice.deviceId, numberArray2Uint8Array(SyncPackage.pack(syncPackageData.id + 1)).buffer)
|
|
461
|
-
} else {
|
|
462
|
-
// 结束同步
|
|
463
|
-
GizLog.debug("send cancel sync to device")
|
|
464
|
-
this.bleHandle.write(curDevice.deviceId, numberArray2Uint8Array(CancelSync.pack()).buffer)
|
|
465
|
-
this.syncDataCallBack({
|
|
466
|
-
event: "SYNC_END",
|
|
467
|
-
})
|
|
468
|
-
// 清除相关数据
|
|
469
|
-
this.cleanSyncDeviceData();
|
|
470
|
-
}
|
|
471
|
-
break
|
|
472
|
-
}
|
|
473
|
-
case '0078': {
|
|
474
|
-
if (!this.syncDataCallBack) break;
|
|
475
|
-
// 设备上报停止同步
|
|
476
|
-
|
|
477
|
-
const cancelData = new CancelSync(bleBytesData)
|
|
478
|
-
GizLog.debug("设备上报停止同步 state:", cancelData.state)
|
|
479
|
-
|
|
480
|
-
if (cancelData.state === 0) {
|
|
481
|
-
// 同步完成
|
|
482
|
-
this.syncDataCallBack({
|
|
483
|
-
event: "SYNC_END",
|
|
484
|
-
})
|
|
485
|
-
} else {
|
|
486
|
-
this.syncDataCallBack({
|
|
487
|
-
event: "SYNC_FAIL",
|
|
488
|
-
})
|
|
489
|
-
}
|
|
490
|
-
// 清除相关数据
|
|
491
|
-
this.cleanSyncDeviceData();
|
|
492
|
-
break
|
|
493
|
-
}
|
|
494
|
-
case '0060': {
|
|
495
|
-
// NTP回复
|
|
496
|
-
break
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
};
|
|
501
|
-
|
|
502
|
-
private handleSocketDeviceData: IOnDeviceStatusChanged = async (data) => {
|
|
503
|
-
const target = this.allDevices.find((item) => item.did === data.did);
|
|
504
|
-
if (!target) {
|
|
505
|
-
return;
|
|
506
|
-
}
|
|
507
|
-
if ((data as IDeviceStatusChangedProps).attrs && (data as IDeviceStatusChangedProps).attrs.is_online != null) {
|
|
508
|
-
const isOnline = (data as IDeviceStatusChangedProps).attrs.is_online;
|
|
509
|
-
this.setDeviceMeta(target, 'isOnline', isOnline);
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
if (this.listenerMap['GizDeviceAttrsNotifications']) {
|
|
513
|
-
|
|
514
|
-
// 需要把枚举 拓展格式化回来
|
|
515
|
-
if ((data as IDeviceStatusChangedProps).attrs) {
|
|
516
|
-
(data as IDeviceStatusChangedProps).attrs = await formatEnum((data as IDeviceStatusChangedProps).attrs, target.productKey, 'int');
|
|
517
|
-
}
|
|
518
|
-
this.listenerMap['GizDeviceAttrsNotifications'].map((item) => {
|
|
519
|
-
item({
|
|
520
|
-
device: target,
|
|
521
|
-
...('attrs' in data ? { data: data.attrs } : { raw: data.raw }),
|
|
522
|
-
});
|
|
523
|
-
});
|
|
524
|
-
}
|
|
525
|
-
};
|
|
526
|
-
|
|
527
|
-
private handleSocketStatus: IOnSocketStatusChanged = (dids, enabled) => {
|
|
528
|
-
const devices = this.allDevices.filter(
|
|
529
|
-
(item) => dids.includes(item.did) && item.connectType === 'WAN'
|
|
530
|
-
);
|
|
531
|
-
GizLog.log('handleSocketStatus', dids, enabled);
|
|
532
|
-
devices.forEach((item) => {
|
|
533
|
-
this.setDeviceMeta(item, 'connectType', enabled ? 'WAN' : 'NONE');
|
|
534
|
-
});
|
|
535
|
-
};
|
|
536
|
-
|
|
537
|
-
private notiDeviceList = () => {
|
|
538
|
-
// 找到所有挂载的列表通知
|
|
539
|
-
this.listenerMap['GizDeviceListNotifications']?.map((notifyFn) => {
|
|
540
|
-
notifyFn(this.allDevices);
|
|
541
|
-
});
|
|
542
|
-
|
|
543
|
-
this.listenerMap['onScanListChange']?.map((notifyFn) => {
|
|
544
|
-
notifyFn(this.bleScanDevice);
|
|
545
|
-
});
|
|
546
|
-
};
|
|
547
|
-
|
|
548
|
-
private initLan = () => {
|
|
549
|
-
try {
|
|
550
|
-
this.lanHandle.init();
|
|
551
|
-
this.lanHandle.addEventListener('GizLanDeviceList', this.handleLanDevices);
|
|
552
|
-
this.lanHandle.addEventListener(
|
|
553
|
-
'GizLanDeviceData',
|
|
554
|
-
this.handleLanDeviceData
|
|
555
|
-
);
|
|
556
|
-
} catch (error) {
|
|
557
|
-
GizLog.error('init lan error', error)
|
|
558
|
-
}
|
|
559
|
-
}
|
|
560
|
-
|
|
561
|
-
/**
|
|
562
|
-
* @description 开启后台自动扫描
|
|
563
|
-
*/
|
|
564
|
-
public startAutoScan = (services?: string[]) => {
|
|
565
|
-
// 保持autoscan 运行 因为一些蓝牙的操作会导致搜索终止
|
|
566
|
-
const autoTime = 9000;
|
|
567
|
-
|
|
568
|
-
// 已经启动
|
|
569
|
-
if (this.keepScanTimer) {
|
|
570
|
-
return;
|
|
571
|
-
}
|
|
572
|
-
this.keepScanTimer = setInterval(async () => {
|
|
573
|
-
// 检查是否在搜索中
|
|
574
|
-
// const data = await getBluetoothAdapterState();
|
|
575
|
-
// if (!data.discovering) {
|
|
576
|
-
// this.bleHandle.startScan((deviceList) => {
|
|
577
|
-
// this.bleDevices = deviceList;
|
|
578
|
-
// }, -1);
|
|
579
|
-
// }
|
|
580
|
-
this.bleHandle.startScan( -1, services);
|
|
581
|
-
}, autoTime);
|
|
582
|
-
this.bleHandle.startScan(-1, services);
|
|
583
|
-
};
|
|
584
|
-
|
|
585
|
-
/**
|
|
586
|
-
* @description 停止后台自动扫描
|
|
587
|
-
*/
|
|
588
|
-
|
|
589
|
-
public stopAutoScan = () => {
|
|
590
|
-
this.keepScanTimer && clearInterval(this.keepScanTimer);
|
|
591
|
-
this.bleHandle.stopScan();
|
|
592
|
-
};
|
|
593
|
-
|
|
594
|
-
/**
|
|
595
|
-
* @description 更改设备列表中的元信息,包括设备mac,设备在线状态等
|
|
596
|
-
*/
|
|
597
|
-
public setDeviceMeta = <K extends keyof IDevice>(
|
|
598
|
-
curDev: IDevice,
|
|
599
|
-
key: K,
|
|
600
|
-
value: IDevice[K],
|
|
601
|
-
force: boolean = false
|
|
602
|
-
) => {
|
|
603
|
-
const target = this.allDevices.find((dev) => dev.mac === curDev.mac);
|
|
604
|
-
if (!target || this.allDevices.length === 0) {
|
|
605
|
-
return;
|
|
606
|
-
}
|
|
607
|
-
if (curDev[key] !== value || force) {
|
|
608
|
-
target[key] = value;
|
|
609
|
-
this.notiDeviceList();
|
|
610
|
-
}
|
|
611
|
-
};
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
/**
|
|
615
|
-
* @description 更改设备列表中的元信息,包括设备mac,设备在线状态等
|
|
616
|
-
*/
|
|
617
|
-
public setDevicesMeta = <K extends keyof IDevice>(
|
|
618
|
-
devices: IDevice[],
|
|
619
|
-
key: K,
|
|
620
|
-
value: IDevice[K],
|
|
621
|
-
force: boolean = false
|
|
622
|
-
) => {
|
|
623
|
-
let hasChange = false;
|
|
624
|
-
const targets = devices.map((dev) => this.allDevices.find((item) => item.mac === dev.mac));
|
|
625
|
-
for (let i = 0; i < targets.length; i++) {
|
|
626
|
-
const target = targets[i];
|
|
627
|
-
if (target) {
|
|
628
|
-
if (target[key] !== value || force) {
|
|
629
|
-
target[key] = value;
|
|
630
|
-
hasChange = true;
|
|
631
|
-
}
|
|
632
|
-
}
|
|
633
|
-
}
|
|
634
|
-
hasChange && this.notiDeviceList();
|
|
635
|
-
};
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
static getVersion = () => {
|
|
640
|
-
return {
|
|
641
|
-
success: true,
|
|
642
|
-
data: version,
|
|
643
|
-
};
|
|
644
|
-
};
|
|
645
|
-
|
|
646
|
-
/**
|
|
647
|
-
* 初始化蓝牙能力
|
|
648
|
-
* @returns {Promise<{success: boolean, err: string}>}
|
|
649
|
-
*/
|
|
650
|
-
public initBle = async (): Promise<ISDKResult<null>> => {
|
|
651
|
-
try {
|
|
652
|
-
this.bleHandle.addEventListener(
|
|
653
|
-
'GizBleDeviceData',
|
|
654
|
-
this.handleBleDeviceData
|
|
655
|
-
);
|
|
656
|
-
this.bleHandle.addEventListener('GizBleDeviceList', this.handleBleDevices);
|
|
657
|
-
this.bleHandle.addEventListener('GizBleError', this.handleBleError);
|
|
658
|
-
const permissionData = await this.bleHandle.checkPermission();
|
|
659
|
-
if (!permissionData.success) {
|
|
660
|
-
GizLog.error('init BLE Error', new Error(JSON.stringify(permissionData)));
|
|
661
|
-
return {
|
|
662
|
-
success: false,
|
|
663
|
-
err: errorCode.GIZ_SDK_BLE_BLUETOOTH_FUNCTION_NOT_TURNED_ON,
|
|
664
|
-
};
|
|
665
|
-
}
|
|
666
|
-
GizLog.debug('init BLE succcess', permissionData);
|
|
667
|
-
} catch (error) {
|
|
668
|
-
GizLog.error('init BLE error', error);
|
|
669
|
-
return { success: false, err: errorCode.GIZ_SDK_BLE_BLUETOOTH_FUNCTION_NOT_TURNED_ON }
|
|
670
|
-
}
|
|
671
|
-
return { success: true };
|
|
672
|
-
};
|
|
673
|
-
|
|
674
|
-
/**
|
|
675
|
-
* @description 扫描附近的机智云ble设备
|
|
676
|
-
* @param delay 超时时间
|
|
677
|
-
* @returns {success:boolean,data:IDevice[]}
|
|
678
|
-
*/
|
|
679
|
-
public scanBleDevice = async (
|
|
680
|
-
delay: number,
|
|
681
|
-
services?: string[]
|
|
682
|
-
): Promise<ISDKResult<IDevice[]>> => {
|
|
683
|
-
this.bleHandle.scanList = [];
|
|
684
|
-
const data = await this.bleHandle.startScan(delay, services);
|
|
685
|
-
return {
|
|
686
|
-
success: true,
|
|
687
|
-
data: data.scanList,
|
|
688
|
-
};
|
|
689
|
-
};
|
|
690
|
-
|
|
691
|
-
write = async (device: IDevice, attrs: object) => {
|
|
692
|
-
const target = this.allDevices.find((item) => isSameDevice(item, device));
|
|
693
|
-
if (!target) {
|
|
694
|
-
return { success: false, message: 'target is undefind' };
|
|
695
|
-
}
|
|
696
|
-
if (target.connectType !== 'NONE') {
|
|
697
|
-
const { data: value, raw } = await pack(attrs, target.productKey);
|
|
698
|
-
switch (target.connectType) {
|
|
699
|
-
case 'BLE_BROADCAST':
|
|
700
|
-
// 广播的方式发数据 Action+Flags+DataPoints
|
|
701
|
-
return await this.bleHandle.writeBoradcast(
|
|
702
|
-
target.bleDeviceID,
|
|
703
|
-
raw.cmd
|
|
704
|
-
);
|
|
705
|
-
case 'BLE': {
|
|
706
|
-
if (value) {
|
|
707
|
-
return await this.bleHandle.write(
|
|
708
|
-
target.bleDeviceID,
|
|
709
|
-
numberArray2Uint8Array(value).buffer
|
|
710
|
-
);
|
|
711
|
-
} else {
|
|
712
|
-
// 解码失败
|
|
713
|
-
return {
|
|
714
|
-
success: false,
|
|
715
|
-
};
|
|
716
|
-
}
|
|
717
|
-
}
|
|
718
|
-
case 'WAN': {
|
|
719
|
-
// 枚举要转换成枚举值
|
|
720
|
-
const newAttrs = await formatEnum(attrs, target.productKey, 'string')
|
|
721
|
-
|
|
722
|
-
return this.socketHandle.writeData(target, newAttrs);
|
|
723
|
-
}
|
|
724
|
-
case 'LAN': {
|
|
725
|
-
return this.lanHandle.write(target, numberArray2Uint8Array(value).buffer);
|
|
726
|
-
}
|
|
727
|
-
}
|
|
728
|
-
}
|
|
729
|
-
return {
|
|
730
|
-
success: false,
|
|
731
|
-
};
|
|
732
|
-
};
|
|
733
|
-
|
|
734
|
-
// 发送原始数据
|
|
735
|
-
writeRaw = async (device: IDevice, data: number[]) => {
|
|
736
|
-
const target = this.allDevices.find((item) => isSameDevice(item, device));
|
|
737
|
-
if (!target) {
|
|
738
|
-
return { success: false, message: 'target is undefind' };
|
|
739
|
-
}
|
|
740
|
-
if (target.connectType !== 'NONE') {
|
|
741
|
-
switch (target.connectType) {
|
|
742
|
-
case 'BLE_BROADCAST':
|
|
743
|
-
// 广播的方式发数据 Action+Flags+DataPoints
|
|
744
|
-
return await this.bleHandle.writeBoradcast(
|
|
745
|
-
target.bleDeviceID,
|
|
746
|
-
data
|
|
747
|
-
);
|
|
748
|
-
case 'BLE': {
|
|
749
|
-
if (data) {
|
|
750
|
-
return await this.bleHandle.write(
|
|
751
|
-
target.bleDeviceID,
|
|
752
|
-
numberArray2Uint8Array(data).buffer
|
|
753
|
-
);
|
|
754
|
-
} else {
|
|
755
|
-
// 解码失败
|
|
756
|
-
return {
|
|
757
|
-
success: false,
|
|
758
|
-
};
|
|
759
|
-
}
|
|
760
|
-
}
|
|
761
|
-
case 'WAN': {
|
|
762
|
-
return this.socketHandle.send(target.did, data);
|
|
763
|
-
}
|
|
764
|
-
case 'LAN': {
|
|
765
|
-
return this.lanHandle.write(target, numberArray2Uint8Array(data).buffer);
|
|
766
|
-
}
|
|
767
|
-
}
|
|
768
|
-
}
|
|
769
|
-
return {
|
|
770
|
-
success: false,
|
|
771
|
-
};
|
|
772
|
-
};
|
|
773
|
-
|
|
774
|
-
// 设置设备的时间戳
|
|
775
|
-
setDeviceTimeStamp = async (device: IDevice) => {
|
|
776
|
-
const target = this.allDevices.find((item) => isSameDevice(item, device));
|
|
777
|
-
if (!target) {
|
|
778
|
-
return { success: false, message: 'target is undefind' };
|
|
779
|
-
}
|
|
780
|
-
|
|
781
|
-
const data = Ntp.pack()
|
|
782
|
-
|
|
783
|
-
switch (target.connectType) {
|
|
784
|
-
case 'BLE': {
|
|
785
|
-
return this.bleHandle.write(device.bleDeviceID, numberArray2Uint8Array(data).buffer)
|
|
786
|
-
}
|
|
787
|
-
}
|
|
788
|
-
return {
|
|
789
|
-
success: false,
|
|
790
|
-
message: 'the connectType not support set timestamp'
|
|
791
|
-
}
|
|
792
|
-
}
|
|
793
|
-
|
|
794
|
-
// 同步蓝牙设备的离线数据
|
|
795
|
-
syncDeviceData = async (device: IDevice, callback: SyncCallBack = () => { }) => {
|
|
796
|
-
const target = this.allDevices.find((item) => isSameDevice(item, device));
|
|
797
|
-
if (!target) {
|
|
798
|
-
return { success: false, message: 'target is undefind' };
|
|
799
|
-
}
|
|
800
|
-
if (!target.isBind) {
|
|
801
|
-
return { success: false, message: 'Only the bound devices are supported' };
|
|
802
|
-
}
|
|
803
|
-
|
|
804
|
-
if (this.syncDevice) {
|
|
805
|
-
return {
|
|
806
|
-
success: false,
|
|
807
|
-
message: 'Synchronizing'
|
|
808
|
-
}
|
|
809
|
-
}
|
|
810
|
-
|
|
811
|
-
this.syncDevice = { ...target }
|
|
812
|
-
|
|
813
|
-
switch (target.connectType) {
|
|
814
|
-
case 'BLE': {
|
|
815
|
-
GizLog.debug("start syncDeviceData", device);
|
|
816
|
-
this.syncDataCallBack = callback;
|
|
817
|
-
this.syncDataTimoutTimer = setTimeout(() => {
|
|
818
|
-
this.syncDataCallBack({
|
|
819
|
-
event: 'SYNC_FAIL',
|
|
820
|
-
message: 'sync timeout'
|
|
821
|
-
})
|
|
822
|
-
this.cleanSyncDeviceData();
|
|
823
|
-
}, this.SYNC_TIMEOUT)
|
|
824
|
-
// 设置超时
|
|
825
|
-
const data = RequestSync.pack();
|
|
826
|
-
// 发送开始同步的指令
|
|
827
|
-
GizLog.debug("sent RequestSync data");
|
|
828
|
-
return this.bleHandle.write(device.bleDeviceID, numberArray2Uint8Array(data).buffer)
|
|
829
|
-
}
|
|
830
|
-
}
|
|
831
|
-
return {
|
|
832
|
-
success: false,
|
|
833
|
-
message: 'the connectType not support sync data'
|
|
834
|
-
}
|
|
835
|
-
}
|
|
836
|
-
|
|
837
|
-
// 查询设备里面还有多少数据需要同步
|
|
838
|
-
queryNeedSyncDataLength = async (device: IDevice): Promise<ISDKResult<number>> => {
|
|
839
|
-
const target = this.allDevices.find((item) => isSameDevice(item, device));
|
|
840
|
-
if (!target) {
|
|
841
|
-
return { success: false, message: 'target is undefind' };
|
|
842
|
-
}
|
|
843
|
-
if (!target.isBind) {
|
|
844
|
-
return { success: false, message: 'Only the bound devices are supported' };
|
|
845
|
-
}
|
|
846
|
-
switch (target.connectType) {
|
|
847
|
-
case 'BLE': {
|
|
848
|
-
const data = RequestSync.pack();
|
|
849
|
-
// 发送开始同步的指令
|
|
850
|
-
this.syncDevice = { ...target }
|
|
851
|
-
GizLog.debug("sent RequestSync data len");
|
|
852
|
-
this.bleHandle.write(device.bleDeviceID, numberArray2Uint8Array(data).buffer)
|
|
853
|
-
return new Promise((res) => {
|
|
854
|
-
this.syncDataLengthCallback = res;
|
|
855
|
-
setTimeout(() => {
|
|
856
|
-
try {
|
|
857
|
-
res({ success: false, message: 'timeout' })
|
|
858
|
-
} catch (error) {
|
|
859
|
-
|
|
860
|
-
}
|
|
861
|
-
}, 3000)
|
|
862
|
-
})
|
|
863
|
-
}
|
|
864
|
-
}
|
|
865
|
-
return {
|
|
866
|
-
success: false,
|
|
867
|
-
message: 'the connectType not support sync data'
|
|
868
|
-
}
|
|
869
|
-
}
|
|
870
|
-
|
|
871
|
-
public cancelSyncDeviceData = async () => {
|
|
872
|
-
this.cleanSyncDeviceData();
|
|
873
|
-
}
|
|
874
|
-
|
|
875
|
-
private cleanSyncDeviceData = () => {
|
|
876
|
-
if (this.syncDataCallBack) {
|
|
877
|
-
this.syncDataCallBack({
|
|
878
|
-
event: 'SYNC_CANCEL'
|
|
879
|
-
})
|
|
880
|
-
this.syncDataCallBack = null;
|
|
881
|
-
}
|
|
882
|
-
this.syncCurrnetNum = 0;
|
|
883
|
-
this.syncTotalNum = 0;
|
|
884
|
-
this.syncDevice = null;
|
|
885
|
-
this.syncDataTimoutTimer && clearTimeout(this.syncDataTimoutTimer)
|
|
886
|
-
}
|
|
887
|
-
|
|
888
|
-
getProductConfig = async (
|
|
889
|
-
pk: string
|
|
890
|
-
): Promise<ISDKResult<IDataPointConfig>> => {
|
|
891
|
-
const data = await productConfigFileManage.getConfigFile(pk);
|
|
892
|
-
if (data) {
|
|
893
|
-
return {
|
|
894
|
-
success: true,
|
|
895
|
-
data,
|
|
896
|
-
};
|
|
897
|
-
}
|
|
898
|
-
return {
|
|
899
|
-
success: false,
|
|
900
|
-
};
|
|
901
|
-
};
|
|
902
|
-
|
|
903
|
-
stopScanBleDevice = () => {
|
|
904
|
-
this.bleHandle.stopScan();
|
|
905
|
-
};
|
|
906
|
-
|
|
907
|
-
// 微信匿名登陆
|
|
908
|
-
login = async (openID: string): Promise<ISDKResult<ILoginRes>> => {
|
|
909
|
-
const data = await AnonymousLogin({ uid: openID });
|
|
910
|
-
if (data.success) {
|
|
911
|
-
setGlobalData('token', data.data?.token);
|
|
912
|
-
setGlobalData('uid', data.data?.uid);
|
|
913
|
-
this.socketHandle.updateToken({ token: data.data.token, uid: data.data.uid })
|
|
914
|
-
}
|
|
915
|
-
return data;
|
|
916
|
-
};
|
|
917
|
-
|
|
918
|
-
getDeviceStatus = async (device: IDevice, attrNames?: string[]) => {
|
|
919
|
-
// 根据PK和mac 找到设备,确认他的连接类型,再用不同的渠道下发
|
|
920
|
-
const target = this.allDevices.find((item) => isSameDevice(item, device));
|
|
921
|
-
if (!target) {
|
|
922
|
-
return { success: false, message: 'target is undefind' };
|
|
923
|
-
}
|
|
924
|
-
switch (target.connectType) {
|
|
925
|
-
case 'BLE': {
|
|
926
|
-
return await this.bleHandle.getDeviceStatus(
|
|
927
|
-
target.bleDeviceID as string,
|
|
928
|
-
target.productKey,
|
|
929
|
-
target.rootDeviceId,
|
|
930
|
-
attrNames
|
|
931
|
-
);
|
|
932
|
-
}
|
|
933
|
-
case 'WAN':
|
|
934
|
-
return this.socketHandle.readStatus(target, attrNames);
|
|
935
|
-
default: {
|
|
936
|
-
return {
|
|
937
|
-
success: false,
|
|
938
|
-
message: 'Device connectType is unsupported',
|
|
939
|
-
};
|
|
940
|
-
}
|
|
941
|
-
}
|
|
942
|
-
};
|
|
943
|
-
|
|
944
|
-
/**
|
|
945
|
-
* 发起设备订阅
|
|
946
|
-
* @param device 待订阅设备
|
|
947
|
-
* @param connectType 设备连接类型
|
|
948
|
-
* @param _autoGetDeviceStatus 自动获取设备状态
|
|
949
|
-
* @returns
|
|
950
|
-
*/
|
|
951
|
-
subscribe = async (
|
|
952
|
-
device: IDevice,
|
|
953
|
-
connectType?: TConnectType,
|
|
954
|
-
_autoGetDeviceStatus: boolean = true
|
|
955
|
-
) => {
|
|
956
|
-
GizLog.debug("subscribe device:", device)
|
|
957
|
-
let target = this.allDevices.find((item) => isSameDevice(item, device));
|
|
958
|
-
if (!target) {
|
|
959
|
-
GizLog.warn('Device Target Not Found')
|
|
960
|
-
GizLog.warn('current devices ', this.allDevices)
|
|
961
|
-
return { success: false, message: "device not found" };
|
|
962
|
-
}
|
|
963
|
-
|
|
964
|
-
// 广播模式不需要连接,所以默认是连接状态
|
|
965
|
-
if (target.connectType === 'BLE_BROADCAST') {
|
|
966
|
-
// 广播不需要连接
|
|
967
|
-
return {
|
|
968
|
-
success: true,
|
|
969
|
-
message: '',
|
|
970
|
-
};
|
|
971
|
-
}
|
|
972
|
-
|
|
973
|
-
const type: TConnectType = getFirstConnectType(target, connectType);
|
|
974
|
-
if (target.connectType !== 'NONE' && target.connectType !== type) {
|
|
975
|
-
// 已经存在 并且不等于即将订阅的类型
|
|
976
|
-
// 先解除订阅
|
|
977
|
-
await this.unSubscribe(target);
|
|
978
|
-
}
|
|
979
|
-
switch (type) {
|
|
980
|
-
case 'BLE': {
|
|
981
|
-
// 连接BLE
|
|
982
|
-
const data = await this.bleHandle.connectDevice(target) as IResult<any>;
|
|
983
|
-
if (data.success) {
|
|
984
|
-
|
|
985
|
-
// 蓝牙设备比较特殊,在get 里面从bleHandle里找到连接的设备,再把connectType 设置成BLE,所以这里更新metaData 要强制更新并推送
|
|
986
|
-
this.setDeviceMeta(target, 'connectType', 'BLE', true);
|
|
987
|
-
setTimeout(() => {
|
|
988
|
-
// 蓝牙设备连接成功之后默认设置一次ntp
|
|
989
|
-
this.setDeviceTimeStamp(target)
|
|
990
|
-
}, 250)
|
|
991
|
-
setTimeout(() => {
|
|
992
|
-
this.getDeviceStatus(target);
|
|
993
|
-
}, 500);
|
|
994
|
-
}
|
|
995
|
-
return data;
|
|
996
|
-
}
|
|
997
|
-
case 'WAN': {
|
|
998
|
-
// 大循环设备
|
|
999
|
-
// const target = this.deviceList.find((item) =>
|
|
1000
|
-
// isSameDevice(item, device)
|
|
1001
|
-
// );
|
|
1002
|
-
const data = this.socketHandle.connectDevice(target);
|
|
1003
|
-
if (data.success) {
|
|
1004
|
-
this.setDeviceMeta(device, 'connectType', 'WAN', true);
|
|
1005
|
-
}
|
|
1006
|
-
return data;
|
|
1007
|
-
}
|
|
1008
|
-
case 'LAN': {
|
|
1009
|
-
const data = await this.lanHandle.connectDevice(target);
|
|
1010
|
-
if (data.success) {
|
|
1011
|
-
this.setDeviceMeta(device, 'connectType', 'LAN', true);
|
|
1012
|
-
}
|
|
1013
|
-
return data;
|
|
1014
|
-
}
|
|
1015
|
-
}
|
|
1016
|
-
return {
|
|
1017
|
-
success: false,
|
|
1018
|
-
message: '',
|
|
1019
|
-
};
|
|
1020
|
-
};
|
|
1021
|
-
|
|
1022
|
-
batchSubscribe = async (devices: IDevice[], connectType?: TConnectType) => {
|
|
1023
|
-
GizLog.debug("subscribe devices =>", devices);
|
|
1024
|
-
const sdkDevicesMap = this.allDevices.reduce((acc, cur) => ({
|
|
1025
|
-
...acc,
|
|
1026
|
-
[getDeviceId(cur)]: cur
|
|
1027
|
-
}), {});
|
|
1028
|
-
const foundDevices = devices.filter((device) => sdkDevicesMap[getDeviceId(device)]);
|
|
1029
|
-
if (foundDevices.length === 0) {
|
|
1030
|
-
GizLog.warn('Device Target Not Found')
|
|
1031
|
-
GizLog.warn('current devices ', this.allDevices)
|
|
1032
|
-
return { success: false, message: "device not found" };
|
|
1033
|
-
}
|
|
1034
|
-
|
|
1035
|
-
const connectTypeMap: Record<string, IDevice[]> = foundDevices.reduce((acc, cur) => {
|
|
1036
|
-
const type = cur.connectType === 'BLE_BROADCAST' ? 'BLE_BROADCAST' : getFirstConnectType(cur, connectType);
|
|
1037
|
-
if (!acc[type]) {
|
|
1038
|
-
acc[type] = [];
|
|
1039
|
-
}
|
|
1040
|
-
acc[type].push(cur);
|
|
1041
|
-
return acc;
|
|
1042
|
-
}, {});
|
|
1043
|
-
|
|
1044
|
-
const types = Object.keys(connectTypeMap);
|
|
1045
|
-
// 广播模式不需要连接,所以默认是连接状态
|
|
1046
|
-
if (types.length === 1 && types[0] === 'BLE_BROADCAST') {
|
|
1047
|
-
return {
|
|
1048
|
-
success: true,
|
|
1049
|
-
message: '',
|
|
1050
|
-
};
|
|
1051
|
-
}
|
|
1052
|
-
|
|
1053
|
-
if ((connectTypeMap['BLE'] ?? []).length > 0 || (connectTypeMap['LAN'] ?? []).length > 0) {
|
|
1054
|
-
return {
|
|
1055
|
-
success: false,
|
|
1056
|
-
message: 'BLE and LAN can not subscribe together'
|
|
1057
|
-
}
|
|
1058
|
-
}
|
|
1059
|
-
|
|
1060
|
-
const wanDevices = connectTypeMap['WAN'] ?? [];
|
|
1061
|
-
if (wanDevices.length === 0) {
|
|
1062
|
-
return {
|
|
1063
|
-
success: false,
|
|
1064
|
-
message: 'no WAN device'
|
|
1065
|
-
}
|
|
1066
|
-
}
|
|
1067
|
-
|
|
1068
|
-
const wanUnsubscribeDevices = wanDevices.filter((device) => {
|
|
1069
|
-
return device.connectType !== 'NONE' && device.connectType !== 'WAN';
|
|
1070
|
-
});
|
|
1071
|
-
|
|
1072
|
-
if (wanUnsubscribeDevices.length > 0) {
|
|
1073
|
-
await this.batchWanUnSubscribe(wanUnsubscribeDevices);
|
|
1074
|
-
}
|
|
1075
|
-
|
|
1076
|
-
const data = this.socketHandle.connectDevices(wanDevices);
|
|
1077
|
-
if (data.success) {
|
|
1078
|
-
this.setDevicesMeta(wanDevices, 'connectType', 'WAN', true);
|
|
1079
|
-
}
|
|
1080
|
-
return data;
|
|
1081
|
-
}
|
|
1082
|
-
|
|
1083
|
-
batchWanUnSubscribe = async (devices: IDevice[]) => {
|
|
1084
|
-
const res = await this.socketHandle.disConnectDevices(devices);
|
|
1085
|
-
if (res.success) {
|
|
1086
|
-
this.setDevicesMeta(devices, 'connectType', 'NONE', true);
|
|
1087
|
-
}
|
|
1088
|
-
return res;
|
|
1089
|
-
}
|
|
1090
|
-
|
|
1091
|
-
/**
|
|
1092
|
-
*
|
|
1093
|
-
* @param device 待取消订阅设备
|
|
1094
|
-
* @returns 取消订阅结果
|
|
1095
|
-
*/
|
|
1096
|
-
unSubscribe = async (device: IDevice) => {
|
|
1097
|
-
const target = this.allDevices.find((item) => isSameDevice(item, device));
|
|
1098
|
-
|
|
1099
|
-
switch (target.connectType) {
|
|
1100
|
-
case 'BLE': {
|
|
1101
|
-
const disconnectRes = await this.bleHandle.disConnectDevice(
|
|
1102
|
-
target.bleDeviceID as string
|
|
1103
|
-
);
|
|
1104
|
-
if (disconnectRes.success) {
|
|
1105
|
-
// 更新ctime
|
|
1106
|
-
this.setDeviceMeta(target, 'connectType', 'NONE', true);
|
|
1107
|
-
}
|
|
1108
|
-
return disconnectRes;
|
|
1109
|
-
}
|
|
1110
|
-
case 'WAN': {
|
|
1111
|
-
const res = this.socketHandle.disConnectDevice(target);
|
|
1112
|
-
if (res.success) {
|
|
1113
|
-
this.setDeviceMeta(target, 'connectType', 'NONE', true);
|
|
1114
|
-
}
|
|
1115
|
-
return res;
|
|
1116
|
-
}
|
|
1117
|
-
case 'LAN': {
|
|
1118
|
-
const res = this.lanHandle.disConnectDevice(target);
|
|
1119
|
-
if (res.success) {
|
|
1120
|
-
this.setDeviceMeta(target, 'connectType', 'NONE', true);
|
|
1121
|
-
}
|
|
1122
|
-
return res;
|
|
1123
|
-
}
|
|
1124
|
-
}
|
|
1125
|
-
return {
|
|
1126
|
-
success: false,
|
|
1127
|
-
};
|
|
1128
|
-
};
|
|
1129
|
-
|
|
1130
|
-
/**
|
|
1131
|
-
* 绑定注册到云端的设备
|
|
1132
|
-
* 设备必须要注册到云端才能正常绑定
|
|
1133
|
-
*/
|
|
1134
|
-
bindRemoteDevice = async ({
|
|
1135
|
-
mac,
|
|
1136
|
-
productKey,
|
|
1137
|
-
alias,
|
|
1138
|
-
beOwner,
|
|
1139
|
-
}: IBindRemoteDeviceParams): Promise<ISDKResult<IOpenApiDevice>> => {
|
|
1140
|
-
const target = getProductInfoThroughPK(productKey, this.productInfo);
|
|
1141
|
-
if (target) {
|
|
1142
|
-
const data = await bindMac({
|
|
1143
|
-
mac,
|
|
1144
|
-
productKey,
|
|
1145
|
-
productSecret: target.productSecret,
|
|
1146
|
-
alias,
|
|
1147
|
-
beOwner,
|
|
1148
|
-
});
|
|
1149
|
-
// 绑定成功主动获取设备列表
|
|
1150
|
-
this.getBindingList();
|
|
1151
|
-
return data;
|
|
1152
|
-
}
|
|
1153
|
-
return {
|
|
1154
|
-
success: false,
|
|
1155
|
-
err: errorCode.GIZ_SDK_PRODUCTKEY_NOT_IN_SPECIFY,
|
|
1156
|
-
};
|
|
1157
|
-
};
|
|
1158
|
-
|
|
1159
|
-
checkUpdate = async ({
|
|
1160
|
-
device, type
|
|
1161
|
-
}: { device: IDevice, type: IOTAType }) => {
|
|
1162
|
-
const target = this.allDevices.find((item) => isSameDevice(item, device));
|
|
1163
|
-
if (target) {
|
|
1164
|
-
if (!target.did) {
|
|
1165
|
-
// 设备没有绑定
|
|
1166
|
-
return {
|
|
1167
|
-
success: false,
|
|
1168
|
-
err: errorCode.GIZ_OPENAPI_DEVICE_NOT_BOUND,
|
|
1169
|
-
}
|
|
1170
|
-
}
|
|
1171
|
-
switch (target.connectType) {
|
|
1172
|
-
case 'BLE': {
|
|
1173
|
-
return this.bleHandle.checkUpdate({ device, type })
|
|
1174
|
-
}
|
|
1175
|
-
default: {
|
|
1176
|
-
return {
|
|
1177
|
-
success: false,
|
|
1178
|
-
err: errorCode.GIZ_SDK_OTA_DEVICE_NOT_SUPPORT,
|
|
1179
|
-
}
|
|
1180
|
-
}
|
|
1181
|
-
}
|
|
1182
|
-
}
|
|
1183
|
-
return {
|
|
1184
|
-
success: false,
|
|
1185
|
-
err: errorCode.GIZ_SDK_DEVICE_NOT_SUBSCRIBED,
|
|
1186
|
-
};
|
|
1187
|
-
}
|
|
1188
|
-
|
|
1189
|
-
getDeviceInfo = async ({
|
|
1190
|
-
device
|
|
1191
|
-
}: { device: IDevice }) => {
|
|
1192
|
-
const target = this.allDevices.find((item) => isSameDevice(item, device));
|
|
1193
|
-
if (target) {
|
|
1194
|
-
if (!target.did) {
|
|
1195
|
-
// 设备没有绑定
|
|
1196
|
-
return {
|
|
1197
|
-
success: false,
|
|
1198
|
-
err: errorCode.GIZ_OPENAPI_DEVICE_NOT_BOUND,
|
|
1199
|
-
}
|
|
1200
|
-
}
|
|
1201
|
-
switch (target.connectType) {
|
|
1202
|
-
case 'BLE': {
|
|
1203
|
-
return this.bleHandle.getDeviceInfo({ deviceId: device.bleDeviceID })
|
|
1204
|
-
}
|
|
1205
|
-
default: {
|
|
1206
|
-
return {
|
|
1207
|
-
success: false,
|
|
1208
|
-
err: errorCode.GIZ_SDK_OTA_DEVICE_NOT_SUPPORT,
|
|
1209
|
-
}
|
|
1210
|
-
}
|
|
1211
|
-
}
|
|
1212
|
-
}
|
|
1213
|
-
return {
|
|
1214
|
-
success: false,
|
|
1215
|
-
err: errorCode.GIZ_SDK_DEVICE_NOT_SUBSCRIBED,
|
|
1216
|
-
};
|
|
1217
|
-
}
|
|
1218
|
-
|
|
1219
|
-
startUpgrade = async ({
|
|
1220
|
-
device, type, onProgress, onEvent
|
|
1221
|
-
}: { device: IDevice, type: IOTAType, onProgress: OTAProgressEventCallback, onEvent: OTAEventCallback }) => {
|
|
1222
|
-
const target = this.allDevices.find((item) => isSameDevice(item, device));
|
|
1223
|
-
if (target) {
|
|
1224
|
-
if (!target.did) {
|
|
1225
|
-
// 设备没有绑定
|
|
1226
|
-
return {
|
|
1227
|
-
success: false,
|
|
1228
|
-
err: errorCode.GIZ_OPENAPI_DEVICE_NOT_BOUND,
|
|
1229
|
-
}
|
|
1230
|
-
}
|
|
1231
|
-
switch (target.connectType) {
|
|
1232
|
-
case 'BLE': {
|
|
1233
|
-
return this.bleHandle.startUpgrade({ device, type, productKey: device.productKey, onProgress, onEvent })
|
|
1234
|
-
}
|
|
1235
|
-
default: {
|
|
1236
|
-
return {
|
|
1237
|
-
success: false,
|
|
1238
|
-
err: errorCode.GIZ_SDK_OTA_DEVICE_NOT_SUPPORT,
|
|
1239
|
-
}
|
|
1240
|
-
}
|
|
1241
|
-
}
|
|
1242
|
-
}
|
|
1243
|
-
return {
|
|
1244
|
-
success: false,
|
|
1245
|
-
err: errorCode.GIZ_SDK_DEVICE_NOT_SUBSCRIBED,
|
|
1246
|
-
};
|
|
1247
|
-
}
|
|
1248
|
-
|
|
1249
|
-
unbindDevice = async ({
|
|
1250
|
-
devices,
|
|
1251
|
-
}: {
|
|
1252
|
-
devices: IDevice[];
|
|
1253
|
-
}): Promise<ISDKResult<IUnbindReturn>> => {
|
|
1254
|
-
const data = await unbindDevice({ devices });
|
|
1255
|
-
// 主动刷新列表
|
|
1256
|
-
if (data.success) {
|
|
1257
|
-
this.getBindingList();
|
|
1258
|
-
}
|
|
1259
|
-
return data;
|
|
1260
|
-
};
|
|
1261
|
-
|
|
1262
|
-
deviceSafetyRegister = async ({
|
|
1263
|
-
mac,
|
|
1264
|
-
productKey,
|
|
1265
|
-
}: IDeviceSafetyRegisterParams): Promise<ISDKResult<ISafeRegisterReturn>> => {
|
|
1266
|
-
return await this.deviceSafetyApi({ mac, productKey, isReset: false });
|
|
1267
|
-
};
|
|
1268
|
-
deviceSafetyUnbind = async ({
|
|
1269
|
-
mac,
|
|
1270
|
-
productKey,
|
|
1271
|
-
}: IDeviceSafetyRegisterParams): Promise<ISDKResult<ISafeRegisterReturn>> => {
|
|
1272
|
-
return await this.deviceSafetyApi({ mac, productKey, isReset: true });
|
|
1273
|
-
};
|
|
1274
|
-
|
|
1275
|
-
/**
|
|
1276
|
-
* 配网接口
|
|
1277
|
-
* setDeviceOnboardingDeploy方法不可重复调用
|
|
1278
|
-
*/
|
|
1279
|
-
setDeviceOnboardingDeploy = async ({
|
|
1280
|
-
ssid,
|
|
1281
|
-
bssid = '',
|
|
1282
|
-
password,
|
|
1283
|
-
mode = 0,
|
|
1284
|
-
timeout,
|
|
1285
|
-
isBind = true,
|
|
1286
|
-
softAPSSIDPrefix,
|
|
1287
|
-
eventCallBack
|
|
1288
|
-
}: ISetDeviceOnboardingDeployProps): Promise<ISDKResult<IDevice[]>> => {
|
|
1289
|
-
if (this.currentWifiConfigHandle) {
|
|
1290
|
-
return {
|
|
1291
|
-
success: false,
|
|
1292
|
-
err: errorCode.GIZ_SDK_DEVICE_CONFIG_IS_RUNNING,
|
|
1293
|
-
};
|
|
1294
|
-
}
|
|
1295
|
-
const sTime = new Date().getTime();
|
|
1296
|
-
GizLog.debug('GIZ_SDK: 配网开始时间:' + sTime, mode);
|
|
1297
|
-
try {
|
|
1298
|
-
switch (mode) {
|
|
1299
|
-
case 0: {
|
|
1300
|
-
this.currentWifiConfigHandle = new ApConfig(
|
|
1301
|
-
ssid,
|
|
1302
|
-
bssid,
|
|
1303
|
-
password,
|
|
1304
|
-
this.specialProductKeys,
|
|
1305
|
-
this.specialProductKeySecrets,
|
|
1306
|
-
);
|
|
1307
|
-
const data =
|
|
1308
|
-
await this.currentWifiConfigHandle.setDeviceOnboardingDeploy({
|
|
1309
|
-
timeout,
|
|
1310
|
-
isBind,
|
|
1311
|
-
softAPSSIDPrefix,
|
|
1312
|
-
eventCallBack,
|
|
1313
|
-
});
|
|
1314
|
-
this.currentWifiConfigHandle = null;
|
|
1315
|
-
const eTime = new Date().getTime();
|
|
1316
|
-
GizLog.debug('GIZ_SDK: 配网结束时间:' + eTime);
|
|
1317
|
-
GizLog.debug('GIZ_SDK: 配网用时:' + (eTime - sTime));
|
|
1318
|
-
return data;
|
|
1319
|
-
}
|
|
1320
|
-
case 3: {
|
|
1321
|
-
this.currentWifiConfigHandle = new BLEConfig(
|
|
1322
|
-
ssid,
|
|
1323
|
-
bssid,
|
|
1324
|
-
password,
|
|
1325
|
-
this.specialProductKeys,
|
|
1326
|
-
this.specialProductKeySecrets,
|
|
1327
|
-
this.bleHandle
|
|
1328
|
-
);
|
|
1329
|
-
const data =
|
|
1330
|
-
await this.currentWifiConfigHandle.setDeviceOnboardingDeploy({
|
|
1331
|
-
timeout,
|
|
1332
|
-
isBind,
|
|
1333
|
-
softAPSSIDPrefix,
|
|
1334
|
-
eventCallBack,
|
|
1335
|
-
});
|
|
1336
|
-
this.currentWifiConfigHandle = null;
|
|
1337
|
-
const eTime = new Date().getTime();
|
|
1338
|
-
GizLog.debug('GIZ_SDK: 配网结束时间:' + eTime);
|
|
1339
|
-
GizLog.debug('GIZ_SDK: 配网用时:' + (eTime - sTime));
|
|
1340
|
-
return data;
|
|
1341
|
-
}
|
|
1342
|
-
case 5: {
|
|
1343
|
-
// 只有安卓支持
|
|
1344
|
-
const sysInfo = await wx.getSystemInfoSync();
|
|
1345
|
-
console.log('sysInfosysInfo', sysInfo)
|
|
1346
|
-
if (sysInfo.platform !== 'android') {
|
|
1347
|
-
return {
|
|
1348
|
-
success: false,
|
|
1349
|
-
message: '只支持安卓'
|
|
1350
|
-
}
|
|
1351
|
-
}
|
|
1352
|
-
const cloudServiceInfo = getGlobalData('cloudServiceInfo');
|
|
1353
|
-
|
|
1354
|
-
console.log('cloudServiceInfo', cloudServiceInfo)
|
|
1355
|
-
|
|
1356
|
-
this.currentWifiConfigHandle = new NFCConfig(
|
|
1357
|
-
ssid,
|
|
1358
|
-
bssid,
|
|
1359
|
-
password,
|
|
1360
|
-
this.specialProductKeys,
|
|
1361
|
-
this.specialProductKeySecrets,
|
|
1362
|
-
cloudServiceInfo?.openAPIInfo || 'api.gizwits.com',
|
|
1363
|
-
);
|
|
1364
|
-
const data =
|
|
1365
|
-
await this.currentWifiConfigHandle.setDeviceOnboardingDeploy({
|
|
1366
|
-
timeout,
|
|
1367
|
-
isBind,
|
|
1368
|
-
softAPSSIDPrefix,
|
|
1369
|
-
eventCallBack,
|
|
1370
|
-
});
|
|
1371
|
-
this.currentWifiConfigHandle = null;
|
|
1372
|
-
const eTime = new Date().getTime();
|
|
1373
|
-
GizLog.debug('GIZ_SDK: 配网结束时间:' + eTime);
|
|
1374
|
-
GizLog.debug('GIZ_SDK: 配网用时:' + (eTime - sTime));
|
|
1375
|
-
return data;
|
|
1376
|
-
}
|
|
1377
|
-
}
|
|
1378
|
-
} catch (error) {
|
|
1379
|
-
GizLog.error("GIZ_SDK: setDeviceOnboardingDeploy", error);
|
|
1380
|
-
if (
|
|
1381
|
-
(error as ISDKResult<IDevice[]>).err &&
|
|
1382
|
-
(error as ISDKResult<IDevice[]>).err!.errorCode
|
|
1383
|
-
) {
|
|
1384
|
-
return error as ISDKResult<IDevice[]>;
|
|
1385
|
-
}
|
|
1386
|
-
return {
|
|
1387
|
-
success: false,
|
|
1388
|
-
err: errorCode.GIZ_SDK_OTHERWISE,
|
|
1389
|
-
};
|
|
1390
|
-
} finally {
|
|
1391
|
-
this.currentWifiConfigHandle = null;
|
|
1392
|
-
}
|
|
1393
|
-
};
|
|
1394
|
-
|
|
1395
|
-
/**
|
|
1396
|
-
* 设置域名
|
|
1397
|
-
*/
|
|
1398
|
-
private setDomain = (cloudServiceInfo: ICloudServiceInfo) => {
|
|
1399
|
-
const openAPIInfo =
|
|
1400
|
-
(cloudServiceInfo && cloudServiceInfo.openAPIInfo) || 'api.gizwits.com';
|
|
1401
|
-
|
|
1402
|
-
const newData = { ...(cloudServiceInfo || {}), openAPIInfo };
|
|
1403
|
-
setGlobalData('cloudServiceInfo', newData);
|
|
1404
|
-
};
|
|
1405
|
-
|
|
1406
|
-
private deviceSafetyApi = async ({
|
|
1407
|
-
mac,
|
|
1408
|
-
productKey,
|
|
1409
|
-
isReset,
|
|
1410
|
-
}: IDeviceSafetyParams): Promise<ISDKResult<ISafeRegisterReturn>> => {
|
|
1411
|
-
const target = getProductInfoThroughPK(productKey, this.productInfo);
|
|
1412
|
-
if (target) {
|
|
1413
|
-
return await safeRegister({
|
|
1414
|
-
productKey,
|
|
1415
|
-
productSecret: target.productSecret,
|
|
1416
|
-
mac,
|
|
1417
|
-
isReset,
|
|
1418
|
-
passcode: '1234567890',
|
|
1419
|
-
});
|
|
1420
|
-
}
|
|
1421
|
-
return {
|
|
1422
|
-
success: false,
|
|
1423
|
-
err: errorCode.GIZ_SDK_PRODUCTKEY_NOT_IN_SPECIFY,
|
|
1424
|
-
};
|
|
1425
|
-
};
|
|
1426
|
-
|
|
1427
|
-
// 内部刷新设备列表的方法
|
|
1428
|
-
public getBindingList = async () => {
|
|
1429
|
-
const data = await getBindingList();
|
|
1430
|
-
this.deviceList = data.data || [];
|
|
1431
|
-
// 推送
|
|
1432
|
-
this.notiDeviceList();
|
|
1433
|
-
return data;
|
|
1434
|
-
};
|
|
1435
|
-
|
|
1436
|
-
/**
|
|
1437
|
-
* 刷新云端设备列表
|
|
1438
|
-
* 并合并数据
|
|
1439
|
-
*/
|
|
1440
|
-
public getDevices = async (): Promise<ISDKResult<IDevice[]>> => {
|
|
1441
|
-
// merage
|
|
1442
|
-
await this.getBindingList();
|
|
1443
|
-
// 简单粗暴的先合并
|
|
1444
|
-
return {
|
|
1445
|
-
success: true,
|
|
1446
|
-
data: this.allDevices,
|
|
1447
|
-
};
|
|
1448
|
-
};
|
|
1449
|
-
|
|
1450
|
-
/**
|
|
1451
|
-
* 停止配网
|
|
1452
|
-
*/
|
|
1453
|
-
stopDeviceOnboardingDeploy = (): void => {
|
|
1454
|
-
if (this.currentWifiConfigHandle) {
|
|
1455
|
-
this.currentWifiConfigHandle.stopDeviceOnboardingDeploy();
|
|
1456
|
-
this.currentWifiConfigHandle = null;
|
|
1457
|
-
}
|
|
1458
|
-
};
|
|
1459
|
-
|
|
1460
|
-
/**
|
|
1461
|
-
* setLogLevel
|
|
1462
|
-
*/
|
|
1463
|
-
setLogLevel = (level: TLogType) => {
|
|
1464
|
-
return GizLog.setLogLevel(level);
|
|
1465
|
-
}
|
|
1466
|
-
|
|
1467
|
-
public renameDevice = async (device: IDevice, name: string) => {
|
|
1468
|
-
if (!name) {
|
|
1469
|
-
return {
|
|
1470
|
-
success: false,
|
|
1471
|
-
message: "please input device name"
|
|
1472
|
-
}
|
|
1473
|
-
}
|
|
1474
|
-
if (device.isBind) {
|
|
1475
|
-
return await editBindInfo({
|
|
1476
|
-
did: device.did,
|
|
1477
|
-
name: name,
|
|
1478
|
-
})
|
|
1479
|
-
}
|
|
1480
|
-
return {
|
|
1481
|
-
success: false,
|
|
1482
|
-
message: "device not bind"
|
|
1483
|
-
}
|
|
1484
|
-
}
|
|
1485
|
-
|
|
1486
|
-
/**
|
|
1487
|
-
* 移除事件监听
|
|
1488
|
-
* @param type 事件监听名称,详见下表
|
|
1489
|
-
* @param func 事件监听回调
|
|
1490
|
-
* @returns
|
|
1491
|
-
*/
|
|
1492
|
-
public removeEventListener = <K extends keyof TListenerType>(
|
|
1493
|
-
type: K,
|
|
1494
|
-
func: TListenerType[K]
|
|
1495
|
-
) => {
|
|
1496
|
-
if (this.listenerMap[type]) {
|
|
1497
|
-
const index = this.listenerMap[type].indexOf(func);
|
|
1498
|
-
if (index !== -1) {
|
|
1499
|
-
// 删除数组中的这一行
|
|
1500
|
-
this.listenerMap[type].splice(index, 1);
|
|
1501
|
-
return {
|
|
1502
|
-
success: true,
|
|
1503
|
-
};
|
|
1504
|
-
}
|
|
1505
|
-
}
|
|
1506
|
-
return {
|
|
1507
|
-
success: false,
|
|
1508
|
-
// err: errorCode.GIZ_SDK_BLE_BLUETOOTH_FUNCTION_NOT_TURNED_ON
|
|
1509
|
-
};
|
|
1510
|
-
};
|
|
1511
|
-
|
|
1512
|
-
public addEventListener = async <K extends keyof TListenerType>(
|
|
1513
|
-
type: K,
|
|
1514
|
-
func: TListenerType[K]
|
|
1515
|
-
) => {
|
|
1516
|
-
if (!this.listenerMap[type]) {
|
|
1517
|
-
// 创建一个新的
|
|
1518
|
-
this.listenerMap[type] = [];
|
|
1519
|
-
}
|
|
1520
|
-
|
|
1521
|
-
if (this.listenerMap[type].indexOf(func) === -1) {
|
|
1522
|
-
this.listenerMap[type].push(func);
|
|
1523
|
-
}
|
|
1524
|
-
return {
|
|
1525
|
-
success: true,
|
|
1526
|
-
};
|
|
1527
|
-
};
|
|
1528
|
-
|
|
1529
|
-
public destory = () => {
|
|
1530
|
-
this.bleHandle.destory();
|
|
1531
|
-
this.lanHandle.destory();
|
|
1532
|
-
this.socketHandle.destory();
|
|
1533
|
-
this.keepScanTimer && clearInterval(this.keepScanTimer);
|
|
1534
|
-
};
|
|
1535
|
-
}
|
|
1536
|
-
export default GizwitsMiniSDK;
|