mini_program_gizwits_sdk 3.7.0-kuka → 3.7.0

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