mini_program_gizwits_sdk 3.2.30 → 3.2.32

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.
Files changed (32) hide show
  1. package/dist/index.js +3 -3
  2. package/dist/src/handler/ble.d.ts +3 -10
  3. package/dist/src/sdk.d.ts +200 -0
  4. package/dist/src/wifiConfig/ConfigBase.d.ts +2 -1
  5. package/dist/src/wifiConfig/ble.d.ts +1 -1
  6. package/dist/src/wifiConfig/nfc/disposeData.d.ts +2 -0
  7. package/dist/src/wifiConfig/nfc/formatData.d.ts +3 -0
  8. package/dist/src/wifiConfig/nfc/formatSSID.d.ts +6 -0
  9. package/dist/src/wifiConfig/nfc/index.d.ts +22 -0
  10. package/dist/src/wifiConfig/nfc/isIncludedCH.d.ts +1 -0
  11. package/dist/src/wifiConfig/nfc/nfcAConnect.d.ts +6 -0
  12. package/dist/src/wifiConfig/nfc/nfcATransceive.d.ts +5 -0
  13. package/dist/src/wifiConfig/nfc/ssIDToASC.d.ts +1 -0
  14. package/dist/src/wifiConfig/nfc/stringToGbk.d.ts +1 -0
  15. package/dist/src/wifiConfig/nfc/transceive.wx.d.ts +2 -0
  16. package/dist/src/wifiConfig/nfc/types.d.ts +15 -0
  17. package/package.json +1 -1
  18. package/src/handler/ble.ts +24 -15
  19. package/src/sdk.ts +45 -4
  20. package/src/wifiConfig/ConfigBase.ts +3 -1
  21. package/src/wifiConfig/ble.ts +4 -4
  22. package/src/wifiConfig/nfc/disposeData.ts +63 -0
  23. package/src/wifiConfig/nfc/formatData.ts +46 -0
  24. package/src/wifiConfig/nfc/formatSSID.ts +59 -0
  25. package/src/wifiConfig/nfc/index.ts +136 -0
  26. package/src/wifiConfig/nfc/isIncludedCH.ts +8 -0
  27. package/src/wifiConfig/nfc/nfcAConnect.ts +30 -0
  28. package/src/wifiConfig/nfc/nfcATransceive.ts +29 -0
  29. package/src/wifiConfig/nfc/ssIDToASC.ts +12 -0
  30. package/src/wifiConfig/nfc/stringToGbk.ts +51 -0
  31. package/src/wifiConfig/nfc/transceive.wx.ts +124 -0
  32. package/src/wifiConfig/nfc/types.ts +18 -0
@@ -0,0 +1,46 @@
1
+ type IType = 'BSSID' | 'pwd' | 'address';
2
+ /**
3
+ * BSSID去除冒号,转TLV编码(总长8字节)& pwd | passIP转TLV编码
4
+ * @param data BSSID | pwd | passIP
5
+ * @param type 'BSSID' & 'pwd' & 'passIP'
6
+ * @returns BSSID(TLV) & pwd(TLV) & passIP(TLV): Uint8Array;
7
+ */
8
+ export const formatData = (data: string, type: IType): Uint8Array => {
9
+ const isNull = data.length === 0;
10
+ const Data = type === 'BSSID' ? data.split(':') : data.split('');
11
+ let length: number = 0;
12
+ let T: any = null;
13
+ let L: number = 0;
14
+ switch (type) {
15
+ case 'BSSID':
16
+ length = isNull ? 3 : Data.length + 2;
17
+ T = 'b';
18
+ L = isNull ? 0 : Data.length;
19
+ break;
20
+ case 'pwd':
21
+ length = data.length + 2;
22
+ T = 'p';
23
+ L = data.length;
24
+ break;
25
+ case 'address':
26
+ length = isNull ? 3 : Data.length + 2;
27
+ T = 'i';
28
+ L = isNull ? 0 : Data.length;
29
+ break;
30
+ }
31
+ const buffer = new ArrayBuffer(length);
32
+ const uint8Array = new Uint8Array(buffer);
33
+ uint8Array[0] = T.charCodeAt(0); // Tag
34
+ uint8Array[1] = L; // Length
35
+
36
+ if (isNull) {
37
+ uint8Array[2] = 0;
38
+ } else {
39
+ Data.forEach((byte, index) => {
40
+ const v = type === 'BSSID' ? parseInt(byte, 16) : byte.charCodeAt(0);
41
+ uint8Array[index + 2] = v; // 前两个字节为Tag\Length预留
42
+ });
43
+ }
44
+ console.log(buffer, uint8Array, 'formatData', type);
45
+ return uint8Array;
46
+ };
@@ -0,0 +1,59 @@
1
+ import { formatCodesFromStr, arrayToString } from '../../protocol/tool';
2
+ import { unionBy } from '../../utils';
3
+ import isIncludedCH from './isIncludedCH';
4
+ import { ssIDToASC } from './ssIDToASC';
5
+ import stringToGbk from './stringToGbk';
6
+
7
+ interface IExport {
8
+ SSIDTLV: any;
9
+ isSSIDIncludedCH: boolean;
10
+ }
11
+ /**
12
+ * SSID转TLV编码 / 判断SSID是否含有中文
13
+ * @param SSID
14
+ * @returns SSIDTLV: TLV(Uint8Array), isSSIDIncludedCH: boolean
15
+ */
16
+ export const formatSSID = (SSID: string): IExport => {
17
+ const _export: IExport = {
18
+ SSIDTLV: null,
19
+ isSSIDIncludedCH: isIncludedCH(SSID),
20
+ };
21
+ // const enCode = isIncludedCH(SSID) ? stringToGbk(SSID) : ssIDToASC(SSID);
22
+ // const length = enCode.byteLength;
23
+
24
+ const [enCode, length] = formatCodesFromStr(SSID);
25
+ const codeLength = parseInt(arrayToString(length), 16);
26
+ if (codeLength <= 127) {
27
+ const buffer = new ArrayBuffer(codeLength + 2);
28
+ const uint8Array = new Uint8Array(buffer);
29
+ uint8Array[0] = 's'.charCodeAt(0); // T
30
+ uint8Array[1] = codeLength; // L
31
+ enCode.forEach((byte, index) => {
32
+ uint8Array[index + 2] = byte;
33
+ });
34
+ _export['SSIDTLV'] = uint8Array;
35
+ } else {
36
+ // @ts-ignore
37
+ const extra_L = parseInt(length / 255) + 1;
38
+ const buffer = new ArrayBuffer(codeLength + 2 + extra_L);
39
+ const uint8Array = new Uint8Array(buffer);
40
+ uint8Array[0] = 's'.charCodeAt(0); // T
41
+ uint8Array[1] = 128 + extra_L; // L@No1.Byte
42
+ if (extra_L === 1) {
43
+ uint8Array[2] = codeLength; // L@No2.Byte
44
+ } else {
45
+ uint8Array[extra_L] = 255;
46
+ uint8Array[extra_L + 1] = codeLength - 255 * (extra_L - 1);
47
+ for (let d = extra_L; d >= 1; d--) {
48
+ if (uint8Array[extra_L - d] === 0) {
49
+ uint8Array[extra_L - d] = 255;
50
+ }
51
+ }
52
+ }
53
+ enCode.forEach((byte, index) => {
54
+ uint8Array[index + 2 + extra_L] = byte;
55
+ });
56
+ _export['SSIDTLV'] = uint8Array;
57
+ }
58
+ return _export;
59
+ };
@@ -0,0 +1,136 @@
1
+ import { disposeData } from './disposeData';
2
+ import { formatData } from './formatData';
3
+ import { formatSSID } from './formatSSID';
4
+ import { transceive } from './transceive.wx';
5
+ import errorCode from "../../errorCode";
6
+ import GizLog from "../../GizLog";
7
+ import WifiConfig from "../../protocol/WifiConfig";
8
+ import { IRandomCodesResult, IResult, IntervalHandle } from "../../sdk";
9
+ import ConfigBase from "../ConfigBase";
10
+
11
+ // type IParams = {
12
+ // SSID: string;
13
+ // BSSID: string;
14
+ // pwd: string;
15
+ // passIP: string;
16
+ // };
17
+ /**
18
+ * 配网
19
+ */
20
+ // export const distribution = ({ SSID, BSSID, pwd, passIP }: IParams) => {
21
+ // const { SSIDTLV, isSSIDIncludedCH } = formatSSID(SSID);
22
+ // const BSSIDTLV = formatData(BSSID, 'BSSID');
23
+ // const pwdTLV = formatData(pwd, 'pwd');
24
+ // const passIPTLV = formatData(passIP, 'passIP');
25
+ // const DATA = disposeData(SSIDTLV, isSSIDIncludedCH, pwdTLV, passIPTLV, BSSIDTLV);
26
+ // console.log(DATA, 'DATA');
27
+
28
+ // // 发送数据
29
+ // transceive(DATA);
30
+ // };
31
+
32
+
33
+
34
+ interface configDeviceParams {
35
+ ssid: string;
36
+ password: string;
37
+ bssid: string;
38
+ address: string;
39
+ }
40
+
41
+ interface ISetDeviceOnboardingDeployProps {
42
+ timeout: number;
43
+ isBind: boolean;
44
+ BSSID: string;
45
+ softAPSSIDPrefix: string;
46
+ }
47
+ class NFCConfig extends ConfigBase {
48
+ // 服务器地址
49
+ address: string = '';
50
+ constructor(ssid: string, bssid: string, password: string,
51
+ specialProductKeys: string[], specialProductKeySecrets: string[], address: string) {
52
+ super(ssid, bssid, password, specialProductKeys, specialProductKeySecrets)
53
+ this.address = address;
54
+ }
55
+
56
+ destroy = () => {
57
+ this.cleanTimeout();
58
+ }
59
+
60
+ /**
61
+ * 负责发指令
62
+ */
63
+ configDevice = async ({ ssid, password, bssid, address }: configDeviceParams) => {
64
+ // TODO 发包
65
+ const { SSIDTLV, isSSIDIncludedCH } = formatSSID(ssid);
66
+ const BSSIDTLV = formatData(bssid, 'BSSID');
67
+ const pwdTLV = formatData(password, 'pwd');
68
+ const passIPTLV = formatData(address, 'address');
69
+ const DATA = disposeData(SSIDTLV, isSSIDIncludedCH, pwdTLV, passIPTLV, BSSIDTLV);
70
+ console.log(DATA, 'DATA');
71
+
72
+ // 发送数据
73
+ transceive(DATA);
74
+ // 大循环搜索
75
+ const devicesReturn = await this.searchDevice({ ssid, password });
76
+ return devicesReturn
77
+ };
78
+ /**
79
+ * 配网接口
80
+ * setDeviceOnboardingDeploy方法不可重复调用
81
+ */
82
+ setDeviceOnboardingDeploy = ({
83
+ timeout,
84
+ isBind = true,
85
+ }: ISetDeviceOnboardingDeployProps) => {
86
+ const ssid = this.ssid;
87
+ const password = this.password;
88
+ return new Promise<IResult<IDevice[]>>(async (res, rej) => {
89
+ console.log('ssid0', ssid, password, this.bssid, this.address)
90
+ this.destroy();
91
+ this.initDeviceOnboardingDeploy(res, rej);
92
+ this.startTimeoutTimer(timeout);
93
+ console.log('ssid', ssid, password, this.bssid, this.address)
94
+ try {
95
+ const result = await this.configDevice({
96
+ ssid,
97
+ password,
98
+ bssid: this.bssid,
99
+ address: this.address
100
+ });
101
+ const newData: IDevice[] = (result.data || []).map(item => {
102
+ return {
103
+ mac: item.mac,
104
+ productKey: item.product_key,
105
+ did: item.did,
106
+ name: '',
107
+ isBind: false,
108
+ connectType: 'NONE',
109
+ isBleOnline: false,
110
+ isLanOnline: false,
111
+ isOnline: false,
112
+ }
113
+ })
114
+ if (isBind) {
115
+ try {
116
+ res(await this.bindDevices(newData));
117
+ } catch (error) {
118
+ rej(error);
119
+ }
120
+ } else {
121
+ // 不需要绑定 直接返回成功
122
+ res({
123
+ success: true,
124
+ data: newData,
125
+ });
126
+ }
127
+ } catch (error) {
128
+ rej(error);
129
+ } finally {
130
+ this.destroy();
131
+ }
132
+ });
133
+ };
134
+ }
135
+
136
+ export default NFCConfig;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * 判断SSID是否含有中文
3
+ * @param str SSID
4
+ * @returns boolean 参数含有中文时为true
5
+ */
6
+ export default function isIncludedCH(str: string): boolean {
7
+ return /.*[\u4e00-\u9fa5]+.*$/.test(str);
8
+ }
@@ -0,0 +1,30 @@
1
+ type T = {
2
+ success: boolean;
3
+ nfcA: any;
4
+ };
5
+
6
+ /**
7
+ * 连接NFCA实例
8
+ * @param NFCAdapter NFCAdapter实例
9
+ * @returns Promise<{success: boolean, nfcA: any}>
10
+ */
11
+ export const nfcAConnect = (NFCAdapter: any): Promise<T> => {
12
+ return new Promise((resolve) => {
13
+ // nfcA实例
14
+ const nfcA = NFCAdapter.getNfcA();
15
+ nfcA.connect({
16
+ success() {
17
+ resolve({
18
+ success: true,
19
+ nfcA,
20
+ });
21
+ },
22
+ fail() {
23
+ resolve({
24
+ success: false,
25
+ nfcA,
26
+ });
27
+ },
28
+ });
29
+ });
30
+ };
@@ -0,0 +1,29 @@
1
+ type T = {
2
+ transceiveSuccess: boolean;
3
+ };
4
+
5
+ /**
6
+ * NfcA.transceive
7
+ * @param nfcA: NFCA实例
8
+ * @param buffer: ArrayBuffer实例
9
+ * @returns Promise<T>
10
+ */
11
+ export const nfcATransceive = (nfcA: any, buffer: ArrayBuffer): Promise<T> => {
12
+ return new Promise((resolve) => {
13
+ nfcA.transceive({
14
+ data: buffer,
15
+ success: (res) => {
16
+ console.log('success', res);
17
+ resolve({
18
+ transceiveSuccess: true,
19
+ });
20
+ },
21
+ fail: (err) => {
22
+ console.log('fail', err);
23
+ resolve({
24
+ transceiveSuccess: false,
25
+ });
26
+ },
27
+ });
28
+ });
29
+ };
@@ -0,0 +1,12 @@
1
+ /**
2
+ * 传入不含中文的SSID,返回SSID(TLV)中的Value
3
+ * @param SSID string;
4
+ * @returns Uint8Array SSID(Value部分)
5
+ */
6
+ export const ssIDToASC = (SSID: string): Uint8Array => {
7
+ const uint8Array = new Uint8Array(SSID.length);
8
+ SSID.split('').forEach((byte, index) => {
9
+ uint8Array[index] = byte.charCodeAt(0);
10
+ });
11
+ return uint8Array;
12
+ };
@@ -0,0 +1,51 @@
1
+ const encoding = require('./text-encoding-master/lib/encoding')
2
+ const ranges = [
3
+ [0xA1, 0xA9, 0xA1, 0xFE],
4
+ [0xB0, 0xF7, 0xA1, 0xFE],
5
+ [0x81, 0xA0, 0x40, 0xFE],
6
+ [0xAA, 0xFE, 0x40, 0xA0],
7
+ [0xA8, 0xA9, 0x40, 0xA0],
8
+ [0xAA, 0xAF, 0xA1, 0xFE],
9
+ [0xF8, 0xFE, 0xA1, 0xFE],
10
+ [0xA1, 0xA7, 0x40, 0xA0],
11
+ ];
12
+ const codes = new Uint16Array(23940);
13
+ let i = 0;
14
+
15
+ for (const [b1Begin, b1End, b2Begin, b2End] of ranges) {
16
+ for (let b2 = b2Begin; b2 <= b2End; b2++) {
17
+ if (b2 !== 0x7F) {
18
+ for (let b1 = b1Begin; b1 <= b1End; b1++) {
19
+ codes[i++] = b2 << 8 | b1;
20
+ }
21
+ }
22
+ }
23
+ }
24
+ const str = new encoding.TextDecoder('gbk').decode(codes);
25
+ const table = new Uint16Array(65536);
26
+ for (let i = 0; i < str.length; i++) {
27
+ table[str.charCodeAt(i)] = codes[i];
28
+ }
29
+
30
+ /**
31
+ * string转GBK编码
32
+ * @param str
33
+ * @returns 参数的GBK编码
34
+ */
35
+ export default function stringToGbk(str: string): Uint8Array {
36
+ const buf = new Uint8Array(str.length * 2);
37
+ let n = 0;
38
+
39
+ for (let i = 0; i < str.length; i++) {
40
+ const code = str.charCodeAt(i);
41
+ if (code < 0x80) {
42
+ buf[n++] = code;
43
+ } else {
44
+ const gbk = table[code];
45
+ buf[n++] = gbk & 0xFF;
46
+ buf[n++] = gbk >> 8;
47
+ }
48
+ }
49
+ return buf.subarray(0, n);
50
+ }
51
+
@@ -0,0 +1,124 @@
1
+ import { nfcAConnect } from './nfcAConnect';
2
+ import { nfcATransceive } from './nfcATransceive';
3
+ import { IDATA } from './types';
4
+
5
+ /**
6
+ * 向nfc芯片发送数据
7
+ * @param DATA 需要传输的DATA
8
+ */
9
+ export const transceive = (DATA: IDATA) => {
10
+ // NFCAdapter实例
11
+ const nfc = wx.getNFCAdapter();
12
+ nfc.startDiscovery();
13
+
14
+ // 监听nfc Tag
15
+ nfc.onDiscovered(async (techs) => {
16
+ console.log(techs, 'onDiscovered');
17
+ let allSuccess: boolean = true;
18
+ const { success, nfcA } = await nfcAConnect(nfc);
19
+ if (success) {
20
+ // 先发送SSID、PWD、PaaS IP、BSSID
21
+ const byteLength = DATA.ArrayBuffer_others.byteLength;
22
+ // @ts-ignore
23
+ const remainder = parseInt(byteLength % 4); // 余数
24
+ // @ts-ignore
25
+ const quotient = parseInt(byteLength / 4); // 商数
26
+ let temp_quo = quotient; // 用于计算blockID
27
+ const DATAUint8 = new Uint8Array(DATA.ArrayBuffer_others);
28
+
29
+ // 商数 - 写入数据
30
+ for (let i = 0; i <= quotient * 4 - 4; i += 4) {
31
+ if (temp_quo > 0) {
32
+ const buffer = new ArrayBuffer(6);
33
+ const uint8Array = new Uint8Array(buffer);
34
+ const blockID = 49 + quotient - temp_quo;
35
+ uint8Array[0] = parseInt('0xa2', 16);
36
+ uint8Array[1] = blockID;
37
+ uint8Array[2] = DATAUint8[0 + i];
38
+ uint8Array[3] = DATAUint8[1 + i];
39
+ uint8Array[4] = DATAUint8[2 + i];
40
+ uint8Array[5] = DATAUint8[3 + i];
41
+ const { transceiveSuccess } = await nfcATransceive(nfcA, buffer);
42
+ temp_quo -= 1;
43
+ if (!transceiveSuccess) {
44
+ // 某次写入失败
45
+ allSuccess = false;
46
+ wx.hideLoading();
47
+ wx.showToast({
48
+ title: 'NFC写入失败!',
49
+ icon: 'error',
50
+ duration: 2000,
51
+ });
52
+ break;
53
+ }
54
+ }
55
+
56
+ if (remainder !== 0 && allSuccess) {
57
+ // 余数 - 写入数据
58
+ const buffer = new ArrayBuffer(6);
59
+ const uint8Array = new Uint8Array(buffer);
60
+ const blockID = 49 + quotient;
61
+ uint8Array[0] = parseInt('0xa2', 16);
62
+ uint8Array[1] = blockID;
63
+ switch (remainder) {
64
+ case 1:
65
+ uint8Array[2] = DATAUint8[byteLength - 1];
66
+ break;
67
+ case 2:
68
+ uint8Array[2] = DATAUint8[byteLength - 2];
69
+ uint8Array[3] = DATAUint8[byteLength - 1];
70
+ break;
71
+ case 3:
72
+ uint8Array[2] = DATAUint8[byteLength - 3];
73
+ uint8Array[3] = DATAUint8[byteLength - 2];
74
+ uint8Array[4] = DATAUint8[byteLength - 1];
75
+ break;
76
+ }
77
+ const { transceiveSuccess } = await nfcATransceive(nfcA, buffer);
78
+ if (!transceiveSuccess) {
79
+ // 写入失败
80
+ wx.hideLoading();
81
+ wx.showToast({
82
+ title: 'NFC写入失败!',
83
+ icon: 'error',
84
+ duration: 2000,
85
+ });
86
+ }
87
+ }
88
+ }
89
+ // 再发送标志位数据: block30
90
+ const buffer = new ArrayBuffer(6);
91
+ const uint8Array = new Uint8Array(buffer);
92
+ const uint8Array_sign = new Uint8Array(DATA.ArrayBuffer_sign);
93
+ uint8Array[0] = parseInt('0xa2', 16);
94
+ uint8Array[1] = 48;
95
+ uint8Array[2] = uint8Array_sign[0];
96
+ uint8Array[3] = uint8Array_sign[1];
97
+ uint8Array[4] = uint8Array_sign[2];
98
+ uint8Array[5] = uint8Array_sign[3];
99
+ const { transceiveSuccess } = await nfcATransceive(nfcA, buffer);
100
+ wx.hideLoading();
101
+ wx.showToast(
102
+ transceiveSuccess
103
+ ? {
104
+ title: 'NFC写入成功!',
105
+ icon: 'success',
106
+ duration: 2000,
107
+ }
108
+ : {
109
+ title: 'NFC写入失败!',
110
+ icon: 'error',
111
+ duration: 2000,
112
+ }
113
+ );
114
+ nfcA.close();
115
+ } else {
116
+ wx.hideLoading();
117
+ wx.showToast({
118
+ title: 'NFC连接失败!',
119
+ icon: 'error',
120
+ duration: 2000,
121
+ });
122
+ }
123
+ });
124
+ };
@@ -0,0 +1,18 @@
1
+ type PageStateProps = {
2
+ store: {};
3
+ };
4
+
5
+ interface WifiInfo {
6
+ SSID: string;
7
+ BSSID: string;
8
+ secure: boolean;
9
+ signalStrength: number;
10
+ frequency: number;
11
+ }
12
+
13
+ type IDATA = {
14
+ ArrayBuffer_sign: ArrayBuffer;
15
+ ArrayBuffer_others: ArrayBuffer;
16
+ };
17
+
18
+ export { PageStateProps, WifiInfo, IDATA };