mini_program_gizwits_sdk 3.2.20-beta → 3.2.20
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/README.md +8 -5
- package/dist/index.js +3 -3
- package/dist/src/GizLog.d.ts +1 -1
- package/dist/src/handler/ble.d.ts +3 -3
- package/dist/src/protocol/Ntp.d.ts +5 -0
- package/dist/src/protocol/OffLineData.d.ts +23 -0
- package/dist/src/protocol/tool.d.ts +1 -0
- package/dist/src/sdk.d.ts +38 -7
- package/dist/src/services/devices.d.ts +10 -0
- package/dist/src/services/uploadP0.d.ts +13 -0
- package/global.d.ts +1 -1
- package/package.json +2 -2
- package/src/GizLog.ts +1 -2
- package/src/global.d.ts +1 -0
- package/src/handler/ble.ts +18 -15
- package/src/handler/socket.ts +26 -9
- package/src/protocol/Ntp.ts +13 -0
- package/src/protocol/OffLineData.ts +81 -0
- package/src/protocol/WifiConfig.ts +1 -0
- package/src/protocol/dataPoint.ts +9 -20
- package/src/protocol/tool.ts +11 -0
- package/src/sdk.ts +327 -21
- package/src/services/devices.ts +33 -0
- package/src/services/uploadP0.ts +56 -0
- package/src/utils.ts +9 -0
- package/src/wechatApi.ts +1 -1
package/dist/src/GizLog.d.ts
CHANGED
|
@@ -26,7 +26,6 @@ export declare type OnScanDevice = (scanList: IDevice[]) => void;
|
|
|
26
26
|
interface ConnectDevice {
|
|
27
27
|
deviceId?: string;
|
|
28
28
|
serviceId?: string;
|
|
29
|
-
isLogin?: boolean;
|
|
30
29
|
characteristicId?: string;
|
|
31
30
|
errMsg?: string;
|
|
32
31
|
}
|
|
@@ -43,6 +42,7 @@ export declare class BleHandle extends EventListener<TBleHandleEvent> {
|
|
|
43
42
|
private listenDevOfflineTimer;
|
|
44
43
|
private boradcastTimer;
|
|
45
44
|
private boradcastDataCache;
|
|
45
|
+
private baseServices;
|
|
46
46
|
private tmpBleData;
|
|
47
47
|
private tmpDataNum;
|
|
48
48
|
private disableScanFlag;
|
|
@@ -79,9 +79,10 @@ export declare class BleHandle extends EventListener<TBleHandleEvent> {
|
|
|
79
79
|
success: boolean;
|
|
80
80
|
}>;
|
|
81
81
|
private updateDevice;
|
|
82
|
+
private updateCtime;
|
|
82
83
|
private checkDeviceIsChange;
|
|
83
84
|
private handleBluetoothDeviceFound;
|
|
84
|
-
startScan: (onScanDevice: OnScanDevice, delay?: number) => Promise<IStartScanResult>;
|
|
85
|
+
startScan: (onScanDevice: OnScanDevice, delay?: number, services?: string[]) => Promise<IStartScanResult>;
|
|
85
86
|
stopScan: () => Promise<any>;
|
|
86
87
|
disableScan: () => Promise<WechatMiniprogram.BluetoothError>;
|
|
87
88
|
enableScan: () => void;
|
|
@@ -89,7 +90,6 @@ export declare class BleHandle extends EventListener<TBleHandleEvent> {
|
|
|
89
90
|
success: boolean;
|
|
90
91
|
message: any;
|
|
91
92
|
}>;
|
|
92
|
-
private updateDeviceIsLogin;
|
|
93
93
|
private handleBindReq;
|
|
94
94
|
private handleLoginReq;
|
|
95
95
|
watchBleDevice: (deviceId: any, func: any) => Promise<{
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import ProtocolBase from "./ProtocolBase";
|
|
2
|
+
export declare class RequestSync extends ProtocolBase {
|
|
3
|
+
state: number;
|
|
4
|
+
len: number;
|
|
5
|
+
constructor(data: number[]);
|
|
6
|
+
static pack: () => number[];
|
|
7
|
+
}
|
|
8
|
+
export declare class SyncPackage extends ProtocolBase {
|
|
9
|
+
id: number;
|
|
10
|
+
timestemp: number;
|
|
11
|
+
payloadLen: number;
|
|
12
|
+
payload: number[];
|
|
13
|
+
constructor(data: number[]);
|
|
14
|
+
static pack: (id: number) => number[];
|
|
15
|
+
}
|
|
16
|
+
export declare class DeletePackage extends ProtocolBase {
|
|
17
|
+
static pack: (id: number) => number[];
|
|
18
|
+
}
|
|
19
|
+
export declare class CancelSync extends ProtocolBase {
|
|
20
|
+
state: number;
|
|
21
|
+
constructor(data: number[]);
|
|
22
|
+
static pack: () => number[];
|
|
23
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
declare const checkHeader: (data: any) => number | false;
|
|
2
|
+
export declare const completeBlock: (cmd: string) => number[];
|
|
2
3
|
declare const getProtocolLen: (data: any) => number;
|
|
3
4
|
declare function arrayToString(arr: number[]): string;
|
|
4
5
|
declare const fillString: (string: string, num: number, foot?: boolean) => string;
|
package/dist/src/sdk.d.ts
CHANGED
|
@@ -2,9 +2,18 @@ import { TLogType } from './GizLog';
|
|
|
2
2
|
import { ILoginRes } from './services/login';
|
|
3
3
|
import { IOpenApiDevice, ISafeRegisterReturn, IUnbindReturn } from './services/devices';
|
|
4
4
|
import { IDataPointConfig } from './protocol/DataPoint';
|
|
5
|
-
interface
|
|
5
|
+
interface SyncCallBackParams {
|
|
6
|
+
event: TSyncEvnet;
|
|
7
|
+
currentNum?: number;
|
|
8
|
+
totalNum?: number;
|
|
9
|
+
message?: string;
|
|
10
|
+
}
|
|
11
|
+
declare type TSyncEvnet = 'SYNC_START' | 'SYNC_END' | 'SYNC_FAIL' | 'SYNC_CANCEL' | 'SYNC_PROGRESS';
|
|
12
|
+
export declare type SyncCallBack = (data: SyncCallBackParams) => void;
|
|
13
|
+
export interface ISDKResult<T> {
|
|
6
14
|
data?: T;
|
|
7
15
|
err?: IError;
|
|
16
|
+
message?: string;
|
|
8
17
|
success: boolean;
|
|
9
18
|
}
|
|
10
19
|
export interface ISetCommonDeviceOnboardingDeployProps {
|
|
@@ -103,6 +112,13 @@ declare class GizwitsMiniSDK {
|
|
|
103
112
|
private _lanHandle;
|
|
104
113
|
private _gizSocket;
|
|
105
114
|
private offlineThreshold?;
|
|
115
|
+
private syncDataCallBack?;
|
|
116
|
+
private syncDataLengthCallback?;
|
|
117
|
+
private syncTotalNum;
|
|
118
|
+
private syncCurrnetNum;
|
|
119
|
+
private syncDataTimoutTimer;
|
|
120
|
+
private SYNC_TIMEOUT;
|
|
121
|
+
private syncDevice?;
|
|
106
122
|
constructor({ appID, appSecret, productInfo, cloudServiceInfo, token, uid, offlineThreshold, }: GizwitsSdkOption);
|
|
107
123
|
private get bleHandle();
|
|
108
124
|
private get lanHandle();
|
|
@@ -118,25 +134,36 @@ declare class GizwitsMiniSDK {
|
|
|
118
134
|
private handleSocketStatus;
|
|
119
135
|
private notiDeviceList;
|
|
120
136
|
private initLan;
|
|
121
|
-
startAutoScan: () => void;
|
|
137
|
+
startAutoScan: (services?: string[]) => void;
|
|
122
138
|
stopAutoScan: () => void;
|
|
123
|
-
setDeviceMeta: <K extends "did" | "mac" | "productKey" | "bleWorkStatus" | "name" | "isBind" | "rootDeviceId" | "bleDeviceID" | "remark" | "connectType" | "isOnline" | "isLanOnline" | "isBleOnline" | "host" | "wss_port" | "ctime">(curDev: IDevice, key: K, value: IDevice[K], force?: boolean) => void;
|
|
139
|
+
setDeviceMeta: <K extends "did" | "mac" | "productKey" | "bleWorkStatus" | "name" | "isBind" | "rootDeviceId" | "bleDeviceID" | "remark" | "passcode" | "connectType" | "isOnline" | "isLanOnline" | "isBleOnline" | "host" | "wss_port" | "ctime">(curDev: IDevice, key: K, value: IDevice[K], force?: boolean) => void;
|
|
124
140
|
getVersion: () => {
|
|
125
141
|
success: boolean;
|
|
126
142
|
data: string;
|
|
127
143
|
};
|
|
128
144
|
initBle: () => Promise<ISDKResult<null>>;
|
|
129
|
-
scanBleDevice: (delay: number) => Promise<ISDKResult<IDevice[]>>;
|
|
130
|
-
write: (device: IDevice, attrs: object) => Promise<{
|
|
145
|
+
scanBleDevice: (delay: number, services?: string[]) => Promise<ISDKResult<IDevice[]>>;
|
|
146
|
+
write: (device: IDevice, attrs: object) => Promise<import("./wechatApi").IWechatResult | {
|
|
147
|
+
success: boolean;
|
|
148
|
+
message: string;
|
|
149
|
+
} | {
|
|
131
150
|
success: boolean;
|
|
132
151
|
message?: undefined;
|
|
133
|
-
}
|
|
152
|
+
}>;
|
|
153
|
+
writeRaw: (device: IDevice, data: number[]) => Promise<IError | import("./wechatApi").IWechatResult | {
|
|
154
|
+
success: boolean;
|
|
155
|
+
}>;
|
|
156
|
+
setDeviceTimeStamp: (device: IDevice) => Promise<import("./wechatApi").IWechatResult | {
|
|
134
157
|
success: boolean;
|
|
135
158
|
message: string;
|
|
136
159
|
}>;
|
|
137
|
-
|
|
160
|
+
syncDeviceData: (device: IDevice, callback?: SyncCallBack) => Promise<import("./wechatApi").IWechatResult | {
|
|
138
161
|
success: boolean;
|
|
162
|
+
message: string;
|
|
139
163
|
}>;
|
|
164
|
+
queryNeedSyncDataLength: (device: IDevice) => Promise<ISDKResult<number>>;
|
|
165
|
+
cancelSyncDeviceData: () => Promise<void>;
|
|
166
|
+
private cleanSyncDeviceData;
|
|
140
167
|
getProductConfig: (pk: string) => Promise<ISDKResult<IDataPointConfig>>;
|
|
141
168
|
stopScanBleDevice: () => void;
|
|
142
169
|
login: (openID: string) => Promise<ISDKResult<ILoginRes>>;
|
|
@@ -169,6 +196,10 @@ declare class GizwitsMiniSDK {
|
|
|
169
196
|
success: boolean;
|
|
170
197
|
err: IError;
|
|
171
198
|
};
|
|
199
|
+
renameDevice: (device: IDevice, name: string) => Promise<import("./openApiRequest").IServiceResult<import("./services/devices").IRenamePropsReturn> | {
|
|
200
|
+
success: boolean;
|
|
201
|
+
message: string;
|
|
202
|
+
}>;
|
|
172
203
|
removeEventListener: <K extends "GizDeviceListNotifications" | "GizDeviceStatusNotifications" | "GizDeviceAttrsNotifications" | "onScanListChange" | "onBleHandleError">(type: K, func: TListenerType[K]) => {
|
|
173
204
|
success: boolean;
|
|
174
205
|
};
|
|
@@ -50,4 +50,14 @@ export interface ISafeRegisterReturn {
|
|
|
50
50
|
failedDevices: IDevice[];
|
|
51
51
|
}
|
|
52
52
|
export declare function safeRegister({ productKey, productSecret, mac, passcode, gwDid, isReset, }: ISafeRegisterParams): Promise<IServiceResult<ISafeRegisterReturn>>;
|
|
53
|
+
interface IRenameProps {
|
|
54
|
+
name?: string;
|
|
55
|
+
did: string;
|
|
56
|
+
remark?: string;
|
|
57
|
+
}
|
|
58
|
+
export interface IRenamePropsReturn {
|
|
59
|
+
remark: string;
|
|
60
|
+
dev_alias: string;
|
|
61
|
+
}
|
|
62
|
+
export declare function editBindInfo({ name, remark, did, }: IRenameProps): Promise<IServiceResult<IRenamePropsReturn>>;
|
|
53
63
|
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface IUploadRes {
|
|
2
|
+
}
|
|
3
|
+
interface IData {
|
|
4
|
+
raw: string;
|
|
5
|
+
created_at: number;
|
|
6
|
+
}
|
|
7
|
+
interface IProps {
|
|
8
|
+
device: IDevice;
|
|
9
|
+
data: IData[];
|
|
10
|
+
productSecret: string;
|
|
11
|
+
}
|
|
12
|
+
export declare function uploadP0({ device, data, productSecret }: IProps): Promise<import("../openApiRequest").IServiceResult<IUploadRes>>;
|
|
13
|
+
export {};
|
package/global.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mini_program_gizwits_sdk",
|
|
3
|
-
"version": "3.2.20
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "3.2.20",
|
|
4
|
+
"description": "增加蓝牙搜索services参数",
|
|
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/GizLog.ts
CHANGED
|
@@ -2,7 +2,6 @@ import errorCode from './errorCode';
|
|
|
2
2
|
import sentry from './sentry';
|
|
3
3
|
|
|
4
4
|
export type TLogType = 'debug' | 'info' | 'warn' | 'error';
|
|
5
|
-
|
|
6
5
|
class Log {
|
|
7
6
|
|
|
8
7
|
logMap = [
|
|
@@ -12,7 +11,7 @@ class Log {
|
|
|
12
11
|
{id: 'error'}
|
|
13
12
|
]
|
|
14
13
|
|
|
15
|
-
console =
|
|
14
|
+
console = console
|
|
16
15
|
// debug info(log) warn error
|
|
17
16
|
debug = (...data)=> {
|
|
18
17
|
this.console.debug(...data);
|
package/src/global.d.ts
CHANGED
package/src/handler/ble.ts
CHANGED
|
@@ -14,7 +14,6 @@ import { hexStrint2byte } from '../protocol/tool';
|
|
|
14
14
|
import ProtocolBase from '../protocol/ProtocolBase';
|
|
15
15
|
import Login from '../protocol/Login';
|
|
16
16
|
import GetDeviceStatus from '../protocol/GetDeviceStatus';
|
|
17
|
-
import Sdk from '../Sdk';
|
|
18
17
|
import { padBoradcastData } from '../protocol/DataPoint';
|
|
19
18
|
import GizLog from '../GizLog';
|
|
20
19
|
import EventListener from './EventListener';
|
|
@@ -58,7 +57,6 @@ export type OnScanDevice = (scanList: IDevice[]) => void;
|
|
|
58
57
|
interface ConnectDevice {
|
|
59
58
|
deviceId?: string;
|
|
60
59
|
serviceId?: string;
|
|
61
|
-
isLogin?: boolean;
|
|
62
60
|
characteristicId?: string;
|
|
63
61
|
errMsg?: string;
|
|
64
62
|
}
|
|
@@ -79,6 +77,7 @@ export class BleHandle extends EventListener<TBleHandleEvent> {
|
|
|
79
77
|
|
|
80
78
|
private boradcastTimer: any;
|
|
81
79
|
private boradcastDataCache: any = {}
|
|
80
|
+
private baseServices: string[] = ['ABF8', 'ABF0', 'F0AB', 'F8AB'];
|
|
82
81
|
|
|
83
82
|
private tmpBleData = ''; // 接收分包数据用
|
|
84
83
|
private tmpDataNum = 0; // 剩余字节数
|
|
@@ -190,14 +189,13 @@ export class BleHandle extends EventListener<TBleHandleEvent> {
|
|
|
190
189
|
});
|
|
191
190
|
};
|
|
192
191
|
|
|
193
|
-
private handleOnBLECharacteristicValueChange = (
|
|
192
|
+
private handleOnBLECharacteristicValueChange = async (
|
|
194
193
|
curDevice: WechatMiniprogram.OnBLECharacteristicValueChangeCallbackResult
|
|
195
194
|
) => {
|
|
196
195
|
// 不存在数据
|
|
197
196
|
if (!curDevice.value) {
|
|
198
197
|
return;
|
|
199
198
|
}
|
|
200
|
-
console.log('handleOnBLECharacteristicValueChange', curDevice)
|
|
201
199
|
const hexString = ab2hex(curDevice.value);
|
|
202
200
|
try {
|
|
203
201
|
const data = new ProtocolBase(hexStrint2byte(hexString));
|
|
@@ -348,6 +346,14 @@ export class BleHandle extends EventListener<TBleHandleEvent> {
|
|
|
348
346
|
}
|
|
349
347
|
}
|
|
350
348
|
|
|
349
|
+
private updateCtime = (device: WechatMiniprogram.BlueToothDevice) => {
|
|
350
|
+
const id = device.deviceId;
|
|
351
|
+
const index = this.scanList.findIndex(item => id == item.bleDeviceID);
|
|
352
|
+
if (index !== -1) {
|
|
353
|
+
this.scanList[index].ctime = Date.now();
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
351
357
|
private checkDeviceIsChange = (a: IDevice, b: IDevice) => {
|
|
352
358
|
//检查设备是否发生变更
|
|
353
359
|
|
|
@@ -398,6 +404,8 @@ export class BleHandle extends EventListener<TBleHandleEvent> {
|
|
|
398
404
|
}
|
|
399
405
|
this.boradcastDataCache[deviceId] = ab2hex(advertisData);
|
|
400
406
|
const fullData = padBoradcastData(broadcastCmdData.data.map(item => parseInt(item, 16)))
|
|
407
|
+
|
|
408
|
+
this.updateCtime(device)
|
|
401
409
|
this.notiDeviceDataToSdk(device as any, fullData);
|
|
402
410
|
return;
|
|
403
411
|
}
|
|
@@ -447,7 +455,8 @@ export class BleHandle extends EventListener<TBleHandleEvent> {
|
|
|
447
455
|
|
|
448
456
|
public startScan = async (
|
|
449
457
|
onScanDevice: OnScanDevice,
|
|
450
|
-
delay: number = 5000
|
|
458
|
+
delay: number = 5000,
|
|
459
|
+
services?: string[]
|
|
451
460
|
) => {
|
|
452
461
|
if (this.disableScanFlag) {
|
|
453
462
|
return {
|
|
@@ -461,9 +470,11 @@ export class BleHandle extends EventListener<TBleHandleEvent> {
|
|
|
461
470
|
|
|
462
471
|
GizLog.debug("GIZ_SDK: start scan", delay);
|
|
463
472
|
await wx.startBluetoothDevicesDiscovery({
|
|
473
|
+
// 如果有传的话,就按传入的方式来搜索
|
|
474
|
+
services: services || this.baseServices,
|
|
464
475
|
powerLevel: 'high',
|
|
465
476
|
allowDuplicatesKey: true,
|
|
466
|
-
interval:
|
|
477
|
+
interval: 200,
|
|
467
478
|
});
|
|
468
479
|
|
|
469
480
|
if (delay > 0) {
|
|
@@ -580,8 +591,7 @@ export class BleHandle extends EventListener<TBleHandleEvent> {
|
|
|
580
591
|
this.notifyError(err)
|
|
581
592
|
)) as any;
|
|
582
593
|
if (loginData?.success) {
|
|
583
|
-
|
|
584
|
-
this.updateDeviceIsLogin(deviceId)
|
|
594
|
+
|
|
585
595
|
return {
|
|
586
596
|
success: true,
|
|
587
597
|
message: 'ok',
|
|
@@ -603,13 +613,6 @@ export class BleHandle extends EventListener<TBleHandleEvent> {
|
|
|
603
613
|
}
|
|
604
614
|
};
|
|
605
615
|
|
|
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
|
-
|
|
613
616
|
private handleBindReq = (deviceId: string) => {
|
|
614
617
|
// 处理设备回复绑定消息
|
|
615
618
|
return new Promise<IDeviceReq<string>>((res) => {
|
package/src/handler/socket.ts
CHANGED
|
@@ -375,7 +375,12 @@ export class Connection {
|
|
|
375
375
|
|
|
376
376
|
_connectWS = () => {
|
|
377
377
|
GizLog.log('GIZ_SDK: start connect ws')
|
|
378
|
-
this._websocket = wx.connectSocket({
|
|
378
|
+
this._websocket = wx.connectSocket({
|
|
379
|
+
url: this._wsUrl,
|
|
380
|
+
fail: (res) => {
|
|
381
|
+
GizLog.error('GIZ_SDK: connectSocket error', new Error(JSON.stringify(res)));
|
|
382
|
+
}
|
|
383
|
+
});
|
|
379
384
|
this._websocket.onClose(this.handleClose);
|
|
380
385
|
this._websocket.onOpen(this.handleOpen);
|
|
381
386
|
this._websocket.onError(this.handleError);
|
|
@@ -405,15 +410,27 @@ export class Connection {
|
|
|
405
410
|
}
|
|
406
411
|
|
|
407
412
|
_send = (data: object, forced: boolean = false) => {
|
|
408
|
-
GizLog.log('Socket send', data, forced);
|
|
413
|
+
GizLog.log('GIZ_SDK: Socket send', data, forced);
|
|
409
414
|
return new Promise<IWriteRes>((resolve) => {
|
|
410
|
-
if (!this._websocket
|
|
411
|
-
this._waitSends.push(data);
|
|
415
|
+
if (!this._websocket) {
|
|
412
416
|
resolve({
|
|
413
|
-
success:
|
|
414
|
-
message: ''
|
|
417
|
+
success: false,
|
|
418
|
+
message: 'Socket not init'
|
|
415
419
|
})
|
|
416
|
-
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
if (!forced && !this.ready) {
|
|
423
|
+
resolve({
|
|
424
|
+
success: false,
|
|
425
|
+
message: 'Socket not ready'
|
|
426
|
+
})
|
|
427
|
+
return;
|
|
428
|
+
// this._waitSends.push(data);
|
|
429
|
+
// resolve({
|
|
430
|
+
// success: true,
|
|
431
|
+
// message: '',
|
|
432
|
+
// })
|
|
433
|
+
// GizLog.debug('GIZ_SDK: cache data wait socket ready');
|
|
417
434
|
return;
|
|
418
435
|
}
|
|
419
436
|
if (this._websocket && (forced || this.ready)) {
|
|
@@ -465,7 +482,7 @@ export class Connection {
|
|
|
465
482
|
}
|
|
466
483
|
|
|
467
484
|
handleClose = (res: { code: number, reason: string }) => {
|
|
468
|
-
GizLog.
|
|
485
|
+
GizLog.error('GIZ_SDK: socket close', new Error(JSON.stringify(res)));
|
|
469
486
|
this.close();
|
|
470
487
|
this._stopPing();
|
|
471
488
|
}
|
|
@@ -477,7 +494,7 @@ export class Connection {
|
|
|
477
494
|
}
|
|
478
495
|
|
|
479
496
|
handleMessage = ({ data }: { data: string | ArrayBuffer }) => {
|
|
480
|
-
GizLog.log('message', data);
|
|
497
|
+
GizLog.log('GIZ_SDK: message', data);
|
|
481
498
|
const res = JSON.parse(data as string);
|
|
482
499
|
const handle = this._socketRespHandleMap[res.cmd];
|
|
483
500
|
handle && handle(res.data);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import ProtocolBase from "./ProtocolBase";
|
|
2
|
+
import { completeBlock, fillString, string2Bytes } from "./tool";
|
|
3
|
+
|
|
4
|
+
class Ntp extends ProtocolBase {
|
|
5
|
+
static pack = () => {
|
|
6
|
+
const time = parseInt(`${Date.now() / 1000}`, 10);
|
|
7
|
+
const timeString = fillString(time.toString(2), 5 * 8)
|
|
8
|
+
|
|
9
|
+
return [0,0,0,3,8,0,0,89].concat(completeBlock(timeString));
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export default Ntp;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import ProtocolBase from "./ProtocolBase";
|
|
2
|
+
import { arrayToString, completeBlock, fillString, hexStrint2byte } from "./tool";
|
|
3
|
+
|
|
4
|
+
export class RequestSync extends ProtocolBase {
|
|
5
|
+
state: number = 0; // 0 允许同步 1 没有数据 2其他
|
|
6
|
+
len: number = 0; // 需要同步的数据长度
|
|
7
|
+
constructor(data: number[]) {
|
|
8
|
+
super(data);
|
|
9
|
+
let index = 0
|
|
10
|
+
|
|
11
|
+
this.len = parseInt(arrayToString(this.content.slice(index, index + 2)), 16);
|
|
12
|
+
index += 2
|
|
13
|
+
|
|
14
|
+
this.state = this.content[index];
|
|
15
|
+
}
|
|
16
|
+
static pack = () => {
|
|
17
|
+
return [0,0,0,3,3,0,0,81];
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
export class SyncPackage extends ProtocolBase {
|
|
23
|
+
id: number = 0;
|
|
24
|
+
timestemp: number = 0;
|
|
25
|
+
payloadLen: number = 0;
|
|
26
|
+
payload: number[] = [];
|
|
27
|
+
constructor(data: number[]) {
|
|
28
|
+
super(data);
|
|
29
|
+
|
|
30
|
+
let index = 0;
|
|
31
|
+
this.id = parseInt(arrayToString(this.content.slice(index, index + 2)), 16);
|
|
32
|
+
|
|
33
|
+
index += 2;
|
|
34
|
+
|
|
35
|
+
this.timestemp = parseInt(arrayToString(this.content.slice(index, index + 5)), 16);
|
|
36
|
+
index += 5;
|
|
37
|
+
|
|
38
|
+
this.payloadLen = parseInt(arrayToString(this.content.slice(index, index + 2)), 16);
|
|
39
|
+
index += 2;
|
|
40
|
+
|
|
41
|
+
this.payload = this.content.slice(index, this.content.length)
|
|
42
|
+
}
|
|
43
|
+
static pack = (id: number) => {
|
|
44
|
+
let stringData = id.toString(2)
|
|
45
|
+
stringData = fillString(stringData, 8 * 2)
|
|
46
|
+
const packageData = [0,0,0,3, 5].concat([0,0,83]).concat(completeBlock(stringData))
|
|
47
|
+
return packageData;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// 删除数据
|
|
52
|
+
export class DeletePackage extends ProtocolBase {
|
|
53
|
+
static pack = (id: number) => {
|
|
54
|
+
let stringData = id.toString(2)
|
|
55
|
+
stringData = fillString(stringData, 8 * 2)
|
|
56
|
+
const packageData = [0,0,0,3,5].concat([0,0,85]).concat(completeBlock(stringData))
|
|
57
|
+
return packageData;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// let time = 1669711410;
|
|
62
|
+
// let timeBytes: any = time.toString(16)
|
|
63
|
+
// timeBytes = fillString(timeBytes, 10)
|
|
64
|
+
// timeBytes = hexStrint2byte(timeBytes)
|
|
65
|
+
// console.log(timeBytes)
|
|
66
|
+
|
|
67
|
+
// const syncPackage = new SyncPackage([0,0,0,3,15 ,0, 0, 83,1,1,1,1].concat(timeBytes).concat([0, 1, 1]))
|
|
68
|
+
// console.log(syncPackage)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
export class CancelSync extends ProtocolBase {
|
|
72
|
+
state: number = 0; // 0 同步完成 01 异常 02 其他
|
|
73
|
+
constructor(data: number[]) {
|
|
74
|
+
super(data);
|
|
75
|
+
this.state = this.content[0];
|
|
76
|
+
}
|
|
77
|
+
static pack = () => {
|
|
78
|
+
const packageData = [0,0,0,3, 3, 0, 0, 87]
|
|
79
|
+
return packageData;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import productConfigFileManage from "../productConfigFileManage";
|
|
2
|
-
import { arrayToString, checkHeader, fillString, getProtocolLen, hexStrint2byte } from "./tool";
|
|
2
|
+
import { arrayToString, checkHeader, fillString, getProtocolLen, hexStrint2byte, completeBlock} from "./tool";
|
|
3
3
|
|
|
4
4
|
export interface IDataPointConfig {
|
|
5
5
|
name: string;
|
|
@@ -44,16 +44,6 @@ export interface IDataPointAttr {
|
|
|
44
44
|
desc: string;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
// 从协议计算一个区块的数据
|
|
48
|
-
const completeBlock = (cmd: string) => {
|
|
49
|
-
// 补0到8的倍数
|
|
50
|
-
const newCmd = fillString(cmd, Math.ceil(cmd.length / 8) * 8);
|
|
51
|
-
const data: number[] = [];
|
|
52
|
-
for (let i = 0; i < newCmd.length; i += 8) {
|
|
53
|
-
data.push(parseInt(newCmd.substring(i, i + 8), 2));
|
|
54
|
-
}
|
|
55
|
-
return data;
|
|
56
|
-
};
|
|
57
47
|
const fixType = (type: TDataType) => {
|
|
58
48
|
return type.indexOf('uint') !== -1 ? 'number' : type;
|
|
59
49
|
};
|
|
@@ -284,15 +274,6 @@ const DataPointToP = ({ config, data, isAdaptiveDatapoint }: IDataPointToP0Param
|
|
|
284
274
|
}
|
|
285
275
|
};
|
|
286
276
|
};
|
|
287
|
-
|
|
288
|
-
/**
|
|
289
|
-
* p0 转json
|
|
290
|
-
* @param {*} config
|
|
291
|
-
* @param {*} data
|
|
292
|
-
* 设备到app
|
|
293
|
-
* header len flag cmd sn action data
|
|
294
|
-
* action 0x04定长上报 0x05定长下发 0x11变长下发 0x14变长上报 0x12变长下发读数据点 0x02定长读数据点
|
|
295
|
-
*/
|
|
296
277
|
|
|
297
278
|
interface IPToDataPointParams {
|
|
298
279
|
config: IDataPointAttr[];
|
|
@@ -323,6 +304,14 @@ function getVariableLength(data: number[]) {
|
|
|
323
304
|
return lengthArr;
|
|
324
305
|
}
|
|
325
306
|
|
|
307
|
+
/**
|
|
308
|
+
* p0 转json
|
|
309
|
+
* @param {*} config
|
|
310
|
+
* @param {*} data
|
|
311
|
+
* 设备到app
|
|
312
|
+
* header len flag cmd sn action data
|
|
313
|
+
* action 0x04定长上报 0x05定长下发 0x11变长下发 0x14变长上报 0x12变长下发读数据点 0x02定长读数据点
|
|
314
|
+
*/
|
|
326
315
|
function PToDataPoint({ config = [], data }: IPToDataPointParams) {
|
|
327
316
|
let action: number;
|
|
328
317
|
let message = '';
|
package/src/protocol/tool.ts
CHANGED
|
@@ -9,6 +9,17 @@ const checkHeader = (data) => {
|
|
|
9
9
|
return pointer;
|
|
10
10
|
};
|
|
11
11
|
|
|
12
|
+
// 从2进制,转换成10进制数组
|
|
13
|
+
export const completeBlock = (cmd: string) => {
|
|
14
|
+
// 补0到8的倍数
|
|
15
|
+
const newCmd = fillString(cmd, Math.ceil(cmd.length / 8) * 8);
|
|
16
|
+
const data: number[] = [];
|
|
17
|
+
for (let i = 0; i < newCmd.length; i += 8) {
|
|
18
|
+
data.push(parseInt(newCmd.substring(i, i + 8), 2));
|
|
19
|
+
}
|
|
20
|
+
return data;
|
|
21
|
+
};
|
|
22
|
+
|
|
12
23
|
const getProtocolLen = (data) => {
|
|
13
24
|
/**
|
|
14
25
|
* 插入len
|