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/utils.ts
DELETED
|
@@ -1,449 +0,0 @@
|
|
|
1
|
-
import { lib } from "crypto-js/core";
|
|
2
|
-
|
|
3
|
-
import GizLog from './GizLog';
|
|
4
|
-
import { IDevice, IError, IWXDevicesResult, TConnectType, IProductInfo, GizwitsWxBlueToothDevice } from "./types";
|
|
5
|
-
|
|
6
|
-
export function ab2hex(buffer: ArrayBuffer) {
|
|
7
|
-
let hexArr = Array.prototype.map.call(new Uint8Array(buffer), function (bit) {
|
|
8
|
-
return ('00' + bit.toString(16)).slice(-2);
|
|
9
|
-
});
|
|
10
|
-
return hexArr.join('');
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function compareWXSDKVersion(v1: string, v2: string) {
|
|
14
|
-
const v1Arr = v1.split('.');
|
|
15
|
-
const v2Arr = v2.split('.');
|
|
16
|
-
const len = Math.max(v1.length, v2.length);
|
|
17
|
-
|
|
18
|
-
while (v1Arr.length < len) {
|
|
19
|
-
v1Arr.push('0');
|
|
20
|
-
}
|
|
21
|
-
while (v2Arr.length < len) {
|
|
22
|
-
v2Arr.push('0');
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
for (let i = 0; i < len; i++) {
|
|
26
|
-
const num1 = parseInt(v1[i]);
|
|
27
|
-
const num2 = parseInt(v2[i]);
|
|
28
|
-
|
|
29
|
-
if (num1 > num2) {
|
|
30
|
-
return 1;
|
|
31
|
-
} else if (num1 < num2) {
|
|
32
|
-
return -1;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
return 0;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export const isError = (err: unknown): err is IError => {
|
|
39
|
-
return err != null && typeof (err as IError).errorCode === 'symbol';
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
export const wrapErrorInfo = (errorMessage: string) => ({
|
|
43
|
-
errorCode: errorMessage,
|
|
44
|
-
errorMessage,
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
export const unionBy = <T>(array: T[], key: string) => {
|
|
48
|
-
return array.reduce<T[]>((acc, item) => {
|
|
49
|
-
const keyValue = item[key];
|
|
50
|
-
if (!acc.some((i) => i[key] === keyValue)) {
|
|
51
|
-
acc.push(item);
|
|
52
|
-
}
|
|
53
|
-
return acc;
|
|
54
|
-
}, []);
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
export function isWXDevicesResult(res: unknown): res is IWXDevicesResult {
|
|
58
|
-
return (
|
|
59
|
-
(res as IWXDevicesResult).success &&
|
|
60
|
-
Object.prototype.toString.call((res as IWXDevicesResult).bleDevices) ===
|
|
61
|
-
'[object Array]'
|
|
62
|
-
);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// hex转ArrayBuffer
|
|
66
|
-
export function str2Buf(arr: string) {
|
|
67
|
-
var length = arr.length;
|
|
68
|
-
var buffer = new ArrayBuffer(length + 2);
|
|
69
|
-
var dataview = new DataView(buffer);
|
|
70
|
-
for (let i = 0; i < length; i++) {
|
|
71
|
-
dataview.setUint8(i, parseInt(arr[i], 16));
|
|
72
|
-
}
|
|
73
|
-
return buffer;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// 把协议导出的数据转换为可以发送给蓝牙的数据
|
|
77
|
-
export const numberArray2Uint8Array = (numArr: number[]) => {
|
|
78
|
-
const buffer = new ArrayBuffer(numArr.length);
|
|
79
|
-
const uint8Array = new Uint8Array(buffer);
|
|
80
|
-
for (let i = 0; i < buffer.byteLength; i++) {
|
|
81
|
-
uint8Array[i] = numArr[i];
|
|
82
|
-
}
|
|
83
|
-
return uint8Array;
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
const oldParseAdv = (advertisData: ArrayBuffer, pks: string[]) => {
|
|
87
|
-
let pkUintArray = advertisData.slice(advertisData.byteLength - 16);
|
|
88
|
-
let macUintArray = advertisData.slice(0, advertisData.byteLength - 16);
|
|
89
|
-
let productKey = ab2hex(pkUintArray);
|
|
90
|
-
|
|
91
|
-
if (pks.find((item) => item === productKey)) {
|
|
92
|
-
// 存在
|
|
93
|
-
const mac = ab2hex(macUintArray);
|
|
94
|
-
return { productKey, mac };
|
|
95
|
-
} else {
|
|
96
|
-
// 由于小程序安卓只接收ff type的广播数据,会乱序,所以这里需要做一次判断
|
|
97
|
-
pkUintArray = advertisData.slice(0, 16);
|
|
98
|
-
macUintArray = advertisData.slice(16);
|
|
99
|
-
productKey = ab2hex(pkUintArray);
|
|
100
|
-
const mac = ab2hex(macUintArray);
|
|
101
|
-
return { productKey, mac };
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
const parseAdv = (advertisData: ArrayBuffer, pks: string[]) => {
|
|
105
|
-
|
|
106
|
-
if (!advertisData || advertisData.byteLength === 0) {
|
|
107
|
-
return {
|
|
108
|
-
bleWorkStatus: 0,
|
|
109
|
-
mac: '',
|
|
110
|
-
productKey: ''
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
let index = 0;
|
|
115
|
-
let mac = '';
|
|
116
|
-
let productKey = '';
|
|
117
|
-
let bleWorkStatus = 1;
|
|
118
|
-
|
|
119
|
-
const u8Array = new Uint8Array(advertisData);
|
|
120
|
-
|
|
121
|
-
while (true) {
|
|
122
|
-
if (index > advertisData.byteLength - 1) {
|
|
123
|
-
break;
|
|
124
|
-
}
|
|
125
|
-
const len = u8Array[index];
|
|
126
|
-
if (len <= 0 || !len) {
|
|
127
|
-
break;
|
|
128
|
-
}
|
|
129
|
-
if (advertisData.byteLength < len + index + 1) {
|
|
130
|
-
break;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const tmpBytes = advertisData.slice(index + 1, index + len + 1);
|
|
134
|
-
if (len >= 6 && len <= 8) {
|
|
135
|
-
mac = ab2hex(tmpBytes);
|
|
136
|
-
if (mac.length == 16) {
|
|
137
|
-
mac = mac.substring(0, 15);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
if (len == 16) {
|
|
142
|
-
const pk = ab2hex(tmpBytes).toLowerCase();
|
|
143
|
-
if (pks.find((item) => item === pk)) {
|
|
144
|
-
productKey = pk;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
if (len == 1) {
|
|
148
|
-
bleWorkStatus = new Uint8Array(tmpBytes)[0];
|
|
149
|
-
}
|
|
150
|
-
index += 1 + len;
|
|
151
|
-
}
|
|
152
|
-
return { bleWorkStatus, mac, productKey }
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
export const advertisData2PkAndMac = (
|
|
157
|
-
advertisData: ArrayBuffer,
|
|
158
|
-
pks: string[]
|
|
159
|
-
) => {
|
|
160
|
-
|
|
161
|
-
if (advertisData && advertisData.byteLength >= 22 && advertisData.byteLength <= 24) {
|
|
162
|
-
return { ...oldParseAdv(advertisData, pks), bleWorkStatus: 1 };
|
|
163
|
-
}
|
|
164
|
-
if (advertisData && advertisData.byteLength > 24) {
|
|
165
|
-
return parseAdv(advertisData, pks);
|
|
166
|
-
}
|
|
167
|
-
return { mac: '', productKey: '', bleWorkStatus: 1 }
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
export const isSameDevice = (deviceA: IDevice, deviceB: IDevice) => {
|
|
171
|
-
return (
|
|
172
|
-
deviceA.mac === deviceB.mac && deviceA.productKey === deviceB.productKey
|
|
173
|
-
);
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
export const getDeviceId = (deivce: IDevice) => {
|
|
177
|
-
return `${deivce.mac}${deivce.productKey}`;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
export const merageBleLocalDevices = (
|
|
181
|
-
mainDevice: IDevice[],
|
|
182
|
-
localDevice: IDevice[]
|
|
183
|
-
) => {
|
|
184
|
-
mainDevice.map(device => {
|
|
185
|
-
// 重置成离线
|
|
186
|
-
device.isBleOnline = false;
|
|
187
|
-
device.bleWorkStatus = 0;
|
|
188
|
-
})
|
|
189
|
-
localDevice.map((localD) => {
|
|
190
|
-
const target = mainDevice.find((device) => isSameDevice(device, localD));
|
|
191
|
-
if (!target) {
|
|
192
|
-
// 没有则插入这条本地设备
|
|
193
|
-
mainDevice.push(localD);
|
|
194
|
-
} else {
|
|
195
|
-
target.isBleOnline = true;
|
|
196
|
-
target.bleDeviceID = localD.bleDeviceID;
|
|
197
|
-
// 蓝牙设备比较特殊,有广播类型 广播类型是写在本地设备属性里面的,但是又不属于已经连接的设备,先骚处理一下
|
|
198
|
-
target.connectType = localD.connectType;
|
|
199
|
-
target.bleWorkStatus = localD.bleWorkStatus;
|
|
200
|
-
}
|
|
201
|
-
});
|
|
202
|
-
return mainDevice;
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
export const merageLanLocalDevices = (
|
|
206
|
-
mainDevice: IDevice[],
|
|
207
|
-
localDevice: IDevice[]
|
|
208
|
-
) => {
|
|
209
|
-
mainDevice.map(device => {
|
|
210
|
-
// 重置成离线
|
|
211
|
-
device.isLanOnline = false;
|
|
212
|
-
})
|
|
213
|
-
localDevice.map((localD) => {
|
|
214
|
-
const target = mainDevice.find((device) => isSameDevice(device, localD));
|
|
215
|
-
if (!target) {
|
|
216
|
-
// 没有则插入这条本地设备
|
|
217
|
-
mainDevice.push(localD);
|
|
218
|
-
} else {
|
|
219
|
-
target.isLanOnline = true;
|
|
220
|
-
}
|
|
221
|
-
});
|
|
222
|
-
return mainDevice;
|
|
223
|
-
};
|
|
224
|
-
|
|
225
|
-
type TAdvData = {
|
|
226
|
-
productKey: string;
|
|
227
|
-
mac: string;
|
|
228
|
-
bleWorkStatus: number;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
export const BTDevice2GDevice = (
|
|
232
|
-
BTDevice: GizwitsWxBlueToothDevice,
|
|
233
|
-
pks: string[]
|
|
234
|
-
) => {
|
|
235
|
-
|
|
236
|
-
let advData: TAdvData = {
|
|
237
|
-
productKey: '',
|
|
238
|
-
mac: '',
|
|
239
|
-
bleWorkStatus: 0,
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
let device: IDevice = {
|
|
243
|
-
did: '',
|
|
244
|
-
productKey: '',
|
|
245
|
-
mac: '',
|
|
246
|
-
bleWorkStatus: 0,
|
|
247
|
-
connectType: 'NONE',
|
|
248
|
-
isBleOnline: true,
|
|
249
|
-
isLanOnline: false,
|
|
250
|
-
isOnline: false,
|
|
251
|
-
name: BTDevice.localName,
|
|
252
|
-
localName: BTDevice.localName,
|
|
253
|
-
isBind: false,
|
|
254
|
-
bleDeviceID: BTDevice.deviceId,
|
|
255
|
-
ctime: new Date().getTime(),
|
|
256
|
-
};
|
|
257
|
-
|
|
258
|
-
switch (BTDevice.communicationType) {
|
|
259
|
-
case 'BLE_BROADCAST': {
|
|
260
|
-
try {
|
|
261
|
-
GizLog.log('parse BLE_BROADCAST');
|
|
262
|
-
const broadcastCmdData = parseBroadcastData(ab2numbers(BTDevice.advertisData));
|
|
263
|
-
advData.productKey = broadcastCmdData.data.join('');
|
|
264
|
-
advData.mac = broadcastCmdData.mac;
|
|
265
|
-
device.connectType = 'BLE_BROADCAST';
|
|
266
|
-
} catch (error) {
|
|
267
|
-
}
|
|
268
|
-
break;
|
|
269
|
-
}
|
|
270
|
-
default: {
|
|
271
|
-
advData = advertisData2PkAndMac(BTDevice.advertisData, pks);
|
|
272
|
-
break;
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
device = {
|
|
277
|
-
...device,
|
|
278
|
-
...advData,
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
return device;
|
|
282
|
-
};
|
|
283
|
-
|
|
284
|
-
// 如果都不存在 就去连接大循环
|
|
285
|
-
export const getFirstConnectType = (
|
|
286
|
-
device: IDevice,
|
|
287
|
-
type?: TConnectType
|
|
288
|
-
): TConnectType => {
|
|
289
|
-
if (!type) {
|
|
290
|
-
if (device.isLanOnline) return 'LAN';
|
|
291
|
-
if (device.isBleOnline) return 'BLE';
|
|
292
|
-
return 'WAN';
|
|
293
|
-
} else {
|
|
294
|
-
if (type === 'WAN') return 'WAN';
|
|
295
|
-
|
|
296
|
-
switch (type) {
|
|
297
|
-
case 'BLE': {
|
|
298
|
-
return device.isBleOnline ? 'BLE' : 'NONE';
|
|
299
|
-
}
|
|
300
|
-
case 'LAN': {
|
|
301
|
-
return device.isLanOnline ? 'LAN' : 'NONE';
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
return 'WAN';
|
|
307
|
-
};
|
|
308
|
-
|
|
309
|
-
export const getProductInfoThroughPK = (
|
|
310
|
-
pk: string,
|
|
311
|
-
productInfos: IProductInfo[]
|
|
312
|
-
) => {
|
|
313
|
-
return productInfos.find((item) => item.productKey === pk);
|
|
314
|
-
};
|
|
315
|
-
|
|
316
|
-
export const mergeObject = (obj1: any, obj2: any) => {
|
|
317
|
-
for (let key in obj2) {
|
|
318
|
-
if (obj2[key] == null || obj2[key] === '') continue;
|
|
319
|
-
obj1[key] = obj2[key];
|
|
320
|
-
}
|
|
321
|
-
return obj1;
|
|
322
|
-
};
|
|
323
|
-
export function hex2ab(str) {
|
|
324
|
-
const res = [];
|
|
325
|
-
for (let i = 0; i < str.length; i += 2) {
|
|
326
|
-
var hex = str.slice(i, i + 2);
|
|
327
|
-
res.push(parseInt(hex, 16));
|
|
328
|
-
}
|
|
329
|
-
return new Uint8Array(res);
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
// 0x94 ack 和上报
|
|
333
|
-
// 0x19 设备信息广播
|
|
334
|
-
type BroadcastCmd = 0x19 | 0x94;
|
|
335
|
-
type TBoradcastRes = {
|
|
336
|
-
mac: string;
|
|
337
|
-
cmd: BroadcastCmd;
|
|
338
|
-
data: string[];
|
|
339
|
-
}
|
|
340
|
-
export function parseBroadcastData(data: number[]) {
|
|
341
|
-
data = data.slice(0, 25)
|
|
342
|
-
if (data.length != 25) {
|
|
343
|
-
throw new Error('数据不合法');
|
|
344
|
-
}
|
|
345
|
-
const mac = `${number2hex(data[0])}${number2hex(data[1])}`;
|
|
346
|
-
const sum = data[2];
|
|
347
|
-
if (sum !== (data.slice(3).reduce((a, b) => a + b, 0) & 255)) {
|
|
348
|
-
throw new Error('校验和不合法');
|
|
349
|
-
}
|
|
350
|
-
const cmd = number2hex(data[3]);
|
|
351
|
-
const cmdData = (data.slice(5, 5 + data[4])).map(item => number2hex(item))
|
|
352
|
-
|
|
353
|
-
return {
|
|
354
|
-
mac,
|
|
355
|
-
cmd: parseInt(cmd, 16) as BroadcastCmd,
|
|
356
|
-
data: cmdData,
|
|
357
|
-
} as TBoradcastRes;
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
export function ab2numbers(buffer: ArrayBuffer) {
|
|
361
|
-
return Array.prototype.map.call(
|
|
362
|
-
new Uint8Array(buffer),
|
|
363
|
-
function (bit) {
|
|
364
|
-
return bit;
|
|
365
|
-
}
|
|
366
|
-
)
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
function number2hex(num: number, digits = 2) {
|
|
370
|
-
return `${(new Array(digits)).fill(0).join('')}${num.toString(16)}`.slice(-digits)
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
let BASE_SN = 0;
|
|
374
|
-
const getSn = () => {
|
|
375
|
-
const cur = BASE_SN;
|
|
376
|
-
BASE_SN = (BASE_SN + 1) & 255;
|
|
377
|
-
return cur;
|
|
378
|
-
};
|
|
379
|
-
|
|
380
|
-
export function encrypt(uuids: number[], key: number) {
|
|
381
|
-
let offset = 1;
|
|
382
|
-
let nextKey = key;
|
|
383
|
-
return uuids.map((uuid) => {
|
|
384
|
-
const res = (uuid ^ nextKey) & 0xffff;
|
|
385
|
-
nextKey = (offset - res) & 0xffff;
|
|
386
|
-
offset = offset << 1;
|
|
387
|
-
return number2hex(res, 4);
|
|
388
|
-
})
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
// 加密方式生成uuid广播数据
|
|
392
|
-
export const convertToServiceUuids = ({
|
|
393
|
-
len = null, data = null, index = 0, mac
|
|
394
|
-
}) => {
|
|
395
|
-
const length = len == null ? Math.round(Math.random() * 18) + 1 : len;
|
|
396
|
-
const newData = data == null ? Array.from({ length }, (_) => {
|
|
397
|
-
return Math.round(Math.random() * 255);
|
|
398
|
-
}) : data;
|
|
399
|
-
const sourceData = [0x93, length, ...newData];
|
|
400
|
-
const serviceUuids = convertToUuids(mac, getSn(), sourceData);
|
|
401
|
-
if ([...(new Set(serviceUuids))].length !== serviceUuids.length) {
|
|
402
|
-
return convertToServiceUuids({
|
|
403
|
-
len: length, data: newData, index: index++, mac
|
|
404
|
-
});
|
|
405
|
-
}
|
|
406
|
-
return serviceUuids;
|
|
407
|
-
};
|
|
408
|
-
|
|
409
|
-
export function convertToUuids(mac: number, sn: number, data: number[]) {
|
|
410
|
-
if (data.length > 22) {
|
|
411
|
-
throw new Error('最大长度为22');
|
|
412
|
-
}
|
|
413
|
-
const fullData = [...data, ...new Array(22 - data.length).fill(0)];
|
|
414
|
-
const sum = data.reduce((a, b) => a + b, 0) & 255;
|
|
415
|
-
const secret = ((sn >> 4) << 12) + ((sum >> 4) << 8) + ((sn & 15) << 4) + (sum & 15);
|
|
416
|
-
const srcUuids = (Array.from({ length: 11 }, (_, i) => {
|
|
417
|
-
return (fullData[i * 2] << 8) + fullData[i * 2 + 1];
|
|
418
|
-
}));
|
|
419
|
-
GizLog.log(
|
|
420
|
-
'明文',
|
|
421
|
-
number2hex(mac, 4),
|
|
422
|
-
number2hex(((sn << 8) + sum), 4),
|
|
423
|
-
srcUuids.map(uuid => number2hex(uuid, 4))
|
|
424
|
-
)
|
|
425
|
-
|
|
426
|
-
const finalData = [
|
|
427
|
-
number2hex(mac, 4),
|
|
428
|
-
number2hex(((sn << 8) + sum), 4),
|
|
429
|
-
...encrypt(srcUuids, secret)
|
|
430
|
-
];
|
|
431
|
-
// if (wx.getSystemInfoSync().platform === 'ios') {
|
|
432
|
-
// return finalData.map((uuid) => `${uuid.slice(2)}${uuid.slice(0, 2)}`);
|
|
433
|
-
// }
|
|
434
|
-
return finalData.map((uuid) => `${uuid.slice(2)}${uuid.slice(0, 2)}`);
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
// 定义一个函数,用于将 ArrayBuffer 转换为 WordArray
|
|
438
|
-
export function arrayBufferToWordArray(arrayBuffer) {
|
|
439
|
-
var words = [];
|
|
440
|
-
var uint8View = new Uint8Array(arrayBuffer);
|
|
441
|
-
for (var i = 0; i < uint8View.length; i += 4) {
|
|
442
|
-
var word = (uint8View[i] << 24) |
|
|
443
|
-
(uint8View[i + 1] << 16) |
|
|
444
|
-
(uint8View[i + 2] << 8) |
|
|
445
|
-
uint8View[i + 3];
|
|
446
|
-
words.push(word);
|
|
447
|
-
}
|
|
448
|
-
return lib.WordArray.create(words, uint8View.length);
|
|
449
|
-
}
|
package/src/wechatApi.ts
DELETED
|
@@ -1,211 +0,0 @@
|
|
|
1
|
-
import GizLog from "./GizLog";
|
|
2
|
-
import sleep from "./sleep";
|
|
3
|
-
import { IBluetoothAdapterStateResult, IWechatResult, IBLEService, IBLECharacteristic } from "./types";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* 重连
|
|
8
|
-
*/
|
|
9
|
-
export async function retryConnect(deviceId: string, timeout: number = 3 * 1000) {
|
|
10
|
-
let res = {
|
|
11
|
-
success: false,
|
|
12
|
-
}
|
|
13
|
-
for (let i = 0; i < 5; i++) {
|
|
14
|
-
try {
|
|
15
|
-
console.debug('start createBLEConnection')
|
|
16
|
-
return (await createBLEConnection(
|
|
17
|
-
deviceId,
|
|
18
|
-
timeout));
|
|
19
|
-
} catch (error) {
|
|
20
|
-
res = error;
|
|
21
|
-
console.debug('createBLEConnection error retry')
|
|
22
|
-
await sleep(1000);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
return res;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export function openBluetoothAdapter() {
|
|
29
|
-
return new Promise<IWechatResult>((res, rej) => {
|
|
30
|
-
wx.openBluetoothAdapter({
|
|
31
|
-
success: res,
|
|
32
|
-
fail: rej,
|
|
33
|
-
})
|
|
34
|
-
})
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export function closeBluetoothAdapter() {
|
|
38
|
-
return new Promise<IWechatResult>((res, rej) => {
|
|
39
|
-
wx.closeBluetoothAdapter({
|
|
40
|
-
success: res,
|
|
41
|
-
fail: rej,
|
|
42
|
-
})
|
|
43
|
-
})
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export function getBluetoothAdapterState() {
|
|
47
|
-
return new Promise<IBluetoothAdapterStateResult>((res, rej) => {
|
|
48
|
-
wx.getBluetoothAdapterState({
|
|
49
|
-
success: res,
|
|
50
|
-
fail: rej,
|
|
51
|
-
})
|
|
52
|
-
})
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export function startBluetoothDevicesDiscovery(allowDuplicatesKey = true) {
|
|
56
|
-
return new Promise<IWechatResult>((res, rej) => {
|
|
57
|
-
wx.startBluetoothDevicesDiscovery({
|
|
58
|
-
success: res,
|
|
59
|
-
fail: rej,
|
|
60
|
-
allowDuplicatesKey,
|
|
61
|
-
powerLevel: 'high',
|
|
62
|
-
interval: 200
|
|
63
|
-
})
|
|
64
|
-
})
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export function getBluetoothDevices() {
|
|
68
|
-
return new Promise<WechatMiniprogram.BlueToothDevice[]>((res, rej) => {
|
|
69
|
-
wx.getBluetoothDevices({
|
|
70
|
-
success: (({ devices }) => res(devices)),
|
|
71
|
-
fail: rej,
|
|
72
|
-
})
|
|
73
|
-
})
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export function createBLEConnection(deviceId: string, timeout: number) {
|
|
77
|
-
return new Promise<IWechatResult>((res, rej) => {
|
|
78
|
-
wx.createBLEConnection({
|
|
79
|
-
deviceId,
|
|
80
|
-
timeout,
|
|
81
|
-
success: (r => res(r as IWechatResult)),
|
|
82
|
-
fail: ((err) => {
|
|
83
|
-
if (err.errCode === -1) {
|
|
84
|
-
res(err);
|
|
85
|
-
} else {
|
|
86
|
-
rej(err);
|
|
87
|
-
}
|
|
88
|
-
}),
|
|
89
|
-
})
|
|
90
|
-
})
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
export function getBLEDeviceServices(deviceId: string) {
|
|
95
|
-
return new Promise<IBLEService[]>((res, rej) => {
|
|
96
|
-
wx.getBLEDeviceServices({
|
|
97
|
-
deviceId,
|
|
98
|
-
success: (({ services }) => res(services)),
|
|
99
|
-
fail: rej,
|
|
100
|
-
})
|
|
101
|
-
})
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
export function getBLEDeviceCharacteristics(deviceId: string, serviceId: string) {
|
|
106
|
-
return new Promise<IBLECharacteristic[]>((res, rej) => {
|
|
107
|
-
wx.getBLEDeviceCharacteristics({
|
|
108
|
-
deviceId,
|
|
109
|
-
serviceId,
|
|
110
|
-
success: (({ characteristics }) => res(characteristics)),
|
|
111
|
-
fail: rej,
|
|
112
|
-
})
|
|
113
|
-
})
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export function notifyBLECharacteristicValueChange(
|
|
117
|
-
deviceId: string,
|
|
118
|
-
serviceId: string,
|
|
119
|
-
characteristicId: string,
|
|
120
|
-
state: boolean = true
|
|
121
|
-
) {
|
|
122
|
-
return new Promise<IWechatResult>((res, rej) => {
|
|
123
|
-
wx.notifyBLECharacteristicValueChange({
|
|
124
|
-
deviceId,
|
|
125
|
-
serviceId,
|
|
126
|
-
characteristicId,
|
|
127
|
-
state,
|
|
128
|
-
success: res,
|
|
129
|
-
fail: rej,
|
|
130
|
-
})
|
|
131
|
-
})
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
export function writeBLECharacteristicValue(
|
|
135
|
-
deviceId: string,
|
|
136
|
-
serviceId: string,
|
|
137
|
-
characteristicId: string,
|
|
138
|
-
value: ArrayBuffer,
|
|
139
|
-
) {
|
|
140
|
-
return new Promise<IWechatResult>((res) => {
|
|
141
|
-
wx.writeBLECharacteristicValue({
|
|
142
|
-
deviceId,
|
|
143
|
-
serviceId,
|
|
144
|
-
characteristicId,
|
|
145
|
-
value,
|
|
146
|
-
complete: res,
|
|
147
|
-
})
|
|
148
|
-
})
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
export function unpackWriteBLECharacteristicValue(
|
|
152
|
-
deviceId: string,
|
|
153
|
-
serviceId: string,
|
|
154
|
-
characteristicId: string,
|
|
155
|
-
value: ArrayBuffer,
|
|
156
|
-
mtu = 20
|
|
157
|
-
) {
|
|
158
|
-
if (!value || value.byteLength === 0) {
|
|
159
|
-
throw new Error('value is empty');
|
|
160
|
-
}
|
|
161
|
-
return new Promise<IWechatResult>(async (res, rej) => {
|
|
162
|
-
let pos = 0;
|
|
163
|
-
let bytes = value.byteLength;
|
|
164
|
-
let writeRes;
|
|
165
|
-
while (bytes > 0) {
|
|
166
|
-
await sleep(10);
|
|
167
|
-
const tmpBuffer = value.slice(pos, pos + mtu);
|
|
168
|
-
pos += mtu;
|
|
169
|
-
bytes -= mtu;
|
|
170
|
-
writeRes = await writeBLECharacteristicValue(
|
|
171
|
-
deviceId,
|
|
172
|
-
serviceId,
|
|
173
|
-
characteristicId,
|
|
174
|
-
tmpBuffer,
|
|
175
|
-
);
|
|
176
|
-
// GizLog.log('unpackWriteBLECharacteristicValue', pos / mtu, writeRes);
|
|
177
|
-
if (writeRes.errCode !== 0) {
|
|
178
|
-
break;
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
writeRes.errCode !== 0 ? rej(writeRes) : res(writeRes);
|
|
182
|
-
})
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
export async function downloadFile({url}) {
|
|
186
|
-
return new Promise((resFunc, rejFunc) => {
|
|
187
|
-
wx.downloadFile({
|
|
188
|
-
url: url,
|
|
189
|
-
success (res) {
|
|
190
|
-
resFunc(res);
|
|
191
|
-
},
|
|
192
|
-
fail (err) {
|
|
193
|
-
rejFunc(err)
|
|
194
|
-
}
|
|
195
|
-
})
|
|
196
|
-
})
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
export async function setMtu(mtu:number, deviceId: string) {
|
|
200
|
-
return new Promise(res => {
|
|
201
|
-
wx.setBLEMTU({
|
|
202
|
-
mtu, deviceId,
|
|
203
|
-
success: (data) => {
|
|
204
|
-
res(data.mtu)
|
|
205
|
-
},
|
|
206
|
-
fail: () => {
|
|
207
|
-
res(20)
|
|
208
|
-
},
|
|
209
|
-
})
|
|
210
|
-
})
|
|
211
|
-
}
|