mini_program_gizwits_sdk 3.0.1 → 3.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +3 -3
- package/dist/src/errorCode.d.ts +5 -0
- package/dist/src/globalData.d.ts +2 -0
- package/dist/src/openApiRequest.d.ts +9 -0
- package/dist/src/productConfigFileManage.d.ts +12 -0
- package/dist/src/protocol/Bind.d.ts +7 -0
- package/dist/src/protocol/DataPoint.d.ts +52 -0
- package/dist/src/protocol/GetDeviceStatus.d.ts +5 -0
- package/dist/src/protocol/Login.d.ts +12 -0
- package/dist/src/protocol/ProtocolBase.d.ts +11 -0
- package/dist/src/protocol/WifiConfig.d.ts +6 -0
- package/dist/src/protocol/tool.d.ts +9 -0
- package/dist/src/randomCode.d.ts +7 -0
- package/dist/src/request.d.ts +1 -0
- package/dist/src/services/devices.d.ts +53 -0
- package/dist/src/services/login.d.ts +8 -0
- package/dist/src/services/tool.d.ts +4 -0
- package/dist/src/sleep.d.ts +2 -0
- package/dist/src/socket.d.ts +107 -0
- package/dist/src/utils.d.ts +29 -0
- package/dist/src/wechatApi.d.ts +33 -0
- package/dist/src/wifiConfig/ConfigBase.d.ts +26 -0
- package/dist/src/wifiConfig/ap.d.ts +21 -0
- package/dist/src/wifiConfig/ble.d.ts +40 -0
- package/global.d.ts +28 -0
- package/index.ts +7 -0
- package/package.json +6 -3
- package/src/ble.ts +566 -0
- package/src/errorCode.ts +60 -0
- package/src/global.d.ts +42 -0
- package/src/globalData.ts +9 -0
- package/src/openApiRequest.ts +103 -0
- package/src/productConfigFileManage.ts +44 -0
- package/src/protocol/Bind.ts +22 -0
- package/src/protocol/GetDeviceStatus.ts +61 -0
- package/src/protocol/Login.ts +43 -0
- package/src/protocol/ProtocolBase.ts +53 -0
- package/src/protocol/WifiConfig.ts +41 -0
- package/src/protocol/dataPoint.ts +663 -0
- package/src/protocol/tool.ts +91 -0
- package/src/randomCode.ts +36 -0
- package/src/request.ts +22 -0
- package/src/sdk.ts +811 -0
- package/src/services/devices.ts +210 -0
- package/src/services/login.ts +15 -0
- package/src/services/tool.ts +10 -0
- package/src/sleep.ts +2 -0
- package/src/socket.ts +449 -0
- package/src/utils.ts +215 -0
- package/src/wechatApi.ts +179 -0
- package/src/wifiConfig/ConfigBase.ts +183 -0
- package/src/wifiConfig/ap.ts +193 -0
- package/src/wifiConfig/ble.ts +417 -0
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import errorCode from "../errorCode";
|
|
2
|
+
import WifiConfig from "../protocol/WifiConfig";
|
|
3
|
+
import { IRandomCodesResult, IResult, IntervalHandle } from "../sdk";
|
|
4
|
+
import ConfigBase from "./ConfigBase";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
interface configDeviceParams {
|
|
8
|
+
ssid: string;
|
|
9
|
+
password: string;
|
|
10
|
+
softAPSSIDPrefix: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface ISetDeviceOnboardingDeployProps {
|
|
14
|
+
timeout: number;
|
|
15
|
+
isBind: boolean;
|
|
16
|
+
softAPSSIDPrefix: string;
|
|
17
|
+
}
|
|
18
|
+
class ApConfig extends ConfigBase {
|
|
19
|
+
|
|
20
|
+
disableSendUDP = false;
|
|
21
|
+
private sendMessageInterval: IntervalHandle = null;
|
|
22
|
+
private UDPSocketHandler: WechatMiniprogram.UDPSocket | null = null;
|
|
23
|
+
|
|
24
|
+
destroy = () => {
|
|
25
|
+
this.cleanTimeout();
|
|
26
|
+
this.sendMessageInterval && clearInterval(this.sendMessageInterval);
|
|
27
|
+
this.sendMessageInterval = null;
|
|
28
|
+
|
|
29
|
+
if (this.UDPSocketHandler) {
|
|
30
|
+
this.UDPSocketHandler.offError();
|
|
31
|
+
this.UDPSocketHandler.offMessage();
|
|
32
|
+
this.UDPSocketHandler.close();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* 负责发指令
|
|
38
|
+
*/
|
|
39
|
+
configDevice = ({ ssid, password, softAPSSIDPrefix }: configDeviceParams) => {
|
|
40
|
+
return new Promise<IResult<IRandomCodesResult[]>>((res, rej) => {
|
|
41
|
+
console.debug('GIZ_SDK: start config device');
|
|
42
|
+
let searchingDevice = false;
|
|
43
|
+
let UDP_ERROR_NUM = 0;
|
|
44
|
+
const uint8Array = WifiConfig.pack(ssid,password);
|
|
45
|
+
/**
|
|
46
|
+
* 连接socket 发送
|
|
47
|
+
*/
|
|
48
|
+
this.UDPSocketHandler = wx.createUDPSocket();
|
|
49
|
+
this.UDPSocketHandler.bind();
|
|
50
|
+
|
|
51
|
+
this.disableSendUDP = false;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* TODO
|
|
55
|
+
* 收到设备回的包后终止
|
|
56
|
+
* 或者超时
|
|
57
|
+
* 或者成功
|
|
58
|
+
*/
|
|
59
|
+
const sendMessage = () => {
|
|
60
|
+
try {
|
|
61
|
+
!this.disableSendUDP &&
|
|
62
|
+
this.UDPSocketHandler!.send({
|
|
63
|
+
address: '10.10.100.254',
|
|
64
|
+
port: 12414,
|
|
65
|
+
message: uint8Array,
|
|
66
|
+
offset: 0,
|
|
67
|
+
length: uint8Array.byteLength,
|
|
68
|
+
});
|
|
69
|
+
} catch (error) {
|
|
70
|
+
// console.log('sendMessage', error);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
// 执行
|
|
75
|
+
this.sendMessageInterval = setInterval(() => {
|
|
76
|
+
// console.debug('发送配置包')
|
|
77
|
+
sendMessage();
|
|
78
|
+
}, 2000);
|
|
79
|
+
|
|
80
|
+
this.UDPSocketHandler.onError((data: any) => {
|
|
81
|
+
UDP_ERROR_NUM += 1;
|
|
82
|
+
// UDPSocketHandler.close();
|
|
83
|
+
// 重试三次不行还是失败
|
|
84
|
+
if (UDP_ERROR_NUM > 2) {
|
|
85
|
+
this.destroy();
|
|
86
|
+
rej({
|
|
87
|
+
success: false,
|
|
88
|
+
err: {
|
|
89
|
+
errorCode: errorCode.WECHAT_ERROR.errorCode,
|
|
90
|
+
errorMessage: data.errMsg,
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
// 清理一些监听,调用搜索设备
|
|
97
|
+
const searchDeviceHandle = async () => {
|
|
98
|
+
if (searchingDevice) return;
|
|
99
|
+
this.UDPSocketHandler!.offMessage();
|
|
100
|
+
this.UDPSocketHandler!.offError();
|
|
101
|
+
this.sendMessageInterval && clearInterval(this.sendMessageInterval);
|
|
102
|
+
this.disableSendUDP = true;
|
|
103
|
+
// 标记可以停止监听
|
|
104
|
+
searchingDevice = true;
|
|
105
|
+
// 关闭socket
|
|
106
|
+
try {
|
|
107
|
+
const devicesReturn = await this.searchDevice({ ssid, password });
|
|
108
|
+
res(devicesReturn);
|
|
109
|
+
} catch (error) {
|
|
110
|
+
rej(error);
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
this.UDPSocketHandler.onMessage((_data) => {
|
|
115
|
+
// 收到回调 可以停止发包
|
|
116
|
+
searchDeviceHandle();
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
// 没有收到udp回复的时候不进入配网
|
|
120
|
+
wx.onNetworkStatusChange(async () => {
|
|
121
|
+
// 发生网络切换的时候也停止发包,进入大循环配网
|
|
122
|
+
// 搜索中的时候不要重复搜索
|
|
123
|
+
!searchingDevice &&
|
|
124
|
+
wx.getConnectedWifi({
|
|
125
|
+
success: async (data) => {
|
|
126
|
+
/**
|
|
127
|
+
* 检查当前网络还是不是热点网络
|
|
128
|
+
*/
|
|
129
|
+
data.wifi.SSID.indexOf(softAPSSIDPrefix) === -1 &&
|
|
130
|
+
searchDeviceHandle();
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* 配网接口
|
|
138
|
+
* setDeviceOnboardingDeploy方法不可重复调用
|
|
139
|
+
*/
|
|
140
|
+
setDeviceOnboardingDeploy = ({
|
|
141
|
+
timeout,
|
|
142
|
+
isBind = true,
|
|
143
|
+
softAPSSIDPrefix,
|
|
144
|
+
}: ISetDeviceOnboardingDeployProps) => {
|
|
145
|
+
const ssid = this.ssid;
|
|
146
|
+
const password = this.password;
|
|
147
|
+
return new Promise<IResult<IDevice[]>>(async (res, rej) => {
|
|
148
|
+
this.destroy();
|
|
149
|
+
this.initDeviceOnboardingDeploy(res, rej);
|
|
150
|
+
this.startTimeoutTimer(timeout);
|
|
151
|
+
|
|
152
|
+
try {
|
|
153
|
+
const result = await this.configDevice({
|
|
154
|
+
ssid,
|
|
155
|
+
password,
|
|
156
|
+
softAPSSIDPrefix,
|
|
157
|
+
});
|
|
158
|
+
const newData: IDevice[] = (result.data || []).map(item => {
|
|
159
|
+
return {
|
|
160
|
+
mac: item.mac,
|
|
161
|
+
productKey: item.product_key,
|
|
162
|
+
did: item.did,
|
|
163
|
+
name: '',
|
|
164
|
+
isBind: false,
|
|
165
|
+
connectType: 'NONE',
|
|
166
|
+
isBleOnline: false,
|
|
167
|
+
isLanOnline: false,
|
|
168
|
+
isOnline: false,
|
|
169
|
+
}
|
|
170
|
+
})
|
|
171
|
+
if (isBind) {
|
|
172
|
+
try {
|
|
173
|
+
res(await this.bindDevices(newData));
|
|
174
|
+
} catch (error) {
|
|
175
|
+
rej(error);
|
|
176
|
+
}
|
|
177
|
+
} else {
|
|
178
|
+
// 不需要绑定 直接返回成功
|
|
179
|
+
res({
|
|
180
|
+
success: true,
|
|
181
|
+
data: newData,
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
} catch (error) {
|
|
185
|
+
rej(error);
|
|
186
|
+
} finally {
|
|
187
|
+
this.destroy();
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export default ApConfig;
|
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
import errorCode from "../errorCode";
|
|
2
|
+
import { IRandomCodesResult, IResult } from "../sdk";
|
|
3
|
+
import { ab2hex, advertisData2PkAndMac, isWXDevicesResult } from '../utils';
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
getBluetoothAdapterState,
|
|
7
|
+
openBluetoothAdapter,
|
|
8
|
+
startBluetoothDevicesDiscovery,
|
|
9
|
+
getBluetoothDevices,
|
|
10
|
+
closeBluetoothAdapter,
|
|
11
|
+
notifyBLECharacteristicValueChange,
|
|
12
|
+
unpackWriteBLECharacteristicValue,
|
|
13
|
+
createBLEConnection,
|
|
14
|
+
getBLEDeviceServices,
|
|
15
|
+
getBLEDeviceCharacteristics,
|
|
16
|
+
} from '../wechatApi';
|
|
17
|
+
import sleep from "../sleep";
|
|
18
|
+
import ConfigBase from "./ConfigBase";
|
|
19
|
+
import WifiConfig from "../protocol/WifiConfig";
|
|
20
|
+
|
|
21
|
+
interface IArgs {
|
|
22
|
+
bleDeviceId: string;
|
|
23
|
+
arrayBuffer: ArrayBuffer;
|
|
24
|
+
serviceUUIDSuffix?: string;
|
|
25
|
+
characteristicUUIDSuffix?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const configAck = '0000000303000002';
|
|
29
|
+
|
|
30
|
+
export function sendBLEConfigCmd({
|
|
31
|
+
bleDeviceId,
|
|
32
|
+
arrayBuffer,
|
|
33
|
+
serviceUUIDSuffix = 'abf0',
|
|
34
|
+
characteristicUUIDSuffix = 'abf7',
|
|
35
|
+
}: IArgs) {
|
|
36
|
+
let sendInterval = null;
|
|
37
|
+
// 10秒循环发送
|
|
38
|
+
let closeTimeout = null;
|
|
39
|
+
return new Promise<boolean>(async (resolve, reject) => {
|
|
40
|
+
try {
|
|
41
|
+
console.debug(
|
|
42
|
+
'GIZ_SDK: ssid info: ', arrayBuffer
|
|
43
|
+
);
|
|
44
|
+
// 设置超时
|
|
45
|
+
closeTimeout = setTimeout(() => {
|
|
46
|
+
sendInterval && clearInterval(sendInterval);
|
|
47
|
+
// 发送超时 返回失败
|
|
48
|
+
closeTimeout = null;
|
|
49
|
+
resolve(false);
|
|
50
|
+
}, 5 * 1000)
|
|
51
|
+
console.debug(
|
|
52
|
+
'GIZ_SDK: createBLEConnection start: ', bleDeviceId
|
|
53
|
+
);
|
|
54
|
+
const connectData = await createBLEConnection(bleDeviceId, 10 * 1000);
|
|
55
|
+
console.debug(
|
|
56
|
+
'GIZ_SDK: createBLEConnection end, res:', connectData
|
|
57
|
+
);
|
|
58
|
+
const services = await getBLEDeviceServices(bleDeviceId);
|
|
59
|
+
console.debug(
|
|
60
|
+
'GIZ_SDK: getBLEDeviceServices end, res:', services
|
|
61
|
+
);
|
|
62
|
+
const service = services.find((s) =>
|
|
63
|
+
s.uuid.split('-')[0].toLowerCase().endsWith(serviceUUIDSuffix)
|
|
64
|
+
);
|
|
65
|
+
if (!service) {
|
|
66
|
+
// 获取蓝牙设备服务异常
|
|
67
|
+
console.debug(
|
|
68
|
+
'GIZ_SDK: get ble device services fail',
|
|
69
|
+
bleDeviceId,
|
|
70
|
+
services
|
|
71
|
+
);
|
|
72
|
+
resolve(false);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const characteristics = await getBLEDeviceCharacteristics(
|
|
77
|
+
bleDeviceId,
|
|
78
|
+
service.uuid
|
|
79
|
+
);
|
|
80
|
+
console.debug(
|
|
81
|
+
'GIZ_SDK: getBLEDeviceCharacteristics end, res:', characteristics
|
|
82
|
+
);
|
|
83
|
+
const characteristic = characteristics.find((c) =>
|
|
84
|
+
c.uuid.split('-')[0].toLowerCase().endsWith(characteristicUUIDSuffix)
|
|
85
|
+
);
|
|
86
|
+
if (!characteristic) {
|
|
87
|
+
// 获取蓝牙设备特征值异常
|
|
88
|
+
console.debug(
|
|
89
|
+
'GIZ_SDK: get ble device characteristics fail',
|
|
90
|
+
bleDeviceId,
|
|
91
|
+
characteristics
|
|
92
|
+
);
|
|
93
|
+
resolve(false);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (
|
|
98
|
+
!characteristic.properties.notify &&
|
|
99
|
+
!characteristic.properties.indicate
|
|
100
|
+
) {
|
|
101
|
+
console.debug(
|
|
102
|
+
'GIZ_SDK: the ble device characteristic not support notify or indicate',
|
|
103
|
+
bleDeviceId,
|
|
104
|
+
characteristic
|
|
105
|
+
);
|
|
106
|
+
// 该设备不支持 notify & indicate 操作
|
|
107
|
+
resolve(false);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
console.debug(
|
|
112
|
+
'GIZ_SDK: check characteristic and service success',
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
await notifyBLECharacteristicValueChange(
|
|
116
|
+
bleDeviceId,
|
|
117
|
+
service.uuid,
|
|
118
|
+
characteristic.uuid
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
// 这里有个问题,模组好像不一定每次都会回
|
|
122
|
+
const handleBLECharacteristicValueChange = (res: WechatMiniprogram.OnBLECharacteristicValueChangeCallbackResult) => {
|
|
123
|
+
const hexString = ab2hex(res.value);
|
|
124
|
+
console.debug('GIZ_SDK: 收到设备返回ack', hexString)
|
|
125
|
+
if (hexString === configAck) {
|
|
126
|
+
// 发送成功
|
|
127
|
+
wx.offBLECharacteristicValueChange(handleBLECharacteristicValueChange);
|
|
128
|
+
resolve(true);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
// 订阅特征值变化
|
|
133
|
+
wx.onBLECharacteristicValueChange(handleBLECharacteristicValueChange);
|
|
134
|
+
|
|
135
|
+
console.debug(
|
|
136
|
+
'GIZ_SDK: on notifyBLECharacteristicValueChange success',
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
// 写特征值
|
|
140
|
+
if (closeTimeout === null) {
|
|
141
|
+
sendInterval && clearInterval(sendInterval);
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
await unpackWriteBLECharacteristicValue(
|
|
145
|
+
bleDeviceId,
|
|
146
|
+
service.uuid,
|
|
147
|
+
characteristic.uuid,
|
|
148
|
+
arrayBuffer
|
|
149
|
+
);
|
|
150
|
+
sendInterval = setInterval(() => {
|
|
151
|
+
if (closeTimeout === null) {
|
|
152
|
+
sendInterval && clearInterval(sendInterval);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
unpackWriteBLECharacteristicValue(
|
|
156
|
+
bleDeviceId,
|
|
157
|
+
service.uuid,
|
|
158
|
+
characteristic.uuid,
|
|
159
|
+
arrayBuffer
|
|
160
|
+
);
|
|
161
|
+
}, 2000)
|
|
162
|
+
|
|
163
|
+
// if (res.errCode === 0) {
|
|
164
|
+
// resolve(true);
|
|
165
|
+
// } else{
|
|
166
|
+
// resolve(false);
|
|
167
|
+
// }
|
|
168
|
+
console.debug('GIZ_SDK: unpackWriteBLECharacteristicValue end');
|
|
169
|
+
} catch (error) {
|
|
170
|
+
reject(false);
|
|
171
|
+
console.debug('GIZ_SDK: sendBLEConfigCmd error', error);
|
|
172
|
+
}
|
|
173
|
+
})
|
|
174
|
+
.catch((error) => {
|
|
175
|
+
console.debug('GIZ_SDK: sendBLEConfigCmd error', error);
|
|
176
|
+
})
|
|
177
|
+
.finally(() => {
|
|
178
|
+
// 关闭连接
|
|
179
|
+
closeTimeout && clearTimeout(closeTimeout);
|
|
180
|
+
sendInterval && clearInterval(sendInterval);
|
|
181
|
+
wx.closeBLEConnection({ deviceId: bleDeviceId });
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
interface configBLEDeviceParams {
|
|
186
|
+
ssid: string;
|
|
187
|
+
password: string;
|
|
188
|
+
softAPSSIDPrefix?: string;
|
|
189
|
+
timeout: number;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
interface ISetDeviceOnboardingDeployProps {
|
|
194
|
+
timeout: number;
|
|
195
|
+
isBind: boolean;
|
|
196
|
+
softAPSSIDPrefix: string;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
class BLEConfig extends ConfigBase {
|
|
201
|
+
destroy = () => {
|
|
202
|
+
this.cleanTimeout();
|
|
203
|
+
}
|
|
204
|
+
isValidBleDevice = (
|
|
205
|
+
bleDevice: WechatMiniprogram.BlueToothDevice,
|
|
206
|
+
softAPSSIDPrefix?: string
|
|
207
|
+
) => {
|
|
208
|
+
if (!bleDevice || !bleDevice.advertisData) {
|
|
209
|
+
// 无效蓝牙设备或者蓝牙设备广播数据为空,返回失败
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const {productKey, mac} = advertisData2PkAndMac(bleDevice.advertisData, this.specialProductKeys)
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
if (!this.specialProductKeys.includes(productKey)) {
|
|
217
|
+
// 不在PK列表
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
console.debug(`GIZ_SDK: isValidBleDevice mac: ${mac} pk: ${productKey} name: ${bleDevice.name}`)
|
|
221
|
+
console.debug(`GIZ_SDK: softAPSSIDPrefix: ${softAPSSIDPrefix} ismatch : ${bleDevice.name && bleDevice.name.startsWith(softAPSSIDPrefix)}`)
|
|
222
|
+
return (
|
|
223
|
+
!softAPSSIDPrefix ||
|
|
224
|
+
(bleDevice.name && bleDevice.name.startsWith(softAPSSIDPrefix))
|
|
225
|
+
);
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
enableBluetoothDevicesDescovery = async (): Promise<
|
|
230
|
+
{ success: false; err: IError } | { success: true }
|
|
231
|
+
> => {
|
|
232
|
+
try {
|
|
233
|
+
await closeBluetoothAdapter();
|
|
234
|
+
await openBluetoothAdapter();
|
|
235
|
+
} catch (error) {
|
|
236
|
+
|
|
237
|
+
}
|
|
238
|
+
const stateRes = await getBluetoothAdapterState();
|
|
239
|
+
if (!stateRes.available) {
|
|
240
|
+
return {
|
|
241
|
+
success: false,
|
|
242
|
+
err: {
|
|
243
|
+
errorCode: errorCode.GIZ_SDK_BLE_BLUETOOTH_FUNCTION_NOT_TURNED_ON.errorCode,
|
|
244
|
+
errorMessage: '蓝牙状态不可用',
|
|
245
|
+
},
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
await startBluetoothDevicesDiscovery();
|
|
250
|
+
return { success: true };
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
enableAndGetBluetoothDevices = async (
|
|
255
|
+
softAPSSIDPrefix?: string
|
|
256
|
+
): Promise<
|
|
257
|
+
| { success: false; err: IError }
|
|
258
|
+
| { success: true; bleDevices?: WechatMiniprogram.BlueToothDevice[] }
|
|
259
|
+
> => {
|
|
260
|
+
const discoveryRes = await this.enableBluetoothDevicesDescovery();
|
|
261
|
+
if (!discoveryRes.success) {
|
|
262
|
+
return discoveryRes;
|
|
263
|
+
}
|
|
264
|
+
console.debug('GIZ_SDK: start enableAndGetBluetoothDevices');
|
|
265
|
+
|
|
266
|
+
const bleDevices: WechatMiniprogram.BlueToothDevice[] = (
|
|
267
|
+
await getBluetoothDevices()
|
|
268
|
+
).filter((d) => this.isValidBleDevice(d, softAPSSIDPrefix));
|
|
269
|
+
console.debug('GIZ_SDK: getBluetoothDevices success', bleDevices);
|
|
270
|
+
return {
|
|
271
|
+
success: true,
|
|
272
|
+
bleDevices,
|
|
273
|
+
};
|
|
274
|
+
};
|
|
275
|
+
setDeviceOnboardingDeploy = ({
|
|
276
|
+
timeout,
|
|
277
|
+
isBind = true,
|
|
278
|
+
softAPSSIDPrefix,
|
|
279
|
+
}: ISetDeviceOnboardingDeployProps) => {
|
|
280
|
+
return new Promise<IResult<IDevice[]>>(async (res, rej) => {
|
|
281
|
+
const ssid = this.ssid;
|
|
282
|
+
const password = this.password;
|
|
283
|
+
this.destroy();
|
|
284
|
+
this.initDeviceOnboardingDeploy(res, rej);
|
|
285
|
+
this.startTimeoutTimer(timeout);
|
|
286
|
+
try {
|
|
287
|
+
const result = await this.configBLEDevice({
|
|
288
|
+
ssid,
|
|
289
|
+
password,
|
|
290
|
+
timeout: timeout * 1000,
|
|
291
|
+
softAPSSIDPrefix,
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
const newData: IDevice[] = (result.data || []).map(item => {
|
|
295
|
+
return {
|
|
296
|
+
mac: item.mac,
|
|
297
|
+
productKey: item.product_key,
|
|
298
|
+
did: item.did,
|
|
299
|
+
name: '',
|
|
300
|
+
isBind: false,
|
|
301
|
+
connectType: 'NONE',
|
|
302
|
+
isBleOnline: false,
|
|
303
|
+
isLanOnline: false,
|
|
304
|
+
isOnline: false,
|
|
305
|
+
}
|
|
306
|
+
})
|
|
307
|
+
if (isBind) {
|
|
308
|
+
res(await this.bindDevices(newData));
|
|
309
|
+
} else {
|
|
310
|
+
// 不需要绑定 直接返回成功
|
|
311
|
+
res({
|
|
312
|
+
success: true,
|
|
313
|
+
data: newData,
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
} catch (error) {
|
|
317
|
+
rej(error);
|
|
318
|
+
} finally {
|
|
319
|
+
this.destroy();
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* 负责发蓝牙设备指令
|
|
325
|
+
*/
|
|
326
|
+
configBLEDevice = ({
|
|
327
|
+
ssid,
|
|
328
|
+
password,
|
|
329
|
+
softAPSSIDPrefix,
|
|
330
|
+
}: configBLEDeviceParams) => {
|
|
331
|
+
return new Promise<IResult<IRandomCodesResult[]>>(async (res, rej) => {
|
|
332
|
+
console.debug('GIZ_SDK: start config ble device');
|
|
333
|
+
const enableAndGetRes = await this.enableAndGetBluetoothDevices(
|
|
334
|
+
softAPSSIDPrefix
|
|
335
|
+
).catch((error) => {
|
|
336
|
+
return {
|
|
337
|
+
success: false,
|
|
338
|
+
err: {
|
|
339
|
+
errorCode: errorCode.WECHAT_ERROR.errorCode,
|
|
340
|
+
errorMessage: JSON.stringify(error),
|
|
341
|
+
},
|
|
342
|
+
};
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
if (!isWXDevicesResult(enableAndGetRes)) {
|
|
346
|
+
// 开启获取蓝牙失败
|
|
347
|
+
return rej(enableAndGetRes);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
const { bleDevices } = enableAndGetRes;
|
|
351
|
+
|
|
352
|
+
console.debug('GIZ_SDK: enableAndGetBluetoothDevices success, target devices: ', bleDevices);
|
|
353
|
+
|
|
354
|
+
const handleFoundDevices = async ({
|
|
355
|
+
devices,
|
|
356
|
+
}: {
|
|
357
|
+
devices: WechatMiniprogram.BlueToothDevice[];
|
|
358
|
+
}) => {
|
|
359
|
+
this.hasTimeoutHandler()
|
|
360
|
+
? Array.prototype.push.apply(
|
|
361
|
+
bleDevices,
|
|
362
|
+
devices.filter((d) => this.isValidBleDevice(d, softAPSSIDPrefix))
|
|
363
|
+
)
|
|
364
|
+
: wx.offBluetoothDeviceFound(handleFoundDevices);
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
wx.onBluetoothDeviceFound(handleFoundDevices);
|
|
368
|
+
|
|
369
|
+
const uint8Array = WifiConfig.pack(ssid,password);
|
|
370
|
+
const startConfigDevice = async () => {
|
|
371
|
+
console.debug('GIZ_SDK: startConfigDevice');
|
|
372
|
+
if (!this.hasTimeoutHandler()) {
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
const bleDevice = bleDevices.shift();
|
|
376
|
+
console.debug('GIZ_SDK: startConfigDevice, target device: ', bleDevice);
|
|
377
|
+
|
|
378
|
+
const success =
|
|
379
|
+
bleDevice &&
|
|
380
|
+
(await sendBLEConfigCmd({
|
|
381
|
+
bleDeviceId: bleDevice.deviceId,
|
|
382
|
+
arrayBuffer: uint8Array.buffer,
|
|
383
|
+
}));
|
|
384
|
+
|
|
385
|
+
if (!success) {
|
|
386
|
+
// 如果校验设备或者发送不成功,重试下一个设备
|
|
387
|
+
console.debug("GIZ_SDK: send config fail retry next device")
|
|
388
|
+
await sleep(500);
|
|
389
|
+
await startBluetoothDevicesDiscovery();
|
|
390
|
+
console.debug("GIZ_SDK: startBluetoothDevicesDiscovery")
|
|
391
|
+
await startConfigDevice();
|
|
392
|
+
console.debug("GIZ_SDK: search ble device")
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// 如果有一个设备发送成功,则不再发现新设备
|
|
397
|
+
console.debug("GIZ_SDK: offBluetoothDeviceFound ready search device")
|
|
398
|
+
wx.offBluetoothDeviceFound(handleFoundDevices);
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
// 开始大循环搜索
|
|
402
|
+
try {
|
|
403
|
+
// const devicesReturn = await this.searchDevice({ ssid, password });
|
|
404
|
+
this.searchDevice({ ssid, password }).then(devicesReturn => {
|
|
405
|
+
res(devicesReturn);
|
|
406
|
+
})
|
|
407
|
+
} catch (error) {
|
|
408
|
+
rej(error);
|
|
409
|
+
console.error("GIZ_SDK: searchDevice error", error)
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
await startConfigDevice();
|
|
413
|
+
});
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
export default BLEConfig;
|