mini_program_gizwits_sdk 3.1.20 → 3.2.20-beta

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.
@@ -26,6 +26,7 @@ export declare type OnScanDevice = (scanList: IDevice[]) => void;
26
26
  interface ConnectDevice {
27
27
  deviceId?: string;
28
28
  serviceId?: string;
29
+ isLogin?: boolean;
29
30
  characteristicId?: string;
30
31
  errMsg?: string;
31
32
  }
@@ -49,6 +50,7 @@ export declare class BleHandle extends EventListener<TBleHandleEvent> {
49
50
  private isActive;
50
51
  scanList: IDevice[];
51
52
  constructor({ pks, bleHandleParams, offlineThreshold, }: ConstructorParams);
53
+ private onBluetoothAdapterStateChange;
52
54
  private onAppShow;
53
55
  private onAppHide;
54
56
  private listenDevOffline;
@@ -84,12 +86,10 @@ export declare class BleHandle extends EventListener<TBleHandleEvent> {
84
86
  disableScan: () => Promise<WechatMiniprogram.BluetoothError>;
85
87
  enableScan: () => void;
86
88
  connectDevice: (deviceId: string) => Promise<{
87
- success: boolean;
88
- message?: undefined;
89
- } | {
90
89
  success: boolean;
91
90
  message: any;
92
91
  }>;
92
+ private updateDeviceIsLogin;
93
93
  private handleBindReq;
94
94
  private handleLoginReq;
95
95
  watchBleDevice: (deviceId: any, func: any) => Promise<{
@@ -10,9 +10,11 @@ interface ILanDevice extends IDevice {
10
10
  expandData: DiscoverUDP;
11
11
  ip: string;
12
12
  }
13
- declare type GizLanDeviceDataNotifications = (devices: IDevice[]) => void;
13
+ declare type GizLanDeviceListNotifications = (devices: IDevice[]) => void;
14
+ declare type GizLanDeviceDataNotifications = (mac: string, data: string) => void;
14
15
  export interface TLanHandleEvent {
15
- GizLanDeviceList: GizLanDeviceDataNotifications;
16
+ GizLanDeviceList: GizLanDeviceListNotifications;
17
+ GizLanDeviceData: GizLanDeviceDataNotifications;
16
18
  }
17
19
  export declare class LanHandle extends EventListener<TLanHandleEvent> {
18
20
  connectedList: ConnectDevice[];
@@ -33,6 +35,7 @@ export declare class LanHandle extends EventListener<TLanHandleEvent> {
33
35
  private onAppHide;
34
36
  private listenDevOffline;
35
37
  private notifyDevices;
38
+ private notifyDeviceData;
36
39
  isSameDevice: (deviceA: IDevice, deviceB: IDevice) => boolean;
37
40
  disConnectDevice: (target: IDevice) => {
38
41
  success: boolean;
@@ -41,6 +44,7 @@ export declare class LanHandle extends EventListener<TLanHandleEvent> {
41
44
  success: boolean;
42
45
  err: any;
43
46
  };
47
+ onData: (mac: string, hexString: string) => void;
44
48
  connectDevice: (device: any) => Promise<IResult<any>>;
45
49
  onError: (err: any) => void;
46
50
  isMatchPk: (pk: any) => boolean;
@@ -65,6 +69,7 @@ interface ITCPHandlerProps {
65
69
  mac: string;
66
70
  productKey: string;
67
71
  onClose: (ip: any) => void;
72
+ onData: (mac: string, data: string) => void;
68
73
  }
69
74
  declare class TCPHandler {
70
75
  private deviceTCPPort;
@@ -72,8 +77,9 @@ declare class TCPHandler {
72
77
  private productKey;
73
78
  private mac;
74
79
  private tcpHandler;
80
+ private onData;
75
81
  private onCloseListener;
76
- constructor({ ip, onClose, mac, productKey }: ITCPHandlerProps);
82
+ constructor({ ip, onClose, mac, productKey, onData }: ITCPHandlerProps);
77
83
  connect: () => Promise<IResult<any>>;
78
84
  write: (data: ArrayBuffer) => {
79
85
  success: boolean;
@@ -29,6 +29,10 @@ export default class GizwitsWS {
29
29
  _onBindingChanged?: IEmptyFn;
30
30
  constructor({ appID, token, uid, limitSocketNum, }: IGWSProps);
31
31
  init(): Promise<IError>;
32
+ updateToken: ({ token, uid }: {
33
+ token: any;
34
+ uid: any;
35
+ }) => void;
32
36
  _getDevice: (did: string) => IDevice | IError;
33
37
  _getConnect: (device: IDevice) => Connection;
34
38
  _getDeviceAndConnect: (did: string) => IError | [IDevice, Connection];
@@ -7,6 +7,7 @@ declare class ProductConfigFileManage {
7
7
  key: string;
8
8
  init: () => Promise<void>;
9
9
  getConfigFile: (pk: string) => Promise<IDataPointConfig>;
10
+ getAttrItem: (pk: string, key: string) => Promise<import("./protocol/DataPoint").IDataPointAttr>;
10
11
  }
11
12
  declare const productConfigFileManage: ProductConfigFileManage;
12
13
  export default productConfigFileManage;
@@ -60,4 +60,8 @@ declare const pack: (data: object, pk: string) => Promise<{
60
60
  };
61
61
  }>;
62
62
  declare const padBoradcastData: (data: number[]) => string;
63
- export { pack, unpack, padBoradcastData };
63
+ declare type TargetType = 'int' | 'string';
64
+ declare const formatEnum: (attrs: any, pk: string, targetType: TargetType) => Promise<any>;
65
+ declare type BTargetType = 'string' | 'object';
66
+ declare const formatBinary: (attrs: any, pk: string, targetType: BTargetType) => Promise<any>;
67
+ export { pack, unpack, padBoradcastData, formatEnum, formatBinary };
package/dist/src/sdk.d.ts CHANGED
@@ -112,6 +112,7 @@ declare class GizwitsMiniSDK {
112
112
  private handleBleError;
113
113
  private handleBleDevices;
114
114
  handleLanDevices: (devices: IDevice[]) => void;
115
+ private handleLanDeviceData;
115
116
  private handleBleDeviceData;
116
117
  private handleSocketDeviceData;
117
118
  private handleSocketStatus;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mini_program_gizwits_sdk",
3
- "version": "3.1.20",
4
- "description": "云端推送解除绑定的时候,移除m2m的订阅",
3
+ "version": "3.2.20-beta",
4
+ "description": "兼容协议解析拓展性,支持数组",
5
5
  "main": "./dist/index.js",
6
6
  "scripts": {
7
7
  "test": "node_modules/mocha/bin/_mocha -r ts-node/register tests/**/*.spec.ts -file ./tests/setup-wx.js -timeout 60s",
@@ -58,6 +58,7 @@ export type OnScanDevice = (scanList: IDevice[]) => void;
58
58
  interface ConnectDevice {
59
59
  deviceId?: string;
60
60
  serviceId?: string;
61
+ isLogin?: boolean;
61
62
  characteristicId?: string;
62
63
  errMsg?: string;
63
64
  }
@@ -108,9 +109,23 @@ export class BleHandle extends EventListener<TBleHandleEvent> {
108
109
  // this.globalListenerMap = globalListenerMap;
109
110
 
110
111
  this.listenDevOffline(offlineThreshold ?? 10000);
112
+ this.listenConnection();
111
113
 
112
114
  wx.onAppShow(this.onAppShow);
113
115
  wx.onAppHide(this.onAppHide);
116
+
117
+ wx.onBluetoothAdapterStateChange(this.onBluetoothAdapterStateChange)
118
+ }
119
+
120
+ private onBluetoothAdapterStateChange = (res: WechatMiniprogram.OnBluetoothAdapterStateChangeCallbackResult) => {
121
+ GizLog.debug("onBluetoothAdapterStateChange", res)
122
+ if (res.available) {
123
+ // this.startScan(() => {}, 4000);
124
+ } else {
125
+ // 不可用状态
126
+ this.scanList = [];
127
+ this.notifyDevices();
128
+ }
114
129
  }
115
130
 
116
131
  private onAppShow = () => {
@@ -241,7 +256,7 @@ export class BleHandle extends EventListener<TBleHandleEvent> {
241
256
  }
242
257
  }
243
258
 
244
- private listenConnection = (deviceId: string) => {
259
+ private listenConnection = () => {
245
260
  /**
246
261
  * 之前的写法会导致每订阅一个设备都会创建一次监听
247
262
  * 监听也不会销毁
@@ -250,22 +265,12 @@ export class BleHandle extends EventListener<TBleHandleEvent> {
250
265
  * 另一个角度来分析,蓝牙连接上之后,没有那么容易断开
251
266
  */
252
267
  wx.onBLEConnectionStateChange((res) => {
253
- const isInConnectedList = this.connectedList
254
- .map((d) => d.deviceId)
255
- .includes(deviceId);
256
-
257
- const shouldReConnect =
258
- isInConnectedList &&
259
- res.deviceId === deviceId &&
260
- res.connected === false;
261
- GizLog.log('蓝牙状态变化', {
262
- connectedList: this.connectedList,
263
- res,
264
- deviceId,
265
- });
266
-
267
- if (shouldReConnect) {
268
- this.connectDevice(deviceId);
268
+ if (res.connected === false) {
269
+ const index = this.connectedList.findIndex(item => item.deviceId === res.deviceId);
270
+ if (index !== -1) {
271
+ this.connectedList.splice(index, 1);
272
+ this.notifyDevices();
273
+ }
269
274
  }
270
275
  });
271
276
  };
@@ -502,19 +507,21 @@ export class BleHandle extends EventListener<TBleHandleEvent> {
502
507
  await this.disableScan();
503
508
  const connectRes = await retryConnect(deviceId)
504
509
  this.enableScan();
510
+ GizLog.debug("connect res", connectRes)
505
511
 
506
512
  // if (connectRes && connectRes.errCode == 0 && connectRes.errMsg === 'createBLEConnection:ok') {
507
513
  // this.listenConnection(deviceId);
508
514
  // }
509
515
  } catch (error) {
510
- // 已经连接,直接返回成功
516
+ // 已经连接,继续往下走登录流程
511
517
  GizLog.error('GIZ_SDK: createBLEConnection error', new Error(JSON.stringify(error)))
512
- if (error?.errMsg === 'createBLEConnection:fail:already connect') {
513
- return {success: true}
518
+ if (error?.errMsg !== 'createBLEConnection:fail:already connect') {
519
+ this.notifyError(error);
520
+ return { success: false, message: error?.errMsg };
514
521
  }
515
- this.notifyError(error);
516
- return { success: false, message: error?.errMsg };
522
+
517
523
  }
524
+
518
525
  const getServicesRes = (await wx
519
526
  .getBLEDeviceServices({ deviceId })
520
527
  .catch((err) => this.notifyError(err))) as any;
@@ -540,6 +547,9 @@ export class BleHandle extends EventListener<TBleHandleEvent> {
540
547
  .catch((err) => this.notifyError(err))) as any;
541
548
 
542
549
  const characteristicId = getCharRes?.characteristics[0].uuid!;
550
+
551
+
552
+ // 登录成功 把设备插入 connectedList
543
553
  const isNewDevice = this.connectedList.every(
544
554
  (device) => device.deviceId !== deviceId
545
555
  );
@@ -570,18 +580,20 @@ export class BleHandle extends EventListener<TBleHandleEvent> {
570
580
  this.notifyError(err)
571
581
  )) as any;
572
582
  if (loginData?.success) {
583
+ // 登录成功,标记成已经登陆
584
+ this.updateDeviceIsLogin(deviceId)
573
585
  return {
574
586
  success: true,
575
587
  message: 'ok',
576
588
  };
577
589
  }
578
- } else {
579
- // 绑定失败,异常退出断开蓝牙
580
- await wx.closeBLEConnection({
581
- deviceId,
582
- });
583
590
  }
584
591
 
592
+ // 绑定失败,异常退出断开蓝牙
593
+ await wx.closeBLEConnection({
594
+ deviceId,
595
+ });
596
+
585
597
  return {
586
598
  success: false,
587
599
  message: 'ok',
@@ -591,6 +603,13 @@ export class BleHandle extends EventListener<TBleHandleEvent> {
591
603
  }
592
604
  };
593
605
 
606
+ private updateDeviceIsLogin = (deviceId: string) => {
607
+ const index = this.connectedList.findIndex(item => item.deviceId === deviceId);
608
+ if (index !== -1) {
609
+ this.connectedList[index].isLogin = true;
610
+ }
611
+ }
612
+
594
613
  private handleBindReq = (deviceId: string) => {
595
614
  // 处理设备回复绑定消息
596
615
  return new Promise<IDeviceReq<string>>((res) => {
@@ -801,5 +820,6 @@ export class BleHandle extends EventListener<TBleHandleEvent> {
801
820
  this.listenDevOfflineTimer && clearInterval(this.listenDevOfflineTimer);
802
821
  wx.offAppShow(this.onAppShow);
803
822
  wx.offAppHide(this.onAppHide);
823
+ wx.offBluetoothAdapterStateChange(this.onBluetoothAdapterStateChange)
804
824
  }
805
825
  }
@@ -3,10 +3,10 @@ import Bind from "../protocol/Bind";
3
3
  import DiscoverUDP from "../protocol/DiscoverUdp";
4
4
  import Login from "../protocol/Login";
5
5
  import ProtocolBase from "../protocol/ProtocolBase";
6
- import { arrayToString, arrayToUint8 } from "../protocol/tool";
6
+ import { arrayToString, arrayToUint8, hexStrint2byte } from "../protocol/tool";
7
7
  import { IResult } from "../sdk";
8
8
  import sleep from "../sleep";
9
- import { ab2numbers } from "../utils";
9
+ import { ab2hex, ab2numbers } from "../utils";
10
10
  import EventListener from "./EventListener";
11
11
 
12
12
  // 局域网连接设备结构
@@ -22,9 +22,11 @@ interface ILanDevice extends IDevice {
22
22
  ip: string
23
23
  }
24
24
 
25
- type GizLanDeviceDataNotifications = (devices: IDevice[]) => void;
25
+ type GizLanDeviceListNotifications = (devices: IDevice[]) => void;
26
+ type GizLanDeviceDataNotifications = (mac: string, data: string) => void;
26
27
  export interface TLanHandleEvent {
27
- GizLanDeviceList: GizLanDeviceDataNotifications;
28
+ GizLanDeviceList: GizLanDeviceListNotifications;
29
+ GizLanDeviceData: GizLanDeviceDataNotifications
28
30
  }
29
31
 
30
32
 
@@ -53,15 +55,17 @@ export class LanHandle extends EventListener<TLanHandleEvent> {
53
55
  }
54
56
 
55
57
  init = () => {
56
- this.UDPSocket = wx.createUDPSocket();
57
- this.UDPSocket.bind();
58
- this.UDPSocket.onMessage(this.onMessage);
59
- this.UDPSocket.onError(this.onError);
60
- GizLog.info('Start UDP Discover')
61
- this.startDiscover();
62
- this.listenDevOffline(this.offlineThreshold);
63
- wx.onAppShow(this.onAppShow);
64
- wx.onAppHide(this.onAppHide);
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
+ }
65
69
  }
66
70
 
67
71
  private onAppShow = () => {
@@ -100,6 +104,13 @@ export class LanHandle extends EventListener<TLanHandleEvent> {
100
104
  });
101
105
  }
102
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
+ }
103
114
 
104
115
  isSameDevice = (deviceA: IDevice, deviceB: IDevice) => {
105
116
  return deviceA.mac === deviceB.mac && deviceA.productKey === deviceB.productKey
@@ -121,6 +132,10 @@ export class LanHandle extends EventListener<TLanHandleEvent> {
121
132
  }
122
133
  }
123
134
 
135
+ onData = (mac: string, hexString: string) => {
136
+ this.notifyDeviceData(mac, hexString);
137
+ }
138
+
124
139
  connectDevice = async (device) => {
125
140
  const target = this.scanList.find(item => this.isSameDevice(device, item));
126
141
  // 检查是否有连接
@@ -136,7 +151,7 @@ export class LanHandle extends EventListener<TLanHandleEvent> {
136
151
  success: true
137
152
  }
138
153
  } else {
139
- const tcpHandler = new TCPHandler({ip: target.ip, mac: target.mac, productKey: target.productKey, onClose: this.onDeviceClose})
154
+ const tcpHandler = new TCPHandler({ip: target.ip, mac: target.mac, productKey: target.productKey, onClose: this.onDeviceClose, onData: this.onData})
140
155
  const res = await tcpHandler.connect()
141
156
  if (res.success) {
142
157
  this.connectedList.push({
@@ -256,6 +271,7 @@ interface ITCPHandlerProps {
256
271
  mac: string
257
272
  productKey: string
258
273
  onClose: (ip) => void
274
+ onData: (mac: string, data: string) => void
259
275
  }
260
276
  class TCPHandler {
261
277
  private deviceTCPPort = 12416
@@ -263,13 +279,15 @@ class TCPHandler {
263
279
  private productKey = ""
264
280
  private mac = ""
265
281
  private tcpHandler: WechatMiniprogram.TCPSocket = null;
282
+ private onData = (mac: string, data: string) => {}
266
283
 
267
284
  private onCloseListener = ({mac, productKey}) => {}
268
285
 
269
- constructor({ip, onClose, mac, productKey}: ITCPHandlerProps) {
286
+ constructor({ip, onClose, mac, productKey, onData}: ITCPHandlerProps) {
270
287
  this.ip = ip;
271
288
  this.mac = mac;
272
289
  this.productKey = productKey;
290
+ this.onData = onData;
273
291
  this.onCloseListener = onClose;
274
292
  }
275
293
 
@@ -360,8 +378,14 @@ class TCPHandler {
360
378
  }
361
379
 
362
380
  onMessage = (message) => {
363
- const baseProtocol = new ProtocolBase(ab2numbers(message.message))
364
- GizLog.debug("on tcp message", baseProtocol)
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
+ }
365
389
  }
366
390
 
367
391
  onError = () => {}
@@ -70,6 +70,11 @@ export default class GizwitsWS {
70
70
  }
71
71
  }
72
72
 
73
+ updateToken = ({token, uid}) => {
74
+ this.token = token;
75
+ this.uid = uid;
76
+ }
77
+
73
78
  _getDevice = (did: string): IDevice | IError => {
74
79
  if (this._bindingDevices === null) {
75
80
  return errorCode.GIZ_SDK_SDK_NOT_INITIALIZED
@@ -36,6 +36,20 @@ class ProductConfigFileManage {
36
36
  }
37
37
  return this.data[pk];
38
38
  }
39
+
40
+ public getAttrItem = async (pk:string, key: string) => {
41
+ if (!this.data[pk]) {
42
+ await this.getConfigFile(pk)
43
+ }
44
+
45
+ for (let index in this.data[pk].entities[0].attrs) {
46
+ const item = this.data[pk].entities[0].attrs[index]
47
+ if (item.name === key) {
48
+ return item;
49
+ }
50
+ }
51
+ return null;
52
+ }
39
53
  }
40
54
 
41
55
  const productConfigFileManage = new ProductConfigFileManage();
@@ -1,5 +1,5 @@
1
1
  import productConfigFileManage from "../productConfigFileManage";
2
- import { arrayToString, checkHeader, fillString, getProtocolLen } from "./tool";
2
+ import { arrayToString, checkHeader, fillString, getProtocolLen, hexStrint2byte } from "./tool";
3
3
 
4
4
  export interface IDataPointConfig {
5
5
  name: string;
@@ -89,7 +89,12 @@ const getData = (type: TDataType, data: any, item: IDataPointAttr) => {
89
89
  break;
90
90
  }
91
91
  case 'binary': {
92
- const value = data[item.name] || '';
92
+ // 做个兼容,兼容16进制字符串和数组
93
+ let value = data[item.name] || '';
94
+ if (value instanceof Array) {
95
+ // 数组转string
96
+ value = arrayToString(value);
97
+ }
93
98
  // 数量不够在后面补0
94
99
  const newValue = fillString(value, item.position.len * 2, true);
95
100
  for (let i = 0; i < newValue.length / 2; i++) {
@@ -668,7 +673,12 @@ function calculationCmdData({ attrs, flagString, boolEnumLength, cmdData, accumu
668
673
  const unpack = async (data: number[], pk: string) => {
669
674
  const productConfig = await productConfigFileManage.getConfigFile(pk);
670
675
  if (productConfig) {
671
- return PToDataPoint({config: (await productConfig).entities[0].attrs, data})
676
+ const resData = PToDataPoint({config: (await productConfig).entities[0].attrs, data})
677
+ if (resData.kvData) {
678
+ // 格式化binary
679
+ resData.kvData = await formatBinary(resData?.kvData, pk, 'object');
680
+ }
681
+ return resData;
672
682
  }
673
683
  return null;
674
684
  }
@@ -691,5 +701,39 @@ const padBoradcastData = (data: number[]) => {
691
701
  return arrayToString(head.concat(lengthArr).concat(flagToEnd));
692
702
  }
693
703
 
704
+ type TargetType = 'int' | 'string';
705
+ // 格式化enum 兼容sdk 和 socket 之间的差异
706
+ const formatEnum = async (attrs: any, pk: string, targetType: TargetType ) => {
707
+ for(let key in attrs) {
708
+ const itemData = await productConfigFileManage.getAttrItem(pk, key);
709
+ if (itemData && itemData.data_type ==='enum') {
710
+ if (targetType === 'int' && typeof attrs[key] === 'string') {
711
+ // 转成int
712
+ const enumIndex = itemData.enum.findIndex(item => item === attrs[key]);
713
+ attrs[key] = enumIndex || 0;
714
+ }
715
+ if (targetType === 'string' && typeof attrs[key] === 'number') {
716
+ attrs[key] = itemData.enum[attrs[key]]
717
+ }
718
+ }
719
+ }
720
+ return attrs
721
+ }
722
+
723
+ type BTargetType = 'string' | 'object';
724
+ const formatBinary = async (attrs: any, pk: string, targetType: BTargetType ) => {
725
+ for(let key in attrs) {
726
+ const itemData = await productConfigFileManage.getAttrItem(pk, key);
727
+ if (itemData && itemData.data_type ==='binary') {
728
+ if (targetType === 'object' && typeof attrs[key] === 'string') {
729
+ attrs[key] = hexStrint2byte(attrs[key])
730
+ }
731
+ if (targetType === 'string' && typeof attrs[key] === 'object') {
732
+ attrs[key] = arrayToString(attrs[key])
733
+ }
734
+ }
735
+ }
736
+ return attrs
737
+ }
694
738
 
695
- export {pack, unpack, padBoradcastData};
739
+ export {pack, unpack, padBoradcastData, formatEnum, formatBinary};
package/src/sdk.ts CHANGED
@@ -25,7 +25,7 @@ import {
25
25
  numberArray2Uint8Array,
26
26
  } from './utils';
27
27
  import productConfigFileManage from './productConfigFileManage';
28
- import { IDataPointConfig, pack, unpack } from './protocol/DataPoint';
28
+ import { formatEnum, IDataPointConfig, pack, unpack } from './protocol/DataPoint';
29
29
  import { hexStrint2byte } from './protocol/tool';
30
30
  import { LanHandle } from './handler/lan';
31
31
 
@@ -182,7 +182,7 @@ class GizwitsMiniSDK {
182
182
  const index = newDevices.findIndex(
183
183
  (device) => device.bleDeviceID === item.deviceId
184
184
  );
185
- if (index !== -1) {
185
+ if (index !== -1 && item.isLogin) {
186
186
  newDevices[index].connectType = 'BLE';
187
187
  }
188
188
  });
@@ -326,12 +326,30 @@ class GizwitsMiniSDK {
326
326
  private handleBleDevices = (devices: IDevice[]) => {
327
327
  GizLog.log('handleBleDevices', devices);
328
328
  this.bleDevices = devices;
329
+ // this.notiDeviceList();
329
330
  };
330
331
 
331
332
  handleLanDevices = (devices: IDevice[]) => {
332
333
  this.lanDevices = devices;
333
334
  }
334
335
 
336
+ private handleLanDeviceData = async (mac: string, hexString: string) => {
337
+ const target = this.allDevices.find(
338
+ (item) => item.mac === mac
339
+ );
340
+ if (target) {
341
+ const data = await unpack(hexStrint2byte(hexString), target.productKey);
342
+ // 如果有kydata 则上报
343
+ if (data?.kvData && this.listenerMap['GizDeviceAttrsNotifications']) {
344
+ this.listenerMap['GizDeviceAttrsNotifications'].map((item) => {
345
+ item({
346
+ device: target,
347
+ data: data?.kvData,
348
+ });
349
+ });
350
+ }
351
+ }
352
+ }
335
353
  private handleBleDeviceData = async (
336
354
  curDevice: WechatMiniprogram.OnBLECharacteristicValueChangeCallbackResult,
337
355
  hexString: string
@@ -353,7 +371,7 @@ class GizwitsMiniSDK {
353
371
  }
354
372
  };
355
373
 
356
- private handleSocketDeviceData: IOnDeviceStatusChanged = (data) => {
374
+ private handleSocketDeviceData: IOnDeviceStatusChanged = async (data) => {
357
375
  const target = this.allDevices.find((item) => item.did === data.did);
358
376
  if (!target) {
359
377
  return;
@@ -364,6 +382,11 @@ class GizwitsMiniSDK {
364
382
  }
365
383
 
366
384
  if (this.listenerMap['GizDeviceAttrsNotifications']) {
385
+
386
+ // 需要把枚举 拓展格式化回来
387
+ if ((data as IDeviceStatusChangedProps).attrs) {
388
+ (data as IDeviceStatusChangedProps).attrs = await formatEnum((data as IDeviceStatusChangedProps).attrs, target.productKey, 'int');
389
+ }
367
390
  this.listenerMap['GizDeviceAttrsNotifications'].map((item) => {
368
391
  item({
369
392
  device: target,
@@ -395,8 +418,16 @@ class GizwitsMiniSDK {
395
418
  };
396
419
 
397
420
  private initLan = () => {
398
- this.lanHandle.init();
399
- this.lanHandle.addEventListener('GizLanDeviceList', this.handleLanDevices);
421
+ try {
422
+ this.lanHandle.init();
423
+ this.lanHandle.addEventListener('GizLanDeviceList', this.handleLanDevices);
424
+ this.lanHandle.addEventListener(
425
+ 'GizLanDeviceData',
426
+ this.handleLanDeviceData
427
+ );
428
+ } catch (error) {
429
+ GizLog.error('init lan error', error)
430
+ }
400
431
  }
401
432
 
402
433
  /**
@@ -532,7 +563,10 @@ class GizwitsMiniSDK {
532
563
  }
533
564
  }
534
565
  case 'WAN': {
535
- return this.socketHandle.writeData(target, attrs);
566
+ // 枚举要转换成枚举值
567
+ const newAttrs = await formatEnum(attrs, target.productKey, 'string')
568
+
569
+ return this.socketHandle.writeData(target, newAttrs);
536
570
  }
537
571
  case 'LAN': {
538
572
  return this.lanHandle.write(target, numberArray2Uint8Array(value).buffer);
@@ -609,6 +643,7 @@ class GizwitsMiniSDK {
609
643
  if (data.success) {
610
644
  setGlobalData('token', data.data?.token);
611
645
  setGlobalData('uid', data.data?.uid);
646
+ this.socketHandle.updateToken({token: data.data.token, uid: data.data.uid})
612
647
  }
613
648
  return data;
614
649
  };
@@ -655,7 +690,8 @@ class GizwitsMiniSDK {
655
690
  let target = this.allDevices.find((item) => isSameDevice(item, device));
656
691
  if (!target) {
657
692
  GizLog.warn('Device Target Not Found')
658
- return { success: false };
693
+ GizLog.warn('current devices ', this.allDevices)
694
+ return { success: false, message: "device not found" };
659
695
  }
660
696
 
661
697
  // 广播模式不需要连接,所以默认是连接状态
package/src/utils.ts CHANGED
@@ -188,6 +188,8 @@ export const merageBleLocalDevices = (
188
188
  } else {
189
189
  target.isBleOnline = true;
190
190
  target.bleDeviceID = localD.bleDeviceID;
191
+ // 蓝牙设备比较特殊,有广播类型 广播类型是写在本地设备属性里面的,但是又不属于已经连接的设备,先骚处理一下
192
+ target.connectType = localD.connectType;
191
193
  target.bleWorkStatus = localD.bleWorkStatus;
192
194
  }
193
195
  });