mini_program_gizwits_sdk 3.3.2 → 3.3.25-kuka
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/README.md +11 -28
- package/dist/index.js +3 -3
- package/dist/src/aepApi/aepApiRequest.d.ts +7 -0
- package/dist/src/handler/ble.d.ts +1 -4
- package/dist/src/handler/lan.d.ts +1 -1
- package/dist/src/sdk.d.ts +5 -5
- package/dist/src/wifiConfig/ble_ailCloud.d.ts +67 -0
- package/package.json +2 -2
- package/src/aepApi/aepApiRequest.ts +62 -0
- package/src/handler/ble.ts +35 -48
- package/src/handler/lan.ts +1 -1
- package/src/handler/socket.ts +10 -3
- package/src/sdk.ts +61 -93
- package/src/services/devices.ts +4 -92
- package/src/wifiConfig/ble.ts +2 -2
- package/src/wifiConfig/ble_ailCloud.ts +562 -0
- package/dist/src/protocol/Reset.d.ts +0 -8
- package/dist/src/protocol/SetReset.d.ts +0 -7
- package/src/protocol/Reset.ts +0 -16
- package/src/protocol/SetReset.ts +0 -13
package/src/services/devices.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { enc, AES, mode, pad } from 'crypto-js';
|
|
2
1
|
import openApiRequest, { IServiceResult } from '../openApiRequest';
|
|
3
2
|
import getRandomCodes from '../randomCode';
|
|
4
3
|
import { IRandomCodesResult } from '../sdk';
|
|
5
4
|
import { psKeySign } from './tool';
|
|
6
5
|
// eslint-disable-next-line import/no-commonjs
|
|
7
6
|
// const CryptoJS = require('crypto-js');
|
|
7
|
+
import { enc, AES, mode, pad } from 'crypto-js';
|
|
8
8
|
|
|
9
9
|
export interface IOpenApiDevice {
|
|
10
10
|
product_key: string;
|
|
@@ -26,59 +26,6 @@ interface IDeviceRes {
|
|
|
26
26
|
devices: IOpenApiDevice[];
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
// 云端aes 特殊解码
|
|
30
|
-
class Padding {
|
|
31
|
-
/**
|
|
32
|
-
* Pads data using the algorithm defined in PKCS #5/7.
|
|
33
|
-
*
|
|
34
|
-
* @param data The data to pad.
|
|
35
|
-
* @param blockSize The multiple that the data should be padded to.
|
|
36
|
-
*
|
|
37
|
-
* @example
|
|
38
|
-
*
|
|
39
|
-
* CryptoJS.pad.Pkcs7.pad(wordArray, 4);
|
|
40
|
-
*/
|
|
41
|
-
pad = (data, blockSize) => {
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Unpads data that had been padded using the algorithm defined in PKCS #5/7.
|
|
47
|
-
*
|
|
48
|
-
* @param data The data to unpad.
|
|
49
|
-
*
|
|
50
|
-
* @example
|
|
51
|
-
*
|
|
52
|
-
* CryptoJS.pad.Pkcs7.unpad(wordArray);
|
|
53
|
-
*/
|
|
54
|
-
unpad = (data) => {
|
|
55
|
-
const AES_BLOCK_LEN = 16;
|
|
56
|
-
const len = data.sigBytes;
|
|
57
|
-
let readed = 0;
|
|
58
|
-
let repeat = 1;
|
|
59
|
-
const stringData= data.toString(enc.Utf8);
|
|
60
|
-
while(readed + AES_BLOCK_LEN <= len)
|
|
61
|
-
{
|
|
62
|
-
readed += AES_BLOCK_LEN;
|
|
63
|
-
}
|
|
64
|
-
for(let i = 0; i < (AES_BLOCK_LEN - 2) / 2; i++)
|
|
65
|
-
{
|
|
66
|
-
const start = readed - i * 2;
|
|
67
|
-
if(stringData.substring(start, start - 2) == stringData.substring(start - 2, start - 4))
|
|
68
|
-
{
|
|
69
|
-
repeat++;
|
|
70
|
-
}
|
|
71
|
-
else
|
|
72
|
-
{
|
|
73
|
-
readed -= repeat * 2;
|
|
74
|
-
|
|
75
|
-
break;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
data.sigBytes = readed;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
29
|
export const getBindingList = async (): Promise<IServiceResult<IDevice[]>> => {
|
|
83
30
|
const data = await openApiRequest<IDeviceRes>(
|
|
84
31
|
'/app/bindings?show_disabled=0&limit=1000&skip=0',
|
|
@@ -248,23 +195,12 @@ export async function safeRegister({
|
|
|
248
195
|
false
|
|
249
196
|
);
|
|
250
197
|
if (data.success) {
|
|
251
|
-
|
|
252
|
-
const failedDevices = [];
|
|
253
|
-
|
|
254
|
-
const targetDevice = successDevices.find(item => item.mac === mac);
|
|
255
|
-
if (!targetDevice) {
|
|
256
|
-
// 不存在
|
|
257
|
-
failedDevices.push({
|
|
258
|
-
mac,
|
|
259
|
-
})
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
// TODO 解码
|
|
198
|
+
// 解码
|
|
263
199
|
return {
|
|
264
200
|
success: true,
|
|
265
201
|
data: {
|
|
266
|
-
successDevices,
|
|
267
|
-
failedDevices,
|
|
202
|
+
successDevices: [],
|
|
203
|
+
failedDevices: [],
|
|
268
204
|
},
|
|
269
205
|
};
|
|
270
206
|
}
|
|
@@ -274,30 +210,6 @@ export async function safeRegister({
|
|
|
274
210
|
};
|
|
275
211
|
}
|
|
276
212
|
|
|
277
|
-
const unPackSafeRegisterRes = ({productSecret, data, iv}: {productSecret: string, data: string, iv: any}) => {
|
|
278
|
-
const aesKey = enc.Hex.parse(productSecret)
|
|
279
|
-
const bodyHex = enc.Hex.parse(data)
|
|
280
|
-
let resEncrypted = AES.decrypt(enc.Base64.stringify(bodyHex), aesKey, { iv: iv, mode: mode.ECB, padding: new Padding() });
|
|
281
|
-
const resString = resEncrypted.toString(enc.Utf8);
|
|
282
|
-
const newData = [];
|
|
283
|
-
resString.split(',').map(item => {
|
|
284
|
-
const tmp = item.split('&');
|
|
285
|
-
const mac = tmp[0].replace('mac=', '');
|
|
286
|
-
const did = tmp[1].replace('did=', '');
|
|
287
|
-
newData.push({
|
|
288
|
-
mac, did
|
|
289
|
-
})
|
|
290
|
-
})
|
|
291
|
-
return newData;
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
// const unPackSafeRegister = unPackSafeRegisterRes({
|
|
295
|
-
// productSecret: 'aaa0840301e04748adaf9fcd160e7508',
|
|
296
|
-
// iv: enc.Hex.parse(''),
|
|
297
|
-
// data: '56ab36690dcb1e42bc3e69dab30c2e670c2188dfb76cb5a2f12986d18eb069f0f63d3c89ab069f3573ef81ca02936ad4'
|
|
298
|
-
// })
|
|
299
|
-
// console.log('unPackSafeRegisterunPackSafeRegister', unPackSafeRegister)
|
|
300
|
-
|
|
301
213
|
interface IRenameProps {
|
|
302
214
|
name?: string;
|
|
303
215
|
did: string;
|
package/src/wifiConfig/ble.ts
CHANGED
|
@@ -31,8 +31,8 @@ const configAck = '0000000303000002';
|
|
|
31
31
|
export function sendBLEConfigCmd({
|
|
32
32
|
bleDeviceId,
|
|
33
33
|
arrayBuffer,
|
|
34
|
-
serviceUUIDSuffix = '
|
|
35
|
-
characteristicUUIDSuffix = '
|
|
34
|
+
serviceUUIDSuffix = 'fe60',
|
|
35
|
+
characteristicUUIDSuffix = 'fe61',
|
|
36
36
|
bleHandle
|
|
37
37
|
}: IArgs) {
|
|
38
38
|
let sendInterval = null;
|
|
@@ -0,0 +1,562 @@
|
|
|
1
|
+
import errorCode from "../errorCode";
|
|
2
|
+
import { IResult } from "../sdk";
|
|
3
|
+
import { ab2hex, advertisData2PkAndMac, isWXDevicesResult } from '../utils';
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
getBluetoothAdapterState,
|
|
7
|
+
startBluetoothDevicesDiscovery,
|
|
8
|
+
getBluetoothDevices,
|
|
9
|
+
notifyBLECharacteristicValueChange,
|
|
10
|
+
unpackWriteBLECharacteristicValue,
|
|
11
|
+
getBLEDeviceServices,
|
|
12
|
+
getBLEDeviceCharacteristics,
|
|
13
|
+
retryConnect,
|
|
14
|
+
} from '../wechatApi';
|
|
15
|
+
import sleep from "../sleep";
|
|
16
|
+
import ConfigBase from "./ConfigBase";
|
|
17
|
+
import WifiConfig from "../protocol/WifiConfig";
|
|
18
|
+
import GizLog from "../GizLog";
|
|
19
|
+
import { BleHandle } from "../handler/ble";
|
|
20
|
+
import aepApiRequest from "../aepApi/aepApiRequest";
|
|
21
|
+
|
|
22
|
+
const productKeyMap = {
|
|
23
|
+
'a57a49b9b265421ca337219cd66a6701': 'icgsfwJZDFX'
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
interface IVerifyOnlineResult {
|
|
27
|
+
data: {
|
|
28
|
+
mac: string;
|
|
29
|
+
onlineStatus: number;
|
|
30
|
+
lastOnlineTime: string;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface IConfigResult {
|
|
35
|
+
mac: string;
|
|
36
|
+
productKey: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
interface IArgs {
|
|
40
|
+
bleDeviceId: string;
|
|
41
|
+
arrayBuffer: ArrayBuffer;
|
|
42
|
+
serviceUUIDSuffix?: string;
|
|
43
|
+
characteristicUUIDSuffix?: string;
|
|
44
|
+
bleHandle: BleHandle;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const configAck = '0000000303000002';
|
|
48
|
+
|
|
49
|
+
export function sendBLEConfigCmd({
|
|
50
|
+
bleDeviceId,
|
|
51
|
+
arrayBuffer,
|
|
52
|
+
serviceUUIDSuffix = 'fe60',
|
|
53
|
+
characteristicUUIDSuffix = 'fe61',
|
|
54
|
+
bleHandle
|
|
55
|
+
}: IArgs) {
|
|
56
|
+
let sendInterval = null;
|
|
57
|
+
let closeTimeout = null;
|
|
58
|
+
|
|
59
|
+
const checkInterrupt = () => {
|
|
60
|
+
if (closeTimeout === null) {
|
|
61
|
+
sendInterval && clearInterval(sendInterval);
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return new Promise<boolean>(async (resolve, reject) => {
|
|
68
|
+
try {
|
|
69
|
+
GizLog.debug(
|
|
70
|
+
'GIZ_SDK: ssid info: ', arrayBuffer
|
|
71
|
+
);
|
|
72
|
+
// 设置超时
|
|
73
|
+
closeTimeout = setTimeout(() => {
|
|
74
|
+
sendInterval && clearInterval(sendInterval);
|
|
75
|
+
// 发送超时 返回失败
|
|
76
|
+
closeTimeout = null;
|
|
77
|
+
resolve(false);
|
|
78
|
+
}, 5 * 1000)
|
|
79
|
+
GizLog.debug(
|
|
80
|
+
'GIZ_SDK: createBLEConnection start: ', bleDeviceId
|
|
81
|
+
);
|
|
82
|
+
await bleHandle.disableScan()
|
|
83
|
+
|
|
84
|
+
const connectData = await retryConnect(bleDeviceId, 10 * 1000);
|
|
85
|
+
bleHandle.enableScan();
|
|
86
|
+
|
|
87
|
+
GizLog.debug(
|
|
88
|
+
'GIZ_SDK: createBLEConnection end, res:', connectData
|
|
89
|
+
);
|
|
90
|
+
GizLog.debug(
|
|
91
|
+
'GIZ_SDK: getBLEDeviceServices start, res:', serviceUUIDSuffix,
|
|
92
|
+
);
|
|
93
|
+
const services = await getBLEDeviceServices(bleDeviceId);
|
|
94
|
+
GizLog.debug(
|
|
95
|
+
'GIZ_SDK: getBLEDeviceServices end, res:', services,
|
|
96
|
+
|
|
97
|
+
);
|
|
98
|
+
const service = services.find((s) =>
|
|
99
|
+
s.uuid.split('-')[0].toLowerCase().endsWith(serviceUUIDSuffix)
|
|
100
|
+
);
|
|
101
|
+
if (!service) {
|
|
102
|
+
// 获取蓝牙设备服务异常
|
|
103
|
+
GizLog.debug(
|
|
104
|
+
'GIZ_SDK: get ble device services fail',
|
|
105
|
+
bleDeviceId,
|
|
106
|
+
services
|
|
107
|
+
);
|
|
108
|
+
resolve(false);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const characteristics = await getBLEDeviceCharacteristics(
|
|
113
|
+
bleDeviceId,
|
|
114
|
+
service.uuid
|
|
115
|
+
);
|
|
116
|
+
GizLog.debug(
|
|
117
|
+
'GIZ_SDK: getBLEDeviceCharacteristics end, res:', characteristics
|
|
118
|
+
);
|
|
119
|
+
const characteristic = characteristics.find((c) =>
|
|
120
|
+
c.uuid.split('-')[0].toLowerCase().endsWith(characteristicUUIDSuffix)
|
|
121
|
+
);
|
|
122
|
+
if (!characteristic) {
|
|
123
|
+
// 获取蓝牙设备特征值异常
|
|
124
|
+
GizLog.debug(
|
|
125
|
+
'GIZ_SDK: get ble device characteristics fail',
|
|
126
|
+
bleDeviceId,
|
|
127
|
+
characteristics
|
|
128
|
+
);
|
|
129
|
+
resolve(false);
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (
|
|
134
|
+
!characteristic.properties.notify &&
|
|
135
|
+
!characteristic.properties.indicate
|
|
136
|
+
) {
|
|
137
|
+
GizLog.debug(
|
|
138
|
+
'GIZ_SDK: the ble device characteristic not support notify or indicate',
|
|
139
|
+
bleDeviceId,
|
|
140
|
+
characteristic
|
|
141
|
+
);
|
|
142
|
+
// 该设备不支持 notify & indicate 操作
|
|
143
|
+
resolve(false);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
GizLog.debug(
|
|
148
|
+
'GIZ_SDK: check characteristic and service success',
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
await notifyBLECharacteristicValueChange(
|
|
152
|
+
bleDeviceId,
|
|
153
|
+
service.uuid,
|
|
154
|
+
characteristic.uuid
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
// 这里有个问题,模组好像不一定每次都会回
|
|
158
|
+
const handleBLECharacteristicValueChange = (res: WechatMiniprogram.OnBLECharacteristicValueChangeCallbackResult) => {
|
|
159
|
+
const hexString = ab2hex(res.value);
|
|
160
|
+
GizLog.debug('GIZ_SDK: 收到设备返回ack', hexString)
|
|
161
|
+
if (hexString === configAck) {
|
|
162
|
+
// 发送成功
|
|
163
|
+
bleHandle.removeEventListener("GizBleDeviceData", handleBLECharacteristicValueChange)
|
|
164
|
+
resolve(true);
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
bleHandle.addEventListener("GizBleDeviceData", handleBLECharacteristicValueChange)
|
|
169
|
+
|
|
170
|
+
// 订阅特征值变化
|
|
171
|
+
// wx.onBLECharacteristicValueChange(handleBLECharacteristicValueChange);
|
|
172
|
+
|
|
173
|
+
GizLog.debug(
|
|
174
|
+
'GIZ_SDK: on notifyBLECharacteristicValueChange success',
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
if (checkInterrupt()) return;
|
|
178
|
+
|
|
179
|
+
await unpackWriteBLECharacteristicValue(
|
|
180
|
+
bleDeviceId,
|
|
181
|
+
service.uuid,
|
|
182
|
+
characteristic.uuid,
|
|
183
|
+
arrayBuffer
|
|
184
|
+
);
|
|
185
|
+
sendInterval = setInterval(() => {
|
|
186
|
+
if (checkInterrupt()) return;
|
|
187
|
+
unpackWriteBLECharacteristicValue(
|
|
188
|
+
bleDeviceId,
|
|
189
|
+
service.uuid,
|
|
190
|
+
characteristic.uuid,
|
|
191
|
+
arrayBuffer
|
|
192
|
+
);
|
|
193
|
+
}, 2000)
|
|
194
|
+
|
|
195
|
+
// if (res.errCode === 0) {
|
|
196
|
+
// resolve(true);
|
|
197
|
+
// } else{
|
|
198
|
+
// resolve(false);
|
|
199
|
+
// }
|
|
200
|
+
GizLog.debug('GIZ_SDK: unpackWriteBLECharacteristicValue end');
|
|
201
|
+
} catch (error) {
|
|
202
|
+
reject(false);
|
|
203
|
+
GizLog.debug('GIZ_SDK: sendBLEConfigCmd error', error);
|
|
204
|
+
}
|
|
205
|
+
})
|
|
206
|
+
.catch((error) => {
|
|
207
|
+
GizLog.debug('GIZ_SDK: sendBLEConfigCmd error', error);
|
|
208
|
+
})
|
|
209
|
+
.finally(() => {
|
|
210
|
+
// 关闭连接
|
|
211
|
+
closeTimeout && clearTimeout(closeTimeout);
|
|
212
|
+
sendInterval && clearInterval(sendInterval);
|
|
213
|
+
wx.closeBLEConnection({ deviceId: bleDeviceId });
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
interface configBLEDeviceParams {
|
|
218
|
+
ssid: string;
|
|
219
|
+
password: string;
|
|
220
|
+
softAPSSIDPrefix?: string;
|
|
221
|
+
timeout: number;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
interface ISetDeviceOnboardingDeployProps {
|
|
226
|
+
timeout: number;
|
|
227
|
+
isBind: boolean;
|
|
228
|
+
softAPSSIDPrefix: string;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
interface IOnlieTimeMap {
|
|
232
|
+
[key: string]: string
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
// 阿里云 蓝牙配网
|
|
238
|
+
class BLEAliCloudConfig extends ConfigBase {
|
|
239
|
+
bleHandle: BleHandle;
|
|
240
|
+
lastestOnlineTimeMap: IOnlieTimeMap = {};
|
|
241
|
+
|
|
242
|
+
constructor(ssid: string, password: string, specialProductKeys: string[], specialProductKeySecrets: string[], bleHandle: BleHandle) {
|
|
243
|
+
super(ssid, password, specialProductKeys, specialProductKeySecrets)
|
|
244
|
+
this.bleHandle = bleHandle;
|
|
245
|
+
}
|
|
246
|
+
destroy = () => {
|
|
247
|
+
this.cleanTimeout();
|
|
248
|
+
}
|
|
249
|
+
isValidBleDevice = (
|
|
250
|
+
bleDevice: WechatMiniprogram.BlueToothDevice,
|
|
251
|
+
softAPSSIDPrefix?: string
|
|
252
|
+
) => {
|
|
253
|
+
if (!bleDevice || !bleDevice.advertisData) {
|
|
254
|
+
// 无效蓝牙设备或者蓝牙设备广播数据为空,返回失败
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const { productKey, mac } = advertisData2PkAndMac(bleDevice.advertisData, this.specialProductKeys)
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
if (!this.specialProductKeys.includes(productKey)) {
|
|
262
|
+
// 不在PK列表
|
|
263
|
+
return false;
|
|
264
|
+
}
|
|
265
|
+
GizLog.debug(`GIZ_SDK: isValidBleDevice mac: ${mac} pk: ${productKey} name: ${bleDevice.name}`)
|
|
266
|
+
GizLog.debug(`GIZ_SDK: softAPSSIDPrefix: ${softAPSSIDPrefix} ismatch : ${bleDevice.name && bleDevice.name.startsWith(softAPSSIDPrefix)}`)
|
|
267
|
+
return (
|
|
268
|
+
!softAPSSIDPrefix ||
|
|
269
|
+
(bleDevice.name && bleDevice.name.startsWith(softAPSSIDPrefix))
|
|
270
|
+
);
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
enableBluetoothDevicesDescovery = async (): Promise<
|
|
275
|
+
{ success: false; err: IError } | { success: true }
|
|
276
|
+
> => {
|
|
277
|
+
// try {
|
|
278
|
+
// await closeBluetoothAdapter();
|
|
279
|
+
// await openBluetoothAdapter();
|
|
280
|
+
// } catch (error) {
|
|
281
|
+
|
|
282
|
+
// }
|
|
283
|
+
const stateRes = await getBluetoothAdapterState();
|
|
284
|
+
if (!stateRes.available) {
|
|
285
|
+
return {
|
|
286
|
+
success: false,
|
|
287
|
+
err: {
|
|
288
|
+
errorCode: errorCode.GIZ_SDK_BLE_BLUETOOTH_FUNCTION_NOT_TURNED_ON.errorCode,
|
|
289
|
+
errorMessage: '蓝牙状态不可用',
|
|
290
|
+
},
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
await startBluetoothDevicesDiscovery();
|
|
295
|
+
return { success: true };
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
enableAndGetBluetoothDevices = async (
|
|
300
|
+
softAPSSIDPrefix?: string
|
|
301
|
+
): Promise<
|
|
302
|
+
| { success: false; err: IError }
|
|
303
|
+
| { success: true; bleDevices?: WechatMiniprogram.BlueToothDevice[] }
|
|
304
|
+
> => {
|
|
305
|
+
const discoveryRes = await this.enableBluetoothDevicesDescovery();
|
|
306
|
+
if (!discoveryRes.success) {
|
|
307
|
+
return discoveryRes;
|
|
308
|
+
}
|
|
309
|
+
GizLog.debug('GIZ_SDK: start enableAndGetBluetoothDevices');
|
|
310
|
+
|
|
311
|
+
const bleDevices: WechatMiniprogram.BlueToothDevice[] = (
|
|
312
|
+
await getBluetoothDevices()
|
|
313
|
+
).filter((d) => this.isValidBleDevice(d, softAPSSIDPrefix));
|
|
314
|
+
GizLog.debug('GIZ_SDK: getBluetoothDevices success', bleDevices);
|
|
315
|
+
return {
|
|
316
|
+
success: true,
|
|
317
|
+
bleDevices,
|
|
318
|
+
};
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
setDeviceOnboardingDeploy = ({
|
|
322
|
+
timeout,
|
|
323
|
+
isBind = true,
|
|
324
|
+
softAPSSIDPrefix,
|
|
325
|
+
}: ISetDeviceOnboardingDeployProps) => {
|
|
326
|
+
return new Promise<IResult<IDevice[]>>(async (res, rej) => {
|
|
327
|
+
const ssid = this.ssid;
|
|
328
|
+
const password = this.password;
|
|
329
|
+
this.destroy();
|
|
330
|
+
this.initDeviceOnboardingDeploy(res, rej);
|
|
331
|
+
this.startTimeoutTimer(timeout);
|
|
332
|
+
|
|
333
|
+
try {
|
|
334
|
+
const result = await this.configBLEDevice({
|
|
335
|
+
ssid,
|
|
336
|
+
password,
|
|
337
|
+
timeout: timeout * 1000,
|
|
338
|
+
softAPSSIDPrefix,
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
const newData: IDevice[] = [result.data as IDevice];
|
|
342
|
+
// const newData: IDevice[] = (result.data || []).map(item => {
|
|
343
|
+
// return {
|
|
344
|
+
// mac: item.mac,
|
|
345
|
+
// productKey: item.product_key,
|
|
346
|
+
// did: item.did,
|
|
347
|
+
// name: '',
|
|
348
|
+
// isBind: false,
|
|
349
|
+
// connectType: 'NONE',
|
|
350
|
+
// isBleOnline: false,
|
|
351
|
+
// isLanOnline: false,
|
|
352
|
+
// isOnline: false,
|
|
353
|
+
// }
|
|
354
|
+
// })
|
|
355
|
+
if (isBind) {
|
|
356
|
+
res(await this.bindDevices(newData));
|
|
357
|
+
} else {
|
|
358
|
+
// 不需要绑定 直接返回成功
|
|
359
|
+
res({
|
|
360
|
+
success: true,
|
|
361
|
+
data: newData,
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
} catch (error) {
|
|
365
|
+
res({
|
|
366
|
+
success: false,
|
|
367
|
+
data: error
|
|
368
|
+
});
|
|
369
|
+
} finally {
|
|
370
|
+
this.destroy();
|
|
371
|
+
}
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
getDeviceLastestOnlineTime = async ({ productKey, mac }:
|
|
376
|
+
{ productKey: string, mac: string }) => {
|
|
377
|
+
// lastOnlineTime
|
|
378
|
+
let realKey = productKeyMap[productKey];
|
|
379
|
+
if (!realKey) {
|
|
380
|
+
realKey = productKey;
|
|
381
|
+
}
|
|
382
|
+
GizLog.debug('GIZ_SDK: getDeviceLastestOnlineTime', realKey, mac);
|
|
383
|
+
const res = await aepApiRequest<IVerifyOnlineResult>(`app/device/deviceInfo/${realKey}/${(mac ?? '').toLocaleLowerCase()}`, { method: 'post' });
|
|
384
|
+
GizLog.debug('GIZ_SDK: 请求结果', res);
|
|
385
|
+
return res;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// 配网后确认设备是否上线
|
|
389
|
+
verifyDeviceOnline = ({ productKey, mac }: { productKey: string, mac: string }) => {
|
|
390
|
+
return new Promise<IResult<IVerifyOnlineResult>>((res, rej) => {
|
|
391
|
+
GizLog.debug('GIZ_SDK: 开始确认设备上线')
|
|
392
|
+
const query = async () => {
|
|
393
|
+
|
|
394
|
+
if (!this.hasTimeoutHandler()) {
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
try {
|
|
399
|
+
const data = await this.getDeviceLastestOnlineTime({
|
|
400
|
+
productKey, mac
|
|
401
|
+
})
|
|
402
|
+
// if (data.err?.errorCode === errorCode.GIZ_OPENAPI_TOKEN_INVALID.errorCode) {
|
|
403
|
+
// // token 失效
|
|
404
|
+
// rej({
|
|
405
|
+
// success: false,
|
|
406
|
+
// err: errorCode.GIZ_OPENAPI_TOKEN_INVALID,
|
|
407
|
+
// });
|
|
408
|
+
// return;
|
|
409
|
+
// }
|
|
410
|
+
GizLog.debug('GIZ_SDK: 确认到设备是否在线', data.data.data.onlineStatus);
|
|
411
|
+
if (data.success && data.data && data.data.data && data.data.data.onlineStatus == 1) {
|
|
412
|
+
// 确认设备最后一次上线时间有更新
|
|
413
|
+
GizLog.debug('GIZ_SDK: 确认到设备是在线的', data);
|
|
414
|
+
res({
|
|
415
|
+
success: true,
|
|
416
|
+
data: data.data,
|
|
417
|
+
});
|
|
418
|
+
} else {
|
|
419
|
+
// 重新请求
|
|
420
|
+
GizLog.debug('GIZ_SDK: 未确认到设备是在线');
|
|
421
|
+
await sleep(1500);
|
|
422
|
+
!this.disableSearchDevice && query();
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// if (data.err || data.data) {
|
|
426
|
+
// // 重新请求
|
|
427
|
+
// GizLog.debug('GIZ_SDK: 未确认到设备上线时间');
|
|
428
|
+
// await sleep(1500);
|
|
429
|
+
// !this.disableSearchDevice && query();
|
|
430
|
+
// } else {
|
|
431
|
+
// // 搜索到设备
|
|
432
|
+
// GizLog.debug('GIZ_SDK: 大循环搜索到设备', data);
|
|
433
|
+
// res({
|
|
434
|
+
// success: true,
|
|
435
|
+
// data: data.data,
|
|
436
|
+
// });
|
|
437
|
+
// }
|
|
438
|
+
} catch (error) {
|
|
439
|
+
// 重新请求
|
|
440
|
+
await sleep(3000);
|
|
441
|
+
!this.disableSearchDevice && query();
|
|
442
|
+
// GizLog.debug('GIZ_SDK: random codes error', error);
|
|
443
|
+
}
|
|
444
|
+
};
|
|
445
|
+
query();
|
|
446
|
+
});
|
|
447
|
+
};
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* 负责发蓝牙设备指令
|
|
451
|
+
*/
|
|
452
|
+
configBLEDevice = ({
|
|
453
|
+
ssid,
|
|
454
|
+
password,
|
|
455
|
+
softAPSSIDPrefix,
|
|
456
|
+
}: configBLEDeviceParams) => {
|
|
457
|
+
return new Promise<IResult<IConfigResult>>(async (res, rej) => {
|
|
458
|
+
GizLog.debug('GIZ_SDK: start config ble device');
|
|
459
|
+
const enableAndGetRes = await this.enableAndGetBluetoothDevices(
|
|
460
|
+
softAPSSIDPrefix
|
|
461
|
+
).catch((error) => {
|
|
462
|
+
return {
|
|
463
|
+
success: false,
|
|
464
|
+
err: {
|
|
465
|
+
errorCode: errorCode.WECHAT_ERROR.errorCode,
|
|
466
|
+
errorMessage: JSON.stringify(error),
|
|
467
|
+
},
|
|
468
|
+
};
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
if (!isWXDevicesResult(enableAndGetRes)) {
|
|
472
|
+
// 开启获取蓝牙失败
|
|
473
|
+
return rej(enableAndGetRes);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
const { bleDevices } = enableAndGetRes;
|
|
477
|
+
|
|
478
|
+
GizLog.debug('GIZ_SDK: enableAndGetBluetoothDevices success, target devices: ', bleDevices);
|
|
479
|
+
|
|
480
|
+
const handleFoundDevices = async ({
|
|
481
|
+
devices,
|
|
482
|
+
}: {
|
|
483
|
+
devices: WechatMiniprogram.BlueToothDevice[];
|
|
484
|
+
}) => {
|
|
485
|
+
this.hasTimeoutHandler()
|
|
486
|
+
? Array.prototype.push.apply(
|
|
487
|
+
bleDevices,
|
|
488
|
+
devices.filter((d) => this.isValidBleDevice(d, softAPSSIDPrefix))
|
|
489
|
+
)
|
|
490
|
+
: wx.offBluetoothDeviceFound(handleFoundDevices);
|
|
491
|
+
};
|
|
492
|
+
|
|
493
|
+
wx.onBluetoothDeviceFound(handleFoundDevices);
|
|
494
|
+
|
|
495
|
+
const uint8Array = WifiConfig.pack(ssid, password);
|
|
496
|
+
|
|
497
|
+
const bleDevice = bleDevices.shift();
|
|
498
|
+
|
|
499
|
+
const startConfigDevice = async (bleDevice) => {
|
|
500
|
+
GizLog.debug('GIZ_SDK: startConfigDevice');
|
|
501
|
+
if (!this.hasTimeoutHandler()) {
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
if (bleDevice) {
|
|
506
|
+
GizLog.debug('GIZ_SDK: startConfigDevice, target device: ', bleDevice);
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
const success =
|
|
510
|
+
bleDevice &&
|
|
511
|
+
(await sendBLEConfigCmd({
|
|
512
|
+
bleDeviceId: bleDevice.deviceId,
|
|
513
|
+
arrayBuffer: uint8Array.buffer,
|
|
514
|
+
bleHandle: this.bleHandle
|
|
515
|
+
}));
|
|
516
|
+
|
|
517
|
+
if (!success) {
|
|
518
|
+
// 如果校验设备或者发送不成功,重试下一个设备
|
|
519
|
+
await sleep(500);
|
|
520
|
+
await startBluetoothDevicesDiscovery();
|
|
521
|
+
await startConfigDevice(bleDevice);
|
|
522
|
+
return;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
// 如果有一个设备发送成功,则不再发现新设备
|
|
526
|
+
GizLog.debug("GIZ_SDK: offBluetoothDeviceFound ready search device")
|
|
527
|
+
wx.offBluetoothDeviceFound(handleFoundDevices);
|
|
528
|
+
};
|
|
529
|
+
|
|
530
|
+
// 开始大循环搜索
|
|
531
|
+
|
|
532
|
+
// try {
|
|
533
|
+
// // const devicesReturn = await this.searchDevice({ ssid, password });
|
|
534
|
+
// this.searchDevice({ ssid, password }).then(devicesReturn => {
|
|
535
|
+
// res(devicesReturn);
|
|
536
|
+
// })
|
|
537
|
+
// } catch (error) {
|
|
538
|
+
// rej(error);
|
|
539
|
+
// GizLog.error("GIZ_SDK: searchDevice error", new Error(JSON.stringify(error)))
|
|
540
|
+
// }
|
|
541
|
+
const { productKey, mac } = advertisData2PkAndMac(bleDevice.advertisData, this.specialProductKeys)
|
|
542
|
+
// const result = await this.getDeviceLastestOnlineTime({ productKey, mac });
|
|
543
|
+
// GizLog.log("GIZ_SDK: 初始设备上线时间 ", result);
|
|
544
|
+
// if (result.success && result.data && result.data.data) {
|
|
545
|
+
// this.lastestOnlineTimeMap[`${productKey}${mac}`] = result.data.data.lastOnlineTime;
|
|
546
|
+
// GizLog.log("GIZ_SDK: 初始设备上线时间 lastestOnlineTimeMap ", this.lastestOnlineTimeMap);
|
|
547
|
+
// }
|
|
548
|
+
await startConfigDevice(bleDevice);
|
|
549
|
+
try {
|
|
550
|
+
// const devicesReturn = await this.searchDevice({ ssid, password });
|
|
551
|
+
this.verifyDeviceOnline({ productKey, mac }).then(devicesReturn => {
|
|
552
|
+
res({ ...devicesReturn, data: { ...devicesReturn.data.data, productKey } });
|
|
553
|
+
});
|
|
554
|
+
} catch (error) {
|
|
555
|
+
rej(error);
|
|
556
|
+
GizLog.error("GIZ_SDK: searchDevice error", new Error(JSON.stringify(error)))
|
|
557
|
+
}
|
|
558
|
+
});
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
export default BLEAliCloudConfig;
|