mini_program_gizwits_sdk 3.1.20 → 3.2.1
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/handler/ble.d.ts +1 -3
- package/dist/src/handler/lan.d.ts +9 -3
- package/dist/src/handler/socket.d.ts +4 -0
- package/dist/src/productConfigFileManage.d.ts +1 -0
- package/dist/src/protocol/DataPoint.d.ts +5 -1
- package/dist/src/sdk.d.ts +1 -0
- package/package.json +2 -2
- package/src/handler/ble.ts +40 -28
- package/src/handler/lan.ts +41 -17
- package/src/handler/socket.ts +5 -0
- package/src/productConfigFileManage.ts +14 -0
- package/src/protocol/dataPoint.ts +48 -4
- package/src/sdk.ts +42 -6
- package/src/utils.ts +2 -0
- package/src/wechatApi.ts +1 -1
|
@@ -49,6 +49,7 @@ export declare class BleHandle extends EventListener<TBleHandleEvent> {
|
|
|
49
49
|
private isActive;
|
|
50
50
|
scanList: IDevice[];
|
|
51
51
|
constructor({ pks, bleHandleParams, offlineThreshold, }: ConstructorParams);
|
|
52
|
+
private onBluetoothAdapterStateChange;
|
|
52
53
|
private onAppShow;
|
|
53
54
|
private onAppHide;
|
|
54
55
|
private listenDevOffline;
|
|
@@ -84,9 +85,6 @@ export declare class BleHandle extends EventListener<TBleHandleEvent> {
|
|
|
84
85
|
disableScan: () => Promise<WechatMiniprogram.BluetoothError>;
|
|
85
86
|
enableScan: () => void;
|
|
86
87
|
connectDevice: (deviceId: string) => Promise<{
|
|
87
|
-
success: boolean;
|
|
88
|
-
message?: undefined;
|
|
89
|
-
} | {
|
|
90
88
|
success: boolean;
|
|
91
89
|
message: any;
|
|
92
90
|
}>;
|
|
@@ -10,9 +10,11 @@ interface ILanDevice extends IDevice {
|
|
|
10
10
|
expandData: DiscoverUDP;
|
|
11
11
|
ip: string;
|
|
12
12
|
}
|
|
13
|
-
declare type
|
|
13
|
+
declare type GizLanDeviceListNotifications = (devices: IDevice[]) => void;
|
|
14
|
+
declare type GizLanDeviceDataNotifications = (mac: string, data: string) => void;
|
|
14
15
|
export interface TLanHandleEvent {
|
|
15
|
-
GizLanDeviceList:
|
|
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
|
-
|
|
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
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "3.2.1",
|
|
4
|
+
"description": "优化扫描策略,根据UUID过滤",
|
|
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",
|
package/src/handler/ble.ts
CHANGED
|
@@ -108,9 +108,23 @@ export class BleHandle extends EventListener<TBleHandleEvent> {
|
|
|
108
108
|
// this.globalListenerMap = globalListenerMap;
|
|
109
109
|
|
|
110
110
|
this.listenDevOffline(offlineThreshold ?? 10000);
|
|
111
|
+
this.listenConnection();
|
|
111
112
|
|
|
112
113
|
wx.onAppShow(this.onAppShow);
|
|
113
114
|
wx.onAppHide(this.onAppHide);
|
|
115
|
+
|
|
116
|
+
wx.onBluetoothAdapterStateChange(this.onBluetoothAdapterStateChange)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
private onBluetoothAdapterStateChange = (res: WechatMiniprogram.OnBluetoothAdapterStateChangeCallbackResult) => {
|
|
120
|
+
GizLog.debug("onBluetoothAdapterStateChange", res)
|
|
121
|
+
if (res.available) {
|
|
122
|
+
// this.startScan(() => {}, 4000);
|
|
123
|
+
} else {
|
|
124
|
+
// 不可用状态
|
|
125
|
+
this.scanList = [];
|
|
126
|
+
this.notifyDevices();
|
|
127
|
+
}
|
|
114
128
|
}
|
|
115
129
|
|
|
116
130
|
private onAppShow = () => {
|
|
@@ -241,7 +255,7 @@ export class BleHandle extends EventListener<TBleHandleEvent> {
|
|
|
241
255
|
}
|
|
242
256
|
}
|
|
243
257
|
|
|
244
|
-
private listenConnection = (
|
|
258
|
+
private listenConnection = () => {
|
|
245
259
|
/**
|
|
246
260
|
* 之前的写法会导致每订阅一个设备都会创建一次监听
|
|
247
261
|
* 监听也不会销毁
|
|
@@ -250,22 +264,12 @@ export class BleHandle extends EventListener<TBleHandleEvent> {
|
|
|
250
264
|
* 另一个角度来分析,蓝牙连接上之后,没有那么容易断开
|
|
251
265
|
*/
|
|
252
266
|
wx.onBLEConnectionStateChange((res) => {
|
|
253
|
-
|
|
254
|
-
.
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
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);
|
|
267
|
+
if (res.connected === false) {
|
|
268
|
+
const index = this.connectedList.findIndex(item => item.deviceId === res.deviceId);
|
|
269
|
+
if (index !== -1) {
|
|
270
|
+
this.connectedList.splice(index, 1);
|
|
271
|
+
this.notifyDevices();
|
|
272
|
+
}
|
|
269
273
|
}
|
|
270
274
|
});
|
|
271
275
|
};
|
|
@@ -456,9 +460,10 @@ export class BleHandle extends EventListener<TBleHandleEvent> {
|
|
|
456
460
|
|
|
457
461
|
GizLog.debug("GIZ_SDK: start scan", delay);
|
|
458
462
|
await wx.startBluetoothDevicesDiscovery({
|
|
463
|
+
services: ['ABF8', 'ABF7'],
|
|
459
464
|
powerLevel: 'high',
|
|
460
465
|
allowDuplicatesKey: true,
|
|
461
|
-
interval:
|
|
466
|
+
interval: 200,
|
|
462
467
|
});
|
|
463
468
|
|
|
464
469
|
if (delay > 0) {
|
|
@@ -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
|
|
513
|
-
|
|
518
|
+
if (error?.errMsg !== 'createBLEConnection:fail:already connect') {
|
|
519
|
+
this.notifyError(error);
|
|
520
|
+
return { success: false, message: error?.errMsg };
|
|
514
521
|
}
|
|
515
|
-
|
|
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,19 @@ export class BleHandle extends EventListener<TBleHandleEvent> {
|
|
|
570
580
|
this.notifyError(err)
|
|
571
581
|
)) as any;
|
|
572
582
|
if (loginData?.success) {
|
|
583
|
+
|
|
573
584
|
return {
|
|
574
585
|
success: true,
|
|
575
586
|
message: 'ok',
|
|
576
587
|
};
|
|
577
588
|
}
|
|
578
|
-
} else {
|
|
579
|
-
// 绑定失败,异常退出断开蓝牙
|
|
580
|
-
await wx.closeBLEConnection({
|
|
581
|
-
deviceId,
|
|
582
|
-
});
|
|
583
589
|
}
|
|
584
590
|
|
|
591
|
+
// 绑定失败,异常退出断开蓝牙
|
|
592
|
+
await wx.closeBLEConnection({
|
|
593
|
+
deviceId,
|
|
594
|
+
});
|
|
595
|
+
|
|
585
596
|
return {
|
|
586
597
|
success: false,
|
|
587
598
|
message: 'ok',
|
|
@@ -801,5 +812,6 @@ export class BleHandle extends EventListener<TBleHandleEvent> {
|
|
|
801
812
|
this.listenDevOfflineTimer && clearInterval(this.listenDevOfflineTimer);
|
|
802
813
|
wx.offAppShow(this.onAppShow);
|
|
803
814
|
wx.offAppHide(this.onAppHide);
|
|
815
|
+
wx.offBluetoothAdapterStateChange(this.onBluetoothAdapterStateChange)
|
|
804
816
|
}
|
|
805
817
|
}
|
package/src/handler/lan.ts
CHANGED
|
@@ -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
|
|
25
|
+
type GizLanDeviceListNotifications = (devices: IDevice[]) => void;
|
|
26
|
+
type GizLanDeviceDataNotifications = (mac: string, data: string) => void;
|
|
26
27
|
export interface TLanHandleEvent {
|
|
27
|
-
GizLanDeviceList:
|
|
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
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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
|
-
|
|
364
|
-
|
|
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 = () => {}
|
package/src/handler/socket.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
|
|
@@ -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
|
-
|
|
399
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
});
|