mini_program_gizwits_sdk 3.6.3 → 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.
Files changed (135) hide show
  1. package/README.md +4 -0
  2. package/dist/index.js +3 -3
  3. package/package.json +1 -1
  4. package/yarn-error.log +6901 -0
  5. package/dist/src/GizLog.d.ts +0 -18
  6. package/dist/src/crc/calculators/crc16modbus.d.ts +0 -2
  7. package/dist/src/crc/calculators/crc32.d.ts +0 -2
  8. package/dist/src/crc/crc16modbus.d.ts +0 -7
  9. package/dist/src/crc/crc32.d.ts +0 -7
  10. package/dist/src/crc/create_buffer.d.ts +0 -3
  11. package/dist/src/crc/define_crc.d.ts +0 -7
  12. package/dist/src/crc/index.d.ts +0 -19
  13. package/dist/src/crc/types.d.ts +0 -11
  14. package/dist/src/errorCode.d.ts +0 -3
  15. package/dist/src/globalData.d.ts +0 -2
  16. package/dist/src/handler/EventListener.d.ts +0 -9
  17. package/dist/src/handler/ble.d.ts +0 -105
  18. package/dist/src/handler/lan.d.ts +0 -78
  19. package/dist/src/handler/socket.d.ts +0 -151
  20. package/dist/src/openApiRequest.d.ts +0 -3
  21. package/dist/src/productConfigFileManage.d.ts +0 -11
  22. package/dist/src/protocol/Bind.d.ts +0 -7
  23. package/dist/src/protocol/DeviceInfo.d.ts +0 -11
  24. package/dist/src/protocol/DiscoverUDP.d.ts +0 -17
  25. package/dist/src/protocol/GetDeviceStatus.d.ts +0 -5
  26. package/dist/src/protocol/Login.d.ts +0 -12
  27. package/dist/src/protocol/Ntp.d.ts +0 -5
  28. package/dist/src/protocol/OTA/AwaitNoti.d.ts +0 -7
  29. package/dist/src/protocol/OTA/CheckCanOTA.d.ts +0 -10
  30. package/dist/src/protocol/OTA/OTAPackPackage.d.ts +0 -8
  31. package/dist/src/protocol/OTA/OtaComplete.d.ts +0 -8
  32. package/dist/src/protocol/OTA/PreOTA.d.ts +0 -11
  33. package/dist/src/protocol/OTA/ResetPosition.d.ts +0 -6
  34. package/dist/src/protocol/OffLineData.d.ts +0 -23
  35. package/dist/src/protocol/ProtocolBase.d.ts +0 -11
  36. package/dist/src/protocol/Reset.d.ts +0 -8
  37. package/dist/src/protocol/SetReset.d.ts +0 -7
  38. package/dist/src/protocol/WifiConfig.d.ts +0 -6
  39. package/dist/src/protocol/dataPoint.d.ts +0 -34
  40. package/dist/src/protocol/tool.d.ts +0 -14
  41. package/dist/src/randomCode.d.ts +0 -3
  42. package/dist/src/request.d.ts +0 -1
  43. package/dist/src/sdk.d.ts +0 -136
  44. package/dist/src/sentry.d.ts +0 -2
  45. package/dist/src/services/devices.d.ts +0 -47
  46. package/dist/src/services/login.d.ts +0 -4
  47. package/dist/src/services/monitor.d.ts +0 -12
  48. package/dist/src/services/ota.d.ts +0 -7
  49. package/dist/src/services/tool.d.ts +0 -4
  50. package/dist/src/services/uploadP0.d.ts +0 -14
  51. package/dist/src/sleep.d.ts +0 -2
  52. package/dist/src/types/index.d.ts +0 -360
  53. package/dist/src/utils.d.ts +0 -46
  54. package/dist/src/wechatApi.d.ts +0 -19
  55. package/dist/src/wifiConfig/ConfigBase.d.ts +0 -27
  56. package/dist/src/wifiConfig/ap.d.ts +0 -11
  57. package/dist/src/wifiConfig/ble.d.ts +0 -41
  58. package/dist/src/wifiConfig/nfc/disposeData.d.ts +0 -2
  59. package/dist/src/wifiConfig/nfc/formatData.d.ts +0 -3
  60. package/dist/src/wifiConfig/nfc/formatSSID.d.ts +0 -6
  61. package/dist/src/wifiConfig/nfc/index.d.ts +0 -22
  62. package/dist/src/wifiConfig/nfc/isIncludedCH.d.ts +0 -1
  63. package/dist/src/wifiConfig/nfc/nfcAConnect.d.ts +0 -6
  64. package/dist/src/wifiConfig/nfc/nfcATransceive.d.ts +0 -5
  65. package/dist/src/wifiConfig/nfc/ssIDToASC.d.ts +0 -1
  66. package/dist/src/wifiConfig/nfc/stringToGbk.d.ts +0 -1
  67. package/dist/src/wifiConfig/nfc/transceive.wx.d.ts +0 -2
  68. package/dist/src/wifiConfig/nfc/types.d.ts +0 -15
  69. package/index.ts +0 -8
  70. package/src/GizLog.ts +0 -51
  71. package/src/crc/calculators/crc16modbus.ts +0 -36
  72. package/src/crc/calculators/crc32.ts +0 -47
  73. package/src/crc/crc16modbus.ts +0 -3
  74. package/src/crc/crc32.ts +0 -4
  75. package/src/crc/create_buffer.ts +0 -7
  76. package/src/crc/define_crc.ts +0 -9
  77. package/src/crc/index.ts +0 -10
  78. package/src/crc/package.json +0 -3
  79. package/src/crc/types.ts +0 -10
  80. package/src/errorCode.ts +0 -83
  81. package/src/globalData.ts +0 -9
  82. package/src/handler/EventListener.ts +0 -44
  83. package/src/handler/ble.ts +0 -1369
  84. package/src/handler/lan.ts +0 -388
  85. package/src/handler/socket.ts +0 -686
  86. package/src/openApiRequest.ts +0 -50
  87. package/src/productConfigFileManage.ts +0 -72
  88. package/src/protocol/Bind.ts +0 -22
  89. package/src/protocol/DeviceInfo.ts +0 -37
  90. package/src/protocol/DiscoverUDP.ts +0 -80
  91. package/src/protocol/GetDeviceStatus.ts +0 -61
  92. package/src/protocol/Login.ts +0 -43
  93. package/src/protocol/Ntp.ts +0 -13
  94. package/src/protocol/OTA/AwaitNoti.ts +0 -14
  95. package/src/protocol/OTA/CheckCanOTA.ts +0 -23
  96. package/src/protocol/OTA/OTAPackPackage.ts +0 -37
  97. package/src/protocol/OTA/OtaComplete.ts +0 -17
  98. package/src/protocol/OTA/PreOTA.ts +0 -48
  99. package/src/protocol/OTA/ResetPosition.ts +0 -14
  100. package/src/protocol/OffLineData.ts +0 -81
  101. package/src/protocol/ProtocolBase.ts +0 -54
  102. package/src/protocol/Reset.ts +0 -16
  103. package/src/protocol/SetReset.ts +0 -13
  104. package/src/protocol/WifiConfig.ts +0 -42
  105. package/src/protocol/dataPoint.ts +0 -687
  106. package/src/protocol/tool.ts +0 -140
  107. package/src/randomCode.ts +0 -25
  108. package/src/request.ts +0 -22
  109. package/src/sdk.ts +0 -1536
  110. package/src/sentry.ts +0 -55
  111. package/src/services/devices.ts +0 -312
  112. package/src/services/login.ts +0 -11
  113. package/src/services/monitor.ts +0 -117
  114. package/src/services/ota.ts +0 -17
  115. package/src/services/tool.ts +0 -10
  116. package/src/services/uploadP0.ts +0 -57
  117. package/src/sleep.ts +0 -2
  118. package/src/types/index.ts +0 -455
  119. package/src/utils.ts +0 -448
  120. package/src/wechatApi.ts +0 -211
  121. package/src/wifiConfig/ConfigBase.ts +0 -235
  122. package/src/wifiConfig/ap.ts +0 -184
  123. package/src/wifiConfig/ble.ts +0 -481
  124. package/src/wifiConfig/nfc/disposeData.ts +0 -63
  125. package/src/wifiConfig/nfc/formatData.ts +0 -46
  126. package/src/wifiConfig/nfc/formatSSID.ts +0 -59
  127. package/src/wifiConfig/nfc/index.ts +0 -133
  128. package/src/wifiConfig/nfc/isIncludedCH.ts +0 -8
  129. package/src/wifiConfig/nfc/nfcAConnect.ts +0 -30
  130. package/src/wifiConfig/nfc/nfcATransceive.ts +0 -29
  131. package/src/wifiConfig/nfc/ssIDToASC.ts +0 -12
  132. package/src/wifiConfig/nfc/stringToGbk.ts +0 -51
  133. package/src/wifiConfig/nfc/transceive.wx.ts +0 -124
  134. package/src/wifiConfig/nfc/types.ts +0 -18
  135. package/src/wifiConfig/timeZoneName.json +0 -1
@@ -1,388 +0,0 @@
1
- import GizLog from "../GizLog";
2
- import Bind from "../protocol/Bind";
3
- import DiscoverUDP from "../protocol/DiscoverUDP";
4
- import Login from "../protocol/Login";
5
- import ProtocolBase from "../protocol/ProtocolBase";
6
- import { arrayToUint8, hexStrint2byte } from "../protocol/tool";
7
- import sleep from "../sleep";
8
- import { IDevice, ILanDevice, IResult, LanConnectDevice, TLanHandleEvent } from "../types";
9
- import { ab2hex, ab2numbers } from "../utils";
10
- import EventListener from "./EventListener";
11
-
12
- export class LanHandle extends EventListener<TLanHandleEvent> {
13
- // private characteristicUUIDSuffix: string;
14
- public connectedList: LanConnectDevice[] = [];
15
- public pks: string[] = [];
16
- // private globalListenerMap = {};
17
- public scanList: ILanDevice[] = [];
18
-
19
- private boradcastTimer: any;
20
-
21
- private UDPSocket: WechatMiniprogram.UDPSocket;
22
-
23
- private deviceUDPPort = 12414
24
- private appUDPPort = 2415
25
- private offlineThreshold = 20000 // 超过20s 没有信号,则认为离线
26
- private isActive = true //当前是否是前台
27
-
28
- private listenDevOfflineTimer: any = null;
29
-
30
-
31
- constructor({pks}) {
32
- super();
33
- this.pks = pks;
34
- }
35
-
36
- init = () => {
37
- if (this.UDPSocket) {
38
- this.UDPSocket = wx.createUDPSocket();
39
- this.UDPSocket.bind();
40
- this.UDPSocket.onMessage(this.onMessage);
41
- this.UDPSocket.onError(this.onError);
42
- GizLog.info('Start UDP Discover')
43
- this.startDiscover();
44
- this.listenDevOffline(this.offlineThreshold);
45
- wx.onAppShow(this.onAppShow);
46
- wx.onAppHide(this.onAppHide);
47
- }
48
- }
49
-
50
- private onAppShow = () => {
51
- // 更新所有蓝牙设备的Ctime
52
- this.isActive = true;
53
- this.scanList.map(item => {
54
- item.ctime = Date.now();
55
- })
56
- }
57
-
58
- private onAppHide = () => {
59
- this.isActive = false;
60
- }
61
-
62
- private listenDevOffline = (offlineThreshold: number) => {
63
- // 定时检查设备列表,移除超时设备
64
- this.listenDevOfflineTimer = setInterval(() => {
65
- // 非激活状态不处理
66
- if (!this.isActive) return;
67
- const now = new Date().getTime();
68
- const newDeviceList = this.scanList.filter((d) => {
69
- return now - d.ctime < offlineThreshold;
70
- });
71
- if (newDeviceList.length !== this.scanList.length) {
72
- GizLog.debug('remove lan device')
73
- this.scanList = newDeviceList;
74
- this.notifyDevices();
75
- }
76
- }, 500);
77
- };
78
-
79
- private notifyDevices = () => {
80
- if (this.listenerMap['GizLanDeviceList']) {
81
- this.listenerMap['GizLanDeviceList'].map((item) => {
82
- item(this.scanList);
83
- });
84
- }
85
- }
86
- private notifyDeviceData = (mac: string, data: string) => {
87
- if (this.listenerMap['GizLanDeviceData']) {
88
- this.listenerMap['GizLanDeviceData'].map((item) => {
89
- item(mac, data);
90
- });
91
- }
92
- }
93
-
94
- isSameDevice = (deviceA: IDevice, deviceB: IDevice) => {
95
- return deviceA.mac === deviceB.mac && deviceA.productKey === deviceB.productKey
96
- }
97
-
98
- // 解除订阅
99
- disConnectDevice = (target: IDevice) => {
100
- const index = this.connectedList.findIndex(item => item.productKey === target.productKey && item.mac === target.mac);
101
- try {
102
- if (index !== -1) {
103
- // 删除这个设备
104
- this.connectedList[index].socketHandle.destory();
105
- this.connectedList.splice(index, 1);
106
- }
107
- return {success: true}
108
-
109
- } catch (error) {
110
- return {success: false, err: error}
111
- }
112
- }
113
-
114
- onData = (mac: string, hexString: string) => {
115
- this.notifyDeviceData(mac, hexString);
116
- }
117
-
118
- connectDevice = async (device) => {
119
- const target = this.scanList.find(item => this.isSameDevice(device, item));
120
- // 检查是否有连接
121
- if (!target) {
122
- return {
123
- success: false,
124
- }
125
- }
126
-
127
- const connectDevice = this.connectedList.find(item => this.isSameDevice(device, (item as unknown as IDevice)))
128
- if (connectDevice) {
129
- return {
130
- success: true
131
- }
132
- } else {
133
- const tcpHandler = new TCPHandler({ip: target.ip, mac: target.mac, productKey: target.productKey, onClose: this.onDeviceClose, onData: this.onData})
134
- const res = await tcpHandler.connect()
135
- if (res.success) {
136
- this.connectedList.push({
137
- mac: target.mac,
138
- productKey: target.productKey,
139
- socketHandle: tcpHandler,
140
- })
141
- } else {
142
- tcpHandler.destory();
143
- }
144
- return res;
145
- }
146
-
147
- }
148
-
149
- onError = (err) => {
150
- GizLog.warn('Discover Error', err)
151
- }
152
-
153
- isMatchPk = (pk) => {
154
- if (this.pks.length === 0 ) {
155
- return true
156
- } else {
157
- return this.pks.includes(pk)
158
- }
159
- }
160
-
161
- onMessage = (res: WechatMiniprogram.UDPSocketOnMessageListenerResult) => {
162
- const numberArrary = ab2numbers(res.message)
163
- const discoverPackage = new DiscoverUDP(numberArrary)
164
- if (discoverPackage.mac !== '' && this.isMatchPk(discoverPackage.productKey)) {
165
- // 合法设备
166
- // GizLog.debug("find device", discoverPackage)
167
- this.updateDevice(discoverPackage, res.remoteInfo.address)
168
- }
169
- }
170
-
171
- // tcp 设备断开连接
172
- onDeviceClose = ({productKey, mac}) => {
173
- const index = this.connectedList.findIndex(item => item.productKey === productKey && item.mac === mac);
174
- if (index !== -1) {
175
- // 删除这个设备
176
- this.connectedList.splice(index, 1);
177
- }
178
- }
179
-
180
- // 数据点控制
181
- public write = async (device: IDevice, value: ArrayBuffer) => {
182
- GizLog.debug('send data to device', device, value, this.connectedList)
183
- const index = this.connectedList.findIndex(item => this.isSameDevice(item as unknown as IDevice, device));
184
- if (index !== -1) {
185
- const socketHandle = this.connectedList[index].socketHandle;
186
- return socketHandle.write(value)
187
- }
188
- return {
189
- success: false,
190
- message: 'device not sub',
191
- };
192
- };
193
-
194
- updateDevice = (updateDevice: DiscoverUDP, ip: string) => {
195
- const target = this.scanList.find(item => item.mac === updateDevice.mac);
196
- if (!target) {
197
- const device: ILanDevice = {
198
- mac: updateDevice.mac,
199
- ip,
200
- productKey: updateDevice.productKey,
201
- did: updateDevice.did,
202
- expandData: updateDevice,
203
- name: "",
204
- isBind: false,
205
- connectType: "NONE",
206
- isOnline: false,
207
- isLanOnline: true,
208
- isBleOnline: false,
209
- ctime: Date.now()
210
- };
211
- this.scanList.push(device);
212
- this.notifyDevices();
213
- } else {
214
- // 存在设备 更新字段
215
- target.ctime = Date.now()
216
- }
217
- }
218
-
219
- sendBoradCast = () => {
220
- // 发送发现包
221
- if(this.UDPSocket) {
222
- this.UDPSocket.send({
223
- address: '255.255.255.255',
224
- message: DiscoverUDP.pack(),
225
- port: this.deviceUDPPort
226
- })
227
- GizLog.debug('Send UDP Discover')
228
- }
229
- }
230
-
231
- private startDiscover = () => {
232
- this.sendBoradCast()
233
- this.boradcastTimer = setInterval(() => {
234
- this.sendBoradCast()
235
- }, 5000)
236
- }
237
-
238
- public destory = () => {
239
- this.boradcastTimer && clearInterval(this.boradcastTimer);
240
- this.UDPSocket && this.UDPSocket.close();
241
- this.listenDevOfflineTimer && clearInterval(this.listenDevOfflineTimer);
242
- wx.offAppShow(this.onAppShow);
243
- wx.offAppHide(this.onAppHide);
244
- }
245
- }
246
-
247
-
248
- interface ITCPHandlerProps {
249
- ip: string
250
- mac: string
251
- productKey: string
252
- onClose: (ip) => void
253
- onData: (mac: string, data: string) => void
254
- }
255
- export class TCPHandler {
256
- private deviceTCPPort = 12416
257
- private ip = ""
258
- private productKey = ""
259
- private mac = ""
260
- private tcpHandler: WechatMiniprogram.TCPSocket = null;
261
- private onData = (mac: string, data: string) => {}
262
-
263
- private onCloseListener = ({mac, productKey}) => {}
264
-
265
- constructor({ip, onClose, mac, productKey, onData}: ITCPHandlerProps) {
266
- this.ip = ip;
267
- this.mac = mac;
268
- this.productKey = productKey;
269
- this.onData = onData;
270
- this.onCloseListener = onClose;
271
- }
272
-
273
- connect = async () => {
274
- // 创建
275
- return new Promise<IResult<any>>(async (res) => {
276
- const onConnect = () => {
277
- // 发送绑定
278
- const config = Bind.pack()
279
- this.tcpHandler.write(arrayToUint8(config))
280
- GizLog.debug('connect success try login')
281
- }
282
- const onError = (err) => {
283
- res({
284
- success: true,
285
- err: err
286
- })
287
- }
288
- const onMessage = (message: WechatMiniprogram.TCPSocketOnMessageListenerResult) => {
289
- // 接收绑定回复
290
- // 然后再发起登录
291
- const baseProtocol = new ProtocolBase(ab2numbers(message.message))
292
- switch (baseProtocol.cmd) {
293
- case '0007': {
294
- // 绑定命令字的回复
295
- const bindRes = new Bind(ab2numbers(message.message));
296
- const loginPack = Login.pack({passcode: bindRes.passcode})
297
- this.tcpHandler.write(arrayToUint8(loginPack))
298
- break;
299
- }
300
- case '0009': {
301
- const loginRes = new Login(ab2numbers(message.message))
302
- if (loginRes.result) {
303
- // 登录成功
304
- res({
305
- success: true
306
- })
307
- // 创建监听
308
- this.initListener()
309
- } else {
310
- // 设备通知登录失败
311
- res({
312
- success: false,
313
- })
314
- }
315
- }
316
- }
317
- }
318
-
319
- try {
320
- this.tcpHandler = wx.createTCPSocket();
321
-
322
- // 设置监听
323
- this.tcpHandler.onConnect(onConnect)
324
- this.tcpHandler.onError(onError)
325
- this.tcpHandler.onMessage(onMessage)
326
-
327
- this.tcpHandler.connect({
328
- address: this.ip,
329
- port: this.deviceTCPPort,
330
- timeout: 2
331
- })
332
- await sleep(4000)
333
- res({
334
- success: false,
335
- err: null
336
- })
337
- } catch(err) {
338
- GizLog.error("connect lan error", err);
339
- res({
340
- success: false,
341
- err: null
342
- })
343
- } finally {
344
- this.tcpHandler.offConnect(onConnect)
345
- this.tcpHandler.offError(onError)
346
- this.tcpHandler.offMessage(onMessage)
347
- }
348
- })
349
-
350
- }
351
-
352
- write = (data: ArrayBuffer) => {
353
- this.tcpHandler.write(data);
354
- return {
355
- success: true
356
- }
357
- }
358
-
359
- onMessage = (message) => {
360
- try {
361
- const hexString = ab2hex(message.message);
362
- const baseProtocol = new ProtocolBase(hexStrint2byte(hexString))
363
- GizLog.debug("on tcp message", baseProtocol)
364
- this.onData(this.mac, hexString)
365
- } catch (error) {
366
-
367
- }
368
- }
369
-
370
- onError = () => {}
371
-
372
- onClose = () => {
373
- this.onCloseListener({productKey: this.productKey, mac: this.mac})
374
- }
375
-
376
- initListener = () => {
377
- this.tcpHandler.onError(this.onError)
378
- this.tcpHandler.onMessage(this.onMessage)
379
- this.tcpHandler.onClose(this.onClose)
380
- }
381
-
382
- destory = () => {
383
- this.tcpHandler && this.tcpHandler.close();
384
- this.tcpHandler.offError(this.onError)
385
- this.tcpHandler.offMessage(this.onMessage)
386
- this.tcpHandler.offClose(this.onClose)
387
- }
388
- }