mini_program_gizwits_sdk 3.0.6 → 3.1.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.
@@ -0,0 +1,97 @@
1
+ import { wrapErrorInfo } from './utils';
2
+ import Sdk from './Sdk';
3
+ interface BleHandleParams {
4
+ serviceUUIDSuffix: string;
5
+ characteristicUUIDSuffix: string;
6
+ }
7
+ export interface IStartScanResult {
8
+ scanList: IDevice[];
9
+ err?: ReturnType<typeof wrapErrorInfo>;
10
+ }
11
+ export declare type OnScanDevice = (scanList: IDevice[]) => void;
12
+ interface ConnectDevice {
13
+ deviceId?: string;
14
+ serviceId?: string;
15
+ characteristicId?: string;
16
+ errMsg?: string;
17
+ }
18
+ interface ConstructorParams {
19
+ pks: string[];
20
+ bleHandleParams?: BleHandleParams;
21
+ globalListenerMap: object;
22
+ gizSdk: Sdk;
23
+ offlineThreshold?: number;
24
+ }
25
+ export declare class BleHandle {
26
+ private serviceUUIDSuffix;
27
+ connectedList: ConnectDevice[];
28
+ private pks;
29
+ private tmpBleData;
30
+ private tmpDataNum;
31
+ private listenerMap;
32
+ private globalListenerMap;
33
+ scanList: IDevice[];
34
+ private gizSdk;
35
+ constructor({ pks, bleHandleParams, globalListenerMap, gizSdk, offlineThreshold, }: ConstructorParams);
36
+ private listenDevOffline;
37
+ private openBluetoothAdapter;
38
+ private handleOnBLECharacteristicValueChange;
39
+ private notifyError;
40
+ private listenConnection;
41
+ checkPermission: () => Promise<{
42
+ data: {
43
+ discovering: boolean;
44
+ };
45
+ err: {
46
+ errorCode: string;
47
+ errorMessage: string;
48
+ };
49
+ success: boolean;
50
+ }>;
51
+ private getBluetoothAdapterState;
52
+ getDeviceStatus: (deviceId: string, productKey: string, gwDid?: string, attrNames?: string[]) => Promise<import("./wechatApi").IWechatResult | {
53
+ success: boolean;
54
+ message: string;
55
+ errbody: {
56
+ serviceId: string;
57
+ characteristicId: string;
58
+ };
59
+ } | {
60
+ success: boolean;
61
+ }>;
62
+ private handleBluetoothDeviceFound;
63
+ startScan: (onScanDevice: OnScanDevice, delay?: number) => Promise<IStartScanResult>;
64
+ stopScan: () => Promise<{
65
+ errMsg: string;
66
+ errCode: number;
67
+ }>;
68
+ connectDevice: (deviceId: string) => Promise<{
69
+ success: boolean;
70
+ message: any;
71
+ }>;
72
+ private handleBindReq;
73
+ private handleLoginReq;
74
+ removeDeviceListener: (deviceId: string, onDeviceStatusChange: any) => {
75
+ message: string;
76
+ success: boolean;
77
+ };
78
+ addDeviceListener: (deviceId: any, onDeviceStatusChange: (curDevice: WechatMiniprogram.OnBLECharacteristicValueChangeCallbackResult, hexString: string) => void) => Promise<{
79
+ success: boolean;
80
+ message: string;
81
+ }>;
82
+ write: (deviceId: string, value: ArrayBuffer) => Promise<import("./wechatApi").IWechatResult | {
83
+ success: boolean;
84
+ message: string;
85
+ errbody: {
86
+ serviceId: string;
87
+ characteristicId: string;
88
+ };
89
+ }>;
90
+ disConnectDevice: (deviceId: string) => Promise<{
91
+ err: string;
92
+ success: boolean;
93
+ }>;
94
+ private removeDevById;
95
+ private getTargetDevice;
96
+ }
97
+ export {};
@@ -5,7 +5,7 @@ declare class ProtocolBase {
5
5
  cmd: string;
6
6
  content: number[];
7
7
  constructor(data?: number[]);
8
- decodeLen: (arr: any) => number;
8
+ static decodeLen: (arr: any) => number;
9
9
  formatP0: (data: any) => void;
10
10
  }
11
11
  export default ProtocolBase;
@@ -0,0 +1,166 @@
1
+ import { ILoginRes } from './services/login';
2
+ import { IOpenApiDevice, ISafeRegisterReturn, IUnbindReturn } from './services/devices';
3
+ import { IDataPointConfig } from './protocol/DataPoint';
4
+ interface ISDKResult<T> {
5
+ data?: T;
6
+ err?: IError;
7
+ success: boolean;
8
+ }
9
+ export interface ISetCommonDeviceOnboardingDeployProps {
10
+ ssid: string;
11
+ password: string;
12
+ timeout: number;
13
+ isBind?: boolean;
14
+ mode?: 0 | 3;
15
+ softAPSSIDPrefix?: string;
16
+ }
17
+ export interface ISetDeviceOnboardingDeployProps extends ISetCommonDeviceOnboardingDeployProps {
18
+ softAPSSIDPrefix: string;
19
+ }
20
+ export interface IRandomCodesResult {
21
+ random_code: string;
22
+ product_key: string;
23
+ mac: string;
24
+ did: string;
25
+ user_id: string;
26
+ timestamp: number;
27
+ type: string;
28
+ wifi_soft_ver: string;
29
+ lan_proto_ver: string;
30
+ }
31
+ interface IBindRemoteDeviceParams {
32
+ productKey: string;
33
+ mac: string;
34
+ beOwner?: boolean;
35
+ alias?: string;
36
+ }
37
+ interface IDeviceStatusNoti {
38
+ device: IDevice;
39
+ connectType: TConnectType;
40
+ }
41
+ interface IDeviceAttrsNoti {
42
+ device: IDevice;
43
+ data: object;
44
+ }
45
+ export interface TListenerType {
46
+ GizDeviceListNotifications: TDeviceListNotifications;
47
+ GizDeviceStatusNotifications: TDeviceStatusNotifications;
48
+ GizDeviceAttrsNotifications: TDeviceAttrsNotifications;
49
+ onScanListChange: OnScanListChange;
50
+ onBleHandleError: OnBleHandleError;
51
+ }
52
+ declare type TDeviceListNotifications = (devices: IDevice[]) => void;
53
+ declare type TDeviceStatusNotifications = (data: IDeviceStatusNoti) => void;
54
+ declare type TDeviceAttrsNotifications = (data: IDeviceAttrsNoti) => void;
55
+ declare type OnScanListChange = (devices: IDevice[]) => void;
56
+ declare type OnBleHandleError = (error: IError) => void;
57
+ interface IDeviceSafetyRegisterParams {
58
+ mac: string;
59
+ productKey: string;
60
+ }
61
+ export interface IResult<T> {
62
+ success: boolean;
63
+ data?: T;
64
+ err?: IError;
65
+ }
66
+ export declare type TimeoutHandle = ReturnType<typeof setTimeout> | null;
67
+ export declare type IntervalHandle = ReturnType<typeof setInterval> | null;
68
+ export interface IProductInfo {
69
+ productKey: string;
70
+ productSecret: string;
71
+ }
72
+ interface GizwitsSdkOption {
73
+ appID: string;
74
+ appSecret: string;
75
+ productInfo: IProductInfo[];
76
+ cloudServiceInfo?: any;
77
+ token?: string;
78
+ uid?: string;
79
+ offlineThreshold?: number;
80
+ }
81
+ declare class SDK {
82
+ private version;
83
+ private listenerMap;
84
+ private _deviceList;
85
+ private get devieList();
86
+ private set devieList(value);
87
+ private _bleDevices;
88
+ get bleDevices(): IDevice[];
89
+ set bleDevices(data: IDevice[]);
90
+ private _udpDevices;
91
+ private get udpDevices();
92
+ private set udpDevices(value);
93
+ private get allDevices();
94
+ private set allDevice(value);
95
+ private get bleScanDevice();
96
+ private productInfo;
97
+ private currentWifiConfigHandle;
98
+ private _bleHandle;
99
+ private autoScanInterval;
100
+ private _gizSocket;
101
+ private offlineThreshold?;
102
+ constructor({ appID, appSecret, productInfo, cloudServiceInfo, token, uid, offlineThreshold, }: GizwitsSdkOption);
103
+ private get bleHandle();
104
+ private get socketHandle();
105
+ get specialProductKeys(): string[];
106
+ get specialProductKeySecrets(): string[];
107
+ private handleBleDeviceData;
108
+ private handleSockerDeviceData;
109
+ private notiDeviveList;
110
+ startAutoScan: () => void;
111
+ stopAutoScan: () => void;
112
+ setDeviceMeta: <K extends "name" | "mac" | "productKey" | "did" | "isBind" | "rootDeviceId" | "bleDeviceID" | "remark" | "connectType" | "isOnline" | "isLanOnline" | "isBleOnline" | "host" | "wss_port" | "ctime">(curDev: IDevice, key: K, value: IDevice[K]) => void;
113
+ getVersion: () => {
114
+ success: boolean;
115
+ data: string;
116
+ };
117
+ initBle: () => Promise<ISDKResult<null>>;
118
+ scanBleDevice: (delay: number) => Promise<ISDKResult<IDevice[]>>;
119
+ write: (device: IDevice, attrs: object) => Promise<{
120
+ success: boolean;
121
+ message: string;
122
+ }>;
123
+ getProductConfig: (pk: string) => Promise<ISDKResult<IDataPointConfig>>;
124
+ stopScanBleDevice: () => void;
125
+ login: (openID: string) => Promise<ISDKResult<ILoginRes>>;
126
+ getDeviceStatus: (device: IDevice, attrNames?: string[]) => Promise<import("./wechatApi").IWechatResult | {
127
+ success: boolean;
128
+ } | {
129
+ success: boolean;
130
+ message: string;
131
+ }>;
132
+ subscribe: (device: IDevice, connectType?: TConnectType, _autoGetDeviceStatus?: boolean) => Promise<{
133
+ success: boolean;
134
+ message: any;
135
+ } | {
136
+ success: boolean;
137
+ errorCode: IError;
138
+ } | {
139
+ success: boolean;
140
+ }>;
141
+ unSubscribe: (device: IDevice) => Promise<{
142
+ err: string;
143
+ success: boolean;
144
+ } | {
145
+ success: boolean;
146
+ }>;
147
+ bindRemoteDevice: ({ mac, productKey, alias, beOwner, }: IBindRemoteDeviceParams) => Promise<ISDKResult<IOpenApiDevice>>;
148
+ unbindDevice: ({ devices, }: {
149
+ devices: IDevice[];
150
+ }) => Promise<ISDKResult<IUnbindReturn>>;
151
+ deviceSafetyRegister: ({ mac, productKey, }: IDeviceSafetyRegisterParams) => Promise<ISDKResult<ISafeRegisterReturn>>;
152
+ deviceSafetyUnbind: ({ mac, productKey, }: IDeviceSafetyRegisterParams) => Promise<ISDKResult<ISafeRegisterReturn>>;
153
+ setDeviceOnboardingDeploy: ({ ssid, password, mode, timeout, isBind, softAPSSIDPrefix, }: ISetDeviceOnboardingDeployProps) => Promise<ISDKResult<IDevice[]>>;
154
+ private setDomain;
155
+ private deviceSafetyApi;
156
+ getBindingList: () => Promise<void>;
157
+ getDevices: () => Promise<ISDKResult<IDevice[]>>;
158
+ stopDeviceOnboardingDeploy: () => void;
159
+ removeEventListener: <K extends "onBleHandleError" | "GizDeviceListNotifications" | "GizDeviceStatusNotifications" | "GizDeviceAttrsNotifications" | "onScanListChange">(type: K, func: TListenerType[K]) => {
160
+ success: boolean;
161
+ };
162
+ addEventListener: <K extends "onBleHandleError" | "GizDeviceListNotifications" | "GizDeviceStatusNotifications" | "GizDeviceAttrsNotifications" | "onScanListChange">(type: K, func: TListenerType[K]) => Promise<{
163
+ success: boolean;
164
+ }>;
165
+ }
166
+ export default SDK;
@@ -2,7 +2,7 @@ interface IBluetoothAdapterStateResult {
2
2
  discovering: boolean;
3
3
  available: boolean;
4
4
  }
5
- interface IWechatResult {
5
+ export interface IWechatResult {
6
6
  errMsg: string;
7
7
  errCode: number;
8
8
  }
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "mini_program_gizwits_sdk",
3
- "version": "3.0.6",
4
- "description": "兼容模组重置,蓝牙卡死的情况",
3
+ "version": "3.1.0",
4
+ "description": "更新文档,修正ps字段",
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",
8
8
  "testMy": "node_modules/mocha/bin/_mocha -r ts-node/register tests/utils/productConfigFileManage.spec.ts -file ./tests/setup-wx.js -timeout 60s",
9
9
  "testBle": "node_modules/mocha/bin/_mocha -r ts-node/register tests/bleConfig/sdk.ble.spec.ts -file ./tests/setup-wx.js -timeout 60s",
10
10
  "build": "rm -r ./dist && webpack",
11
- "release": "release-it",
11
+ "release": "npm run build && release-it",
12
12
  "cover": "istanbul cover node_modules/mocha/bin/_mocha -- -r ts-node/register tests/**/*.spec.ts -file ./tests/setup-wx.js -timeout 60s",
13
13
  "coveralls": "npm run cover -- --report lcovonly && cat ./coverage/lcov.info | coveralls"
14
14
  },
package/src/ble.ts CHANGED
@@ -2,6 +2,7 @@ import {
2
2
  ab2hex,
3
3
  BTDevice2GDevice,
4
4
  numberArray2Uint8Array,
5
+ unionBy,
5
6
  wrapErrorInfo,
6
7
  } from './utils';
7
8
  import { unpackWriteBLECharacteristicValue } from './wechatApi';
@@ -30,7 +31,7 @@ interface IDeviceReq<T> {
30
31
 
31
32
  // export type BleHandle = ReturnType<typeof createBleHandle>;
32
33
 
33
- type OnScanDevice = (scanList: IDevice[]) => void;
34
+ export type OnScanDevice = (scanList: IDevice[]) => void;
34
35
  interface ConnectDevice {
35
36
  deviceId?: string;
36
37
  serviceId?: string;
@@ -75,7 +76,7 @@ export class BleHandle {
75
76
  this.globalListenerMap = globalListenerMap;
76
77
  this.gizSdk = gizSdk;
77
78
 
78
- this.listenDevOffline(offlineThreshold ?? 10000);
79
+ // this.listenDevOffline(offlineThreshold ?? 10000);
79
80
  }
80
81
 
81
82
  private listenDevOffline = (offlineThreshold: number) => {
@@ -117,7 +118,7 @@ export class BleHandle {
117
118
  const hexString = ab2hex(curDevice.value);
118
119
  try {
119
120
  const data = new ProtocolBase(hexStrint2byte(hexString));
120
- console.log('ProtocolBase', data.len, curDevice.value.byteLength,data);
121
+ console.log('ProtocolBase', data.len, curDevice.value.byteLength, data);
121
122
  // 能解析包头,覆盖数据
122
123
  this.tmpBleData = hexString;
123
124
  this.tmpDataNum =
@@ -240,7 +241,7 @@ export class BleHandle {
240
241
  resData: WechatMiniprogram.OnBluetoothDeviceFoundCallbackResult,
241
242
  onScanDevice: OnScanDevice
242
243
  ) => {
243
- const list = resData.devices.filter((device) => {
244
+ const filterList = unionBy(resData.devices, 'deviceId').filter((device) => {
244
245
  const { advertisData, deviceId, name, localName } = device;
245
246
  const advertisDataStr = ab2hex(advertisData);
246
247
 
@@ -260,8 +261,8 @@ export class BleHandle {
260
261
  }
261
262
  });
262
263
 
263
- if (list.length > 0) {
264
- const gDevice = list.map((item) => BTDevice2GDevice(item, this.pks));
264
+ if (filterList.length > 0) {
265
+ const gDevice = filterList.map((item) => BTDevice2GDevice(item, this.pks));
265
266
  this.scanList.push(...gDevice);
266
267
  onScanDevice(this.scanList);
267
268
  }
@@ -279,6 +280,7 @@ export class BleHandle {
279
280
  await wx.startBluetoothDevicesDiscovery({
280
281
  powerLevel: 'high',
281
282
  allowDuplicatesKey: true,
283
+ interval: 500,
282
284
  });
283
285
 
284
286
  setTimeout(() => {
@@ -13,7 +13,7 @@ class ProtocolBase {
13
13
  }
14
14
  }
15
15
 
16
- decodeLen = (arr) => {
16
+ static decodeLen = (arr) => {
17
17
  let multiplier = 1;
18
18
  let value=0;
19
19
  let digit = 0;
@@ -40,7 +40,7 @@ class ProtocolBase {
40
40
 
41
41
  const lengthIndex = getProtocolLen(nextData);
42
42
 
43
- this.len = this.decodeLen(nextData.slice(0, lengthIndex));
43
+ this.len = ProtocolBase.decodeLen(nextData.slice(0, lengthIndex));
44
44
  pointer += lengthIndex;
45
45
  this.flag = data.slice(pointer, pointer + 1);
46
46
  pointer += 1;
@@ -304,7 +304,6 @@ function PToDataPoint({ config = [], data }: IPToDataPointParams) {
304
304
  let message = '';
305
305
  let messageType: messageType;
306
306
  const rawData: IRawData = { header: arrayToString(data.slice(0, 4)), flag: '', len: '', cmd: '', sn: '',action: '', payload: '' };
307
-
308
307
  // 获取数据长度 0,0,0,3 到 0 这一段都是长度 校验长度可以忽略
309
308
  const attrs = config;
310
309
 
@@ -338,18 +337,21 @@ function PToDataPoint({ config = [], data }: IPToDataPointParams) {
338
337
  rawData.sn = '';
339
338
  // cmd 是93 的带sn
340
339
  if (rawData.cmd === '0093' || rawData.cmd === '0094') {
341
- rawData.sn = arrayToString(data.slice(pointer, pointer + 4));
342
- // 跳过sn
343
340
  pointer += 4;
341
+ rawData.sn = arrayToString(data.slice(pointer - 4, pointer));
342
+ // 跳过sn
343
+ if (rawData.flag === '01') {
344
+ // 还要跳过did长度和did
345
+ pointer += 2;
346
+ const didLen = parseInt(arrayToString(data.slice(pointer - 2, pointer)), 16);
347
+ pointer += didLen;
348
+ }
344
349
  }
345
350
 
346
- rawData.action = arrayToString(data.slice(pointer, pointer + 1));
347
-
348
- action = data[pointer];
349
-
350
351
  // 跳过action
351
352
  pointer += 1;
352
-
353
+ rawData.action = arrayToString(data.slice(pointer - 1, pointer));
354
+ action = parseInt(rawData.action, 16);
353
355
  rawData.payload = arrayToString(data.slice(pointer, data.length));
354
356
 
355
357
  // 开始处理数据
package/src/sdk.ts CHANGED
@@ -112,7 +112,7 @@ export type TimeoutHandle = ReturnType<typeof setTimeout> | null;
112
112
  export type IntervalHandle = ReturnType<typeof setInterval> | null;
113
113
  export interface IProductInfo {
114
114
  productKey: string;
115
- productSecrets: string;
115
+ productSecret: string;
116
116
  }
117
117
  interface GizwitsSdkOption {
118
118
  appID: string;
@@ -125,7 +125,7 @@ interface GizwitsSdkOption {
125
125
  }
126
126
 
127
127
  class SDK {
128
- private version = '3.0.5';
128
+ private version = '3.0.7';
129
129
  private listenerMap: any = {};
130
130
 
131
131
  // 云端获取的设备列表
@@ -275,7 +275,7 @@ class SDK {
275
275
  }
276
276
 
277
277
  get specialProductKeySecrets() {
278
- return this.productInfo.map((item) => item.productSecrets ?? '');
278
+ return this.productInfo.map((item) => item.productSecret ?? '');
279
279
  }
280
280
 
281
281
  private handleBleDeviceData = async (
@@ -321,7 +321,6 @@ class SDK {
321
321
  });
322
322
 
323
323
  this.listenerMap['onScanListChange']?.map((notifyFn) => {
324
- console.log('this.scanBleDevice', this.bleScanDevice);
325
324
  notifyFn(this.bleScanDevice);
326
325
  });
327
326
  };
@@ -334,10 +333,12 @@ class SDK {
334
333
  clearInterval(this.autoScanInterval);
335
334
  }
336
335
  this.autoScanInterval = setInterval(async () => {
337
- const data = await this.bleHandle.startScan(() => {}, 6000);
336
+ const data = await this.bleHandle.startScan((deviceList) => {
337
+ this.bleDevices = deviceList;
338
+ }, 6000);
338
339
  console.log('auto scan result', data);
339
340
  this.bleDevices = data.scanList;
340
- }, 6000);
341
+ }, 1000);
341
342
  };
342
343
 
343
344
  /**
@@ -581,7 +582,7 @@ class SDK {
581
582
  const data = await bindMac({
582
583
  mac,
583
584
  productKey,
584
- productSecret: target.productSecrets,
585
+ productSecret: target.productSecret,
585
586
  alias,
586
587
  beOwner,
587
588
  });
@@ -722,7 +723,7 @@ class SDK {
722
723
  if (target) {
723
724
  return await safeRegister({
724
725
  productKey,
725
- productSecret: target.productSecrets,
726
+ productSecret: target.productSecret,
726
727
  mac,
727
728
  isReset,
728
729
  passcode: '1234567890',
package/src/wechatApi.ts CHANGED
@@ -5,7 +5,7 @@ interface IBluetoothAdapterStateResult {
5
5
  available: boolean;
6
6
  }
7
7
 
8
- interface IWechatResult {
8
+ export interface IWechatResult {
9
9
  errMsg: string;
10
10
  errCode: number;
11
11
  }