mini_program_gizwits_sdk 3.6.1 → 3.6.2
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.d.ts +2 -0
- package/dist/index.js +3 -3
- package/dist/package.json +55 -0
- package/dist/src/GizLog.d.ts +1 -1
- package/dist/src/errorCode.d.ts +1 -3
- package/dist/src/handler/ble.d.ts +5 -66
- package/dist/src/handler/lan.d.ts +3 -18
- package/dist/src/handler/socket.d.ts +1 -0
- package/dist/src/openApiRequest.d.ts +1 -5
- package/dist/src/productConfigFileManage.d.ts +3 -6
- package/dist/src/protocol/OTA/CheckCanOTA.d.ts +1 -1
- package/dist/src/protocol/{DataPoint.d.ts → dataPoint.d.ts} +0 -34
- package/dist/src/randomCode.d.ts +1 -5
- package/dist/src/sdk.d.ts +9 -104
- package/dist/src/sdk.js +1101 -0
- package/dist/src/services/devices.d.ts +1 -17
- package/dist/src/services/login.d.ts +2 -6
- package/dist/src/services/monitor.d.ts +2 -1
- package/dist/src/services/monitor.js +72 -0
- package/dist/src/services/ota.d.ts +2 -6
- package/dist/src/services/uploadP0.d.ts +2 -1
- package/dist/src/types/index.d.ts +360 -0
- package/dist/src/utils.d.ts +1 -6
- package/dist/src/wechatApi.d.ts +1 -22
- package/dist/src/wifiConfig/ConfigBase.d.ts +2 -9
- package/dist/src/wifiConfig/ap.d.ts +1 -11
- package/dist/src/wifiConfig/ble.d.ts +2 -8
- package/dist/src/wifiConfig/nfc/index.d.ts +2 -2
- package/global.d.ts +3 -19
- package/index.ts +2 -0
- package/package.json +2 -2
- package/src/errorCode.ts +3 -3
- package/src/handler/ble.ts +4 -79
- package/src/handler/lan.ts +3 -24
- package/src/handler/socket.ts +3 -0
- package/src/openApiRequest.ts +2 -14
- package/src/productConfigFileManage.ts +3 -5
- package/src/protocol/OTA/AwaitNoti.ts +0 -2
- package/src/protocol/OTA/CheckCanOTA.ts +1 -2
- package/src/protocol/OTA/OTAPackPackage.ts +1 -1
- package/src/protocol/OTA/PreOTA.ts +2 -3
- package/src/protocol/OTA/ResetPosition.ts +1 -2
- package/src/protocol/dataPoint.ts +1 -42
- package/src/randomCode.ts +1 -12
- package/src/sdk.ts +11 -114
- package/src/services/devices.ts +2 -22
- package/src/services/login.ts +1 -5
- package/src/services/monitor.ts +1 -0
- package/src/services/ota.ts +1 -5
- package/src/services/uploadP0.ts +1 -0
- package/src/types/index.ts +455 -0
- package/src/utils.ts +1 -6
- package/src/wechatApi.ts +1 -22
- package/src/wifiConfig/ConfigBase.ts +2 -10
- package/src/wifiConfig/ap.ts +1 -13
- package/src/wifiConfig/ble.ts +2 -9
- package/src/wifiConfig/nfc/index.ts +1 -4
- package/src/global.d.ts +0 -52
package/dist/src/sdk.js
ADDED
|
@@ -0,0 +1,1101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const globalData_1 = require("./globalData");
|
|
16
|
+
const package_json_1 = __importDefault(require("../package.json"));
|
|
17
|
+
const lan_1 = require("./handler/lan");
|
|
18
|
+
const productConfigFileManage_1 = __importDefault(require("./productConfigFileManage"));
|
|
19
|
+
const dataPoint_1 = require("./protocol/dataPoint");
|
|
20
|
+
const Ntp_1 = __importDefault(require("./protocol/Ntp"));
|
|
21
|
+
const OffLineData_1 = require("./protocol/OffLineData");
|
|
22
|
+
const ProtocolBase_1 = __importDefault(require("./protocol/ProtocolBase"));
|
|
23
|
+
const tool_1 = require("./protocol/tool");
|
|
24
|
+
const ble_1 = require("./handler/ble");
|
|
25
|
+
const socket_1 = __importDefault(require("./handler/socket"));
|
|
26
|
+
const Reset_1 = __importDefault(require("./protocol/Reset"));
|
|
27
|
+
const SetReset_1 = __importDefault(require("./protocol/SetReset"));
|
|
28
|
+
const GizLog_1 = __importDefault(require("./GizLog"));
|
|
29
|
+
const errorCode_1 = __importDefault(require("./errorCode"));
|
|
30
|
+
const devices_1 = require("./services/devices");
|
|
31
|
+
const login_1 = require("./services/login");
|
|
32
|
+
const uploadP0_1 = require("./services/uploadP0");
|
|
33
|
+
const sleep_1 = __importDefault(require("./sleep"));
|
|
34
|
+
const utils_1 = require("./utils");
|
|
35
|
+
const ap_1 = __importDefault(require("./wifiConfig/ap"));
|
|
36
|
+
const ble_2 = __importDefault(require("./wifiConfig/ble"));
|
|
37
|
+
const nfc_1 = __importDefault(require("./wifiConfig/nfc"));
|
|
38
|
+
const version = package_json_1.default.version;
|
|
39
|
+
class GizwitsMiniSDK {
|
|
40
|
+
get deviceList() {
|
|
41
|
+
return this._deviceList;
|
|
42
|
+
}
|
|
43
|
+
set deviceList(data) {
|
|
44
|
+
this._deviceList = data;
|
|
45
|
+
this.notiDeviceList();
|
|
46
|
+
}
|
|
47
|
+
get bleDevices() {
|
|
48
|
+
return this._bleDevices;
|
|
49
|
+
}
|
|
50
|
+
set bleDevices(data) {
|
|
51
|
+
this._bleDevices = data;
|
|
52
|
+
this.notiDeviceList();
|
|
53
|
+
}
|
|
54
|
+
get lanDevices() {
|
|
55
|
+
return this._lanDevices;
|
|
56
|
+
}
|
|
57
|
+
set lanDevices(data) {
|
|
58
|
+
this._lanDevices = data;
|
|
59
|
+
this.notiDeviceList();
|
|
60
|
+
}
|
|
61
|
+
get allDevices() {
|
|
62
|
+
let newDevices = (0, utils_1.merageBleLocalDevices)([...this.deviceList], this.bleDevices);
|
|
63
|
+
newDevices = (0, utils_1.merageLanLocalDevices)(newDevices, this.lanDevices);
|
|
64
|
+
this.bleHandle.connectedList.map((item) => {
|
|
65
|
+
const index = newDevices.findIndex((device) => device.bleDeviceID === item.deviceId);
|
|
66
|
+
if (index !== -1) {
|
|
67
|
+
newDevices[index].connectType = 'BLE';
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
const connectionMap = this.socketHandle._connections;
|
|
71
|
+
Object.keys(connectionMap).forEach((wsInfo) => {
|
|
72
|
+
const conn = connectionMap[wsInfo];
|
|
73
|
+
if (conn) {
|
|
74
|
+
conn._subDids.forEach((did) => {
|
|
75
|
+
const index = newDevices.findIndex((device) => device.did === did);
|
|
76
|
+
if (index !== -1) {
|
|
77
|
+
newDevices[index].connectType = 'WAN';
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
const lanConnected = this.lanHandle.connectedList;
|
|
83
|
+
lanConnected.map(item => {
|
|
84
|
+
const index = newDevices.findIndex((device) => device.mac === item.mac && device.productKey === item.productKey);
|
|
85
|
+
if (index !== -1) {
|
|
86
|
+
newDevices[index].connectType = 'LAN';
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
return newDevices;
|
|
90
|
+
}
|
|
91
|
+
get bleScanDevice() {
|
|
92
|
+
return this.bleDevices.filter((item) => item.isBleOnline === true);
|
|
93
|
+
}
|
|
94
|
+
constructor(props) {
|
|
95
|
+
this.listenerMap = {};
|
|
96
|
+
this.keepScanTimer = null;
|
|
97
|
+
this._deviceList = [];
|
|
98
|
+
this._bleDevices = [];
|
|
99
|
+
this._lanDevices = [];
|
|
100
|
+
this.productInfo = [];
|
|
101
|
+
this.syncDataCallBack = null;
|
|
102
|
+
this.syncTotalNum = 0;
|
|
103
|
+
this.syncCurrnetNum = 0;
|
|
104
|
+
this.syncDataTimoutTimer = null;
|
|
105
|
+
this.SYNC_TIMEOUT = 4 * 60 * 1000;
|
|
106
|
+
this.init = ({ appID, appSecret, productInfo, cloudServiceInfo, token, uid, offlineThreshold, }) => {
|
|
107
|
+
this.productInfo = productInfo;
|
|
108
|
+
(0, globalData_1.setGlobalData)('appID', appID);
|
|
109
|
+
(0, globalData_1.setGlobalData)('appSecret', appSecret);
|
|
110
|
+
(0, globalData_1.setGlobalData)('productInfo', productInfo);
|
|
111
|
+
(0, globalData_1.setGlobalData)('token', token);
|
|
112
|
+
(0, globalData_1.setGlobalData)('uid', uid);
|
|
113
|
+
this.setDomain(cloudServiceInfo);
|
|
114
|
+
this.offlineThreshold = offlineThreshold;
|
|
115
|
+
GizLog_1.default.debug(`init sdk success, current version: ${GizwitsMiniSDK.getVersion().data}`);
|
|
116
|
+
this.initLan();
|
|
117
|
+
};
|
|
118
|
+
this.handleBleError = (e) => {
|
|
119
|
+
if (this.listenerMap['onBleHandleError']) {
|
|
120
|
+
this.listenerMap['onBleHandleError'].map((item) => {
|
|
121
|
+
item(e);
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
this.handleBleDevices = (devices) => {
|
|
126
|
+
this.bleDevices = devices;
|
|
127
|
+
};
|
|
128
|
+
this.handleLanDevices = (devices) => {
|
|
129
|
+
this.lanDevices = devices;
|
|
130
|
+
};
|
|
131
|
+
this.handleLanDeviceData = (mac, hexString) => __awaiter(this, void 0, void 0, function* () {
|
|
132
|
+
const target = this.allDevices.find((item) => item.mac === mac);
|
|
133
|
+
if (target) {
|
|
134
|
+
const data = yield (0, dataPoint_1.unpack)((0, tool_1.hexStrint2byte)(hexString), target.productKey);
|
|
135
|
+
if ((data === null || data === void 0 ? void 0 : data.kvData) && this.listenerMap['GizDeviceAttrsNotifications']) {
|
|
136
|
+
this.listenerMap['GizDeviceAttrsNotifications'].map((item) => {
|
|
137
|
+
item({
|
|
138
|
+
device: target,
|
|
139
|
+
data: data === null || data === void 0 ? void 0 : data.kvData,
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
this.handleBleDeviceData = (curDevice, hexString) => __awaiter(this, void 0, void 0, function* () {
|
|
146
|
+
const target = this.allDevices.find((item) => item.bleDeviceID === curDevice.deviceId);
|
|
147
|
+
if (target) {
|
|
148
|
+
const bleBytesData = (0, tool_1.hexStrint2byte)(hexString);
|
|
149
|
+
const parseData = new ProtocolBase_1.default(bleBytesData);
|
|
150
|
+
switch (parseData.cmd) {
|
|
151
|
+
case '0093':
|
|
152
|
+
case '0094':
|
|
153
|
+
case '0090':
|
|
154
|
+
case '0091': {
|
|
155
|
+
console.debug('handleBleDeviceData', hexString, target);
|
|
156
|
+
if (target.passcode) {
|
|
157
|
+
const productConfig = this.productInfo.find(item => item.productKey === target.productKey);
|
|
158
|
+
if (productConfig) {
|
|
159
|
+
const uploadTime = parseInt(`${Date.now() / 1000}`, 10);
|
|
160
|
+
(0, uploadP0_1.uploadP0)({
|
|
161
|
+
device: target,
|
|
162
|
+
data: [
|
|
163
|
+
{
|
|
164
|
+
raw: hexString,
|
|
165
|
+
created_at: uploadTime
|
|
166
|
+
}
|
|
167
|
+
],
|
|
168
|
+
productSecret: productConfig.productSecret,
|
|
169
|
+
}).then(data => {
|
|
170
|
+
GizLog_1.default.debug("设备上报数据:timestemp", new Date(uploadTime * 1000).toLocaleString(), data);
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
const data = yield (0, dataPoint_1.unpack)((0, tool_1.hexStrint2byte)(hexString), target.productKey);
|
|
175
|
+
if ((data === null || data === void 0 ? void 0 : data.kvData) && this.listenerMap['GizDeviceAttrsNotifications']) {
|
|
176
|
+
this.listenerMap['GizDeviceAttrsNotifications'].map((item) => {
|
|
177
|
+
item({
|
|
178
|
+
device: target,
|
|
179
|
+
data: data === null || data === void 0 ? void 0 : data.kvData,
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
case '0062': {
|
|
186
|
+
const resetInfo = new Reset_1.default(bleBytesData);
|
|
187
|
+
GizLog_1.default.debug("收到代理重置事件", resetInfo);
|
|
188
|
+
if (resetInfo.status === 1) {
|
|
189
|
+
this.deviceSafetyApi({
|
|
190
|
+
mac: target.mac,
|
|
191
|
+
productKey: target.productKey,
|
|
192
|
+
isReset: true
|
|
193
|
+
}).then((data) => __awaiter(this, void 0, void 0, function* () {
|
|
194
|
+
GizLog_1.default.debug("代理重置结果", data);
|
|
195
|
+
if (data.success && data.data.successDevices.length > 0) {
|
|
196
|
+
GizLog_1.default.debug("重置成功,回写数据");
|
|
197
|
+
yield this.bleHandle.write(curDevice.deviceId, (0, utils_1.numberArray2Uint8Array)(SetReset_1.default.pack(0)).buffer);
|
|
198
|
+
}
|
|
199
|
+
}));
|
|
200
|
+
}
|
|
201
|
+
break;
|
|
202
|
+
}
|
|
203
|
+
case '0064': {
|
|
204
|
+
break;
|
|
205
|
+
}
|
|
206
|
+
case '0072': {
|
|
207
|
+
const requestSyncData = new OffLineData_1.RequestSync(bleBytesData);
|
|
208
|
+
GizLog_1.default.debug("设备回复是否允许同步, state:", requestSyncData.state);
|
|
209
|
+
this.syncDataLengthCallback && this.syncDataLengthCallback({ success: requestSyncData.state == 0, data: requestSyncData.len });
|
|
210
|
+
if (!this.syncDataCallBack)
|
|
211
|
+
break;
|
|
212
|
+
if (requestSyncData.state === 0) {
|
|
213
|
+
this.syncCurrnetNum = 0;
|
|
214
|
+
this.syncTotalNum = requestSyncData.len;
|
|
215
|
+
this.syncDataCallBack({
|
|
216
|
+
event: 'SYNC_START',
|
|
217
|
+
totalNum: this.syncTotalNum,
|
|
218
|
+
currentNum: this.syncCurrnetNum
|
|
219
|
+
});
|
|
220
|
+
GizLog_1.default.debug("查询第一条离线数据");
|
|
221
|
+
yield this.bleHandle.write(curDevice.deviceId, (0, utils_1.numberArray2Uint8Array)(OffLineData_1.SyncPackage.pack(0)).buffer);
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
GizLog_1.default.debug("设备返回状态为1,同步失败");
|
|
225
|
+
this.syncDataCallBack({
|
|
226
|
+
event: 'SYNC_FAIL',
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
break;
|
|
230
|
+
}
|
|
231
|
+
case '0074': {
|
|
232
|
+
if (!this.syncDataCallBack)
|
|
233
|
+
break;
|
|
234
|
+
GizLog_1.default.debug("设备上报离线数据");
|
|
235
|
+
const syncPackageData = new OffLineData_1.SyncPackage(bleBytesData);
|
|
236
|
+
const deletePackageCmd = OffLineData_1.DeletePackage.pack(syncPackageData.id);
|
|
237
|
+
GizLog_1.default.debug("设备上报离线数据:payload", syncPackageData.payload);
|
|
238
|
+
try {
|
|
239
|
+
GizLog_1.default.debug("设备上报离线数据:timestemp", new Date(syncPackageData.timestemp * 1000).toLocaleString());
|
|
240
|
+
}
|
|
241
|
+
catch (error) {
|
|
242
|
+
}
|
|
243
|
+
const productConfig = this.productInfo.find(item => item.productKey === this.syncDevice.productKey);
|
|
244
|
+
if (productConfig) {
|
|
245
|
+
const uploadRes = yield (0, uploadP0_1.uploadP0)({
|
|
246
|
+
device: this.syncDevice,
|
|
247
|
+
data: [
|
|
248
|
+
{
|
|
249
|
+
raw: (0, tool_1.arrayToString)(syncPackageData.payload),
|
|
250
|
+
created_at: syncPackageData.timestemp
|
|
251
|
+
}
|
|
252
|
+
],
|
|
253
|
+
productSecret: productConfig.productSecret
|
|
254
|
+
});
|
|
255
|
+
GizLog_1.default.debug("上传离线数据结果", uploadRes);
|
|
256
|
+
}
|
|
257
|
+
this.syncCurrnetNum += 1;
|
|
258
|
+
this.syncDataCallBack({
|
|
259
|
+
event: "SYNC_PROGRESS",
|
|
260
|
+
totalNum: this.syncTotalNum,
|
|
261
|
+
currentNum: this.syncCurrnetNum
|
|
262
|
+
});
|
|
263
|
+
GizLog_1.default.debug("通知设备删除:", syncPackageData.id);
|
|
264
|
+
yield this.bleHandle.write(curDevice.deviceId, (0, utils_1.numberArray2Uint8Array)(deletePackageCmd).buffer);
|
|
265
|
+
yield (0, sleep_1.default)(100);
|
|
266
|
+
if (syncPackageData.id < this.syncTotalNum - 1) {
|
|
267
|
+
GizLog_1.default.debug("查询新数据:", syncPackageData.id + 1);
|
|
268
|
+
yield this.bleHandle.write(curDevice.deviceId, (0, utils_1.numberArray2Uint8Array)(OffLineData_1.SyncPackage.pack(syncPackageData.id + 1)).buffer);
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
GizLog_1.default.debug("send cancel sync to device");
|
|
272
|
+
this.bleHandle.write(curDevice.deviceId, (0, utils_1.numberArray2Uint8Array)(OffLineData_1.CancelSync.pack()).buffer);
|
|
273
|
+
this.syncDataCallBack({
|
|
274
|
+
event: "SYNC_END",
|
|
275
|
+
});
|
|
276
|
+
this.cleanSyncDeviceData();
|
|
277
|
+
}
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
case '0078': {
|
|
281
|
+
if (!this.syncDataCallBack)
|
|
282
|
+
break;
|
|
283
|
+
const cancelData = new OffLineData_1.CancelSync(bleBytesData);
|
|
284
|
+
GizLog_1.default.debug("设备上报停止同步 state:", cancelData.state);
|
|
285
|
+
if (cancelData.state === 0) {
|
|
286
|
+
this.syncDataCallBack({
|
|
287
|
+
event: "SYNC_END",
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
else {
|
|
291
|
+
this.syncDataCallBack({
|
|
292
|
+
event: "SYNC_FAIL",
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
this.cleanSyncDeviceData();
|
|
296
|
+
break;
|
|
297
|
+
}
|
|
298
|
+
case '0060': {
|
|
299
|
+
break;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
this.handleSocketDeviceData = (data) => __awaiter(this, void 0, void 0, function* () {
|
|
305
|
+
const target = this.allDevices.find((item) => item.did === data.did);
|
|
306
|
+
if (!target) {
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
if (data.attrs && data.attrs.is_online != null) {
|
|
310
|
+
const isOnline = data.attrs.is_online;
|
|
311
|
+
this.setDeviceMeta(target, 'isOnline', isOnline);
|
|
312
|
+
}
|
|
313
|
+
if (this.listenerMap['GizDeviceAttrsNotifications']) {
|
|
314
|
+
if (data.attrs) {
|
|
315
|
+
data.attrs = yield (0, dataPoint_1.formatEnum)(data.attrs, target.productKey, 'int');
|
|
316
|
+
}
|
|
317
|
+
this.listenerMap['GizDeviceAttrsNotifications'].map((item) => {
|
|
318
|
+
item(Object.assign({ device: target }, ('attrs' in data ? { data: data.attrs } : { raw: data.raw })));
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
this.handleSocketStatus = (dids, enabled) => {
|
|
323
|
+
const devices = this.allDevices.filter((item) => dids.includes(item.did) && item.connectType === 'WAN');
|
|
324
|
+
GizLog_1.default.log('handleSocketStatus', dids, enabled);
|
|
325
|
+
devices.forEach((item) => {
|
|
326
|
+
this.setDeviceMeta(item, 'connectType', enabled ? 'WAN' : 'NONE');
|
|
327
|
+
});
|
|
328
|
+
};
|
|
329
|
+
this.notiDeviceList = () => {
|
|
330
|
+
var _a, _b;
|
|
331
|
+
(_a = this.listenerMap['GizDeviceListNotifications']) === null || _a === void 0 ? void 0 : _a.map((notifyFn) => {
|
|
332
|
+
notifyFn(this.allDevices);
|
|
333
|
+
});
|
|
334
|
+
(_b = this.listenerMap['onScanListChange']) === null || _b === void 0 ? void 0 : _b.map((notifyFn) => {
|
|
335
|
+
notifyFn(this.bleScanDevice);
|
|
336
|
+
});
|
|
337
|
+
};
|
|
338
|
+
this.initLan = () => {
|
|
339
|
+
try {
|
|
340
|
+
this.lanHandle.init();
|
|
341
|
+
this.lanHandle.addEventListener('GizLanDeviceList', this.handleLanDevices);
|
|
342
|
+
this.lanHandle.addEventListener('GizLanDeviceData', this.handleLanDeviceData);
|
|
343
|
+
}
|
|
344
|
+
catch (error) {
|
|
345
|
+
GizLog_1.default.error('init lan error', error);
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
this.startAutoScan = (services) => {
|
|
349
|
+
const autoTime = 9000;
|
|
350
|
+
if (this.keepScanTimer) {
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
this.keepScanTimer = setInterval(() => __awaiter(this, void 0, void 0, function* () {
|
|
354
|
+
this.bleHandle.startScan(-1, services);
|
|
355
|
+
}), autoTime);
|
|
356
|
+
this.bleHandle.startScan(-1, services);
|
|
357
|
+
};
|
|
358
|
+
this.stopAutoScan = () => {
|
|
359
|
+
this.keepScanTimer && clearInterval(this.keepScanTimer);
|
|
360
|
+
this.bleHandle.stopScan();
|
|
361
|
+
};
|
|
362
|
+
this.setDeviceMeta = (curDev, key, value, force = false) => {
|
|
363
|
+
const target = this.allDevices.find((dev) => dev.mac === curDev.mac);
|
|
364
|
+
if (!target || this.allDevices.length === 0) {
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
if (curDev[key] !== value || force) {
|
|
368
|
+
target[key] = value;
|
|
369
|
+
this.notiDeviceList();
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
this.setDevicesMeta = (devices, key, value, force = false) => {
|
|
373
|
+
let hasChange = false;
|
|
374
|
+
const targets = devices.map((dev) => this.allDevices.find((item) => item.mac === dev.mac));
|
|
375
|
+
for (let i = 0; i < targets.length; i++) {
|
|
376
|
+
const target = targets[i];
|
|
377
|
+
if (target) {
|
|
378
|
+
if (target[key] !== value || force) {
|
|
379
|
+
target[key] = value;
|
|
380
|
+
hasChange = true;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
hasChange && this.notiDeviceList();
|
|
385
|
+
};
|
|
386
|
+
this.initBle = () => __awaiter(this, void 0, void 0, function* () {
|
|
387
|
+
try {
|
|
388
|
+
this.bleHandle.addEventListener('GizBleDeviceData', this.handleBleDeviceData);
|
|
389
|
+
this.bleHandle.addEventListener('GizBleDeviceList', this.handleBleDevices);
|
|
390
|
+
this.bleHandle.addEventListener('GizBleError', this.handleBleError);
|
|
391
|
+
const permissionData = yield this.bleHandle.checkPermission();
|
|
392
|
+
if (!permissionData.success) {
|
|
393
|
+
GizLog_1.default.error('init BLE Error', new Error(JSON.stringify(permissionData)));
|
|
394
|
+
return {
|
|
395
|
+
success: false,
|
|
396
|
+
err: errorCode_1.default.GIZ_SDK_BLE_BLUETOOTH_FUNCTION_NOT_TURNED_ON,
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
GizLog_1.default.debug('init BLE succcess', permissionData);
|
|
400
|
+
}
|
|
401
|
+
catch (error) {
|
|
402
|
+
GizLog_1.default.error('init BLE error', error);
|
|
403
|
+
return { success: false, err: errorCode_1.default.GIZ_SDK_BLE_BLUETOOTH_FUNCTION_NOT_TURNED_ON };
|
|
404
|
+
}
|
|
405
|
+
return { success: true };
|
|
406
|
+
});
|
|
407
|
+
this.scanBleDevice = (delay, services) => __awaiter(this, void 0, void 0, function* () {
|
|
408
|
+
this.bleHandle.scanList = [];
|
|
409
|
+
const data = yield this.bleHandle.startScan(delay, services);
|
|
410
|
+
return {
|
|
411
|
+
success: true,
|
|
412
|
+
data: data.scanList,
|
|
413
|
+
};
|
|
414
|
+
});
|
|
415
|
+
this.write = (device, attrs) => __awaiter(this, void 0, void 0, function* () {
|
|
416
|
+
const target = this.allDevices.find((item) => (0, utils_1.isSameDevice)(item, device));
|
|
417
|
+
if (!target) {
|
|
418
|
+
return { success: false, message: 'target is undefind' };
|
|
419
|
+
}
|
|
420
|
+
if (target.connectType !== 'NONE') {
|
|
421
|
+
const { data: value, raw } = yield (0, dataPoint_1.pack)(attrs, target.productKey);
|
|
422
|
+
switch (target.connectType) {
|
|
423
|
+
case 'BLE_BROADCAST':
|
|
424
|
+
return yield this.bleHandle.writeBoradcast(target.bleDeviceID, raw.cmd);
|
|
425
|
+
case 'BLE': {
|
|
426
|
+
if (value) {
|
|
427
|
+
return yield this.bleHandle.write(target.bleDeviceID, (0, utils_1.numberArray2Uint8Array)(value).buffer);
|
|
428
|
+
}
|
|
429
|
+
else {
|
|
430
|
+
return {
|
|
431
|
+
success: false,
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
case 'WAN': {
|
|
436
|
+
const newAttrs = yield (0, dataPoint_1.formatEnum)(attrs, target.productKey, 'string');
|
|
437
|
+
return this.socketHandle.writeData(target, newAttrs);
|
|
438
|
+
}
|
|
439
|
+
case 'LAN': {
|
|
440
|
+
return this.lanHandle.write(target, (0, utils_1.numberArray2Uint8Array)(value).buffer);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
return {
|
|
445
|
+
success: false,
|
|
446
|
+
};
|
|
447
|
+
});
|
|
448
|
+
this.writeRaw = (device, data) => __awaiter(this, void 0, void 0, function* () {
|
|
449
|
+
const target = this.allDevices.find((item) => (0, utils_1.isSameDevice)(item, device));
|
|
450
|
+
if (!target) {
|
|
451
|
+
return { success: false, message: 'target is undefind' };
|
|
452
|
+
}
|
|
453
|
+
if (target.connectType !== 'NONE') {
|
|
454
|
+
switch (target.connectType) {
|
|
455
|
+
case 'BLE_BROADCAST':
|
|
456
|
+
return yield this.bleHandle.writeBoradcast(target.bleDeviceID, data);
|
|
457
|
+
case 'BLE': {
|
|
458
|
+
if (data) {
|
|
459
|
+
return yield this.bleHandle.write(target.bleDeviceID, (0, utils_1.numberArray2Uint8Array)(data).buffer);
|
|
460
|
+
}
|
|
461
|
+
else {
|
|
462
|
+
return {
|
|
463
|
+
success: false,
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
case 'WAN': {
|
|
468
|
+
return this.socketHandle.send(target.did, data);
|
|
469
|
+
}
|
|
470
|
+
case 'LAN': {
|
|
471
|
+
return this.lanHandle.write(target, (0, utils_1.numberArray2Uint8Array)(data).buffer);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
return {
|
|
476
|
+
success: false,
|
|
477
|
+
};
|
|
478
|
+
});
|
|
479
|
+
this.setDeviceTimeStamp = (device) => __awaiter(this, void 0, void 0, function* () {
|
|
480
|
+
const target = this.allDevices.find((item) => (0, utils_1.isSameDevice)(item, device));
|
|
481
|
+
if (!target) {
|
|
482
|
+
return { success: false, message: 'target is undefind' };
|
|
483
|
+
}
|
|
484
|
+
const data = Ntp_1.default.pack();
|
|
485
|
+
switch (target.connectType) {
|
|
486
|
+
case 'BLE': {
|
|
487
|
+
return this.bleHandle.write(device.bleDeviceID, (0, utils_1.numberArray2Uint8Array)(data).buffer);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
return {
|
|
491
|
+
success: false,
|
|
492
|
+
message: 'the connectType not support set timestamp'
|
|
493
|
+
};
|
|
494
|
+
});
|
|
495
|
+
this.syncDeviceData = (device, callback = () => { }) => __awaiter(this, void 0, void 0, function* () {
|
|
496
|
+
const target = this.allDevices.find((item) => (0, utils_1.isSameDevice)(item, device));
|
|
497
|
+
if (!target) {
|
|
498
|
+
return { success: false, message: 'target is undefind' };
|
|
499
|
+
}
|
|
500
|
+
if (!target.isBind) {
|
|
501
|
+
return { success: false, message: 'Only the bound devices are supported' };
|
|
502
|
+
}
|
|
503
|
+
if (this.syncDevice) {
|
|
504
|
+
return {
|
|
505
|
+
success: false,
|
|
506
|
+
message: 'Synchronizing'
|
|
507
|
+
};
|
|
508
|
+
}
|
|
509
|
+
this.syncDevice = Object.assign({}, target);
|
|
510
|
+
switch (target.connectType) {
|
|
511
|
+
case 'BLE': {
|
|
512
|
+
GizLog_1.default.debug("start syncDeviceData", device);
|
|
513
|
+
this.syncDataCallBack = callback;
|
|
514
|
+
this.syncDataTimoutTimer = setTimeout(() => {
|
|
515
|
+
this.syncDataCallBack({
|
|
516
|
+
event: 'SYNC_FAIL',
|
|
517
|
+
message: 'sync timeout'
|
|
518
|
+
});
|
|
519
|
+
this.cleanSyncDeviceData();
|
|
520
|
+
}, this.SYNC_TIMEOUT);
|
|
521
|
+
const data = OffLineData_1.RequestSync.pack();
|
|
522
|
+
GizLog_1.default.debug("sent RequestSync data");
|
|
523
|
+
return this.bleHandle.write(device.bleDeviceID, (0, utils_1.numberArray2Uint8Array)(data).buffer);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
return {
|
|
527
|
+
success: false,
|
|
528
|
+
message: 'the connectType not support sync data'
|
|
529
|
+
};
|
|
530
|
+
});
|
|
531
|
+
this.queryNeedSyncDataLength = (device) => __awaiter(this, void 0, void 0, function* () {
|
|
532
|
+
const target = this.allDevices.find((item) => (0, utils_1.isSameDevice)(item, device));
|
|
533
|
+
if (!target) {
|
|
534
|
+
return { success: false, message: 'target is undefind' };
|
|
535
|
+
}
|
|
536
|
+
if (!target.isBind) {
|
|
537
|
+
return { success: false, message: 'Only the bound devices are supported' };
|
|
538
|
+
}
|
|
539
|
+
switch (target.connectType) {
|
|
540
|
+
case 'BLE': {
|
|
541
|
+
const data = OffLineData_1.RequestSync.pack();
|
|
542
|
+
this.syncDevice = Object.assign({}, target);
|
|
543
|
+
GizLog_1.default.debug("sent RequestSync data len");
|
|
544
|
+
this.bleHandle.write(device.bleDeviceID, (0, utils_1.numberArray2Uint8Array)(data).buffer);
|
|
545
|
+
return new Promise((res) => {
|
|
546
|
+
this.syncDataLengthCallback = res;
|
|
547
|
+
setTimeout(() => {
|
|
548
|
+
try {
|
|
549
|
+
res({ success: false, message: 'timeout' });
|
|
550
|
+
}
|
|
551
|
+
catch (error) {
|
|
552
|
+
}
|
|
553
|
+
}, 3000);
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
return {
|
|
558
|
+
success: false,
|
|
559
|
+
message: 'the connectType not support sync data'
|
|
560
|
+
};
|
|
561
|
+
});
|
|
562
|
+
this.cancelSyncDeviceData = () => __awaiter(this, void 0, void 0, function* () {
|
|
563
|
+
this.cleanSyncDeviceData();
|
|
564
|
+
});
|
|
565
|
+
this.cleanSyncDeviceData = () => {
|
|
566
|
+
if (this.syncDataCallBack) {
|
|
567
|
+
this.syncDataCallBack({
|
|
568
|
+
event: 'SYNC_CANCEL'
|
|
569
|
+
});
|
|
570
|
+
this.syncDataCallBack = null;
|
|
571
|
+
}
|
|
572
|
+
this.syncCurrnetNum = 0;
|
|
573
|
+
this.syncTotalNum = 0;
|
|
574
|
+
this.syncDevice = null;
|
|
575
|
+
this.syncDataTimoutTimer && clearTimeout(this.syncDataTimoutTimer);
|
|
576
|
+
};
|
|
577
|
+
this.getProductConfig = (pk) => __awaiter(this, void 0, void 0, function* () {
|
|
578
|
+
const data = yield productConfigFileManage_1.default.getConfigFile(pk);
|
|
579
|
+
if (data) {
|
|
580
|
+
return {
|
|
581
|
+
success: true,
|
|
582
|
+
data,
|
|
583
|
+
};
|
|
584
|
+
}
|
|
585
|
+
return {
|
|
586
|
+
success: false,
|
|
587
|
+
};
|
|
588
|
+
});
|
|
589
|
+
this.stopScanBleDevice = () => {
|
|
590
|
+
this.bleHandle.stopScan();
|
|
591
|
+
};
|
|
592
|
+
this.login = (openID) => __awaiter(this, void 0, void 0, function* () {
|
|
593
|
+
var _a, _b;
|
|
594
|
+
const data = yield (0, login_1.AnonymousLogin)({ uid: openID });
|
|
595
|
+
if (data.success) {
|
|
596
|
+
(0, globalData_1.setGlobalData)('token', (_a = data.data) === null || _a === void 0 ? void 0 : _a.token);
|
|
597
|
+
(0, globalData_1.setGlobalData)('uid', (_b = data.data) === null || _b === void 0 ? void 0 : _b.uid);
|
|
598
|
+
this.socketHandle.updateToken({ token: data.data.token, uid: data.data.uid });
|
|
599
|
+
}
|
|
600
|
+
return data;
|
|
601
|
+
});
|
|
602
|
+
this.getDeviceStatus = (device, attrNames) => __awaiter(this, void 0, void 0, function* () {
|
|
603
|
+
const target = this.allDevices.find((item) => (0, utils_1.isSameDevice)(item, device));
|
|
604
|
+
if (!target) {
|
|
605
|
+
return { success: false, message: 'target is undefind' };
|
|
606
|
+
}
|
|
607
|
+
switch (target.connectType) {
|
|
608
|
+
case 'BLE': {
|
|
609
|
+
return yield this.bleHandle.getDeviceStatus(target.bleDeviceID, target.productKey, target.rootDeviceId, attrNames);
|
|
610
|
+
}
|
|
611
|
+
case 'WAN':
|
|
612
|
+
return this.socketHandle.readStatus(target, attrNames);
|
|
613
|
+
default: {
|
|
614
|
+
return {
|
|
615
|
+
success: false,
|
|
616
|
+
message: 'Device connectType is unsupported',
|
|
617
|
+
};
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
});
|
|
621
|
+
this.subscribe = (device, connectType, _autoGetDeviceStatus = true) => __awaiter(this, void 0, void 0, function* () {
|
|
622
|
+
GizLog_1.default.debug("subscribe device:", device);
|
|
623
|
+
let target = this.allDevices.find((item) => (0, utils_1.isSameDevice)(item, device));
|
|
624
|
+
if (!target) {
|
|
625
|
+
GizLog_1.default.warn('Device Target Not Found');
|
|
626
|
+
GizLog_1.default.warn('current devices ', this.allDevices);
|
|
627
|
+
return { success: false, message: "device not found" };
|
|
628
|
+
}
|
|
629
|
+
if (target.connectType === 'BLE_BROADCAST') {
|
|
630
|
+
return {
|
|
631
|
+
success: true,
|
|
632
|
+
message: '',
|
|
633
|
+
};
|
|
634
|
+
}
|
|
635
|
+
const type = (0, utils_1.getFirstConnectType)(target, connectType);
|
|
636
|
+
if (target.connectType !== 'NONE' && target.connectType !== type) {
|
|
637
|
+
yield this.unSubscribe(target);
|
|
638
|
+
}
|
|
639
|
+
switch (type) {
|
|
640
|
+
case 'BLE': {
|
|
641
|
+
const data = yield this.bleHandle.connectDevice(target);
|
|
642
|
+
if (data.success) {
|
|
643
|
+
this.setDeviceMeta(target, 'connectType', 'BLE', true);
|
|
644
|
+
setTimeout(() => {
|
|
645
|
+
this.setDeviceTimeStamp(target);
|
|
646
|
+
}, 250);
|
|
647
|
+
setTimeout(() => {
|
|
648
|
+
this.getDeviceStatus(target);
|
|
649
|
+
}, 500);
|
|
650
|
+
}
|
|
651
|
+
return data;
|
|
652
|
+
}
|
|
653
|
+
case 'WAN': {
|
|
654
|
+
const data = this.socketHandle.connectDevice(target);
|
|
655
|
+
if (data.success) {
|
|
656
|
+
this.setDeviceMeta(device, 'connectType', 'WAN', true);
|
|
657
|
+
}
|
|
658
|
+
return data;
|
|
659
|
+
}
|
|
660
|
+
case 'LAN': {
|
|
661
|
+
const data = yield this.lanHandle.connectDevice(target);
|
|
662
|
+
if (data.success) {
|
|
663
|
+
this.setDeviceMeta(device, 'connectType', 'LAN', true);
|
|
664
|
+
}
|
|
665
|
+
return data;
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
return {
|
|
669
|
+
success: false,
|
|
670
|
+
message: '',
|
|
671
|
+
};
|
|
672
|
+
});
|
|
673
|
+
this.batchSubscribe = (devices, connectType) => __awaiter(this, void 0, void 0, function* () {
|
|
674
|
+
var _c, _d, _e;
|
|
675
|
+
GizLog_1.default.debug("subscribe devices =>", devices);
|
|
676
|
+
const sdkDevicesMap = this.allDevices.reduce((acc, cur) => (Object.assign(Object.assign({}, acc), { [(0, utils_1.getDeviceId)(cur)]: cur })), {});
|
|
677
|
+
const foundDevices = devices.filter((device) => sdkDevicesMap[(0, utils_1.getDeviceId)(device)]);
|
|
678
|
+
if (foundDevices.length === 0) {
|
|
679
|
+
GizLog_1.default.warn('Device Target Not Found');
|
|
680
|
+
GizLog_1.default.warn('current devices ', this.allDevices);
|
|
681
|
+
return { success: false, message: "device not found" };
|
|
682
|
+
}
|
|
683
|
+
const connectTypeMap = foundDevices.reduce((acc, cur) => {
|
|
684
|
+
const type = cur.connectType === 'BLE_BROADCAST' ? 'BLE_BROADCAST' : (0, utils_1.getFirstConnectType)(cur, connectType);
|
|
685
|
+
if (!acc[type]) {
|
|
686
|
+
acc[type] = [];
|
|
687
|
+
}
|
|
688
|
+
acc[type].push(cur);
|
|
689
|
+
return acc;
|
|
690
|
+
}, {});
|
|
691
|
+
const types = Object.keys(connectTypeMap);
|
|
692
|
+
if (types.length === 1 && types[0] === 'BLE_BROADCAST') {
|
|
693
|
+
return {
|
|
694
|
+
success: true,
|
|
695
|
+
message: '',
|
|
696
|
+
};
|
|
697
|
+
}
|
|
698
|
+
if (((_c = connectTypeMap['BLE']) !== null && _c !== void 0 ? _c : []).length > 0 || ((_d = connectTypeMap['LAN']) !== null && _d !== void 0 ? _d : []).length > 0) {
|
|
699
|
+
return {
|
|
700
|
+
success: false,
|
|
701
|
+
message: 'BLE and LAN can not subscribe together'
|
|
702
|
+
};
|
|
703
|
+
}
|
|
704
|
+
const wanDevices = (_e = connectTypeMap['WAN']) !== null && _e !== void 0 ? _e : [];
|
|
705
|
+
if (wanDevices.length === 0) {
|
|
706
|
+
return {
|
|
707
|
+
success: false,
|
|
708
|
+
message: 'no WAN device'
|
|
709
|
+
};
|
|
710
|
+
}
|
|
711
|
+
const wanUnsubscribeDevices = wanDevices.filter((device) => {
|
|
712
|
+
return device.connectType !== 'NONE' && device.connectType !== 'WAN';
|
|
713
|
+
});
|
|
714
|
+
if (wanUnsubscribeDevices.length > 0) {
|
|
715
|
+
yield this.batchWanUnSubscribe(wanUnsubscribeDevices);
|
|
716
|
+
}
|
|
717
|
+
const data = this.socketHandle.connectDevices(wanDevices);
|
|
718
|
+
if (data.success) {
|
|
719
|
+
this.setDevicesMeta(wanDevices, 'connectType', 'WAN', true);
|
|
720
|
+
}
|
|
721
|
+
return data;
|
|
722
|
+
});
|
|
723
|
+
this.batchWanUnSubscribe = (devices) => __awaiter(this, void 0, void 0, function* () {
|
|
724
|
+
const res = yield this.socketHandle.disConnectDevices(devices);
|
|
725
|
+
if (res.success) {
|
|
726
|
+
this.setDevicesMeta(devices, 'connectType', 'NONE', true);
|
|
727
|
+
}
|
|
728
|
+
return res;
|
|
729
|
+
});
|
|
730
|
+
this.unSubscribe = (device) => __awaiter(this, void 0, void 0, function* () {
|
|
731
|
+
const target = this.allDevices.find((item) => (0, utils_1.isSameDevice)(item, device));
|
|
732
|
+
switch (target.connectType) {
|
|
733
|
+
case 'BLE': {
|
|
734
|
+
const disconnectRes = yield this.bleHandle.disConnectDevice(target.bleDeviceID);
|
|
735
|
+
if (disconnectRes.success) {
|
|
736
|
+
this.setDeviceMeta(target, 'connectType', 'NONE', true);
|
|
737
|
+
}
|
|
738
|
+
return disconnectRes;
|
|
739
|
+
}
|
|
740
|
+
case 'WAN': {
|
|
741
|
+
const res = this.socketHandle.disConnectDevice(target);
|
|
742
|
+
if (res.success) {
|
|
743
|
+
this.setDeviceMeta(target, 'connectType', 'NONE', true);
|
|
744
|
+
}
|
|
745
|
+
return res;
|
|
746
|
+
}
|
|
747
|
+
case 'LAN': {
|
|
748
|
+
const res = this.lanHandle.disConnectDevice(target);
|
|
749
|
+
if (res.success) {
|
|
750
|
+
this.setDeviceMeta(target, 'connectType', 'NONE', true);
|
|
751
|
+
}
|
|
752
|
+
return res;
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
return {
|
|
756
|
+
success: false,
|
|
757
|
+
};
|
|
758
|
+
});
|
|
759
|
+
this.bindRemoteDevice = ({ mac, productKey, alias, beOwner, }) => __awaiter(this, void 0, void 0, function* () {
|
|
760
|
+
const target = (0, utils_1.getProductInfoThroughPK)(productKey, this.productInfo);
|
|
761
|
+
if (target) {
|
|
762
|
+
const data = yield (0, devices_1.bindMac)({
|
|
763
|
+
mac,
|
|
764
|
+
productKey,
|
|
765
|
+
productSecret: target.productSecret,
|
|
766
|
+
alias,
|
|
767
|
+
beOwner,
|
|
768
|
+
});
|
|
769
|
+
this.getBindingList();
|
|
770
|
+
return data;
|
|
771
|
+
}
|
|
772
|
+
return {
|
|
773
|
+
success: false,
|
|
774
|
+
err: errorCode_1.default.GIZ_SDK_PRODUCTKEY_NOT_IN_SPECIFY,
|
|
775
|
+
};
|
|
776
|
+
});
|
|
777
|
+
this.checkUpdate = ({ device, type }) => __awaiter(this, void 0, void 0, function* () {
|
|
778
|
+
const target = this.allDevices.find((item) => (0, utils_1.isSameDevice)(item, device));
|
|
779
|
+
if (target) {
|
|
780
|
+
if (!target.did) {
|
|
781
|
+
return {
|
|
782
|
+
success: false,
|
|
783
|
+
err: errorCode_1.default.GIZ_OPENAPI_DEVICE_NOT_BOUND,
|
|
784
|
+
};
|
|
785
|
+
}
|
|
786
|
+
switch (target.connectType) {
|
|
787
|
+
case 'BLE': {
|
|
788
|
+
return this.bleHandle.checkUpdate({ device, type });
|
|
789
|
+
}
|
|
790
|
+
default: {
|
|
791
|
+
return {
|
|
792
|
+
success: false,
|
|
793
|
+
err: errorCode_1.default.GIZ_SDK_OTA_DEVICE_NOT_SUPPORT,
|
|
794
|
+
};
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
return {
|
|
799
|
+
success: false,
|
|
800
|
+
err: errorCode_1.default.GIZ_SDK_DEVICE_NOT_SUBSCRIBED,
|
|
801
|
+
};
|
|
802
|
+
});
|
|
803
|
+
this.getDeviceInfo = ({ device }) => __awaiter(this, void 0, void 0, function* () {
|
|
804
|
+
const target = this.allDevices.find((item) => (0, utils_1.isSameDevice)(item, device));
|
|
805
|
+
if (target) {
|
|
806
|
+
if (!target.did) {
|
|
807
|
+
return {
|
|
808
|
+
success: false,
|
|
809
|
+
err: errorCode_1.default.GIZ_OPENAPI_DEVICE_NOT_BOUND,
|
|
810
|
+
};
|
|
811
|
+
}
|
|
812
|
+
switch (target.connectType) {
|
|
813
|
+
case 'BLE': {
|
|
814
|
+
return this.bleHandle.getDeviceInfo({ deviceId: device.bleDeviceID });
|
|
815
|
+
}
|
|
816
|
+
default: {
|
|
817
|
+
return {
|
|
818
|
+
success: false,
|
|
819
|
+
err: errorCode_1.default.GIZ_SDK_OTA_DEVICE_NOT_SUPPORT,
|
|
820
|
+
};
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
return {
|
|
825
|
+
success: false,
|
|
826
|
+
err: errorCode_1.default.GIZ_SDK_DEVICE_NOT_SUBSCRIBED,
|
|
827
|
+
};
|
|
828
|
+
});
|
|
829
|
+
this.startUpgrade = ({ device, type, onProgress, onEvent }) => __awaiter(this, void 0, void 0, function* () {
|
|
830
|
+
const target = this.allDevices.find((item) => (0, utils_1.isSameDevice)(item, device));
|
|
831
|
+
if (target) {
|
|
832
|
+
if (!target.did) {
|
|
833
|
+
return {
|
|
834
|
+
success: false,
|
|
835
|
+
err: errorCode_1.default.GIZ_OPENAPI_DEVICE_NOT_BOUND,
|
|
836
|
+
};
|
|
837
|
+
}
|
|
838
|
+
switch (target.connectType) {
|
|
839
|
+
case 'BLE': {
|
|
840
|
+
return this.bleHandle.startUpgrade({ device, type, productKey: device.productKey, onProgress, onEvent });
|
|
841
|
+
}
|
|
842
|
+
default: {
|
|
843
|
+
return {
|
|
844
|
+
success: false,
|
|
845
|
+
err: errorCode_1.default.GIZ_SDK_OTA_DEVICE_NOT_SUPPORT,
|
|
846
|
+
};
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
return {
|
|
851
|
+
success: false,
|
|
852
|
+
err: errorCode_1.default.GIZ_SDK_DEVICE_NOT_SUBSCRIBED,
|
|
853
|
+
};
|
|
854
|
+
});
|
|
855
|
+
this.unbindDevice = ({ devices, }) => __awaiter(this, void 0, void 0, function* () {
|
|
856
|
+
const data = yield (0, devices_1.unbindDevice)({ devices });
|
|
857
|
+
if (data.success) {
|
|
858
|
+
this.getBindingList();
|
|
859
|
+
}
|
|
860
|
+
return data;
|
|
861
|
+
});
|
|
862
|
+
this.deviceSafetyRegister = ({ mac, productKey, }) => __awaiter(this, void 0, void 0, function* () {
|
|
863
|
+
return yield this.deviceSafetyApi({ mac, productKey, isReset: false });
|
|
864
|
+
});
|
|
865
|
+
this.deviceSafetyUnbind = ({ mac, productKey, }) => __awaiter(this, void 0, void 0, function* () {
|
|
866
|
+
return yield this.deviceSafetyApi({ mac, productKey, isReset: true });
|
|
867
|
+
});
|
|
868
|
+
this.setDeviceOnboardingDeploy = ({ ssid, bssid = '', password, mode = 0, timeout, isBind = true, softAPSSIDPrefix, eventCallBack }) => __awaiter(this, void 0, void 0, function* () {
|
|
869
|
+
if (this.currentWifiConfigHandle) {
|
|
870
|
+
return {
|
|
871
|
+
success: false,
|
|
872
|
+
err: errorCode_1.default.GIZ_SDK_DEVICE_CONFIG_IS_RUNNING,
|
|
873
|
+
};
|
|
874
|
+
}
|
|
875
|
+
const sTime = new Date().getTime();
|
|
876
|
+
GizLog_1.default.debug('GIZ_SDK: 配网开始时间:' + sTime, mode);
|
|
877
|
+
try {
|
|
878
|
+
switch (mode) {
|
|
879
|
+
case 0: {
|
|
880
|
+
this.currentWifiConfigHandle = new ap_1.default(ssid, bssid, password, this.specialProductKeys, this.specialProductKeySecrets);
|
|
881
|
+
const data = yield this.currentWifiConfigHandle.setDeviceOnboardingDeploy({
|
|
882
|
+
timeout,
|
|
883
|
+
isBind,
|
|
884
|
+
softAPSSIDPrefix,
|
|
885
|
+
eventCallBack,
|
|
886
|
+
});
|
|
887
|
+
this.currentWifiConfigHandle = null;
|
|
888
|
+
const eTime = new Date().getTime();
|
|
889
|
+
GizLog_1.default.debug('GIZ_SDK: 配网结束时间:' + eTime);
|
|
890
|
+
GizLog_1.default.debug('GIZ_SDK: 配网用时:' + (eTime - sTime));
|
|
891
|
+
return data;
|
|
892
|
+
}
|
|
893
|
+
case 3: {
|
|
894
|
+
this.currentWifiConfigHandle = new ble_2.default(ssid, bssid, password, this.specialProductKeys, this.specialProductKeySecrets, this.bleHandle);
|
|
895
|
+
const data = yield this.currentWifiConfigHandle.setDeviceOnboardingDeploy({
|
|
896
|
+
timeout,
|
|
897
|
+
isBind,
|
|
898
|
+
softAPSSIDPrefix,
|
|
899
|
+
eventCallBack,
|
|
900
|
+
});
|
|
901
|
+
this.currentWifiConfigHandle = null;
|
|
902
|
+
const eTime = new Date().getTime();
|
|
903
|
+
GizLog_1.default.debug('GIZ_SDK: 配网结束时间:' + eTime);
|
|
904
|
+
GizLog_1.default.debug('GIZ_SDK: 配网用时:' + (eTime - sTime));
|
|
905
|
+
return data;
|
|
906
|
+
}
|
|
907
|
+
case 5: {
|
|
908
|
+
const sysInfo = yield wx.getSystemInfoSync();
|
|
909
|
+
console.log('sysInfosysInfo', sysInfo);
|
|
910
|
+
if (sysInfo.platform !== 'android') {
|
|
911
|
+
return {
|
|
912
|
+
success: false,
|
|
913
|
+
message: '只支持安卓'
|
|
914
|
+
};
|
|
915
|
+
}
|
|
916
|
+
const cloudServiceInfo = (0, globalData_1.getGlobalData)('cloudServiceInfo');
|
|
917
|
+
console.log('cloudServiceInfo', cloudServiceInfo);
|
|
918
|
+
this.currentWifiConfigHandle = new nfc_1.default(ssid, bssid, password, this.specialProductKeys, this.specialProductKeySecrets, (cloudServiceInfo === null || cloudServiceInfo === void 0 ? void 0 : cloudServiceInfo.openAPIInfo) || 'api.gizwits.com');
|
|
919
|
+
const data = yield this.currentWifiConfigHandle.setDeviceOnboardingDeploy({
|
|
920
|
+
timeout,
|
|
921
|
+
isBind,
|
|
922
|
+
softAPSSIDPrefix,
|
|
923
|
+
eventCallBack,
|
|
924
|
+
});
|
|
925
|
+
this.currentWifiConfigHandle = null;
|
|
926
|
+
const eTime = new Date().getTime();
|
|
927
|
+
GizLog_1.default.debug('GIZ_SDK: 配网结束时间:' + eTime);
|
|
928
|
+
GizLog_1.default.debug('GIZ_SDK: 配网用时:' + (eTime - sTime));
|
|
929
|
+
return data;
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
catch (error) {
|
|
934
|
+
GizLog_1.default.error("GIZ_SDK: setDeviceOnboardingDeploy", error);
|
|
935
|
+
if (error.err &&
|
|
936
|
+
error.err.errorCode) {
|
|
937
|
+
return error;
|
|
938
|
+
}
|
|
939
|
+
return {
|
|
940
|
+
success: false,
|
|
941
|
+
err: errorCode_1.default.GIZ_SDK_OTHERWISE,
|
|
942
|
+
};
|
|
943
|
+
}
|
|
944
|
+
finally {
|
|
945
|
+
this.currentWifiConfigHandle = null;
|
|
946
|
+
}
|
|
947
|
+
});
|
|
948
|
+
this.setDomain = (cloudServiceInfo) => {
|
|
949
|
+
const openAPIInfo = (cloudServiceInfo && cloudServiceInfo.openAPIInfo) || 'api.gizwits.com';
|
|
950
|
+
const newData = Object.assign(Object.assign({}, (cloudServiceInfo || {})), { openAPIInfo });
|
|
951
|
+
(0, globalData_1.setGlobalData)('cloudServiceInfo', newData);
|
|
952
|
+
};
|
|
953
|
+
this.deviceSafetyApi = ({ mac, productKey, isReset, }) => __awaiter(this, void 0, void 0, function* () {
|
|
954
|
+
const target = (0, utils_1.getProductInfoThroughPK)(productKey, this.productInfo);
|
|
955
|
+
if (target) {
|
|
956
|
+
return yield (0, devices_1.safeRegister)({
|
|
957
|
+
productKey,
|
|
958
|
+
productSecret: target.productSecret,
|
|
959
|
+
mac,
|
|
960
|
+
isReset,
|
|
961
|
+
passcode: '1234567890',
|
|
962
|
+
});
|
|
963
|
+
}
|
|
964
|
+
return {
|
|
965
|
+
success: false,
|
|
966
|
+
err: errorCode_1.default.GIZ_SDK_PRODUCTKEY_NOT_IN_SPECIFY,
|
|
967
|
+
};
|
|
968
|
+
});
|
|
969
|
+
this.getBindingList = () => __awaiter(this, void 0, void 0, function* () {
|
|
970
|
+
const data = yield (0, devices_1.getBindingList)();
|
|
971
|
+
this.deviceList = data.data || [];
|
|
972
|
+
this.notiDeviceList();
|
|
973
|
+
return data;
|
|
974
|
+
});
|
|
975
|
+
this.getDevices = () => __awaiter(this, void 0, void 0, function* () {
|
|
976
|
+
yield this.getBindingList();
|
|
977
|
+
return {
|
|
978
|
+
success: true,
|
|
979
|
+
data: this.allDevices,
|
|
980
|
+
};
|
|
981
|
+
});
|
|
982
|
+
this.stopDeviceOnboardingDeploy = () => {
|
|
983
|
+
if (this.currentWifiConfigHandle) {
|
|
984
|
+
this.currentWifiConfigHandle.stopDeviceOnboardingDeploy();
|
|
985
|
+
this.currentWifiConfigHandle = null;
|
|
986
|
+
}
|
|
987
|
+
};
|
|
988
|
+
this.setLogLevel = (level) => {
|
|
989
|
+
return GizLog_1.default.setLogLevel(level);
|
|
990
|
+
};
|
|
991
|
+
this.renameDevice = (device, name) => __awaiter(this, void 0, void 0, function* () {
|
|
992
|
+
if (!name) {
|
|
993
|
+
return {
|
|
994
|
+
success: false,
|
|
995
|
+
message: "please input device name"
|
|
996
|
+
};
|
|
997
|
+
}
|
|
998
|
+
if (device.isBind) {
|
|
999
|
+
return yield (0, devices_1.editBindInfo)({
|
|
1000
|
+
did: device.did,
|
|
1001
|
+
name: name,
|
|
1002
|
+
});
|
|
1003
|
+
}
|
|
1004
|
+
return {
|
|
1005
|
+
success: false,
|
|
1006
|
+
message: "device not bind"
|
|
1007
|
+
};
|
|
1008
|
+
});
|
|
1009
|
+
this.removeEventListener = (type, func) => {
|
|
1010
|
+
if (this.listenerMap[type]) {
|
|
1011
|
+
const index = this.listenerMap[type].indexOf(func);
|
|
1012
|
+
if (index !== -1) {
|
|
1013
|
+
this.listenerMap[type].splice(index, 1);
|
|
1014
|
+
return {
|
|
1015
|
+
success: true,
|
|
1016
|
+
};
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
return {
|
|
1020
|
+
success: false,
|
|
1021
|
+
};
|
|
1022
|
+
};
|
|
1023
|
+
this.addEventListener = (type, func) => __awaiter(this, void 0, void 0, function* () {
|
|
1024
|
+
if (!this.listenerMap[type]) {
|
|
1025
|
+
this.listenerMap[type] = [];
|
|
1026
|
+
}
|
|
1027
|
+
if (this.listenerMap[type].indexOf(func) === -1) {
|
|
1028
|
+
this.listenerMap[type].push(func);
|
|
1029
|
+
}
|
|
1030
|
+
return {
|
|
1031
|
+
success: true,
|
|
1032
|
+
};
|
|
1033
|
+
});
|
|
1034
|
+
this.destory = () => {
|
|
1035
|
+
this.bleHandle.destory();
|
|
1036
|
+
this.lanHandle.destory();
|
|
1037
|
+
this.socketHandle.destory();
|
|
1038
|
+
this.keepScanTimer && clearInterval(this.keepScanTimer);
|
|
1039
|
+
};
|
|
1040
|
+
this.init(props);
|
|
1041
|
+
}
|
|
1042
|
+
get bleHandle() {
|
|
1043
|
+
if (!this._bleHandle) {
|
|
1044
|
+
this._bleHandle = new ble_1.BleHandle({
|
|
1045
|
+
pks: this.specialProductKeys,
|
|
1046
|
+
offlineThreshold: this.offlineThreshold,
|
|
1047
|
+
});
|
|
1048
|
+
}
|
|
1049
|
+
return this._bleHandle;
|
|
1050
|
+
}
|
|
1051
|
+
get lanHandle() {
|
|
1052
|
+
if (!this._lanHandle) {
|
|
1053
|
+
this._lanHandle = new lan_1.LanHandle({
|
|
1054
|
+
pks: this.specialProductKeys,
|
|
1055
|
+
});
|
|
1056
|
+
}
|
|
1057
|
+
return this._lanHandle;
|
|
1058
|
+
}
|
|
1059
|
+
get socketHandle() {
|
|
1060
|
+
if (!this._gizSocket) {
|
|
1061
|
+
this._gizSocket = new socket_1.default({
|
|
1062
|
+
appID: (0, globalData_1.getGlobalData)('appID'),
|
|
1063
|
+
token: (0, globalData_1.getGlobalData)('token'),
|
|
1064
|
+
uid: (0, globalData_1.getGlobalData)('uid'),
|
|
1065
|
+
});
|
|
1066
|
+
this._gizSocket.subscribeDeviceStatus(this.handleSocketDeviceData);
|
|
1067
|
+
this._gizSocket.subscribeBindingChanged(this.getBindingList);
|
|
1068
|
+
this._gizSocket.subscribeSocketStatus(this.handleSocketStatus);
|
|
1069
|
+
}
|
|
1070
|
+
return this._gizSocket;
|
|
1071
|
+
}
|
|
1072
|
+
reInit(props) {
|
|
1073
|
+
this.init(props);
|
|
1074
|
+
this.lanHandle.pks = this.specialProductKeys;
|
|
1075
|
+
this.bleHandle.pks = this.specialProductKeys;
|
|
1076
|
+
this.bleHandle.offlineThreshold = props.offlineThreshold;
|
|
1077
|
+
const { appID, token, cloudServiceInfo = { openAPIInfo: '' } } = props;
|
|
1078
|
+
const openAPIInfo = (cloudServiceInfo && cloudServiceInfo.openAPIInfo) || 'api.gizwits.com';
|
|
1079
|
+
if (appID !== (0, globalData_1.getGlobalData)('appID') ||
|
|
1080
|
+
token !== (0, globalData_1.getGlobalData)('token') ||
|
|
1081
|
+
openAPIInfo !== (0, globalData_1.getGlobalData)('cloudServiceInfo').openAPIInfo) {
|
|
1082
|
+
this.socketHandle.destory();
|
|
1083
|
+
this._gizSocket = null;
|
|
1084
|
+
this.socketHandle;
|
|
1085
|
+
this.getBindingList();
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
get specialProductKeys() {
|
|
1089
|
+
return this.productInfo.map((item) => { var _a; return (_a = item.productKey) !== null && _a !== void 0 ? _a : ''; });
|
|
1090
|
+
}
|
|
1091
|
+
get specialProductKeySecrets() {
|
|
1092
|
+
return this.productInfo.map((item) => { var _a; return (_a = item.productSecret) !== null && _a !== void 0 ? _a : ''; });
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
GizwitsMiniSDK.getVersion = () => {
|
|
1096
|
+
return {
|
|
1097
|
+
success: true,
|
|
1098
|
+
data: version,
|
|
1099
|
+
};
|
|
1100
|
+
};
|
|
1101
|
+
exports.default = GizwitsMiniSDK;
|