mini_program_gizwits_sdk 3.0.1 → 3.0.6
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/errorCode.d.ts +5 -0
- package/dist/src/globalData.d.ts +2 -0
- package/dist/src/openApiRequest.d.ts +9 -0
- package/dist/src/productConfigFileManage.d.ts +12 -0
- package/dist/src/protocol/Bind.d.ts +7 -0
- package/dist/src/protocol/DataPoint.d.ts +52 -0
- package/dist/src/protocol/GetDeviceStatus.d.ts +5 -0
- package/dist/src/protocol/Login.d.ts +12 -0
- package/dist/src/protocol/ProtocolBase.d.ts +11 -0
- package/dist/src/protocol/WifiConfig.d.ts +6 -0
- package/dist/src/protocol/tool.d.ts +9 -0
- package/dist/src/randomCode.d.ts +7 -0
- package/dist/src/request.d.ts +1 -0
- package/dist/src/services/devices.d.ts +53 -0
- package/dist/src/services/login.d.ts +8 -0
- package/dist/src/services/tool.d.ts +4 -0
- package/dist/src/sleep.d.ts +2 -0
- package/dist/src/socket.d.ts +107 -0
- package/dist/src/utils.d.ts +29 -0
- package/dist/src/wechatApi.d.ts +33 -0
- package/dist/src/wifiConfig/ConfigBase.d.ts +26 -0
- package/dist/src/wifiConfig/ap.d.ts +21 -0
- package/dist/src/wifiConfig/ble.d.ts +40 -0
- package/global.d.ts +28 -0
- package/index.ts +7 -0
- package/package.json +6 -3
- package/src/ble.ts +566 -0
- package/src/errorCode.ts +60 -0
- package/src/global.d.ts +42 -0
- package/src/globalData.ts +9 -0
- package/src/openApiRequest.ts +103 -0
- package/src/productConfigFileManage.ts +44 -0
- package/src/protocol/Bind.ts +22 -0
- package/src/protocol/GetDeviceStatus.ts +61 -0
- package/src/protocol/Login.ts +43 -0
- package/src/protocol/ProtocolBase.ts +53 -0
- package/src/protocol/WifiConfig.ts +41 -0
- package/src/protocol/dataPoint.ts +663 -0
- package/src/protocol/tool.ts +91 -0
- package/src/randomCode.ts +36 -0
- package/src/request.ts +22 -0
- package/src/sdk.ts +811 -0
- package/src/services/devices.ts +210 -0
- package/src/services/login.ts +15 -0
- package/src/services/tool.ts +10 -0
- package/src/sleep.ts +2 -0
- package/src/socket.ts +449 -0
- package/src/utils.ts +215 -0
- package/src/wechatApi.ts +179 -0
- package/src/wifiConfig/ConfigBase.ts +183 -0
- package/src/wifiConfig/ap.ts +193 -0
- package/src/wifiConfig/ble.ts +417 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import request from './request';
|
|
2
|
+
import { getGlobalData } from "./globalData";
|
|
3
|
+
import errorCode from './errorCode';
|
|
4
|
+
|
|
5
|
+
interface IResult<T> {
|
|
6
|
+
data: T;
|
|
7
|
+
errMsg: string;
|
|
8
|
+
statusCode: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface IServiceResult<T> {
|
|
12
|
+
success: boolean;
|
|
13
|
+
data?: T;
|
|
14
|
+
err?: IError;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
const openApiRequest = async <T>(url: string, options: any, needToken: boolean = true): Promise<IServiceResult<T>> => {
|
|
19
|
+
const requestOptions = { ...options };
|
|
20
|
+
const headers: any = {
|
|
21
|
+
'Content-Type': 'application/json',
|
|
22
|
+
'X-Gizwits-Application-Id': getGlobalData('appID'),
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
if (needToken) {
|
|
26
|
+
headers['X-Gizwits-User-token'] = getGlobalData('token');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
requestOptions.header = { ...headers, ...options.headers };
|
|
30
|
+
delete requestOptions.headers;
|
|
31
|
+
// console.log(`openApi request ${url}`, requestOptions);
|
|
32
|
+
const openApiUrl = getGlobalData('cloudServiceInfo').openAPIInfo;
|
|
33
|
+
const res = await request<IResult<T>>('https://' + openApiUrl + url, requestOptions);
|
|
34
|
+
// 统一封装OPEN API的错误码
|
|
35
|
+
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
36
|
+
return {
|
|
37
|
+
success: true,
|
|
38
|
+
data: res.data
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let baseError = {
|
|
43
|
+
success: false,
|
|
44
|
+
err: {
|
|
45
|
+
errorCode: errorCode.GIZ_SDK_HTTP_REQUEST_FAILED.errorCode,
|
|
46
|
+
errorMessage: '',
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
baseError = {
|
|
51
|
+
success: false,
|
|
52
|
+
err: {
|
|
53
|
+
errorCode: (res.data as any).error_code,
|
|
54
|
+
errorMessage: (res.data as any).error_message,
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
} catch (error) {
|
|
58
|
+
|
|
59
|
+
}
|
|
60
|
+
return baseError;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const siteApiRequest = async <T>(url: string, options: any): Promise<IServiceResult<T>> => {
|
|
64
|
+
const requestOptions = { ...options };
|
|
65
|
+
const headers: any = {
|
|
66
|
+
'Content-Type': 'application/json',
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
requestOptions.header = { ...headers, ...options.headers };
|
|
70
|
+
delete requestOptions.headers;
|
|
71
|
+
// console.log(`openApi request ${url}`, requestOptions);
|
|
72
|
+
const siteInfo = (getGlobalData('cloudServiceInfo') || {}).siteInfo || 'site.gizwits.com';
|
|
73
|
+
const res = await request<IResult<T>>('http://' + siteInfo + url, requestOptions);
|
|
74
|
+
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
75
|
+
return {
|
|
76
|
+
success: true,
|
|
77
|
+
data: res.data
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
let baseError = {
|
|
82
|
+
success: false,
|
|
83
|
+
err: {
|
|
84
|
+
errorCode: errorCode.GIZ_SDK_HTTP_REQUEST_FAILED.errorCode,
|
|
85
|
+
errorMessage: '',
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
try {
|
|
89
|
+
baseError = {
|
|
90
|
+
success: false,
|
|
91
|
+
err: {
|
|
92
|
+
errorCode: (res.data as any).error_code,
|
|
93
|
+
errorMessage: (res.data as any).error_message,
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
} catch (error) {
|
|
97
|
+
|
|
98
|
+
}
|
|
99
|
+
return baseError;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export { siteApiRequest };
|
|
103
|
+
export default openApiRequest;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import openApiRequest, { siteApiRequest } from './openApiRequest';
|
|
2
|
+
import { IDataPointConfig } from './protocol/DataPoint';
|
|
3
|
+
// 管理配置文件
|
|
4
|
+
|
|
5
|
+
interface IData {
|
|
6
|
+
[key: string]: IDataPointConfig;
|
|
7
|
+
}
|
|
8
|
+
class ProductConfigFileManage {
|
|
9
|
+
data: IData = {};
|
|
10
|
+
key = 'dataPointConfig';
|
|
11
|
+
|
|
12
|
+
init = async () => {
|
|
13
|
+
const data = await wx.getStorageSync(this.key) || '{}';
|
|
14
|
+
let dataJson = {};
|
|
15
|
+
try {
|
|
16
|
+
dataJson = JSON.parse(data);
|
|
17
|
+
} catch (error) {
|
|
18
|
+
|
|
19
|
+
}
|
|
20
|
+
this.data =dataJson;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public getConfigFile = async (pk: string): Promise<IDataPointConfig | null> => {
|
|
24
|
+
if (!this.data[pk]) {
|
|
25
|
+
// 不在缓存里,去云端获取
|
|
26
|
+
const url = `/app/datapoint?product_key=${pk}`;
|
|
27
|
+
const data = await openApiRequest<IDataPointConfig>(`${url}`, { method: 'GET' });
|
|
28
|
+
if (data.success && data.data?.entities) {
|
|
29
|
+
this.data[pk] = data.data as IDataPointConfig;
|
|
30
|
+
// 更新缓存
|
|
31
|
+
wx.setStorageSync(this.key, JSON.stringify(this.data))
|
|
32
|
+
} else {
|
|
33
|
+
// 请求失败
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return this.data[pk];
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const productConfigFileManage = new ProductConfigFileManage();
|
|
42
|
+
productConfigFileManage.init();
|
|
43
|
+
|
|
44
|
+
export default productConfigFileManage;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import ProtocolBase from "./ProtocolBase";
|
|
2
|
+
import { arrayToString } from "./tool";
|
|
3
|
+
|
|
4
|
+
class Bind extends ProtocolBase {
|
|
5
|
+
passcode: string = '';
|
|
6
|
+
constructor(data: number[]) {
|
|
7
|
+
super(data);
|
|
8
|
+
let index = 0;
|
|
9
|
+
const passcodeLen = parseInt(arrayToString(this.content.slice(index, index + 2)), 16);
|
|
10
|
+
index += 2;
|
|
11
|
+
let passcodeString = '';
|
|
12
|
+
this.content.slice(index, index + passcodeLen).map(item => {
|
|
13
|
+
passcodeString += String.fromCharCode(item);
|
|
14
|
+
});
|
|
15
|
+
this.passcode = passcodeString;
|
|
16
|
+
}
|
|
17
|
+
static pack = () => {
|
|
18
|
+
return [0,0,0,3,3,0,0,6];
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export default Bind;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import productConfigFileManage from "../productConfigFileManage";
|
|
2
|
+
import ProtocolBase from "./ProtocolBase";
|
|
3
|
+
import { fillString, hexStrint2byte, string2Bytes } from "./tool";
|
|
4
|
+
|
|
5
|
+
class GetDeviceStatus extends ProtocolBase {
|
|
6
|
+
static pack = async (productKey: string, gwDid?: string, attrNames?: string[], ) => {
|
|
7
|
+
const configFile = await productConfigFileManage.getConfigFile(productKey);
|
|
8
|
+
if (configFile) {
|
|
9
|
+
const isAdaptiveDatapoint = configFile.protocolType === 'var_len';
|
|
10
|
+
const header = [0,0,0,3];
|
|
11
|
+
const len = [0];
|
|
12
|
+
let flag = [0];
|
|
13
|
+
const cmd = [0,147];
|
|
14
|
+
let sn = [0,0,0,0];
|
|
15
|
+
let payload = [2];
|
|
16
|
+
|
|
17
|
+
if (gwDid) {
|
|
18
|
+
flag = [1];
|
|
19
|
+
const hexLen = fillString(gwDid.length.toString(16), 4)
|
|
20
|
+
|
|
21
|
+
const didLen = hexStrint2byte(hexLen);
|
|
22
|
+
const did = string2Bytes(gwDid);
|
|
23
|
+
sn = sn.concat(didLen).concat(did);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (isAdaptiveDatapoint) {
|
|
27
|
+
// 变长action
|
|
28
|
+
payload = [18];
|
|
29
|
+
// 根据 attrs 补充flag
|
|
30
|
+
let flagString = '';
|
|
31
|
+
|
|
32
|
+
if (!attrNames) {
|
|
33
|
+
// 全部填充
|
|
34
|
+
configFile.entities[0].attrs.map(() => {
|
|
35
|
+
flagString = `1${flagString}`;
|
|
36
|
+
})
|
|
37
|
+
} else {
|
|
38
|
+
const attrs = attrNames.reduce((acc, cur) => ({ ...acc, [cur]: 1 }), {});
|
|
39
|
+
configFile.entities[0].attrs.map((item) => {
|
|
40
|
+
if (attrs[item.name]) {
|
|
41
|
+
flagString = `1${flagString}`;
|
|
42
|
+
} else {
|
|
43
|
+
flagString = `0${flagString}`;
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
flagString = fillString(flagString, Math.ceil(flagString.length / 8) * 8);
|
|
49
|
+
payload = payload.concat(hexStrint2byte(parseInt(flagString, 2).toString(16)));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const endData = flag.concat(cmd).concat(sn).concat(payload);
|
|
53
|
+
len[0] = endData.length;
|
|
54
|
+
|
|
55
|
+
return header.concat(len).concat(endData);
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export default GetDeviceStatus;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import ProtocolBase from "./ProtocolBase";
|
|
2
|
+
import { arrayToString, fillString, hexStrint2byte, string2Bytes } from "./tool";
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
interface IPackParame {
|
|
6
|
+
passcode: string;
|
|
7
|
+
}
|
|
8
|
+
class Login extends ProtocolBase {
|
|
9
|
+
result: boolean = false;
|
|
10
|
+
didLength: number = 0;
|
|
11
|
+
did: string = '';
|
|
12
|
+
constructor(data: number[]) {
|
|
13
|
+
super(data);
|
|
14
|
+
|
|
15
|
+
let index = 0;
|
|
16
|
+
this.result = this.content[0] === 0;
|
|
17
|
+
index ++;
|
|
18
|
+
if (this.flag[0] === 1) {
|
|
19
|
+
this.didLength = parseInt(arrayToString(this.content.slice(index, index + 2)), 16);
|
|
20
|
+
|
|
21
|
+
index += 2;
|
|
22
|
+
let didString = '';
|
|
23
|
+
this.content.slice(index, this.content.length).map(item => {
|
|
24
|
+
didString += String.fromCharCode(item);
|
|
25
|
+
});
|
|
26
|
+
this.did = didString;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
static pack = ({passcode}: IPackParame) => {
|
|
30
|
+
const header = [0,0,0,3];
|
|
31
|
+
let len = [0];
|
|
32
|
+
const flag = [0];
|
|
33
|
+
const cmd = [0,8];
|
|
34
|
+
const passcodeLen = hexStrint2byte(fillString(passcode.length.toString(16), 4));
|
|
35
|
+
const passcodeArr = string2Bytes(passcode);
|
|
36
|
+
|
|
37
|
+
const data = flag.concat(cmd).concat(passcodeLen).concat(passcodeArr);
|
|
38
|
+
len = hexStrint2byte(data.length.toString(16));
|
|
39
|
+
return header.concat(len).concat(data);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export default Login;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { arrayToString, checkHeader, getProtocolLen } from "./tool";
|
|
2
|
+
|
|
3
|
+
class ProtocolBase {
|
|
4
|
+
header: string = '';
|
|
5
|
+
len:number = 0;
|
|
6
|
+
flag:number[] = [0];
|
|
7
|
+
cmd :string = '';
|
|
8
|
+
content:number[] = [];
|
|
9
|
+
|
|
10
|
+
constructor(data?: number[]) {
|
|
11
|
+
if (data && data.length > 0) {
|
|
12
|
+
this.formatP0(data);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
decodeLen = (arr) => {
|
|
17
|
+
let multiplier = 1;
|
|
18
|
+
let value=0;
|
|
19
|
+
let digit = 0;
|
|
20
|
+
let index = 0;
|
|
21
|
+
do {
|
|
22
|
+
digit = arr[index];
|
|
23
|
+
value += (digit & 127) * multiplier;
|
|
24
|
+
multiplier *= 128;
|
|
25
|
+
index += 1;
|
|
26
|
+
} while ((digit & 128) != 0);
|
|
27
|
+
return value;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
formatP0 = (data) => {
|
|
31
|
+
let pointer = checkHeader(data);
|
|
32
|
+
if (pointer === false) {
|
|
33
|
+
// 包头错误
|
|
34
|
+
throw new Error("Header Error");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
this.header = arrayToString(data.slice(0, pointer));
|
|
38
|
+
|
|
39
|
+
const nextData = data.slice(pointer, data.length);
|
|
40
|
+
|
|
41
|
+
const lengthIndex = getProtocolLen(nextData);
|
|
42
|
+
|
|
43
|
+
this.len = this.decodeLen(nextData.slice(0, lengthIndex));
|
|
44
|
+
pointer += lengthIndex;
|
|
45
|
+
this.flag = data.slice(pointer, pointer + 1);
|
|
46
|
+
pointer += 1;
|
|
47
|
+
this.cmd = arrayToString(data.slice(pointer, pointer + 2));
|
|
48
|
+
pointer += 2;
|
|
49
|
+
this.content = data.slice(pointer, data.length)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export default ProtocolBase;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import ProtocolBase from "./ProtocolBase";
|
|
2
|
+
import { formatCodesFromStr } from "./tool";
|
|
3
|
+
|
|
4
|
+
class WifiConfig extends ProtocolBase {
|
|
5
|
+
constructor(data: number[]) {
|
|
6
|
+
super(data);
|
|
7
|
+
}
|
|
8
|
+
static pack = (ssid: string, password: string) => {
|
|
9
|
+
const header = [0, 0, 0, 3];
|
|
10
|
+
const length: number[] = [];
|
|
11
|
+
const flag = [0];
|
|
12
|
+
const cmd = [0, 1];
|
|
13
|
+
|
|
14
|
+
const [ASSID, ssidLength] = formatCodesFromStr(ssid);
|
|
15
|
+
const [APassword, passwordLength] = formatCodesFromStr(password);
|
|
16
|
+
|
|
17
|
+
const content = flag.concat(
|
|
18
|
+
cmd,
|
|
19
|
+
ssidLength,
|
|
20
|
+
ASSID,
|
|
21
|
+
passwordLength,
|
|
22
|
+
APassword
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
let contentLength = content.length;
|
|
26
|
+
while (contentLength > 0) {
|
|
27
|
+
length.push(contentLength);
|
|
28
|
+
contentLength -= 255;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const config = header.concat(length).concat(content);
|
|
32
|
+
const buffer = new ArrayBuffer(config.length);
|
|
33
|
+
const uint8Array = new Uint8Array(buffer);
|
|
34
|
+
for (let i = 0; i < buffer.byteLength; i++) {
|
|
35
|
+
uint8Array[i] = config[i];
|
|
36
|
+
}
|
|
37
|
+
return uint8Array;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export default WifiConfig;
|