@ray-js/lock-sdk 1.0.1 → 1.0.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.
Files changed (86) hide show
  1. package/lib/api/index.js +13 -15
  2. package/lib/api/linkage.d.ts +0 -18
  3. package/lib/api/linkage.js +61 -81
  4. package/lib/api/lock.d.ts +0 -69
  5. package/lib/api/lock.js +91 -113
  6. package/lib/api/log.d.ts +0 -33
  7. package/lib/api/log.js +33 -38
  8. package/lib/api/setting.d.ts +0 -11
  9. package/lib/api/setting.js +17 -30
  10. package/lib/api/temp.d.ts +0 -48
  11. package/lib/api/temp.js +85 -115
  12. package/lib/api/user.d.ts +5 -54
  13. package/lib/api/user.js +40 -52
  14. package/lib/api/video.d.ts +0 -5
  15. package/lib/api/video.js +9 -15
  16. package/lib/config/dp-code/index.d.ts +0 -117
  17. package/lib/config/dp-code/index.js +79 -221
  18. package/lib/config/dp-map/common.d.ts +0 -3
  19. package/lib/config/dp-map/common.js +101 -116
  20. package/lib/config/dp-map/normal.js +29 -50
  21. package/lib/config/dp-map/open.d.ts +0 -16
  22. package/lib/config/dp-map/open.js +62 -63
  23. package/lib/config/dp-map/unlock-method-big.d.ts +0 -60
  24. package/lib/config/dp-map/unlock-method-big.js +196 -233
  25. package/lib/config/dp-map/unlock-method.d.ts +0 -63
  26. package/lib/config/dp-map/unlock-method.js +189 -227
  27. package/lib/config/index.d.ts +0 -25
  28. package/lib/config/index.js +21 -54
  29. package/lib/constant.d.ts +12 -40
  30. package/lib/constant.js +83 -136
  31. package/lib/event.d.ts +0 -6
  32. package/lib/event.js +1 -8
  33. package/lib/index.d.ts +0 -7
  34. package/lib/index.js +45 -71
  35. package/lib/interface.d.ts +0 -523
  36. package/lib/interface.js +1 -1
  37. package/lib/linkage.d.ts +0 -18
  38. package/lib/linkage.js +125 -162
  39. package/lib/log.d.ts +0 -50
  40. package/lib/log.js +255 -290
  41. package/lib/media.d.ts +0 -34
  42. package/lib/media.js +6 -77
  43. package/lib/open.d.ts +0 -35
  44. package/lib/open.js +129 -224
  45. package/lib/other.d.ts +0 -24
  46. package/lib/other.js +98 -159
  47. package/lib/parse/index.d.ts +0 -4
  48. package/lib/parse/index.js +13 -19
  49. package/lib/signal.d.ts +0 -13
  50. package/lib/signal.js +17 -31
  51. package/lib/sleep.d.ts +0 -42
  52. package/lib/sleep.js +41 -98
  53. package/lib/state.d.ts +0 -38
  54. package/lib/state.js +223 -369
  55. package/lib/sync/remote-serect-key.d.ts +0 -4
  56. package/lib/sync/remote-serect-key.js +33 -49
  57. package/lib/sync/t0.d.ts +0 -3
  58. package/lib/sync/t0.js +14 -21
  59. package/lib/sync/temp.d.ts +0 -5
  60. package/lib/sync/temp.js +68 -77
  61. package/lib/sync/unlock-mothod.d.ts +0 -3
  62. package/lib/sync/unlock-mothod.js +28 -40
  63. package/lib/temporary.d.ts +0 -149
  64. package/lib/temporary.js +469 -593
  65. package/lib/unlock-method.d.ts +0 -185
  66. package/lib/unlock-method.js +479 -650
  67. package/lib/user.d.ts +2 -81
  68. package/lib/user.js +202 -336
  69. package/lib/utils/base64-to-hex.js +10 -10
  70. package/lib/utils/byte.d.ts +0 -16
  71. package/lib/utils/byte.js +57 -68
  72. package/lib/utils/constant.js +7 -11
  73. package/lib/utils/device.d.ts +46 -83
  74. package/lib/utils/device.js +294 -322
  75. package/lib/utils/errors.js +75 -122
  76. package/lib/utils/event.js +79 -135
  77. package/lib/utils/hex-to-base64.js +5 -5
  78. package/lib/utils/hex-to-bytes.d.ts +0 -5
  79. package/lib/utils/hex-to-bytes.js +10 -15
  80. package/lib/utils/index.d.ts +0 -42
  81. package/lib/utils/index.js +247 -377
  82. package/lib/utils/log.d.ts +1 -4
  83. package/lib/utils/log.js +71 -74
  84. package/lib/utils/publishDps.d.ts +0 -5
  85. package/lib/utils/publishDps.js +58 -80
  86. package/package.json +3 -4
package/lib/state.js CHANGED
@@ -1,17 +1,12 @@
1
- import "core-js/modules/esnext.iterator.constructor.js";
2
- import "core-js/modules/esnext.iterator.reduce.js";
3
- /**
4
- * 数据状态管理
5
- */
6
1
  import config from "./config";
7
- import { onDpDataChange, onDeviceInfoUpdated, offDeviceInfoUpdated, offDpDataChange, registerDevice, getDeviceOnlineType, onOnlineChange, offOnlineChange, onBleChange, offBleChange, registerBleStatus, getDeviceInfo, onMqttMessage, registerMQTTDeviceListener, registerMQTTProtocolListener, offMqttMessage } from "./utils/device";
2
+ import { onDpDataChange, onDeviceInfoUpdated, offDeviceInfoUpdated, offDpDataChange, registerDevice, getDeviceOnlineType, onOnlineChange, offOnlineChange, onBleChange, offBleChange, registerBleStatus, getDeviceInfo, onMqttMessage, registerMQTTDeviceListener, registerMQTTProtocolListener, offMqttMessage, } from "./utils/device";
8
3
  import { getDeviceProperties } from "./api/setting";
9
4
  import { parseCapabilities } from "./parse";
10
5
  import emitter from "./utils/event";
11
6
  import { DPCHANGE, LOGCHANGE } from "./utils/constant";
12
7
  import dpCodes, { latLogDpCodes } from "./config/dp-code";
13
8
  import { getCurrentUserInfo } from "./api/user";
14
- import { equels, getDataWithPreFetch, isOnlineByType } from "./utils";
9
+ import { equels, getDataWithPreFetch, isOnlineByType, } from "./utils";
15
10
  import { getRotateInfo } from "./api/video";
16
11
  import syncT0 from "./sync/t0";
17
12
  import syncRemoteSerectKey from "./sync/remote-serect-key";
@@ -20,410 +15,269 @@ import { autoSyncTemp } from "./sync/temp";
20
15
  import { DEVICE_STATUS_CHANGE_EVENT, SLEEP_EVENT } from "./event";
21
16
  import { getSleepPeriod, isSleep } from "./sleep";
22
17
  import { ProductCommunicationType } from "./constant";
23
-
24
- // 设备详细信息
25
18
  let dpState = {};
26
19
  let logRefreshTimer = 0;
27
20
  export const clearState = () => {
28
- dpState = {};
21
+ dpState = {};
29
22
  };
30
- export const getDpValue = code => {
31
- return dpState[code];
23
+ export const getDpValue = (code) => {
24
+ return dpState[code];
32
25
  };
33
-
34
- /**
35
- * 更新设备在线类型
36
- */
37
26
  const updateOnlineType = async () => {
38
- const [devInfo, onlineType] = await Promise.all([getDeviceInfo(config.devInfo.devId), getDeviceOnlineType(config.devInfo.devId)]);
39
- config.onlineType = onlineType;
40
- config.devInfo = devInfo;
41
- updateDeviceStatus();
27
+ const [devInfo, onlineType] = await Promise.all([
28
+ getDeviceInfo(config.devInfo.devId),
29
+ getDeviceOnlineType(config.devInfo.devId),
30
+ ]);
31
+ config.onlineType = onlineType;
32
+ config.devInfo = devInfo;
33
+ updateDeviceStatus();
42
34
  };
43
-
44
- /**
45
- * 获取设备状态信息
46
- * @returns
47
- */
48
35
  export const getDeviceStatus = () => {
49
- const {
50
- onlineType,
51
- dpSchema,
52
- communication,
53
- capabilities,
54
- devInfo: {
55
- isCloudOnline,
56
- isLocalOnline,
57
- isOnline
58
- }
59
- } = config;
60
- // 在线状态处理
61
- const status = {
62
- type: isOnline ? "online" : "offline",
63
- connectEanble: false,
64
- onlineType: "unknown"
65
- };
66
- switch (communication.length) {
67
- case 1:
68
- {
69
- // 单模设备
70
- switch (communication[0]) {
71
- case ProductCommunicationType.WIFI:
72
- const hasWirelessAwakeDp = !!dpSchema[dpCodes.wirelessWwake];
73
- const isDeviceOnline = hasWirelessAwakeDp ? getDpValue(dpCodes.wirelessWwake) && isCloudOnline : isCloudOnline;
74
- if (!isDeviceOnline) {
75
- // 判断是否设备是否休眠
76
- const sleep = isSleep();
77
- status.type = sleep ? "sleep" : "offline";
78
- } else {
79
- status.type = "online";
80
- status.onlineType = "cloud";
81
- }
82
- break;
83
- case ProductCommunicationType.BLUETOOTH:
84
- status.connectEanble = true;
85
- if (isOnline) {
86
- status.type = "online";
87
- status.onlineType = "ble";
36
+ const { onlineType, dpSchema, communication, capabilities, devInfo: { isCloudOnline, isLocalOnline, isOnline }, } = config;
37
+ const status = {
38
+ type: isOnline ? "online" : "offline",
39
+ connectEanble: false,
40
+ onlineType: "unknown",
41
+ };
42
+ switch (communication.length) {
43
+ case 1: {
44
+ switch (communication[0]) {
45
+ case ProductCommunicationType.WIFI:
46
+ const hasWirelessAwakeDp = !!dpSchema[dpCodes.wirelessWwake];
47
+ const isDeviceOnline = hasWirelessAwakeDp
48
+ ? getDpValue(dpCodes.wirelessWwake) && isCloudOnline
49
+ : isCloudOnline;
50
+ if (!isDeviceOnline) {
51
+ const sleep = isSleep();
52
+ status.type = sleep ? "sleep" : "offline";
53
+ }
54
+ else {
55
+ status.type = "online";
56
+ status.onlineType = "cloud";
57
+ }
58
+ break;
59
+ case ProductCommunicationType.BLUETOOTH:
60
+ status.connectEanble = true;
61
+ if (isOnline) {
62
+ status.type = "online";
63
+ status.onlineType = "ble";
64
+ }
65
+ break;
88
66
  }
89
67
  break;
90
68
  }
91
- break;
92
- }
93
- case 2:
94
- {
95
- // 双模设备
96
- // wifi + ble
97
- if (communication.includes(ProductCommunicationType.WIFI) && communication.includes(ProductCommunicationType.BLUETOOTH)) {
98
- const hasWirelessAwakeDp = !!dpSchema[dpCodes.wirelessWwake];
99
- const isDeviceOnline = hasWirelessAwakeDp ? getDpValue(dpCodes.wirelessWwake) && isCloudOnline : isCloudOnline;
100
- if (!isDeviceOnline) {
101
- // 判断是否支持蓝牙在线
102
- // 是否蓝牙在线
103
- if (isOnline && isOnlineByType(onlineType, 2)) {
104
- status.type = "online";
105
- status.onlineType = "ble";
106
- break;
69
+ case 2: {
70
+ if (communication.includes(ProductCommunicationType.WIFI) &&
71
+ communication.includes(ProductCommunicationType.BLUETOOTH)) {
72
+ const hasWirelessAwakeDp = !!dpSchema[dpCodes.wirelessWwake];
73
+ const isDeviceOnline = hasWirelessAwakeDp
74
+ ? getDpValue(dpCodes.wirelessWwake) && isCloudOnline
75
+ : isCloudOnline;
76
+ if (!isDeviceOnline) {
77
+ if (isOnline && isOnlineByType(onlineType, 2)) {
78
+ status.type = "online";
79
+ status.onlineType = "ble";
80
+ break;
81
+ }
82
+ const sleep = isSleep();
83
+ status.type = sleep ? "sleep" : "offline";
84
+ }
85
+ else {
86
+ status.type = "online";
87
+ status.onlineType = "cloud";
88
+ }
89
+ break;
107
90
  }
108
- // 判断是否设备是否休眠
109
- const sleep = isSleep();
110
- status.type = sleep ? "sleep" : "offline";
111
- } else {
112
- status.type = "online";
113
- status.onlineType = "cloud";
114
- }
115
- break;
91
+ if (communication.includes(ProductCommunicationType.THREAD) &&
92
+ communication.includes(ProductCommunicationType.BLUETOOTH)) {
93
+ }
94
+ break;
116
95
  }
117
- // metter + ble
118
- if (communication.includes(ProductCommunicationType.THREAD) && communication.includes(ProductCommunicationType.BLUETOOTH)) {
119
- // TODO 后继支持
96
+ }
97
+ if (status.type === "sleep") {
98
+ if (getDpValue(dpCodes.dormantSwitch) || getDpValue(dpCodes.onlineSwitch)) {
99
+ status.sleepPeriod = getSleepPeriod();
120
100
  }
121
- break;
122
- }
123
- }
124
- if (status.type === "sleep") {
125
- if (getDpValue(dpCodes.dormantSwitch) || getDpValue(dpCodes.onlineSwitch)) {
126
- status.sleepPeriod = getSleepPeriod();
127
101
  }
128
- }
129
- return status;
102
+ return status;
130
103
  };
131
104
  const updateDeviceStatus = () => {
132
- const status = getDeviceStatus();
133
- // 是否存在变更,如果是,则触发事件
134
- if (!equels(config.deviceStatus, status)) {
135
- config.deviceStatus = status;
136
- emitter.emit(DEVICE_STATUS_CHANGE_EVENT, status);
137
- }
105
+ const status = getDeviceStatus();
106
+ if (!equels(config.deviceStatus, status)) {
107
+ config.deviceStatus = status;
108
+ emitter.emit(DEVICE_STATUS_CHANGE_EVENT, status);
109
+ }
138
110
  };
139
-
140
- /**
141
- * 处理dp上报
142
- * @param dps
143
- */
144
111
  const handleDpChange = (dps, notEmit) => {
145
- let refleshLogsEnabled = false;
146
- // 将id => value 转为 code => value
147
- const dpData = Object.keys(dps).reduce((acc, key) => {
148
- const code = config.codesById[key];
149
- if (code) {
150
- acc[code] = dps[key];
151
- if (!refleshLogsEnabled && latLogDpCodes.includes(code)) {
152
- refleshLogsEnabled = true;
153
- }
112
+ let refleshLogsEnabled = false;
113
+ const dpData = Object.keys(dps).reduce((acc, key) => {
114
+ const code = config.codesById[key];
115
+ if (code) {
116
+ acc[code] = dps[key];
117
+ if (!refleshLogsEnabled && latLogDpCodes.includes(code)) {
118
+ refleshLogsEnabled = true;
119
+ }
120
+ }
121
+ return acc;
122
+ }, {});
123
+ if (dpData["device_info"]) {
124
+ config.capabilities = parseCapabilities(dpData["device_info"]);
154
125
  }
155
- return acc;
156
- }, {});
157
- if (dpData["device_info"]) {
158
- // 设备能力处理
159
- config.capabilities = parseCapabilities(dpData["device_info"]);
160
- }
161
- Object.assign(dpState, dpData);
162
- if (!notEmit) {
163
- emitter.emit(DPCHANGE, dpData);
164
-
165
- // 是否休眠状态变更
166
- if (typeof dpData[dpCodes.linkMode] !== "undefined") {
167
- emitter.emit(SLEEP_EVENT, {
168
- sleep: isSleep()
169
- });
126
+ Object.assign(dpState, dpData);
127
+ if (!notEmit) {
128
+ emitter.emit(DPCHANGE, dpData);
129
+ if (typeof dpData[dpCodes.linkMode] !== "undefined") {
130
+ emitter.emit(SLEEP_EVENT, {
131
+ sleep: isSleep(),
132
+ });
133
+ }
134
+ if (refleshLogsEnabled) {
135
+ clearTimeout(logRefreshTimer);
136
+ logRefreshTimer = setTimeout(() => {
137
+ emitter.emit(LOGCHANGE, dpData);
138
+ }, 2000);
139
+ }
170
140
  }
171
-
172
- // 是否触发日志更新
173
- if (refleshLogsEnabled) {
174
- clearTimeout(logRefreshTimer);
175
- // 2 秒后触发刷新
176
- // @ts-expect-error
177
- logRefreshTimer = setTimeout(() => {
178
- emitter.emit(LOGCHANGE, dpData);
179
- }, 2000);
141
+ };
142
+ const handleDeviceDpsChange = ({ dps, deviceId, }) => {
143
+ if (deviceId === config.devInfo.devId) {
144
+ handleDpChange(dps);
145
+ updateDeviceStatus();
180
146
  }
181
- }
182
147
  };
183
-
184
- /**
185
- * 设备 dp 变化事件
186
- * @param param0
187
- */
188
- const handleDeviceDpsChange = _ref => {
189
- let {
190
- dps,
191
- deviceId
192
- } = _ref;
193
- if (deviceId === config.devInfo.devId) {
194
- // 处理dp
195
- handleDpChange(dps);
196
-
197
- // 更新设备状态
148
+ const handleDeviceInfoChange = ({ dps, deviceId, }) => {
149
+ if (deviceId === config.devInfo.devId && dps) {
150
+ handleDpChange(dps);
151
+ }
152
+ updateOnlineType();
198
153
  updateDeviceStatus();
199
- }
200
- };
201
-
202
- /**
203
- * 设备信息变化事件
204
- * @param param0
205
- */
206
- const handleDeviceInfoChange = _ref2 => {
207
- let {
208
- dps,
209
- deviceId
210
- } = _ref2;
211
- if (deviceId === config.devInfo.devId && dps) {
212
- // 处理dp
213
- handleDpChange(dps);
214
- }
215
- // 更新在线状态
216
- updateOnlineType();
217
-
218
- // 更新设备状态
219
- updateDeviceStatus();
220
154
  };
221
155
  let currentUser = null;
222
-
223
- /**
224
- * 获取当前用户信息
225
- * @param isForce 是否强制刷新
226
- * @returns
227
- */
228
- export const getCurrentUser = async isForce => {
229
- if (currentUser && !isForce) {
156
+ export const getCurrentUser = async (isForce) => {
157
+ if (currentUser && !isForce) {
158
+ return currentUser;
159
+ }
160
+ if (isForce) {
161
+ currentUser = await getCurrentUserInfo(config.devInfo.devId);
162
+ }
163
+ else {
164
+ currentUser = await getDataWithPreFetch("getCurrentUser", () => getCurrentUserInfo(config.devInfo.devId));
165
+ }
166
+ currentUser.allOpenDps = currentUser.allOpenDps || "";
230
167
  return currentUser;
231
- }
232
- if (isForce) {
233
- currentUser = await getCurrentUserInfo(config.devInfo.devId);
234
- } else {
235
- currentUser = await getDataWithPreFetch("getCurrentUser", () => getCurrentUserInfo(config.devInfo.devId));
236
- }
237
- currentUser.allOpenDps = currentUser.allOpenDps || "";
238
- return currentUser;
239
168
  };
240
-
241
- /**
242
- * 获取当前用户信息
243
- * 注意:此方法只能在初始化后使用
244
- * @returns
245
- */
246
169
  export const getCurrentUserSync = () => {
247
- return currentUser;
170
+ return currentUser;
248
171
  };
249
-
250
- /**
251
- * 获取设备旋转角度
252
- * @returns
253
- */
254
172
  export const getMediaRotate = () => {
255
- return {
256
- imageAngle: config.imageAngle,
257
- videoAngle: config.videoAngle
258
- };
173
+ return {
174
+ imageAngle: config.imageAngle,
175
+ videoAngle: config.videoAngle,
176
+ };
259
177
  };
260
-
261
- /**
262
- * 远程开关门权限列表
263
- */
264
- export const permissions = ["adminsOnly", "everyOne", "noOne"];
265
-
266
- /**
267
- * 初始化数据状态
268
- */
178
+ export const permissions = [
179
+ "adminsOnly",
180
+ "everyOne",
181
+ "noOne",
182
+ ];
269
183
  export const initState = async () => {
270
- const {
271
- devInfo: {
272
- dps,
273
- devId
274
- }
275
- } = config;
276
- handleDpChange(dps, true);
277
-
278
- // 设备状态信息
279
- config.deviceStatus = getDeviceStatus();
280
- const [,, rotateInfo] = await Promise.all([
281
- // 更新在线状态
282
- updateOnlineType(),
283
- // 获取当前用户
284
- getCurrentUser(),
285
- // 获取角度
286
- getDataWithPreFetch("getRotate", () => getRotateInfo(devId))]);
287
-
288
- // 处理角度
289
- config.imageAngle = rotateInfo.angle;
290
- config.videoAngle = rotateInfo.actualAngle;
291
-
292
- // 近场通讯时,尝试同步密钥
293
- syncRemoteSerectKey();
294
-
295
- // 由于 密钥 T0 时间都需要缓存,设备侧处理没这么快,这里间隔 2 秒后下发 T0
296
-
297
- // 2秒后, 尝试同步开锁方式
298
- setTimeout(() => {
299
- syncT0();
300
- autoSyncUnlockMethod();
301
- }, 2000);
302
-
303
- // 近场通讯时,尝试临时密码同步
304
- setTimeout(() => {
305
- autoSyncTemp();
306
- }, 3000);
307
-
308
- // TODO 近场通讯时,尝试同步用户删除状态
309
-
310
- // 异步获取总倒计时时间
311
- getDataWithPreFetch("getDeviceProperties", () => {
312
- return getDeviceProperties(devId, ["REMOTE_UNLOCK_AUTH", "UNLOCK_PHONE_REMOTE"]);
313
- }).then(data => {
314
- // 远程开锁是否开启
315
- config.remoteOpenEnabled = data["UNLOCK_PHONE_REMOTE"] === "true";
316
- // 远程开锁权限
317
- config.remoteOpenPermission = permissions[+data["REMOTE_UNLOCK_AUTH"] || 0];
318
- }).catch(() => {
319
- // 不处理
320
- });
184
+ const { devInfo: { dps, devId }, } = config;
185
+ handleDpChange(dps, true);
186
+ config.deviceStatus = getDeviceStatus();
187
+ const [, , rotateInfo] = await Promise.all([
188
+ updateOnlineType(),
189
+ getCurrentUser(),
190
+ getDataWithPreFetch("getRotate", () => getRotateInfo(devId)),
191
+ ]);
192
+ config.imageAngle = rotateInfo.angle;
193
+ config.videoAngle = rotateInfo.actualAngle;
194
+ syncRemoteSerectKey();
195
+ setTimeout(() => {
196
+ syncT0();
197
+ autoSyncUnlockMethod();
198
+ }, 2000);
199
+ setTimeout(() => {
200
+ autoSyncTemp();
201
+ }, 3000);
202
+ getDataWithPreFetch("getDeviceProperties", () => {
203
+ return getDeviceProperties(devId, [
204
+ "REMOTE_UNLOCK_AUTH",
205
+ "UNLOCK_PHONE_REMOTE",
206
+ ]);
207
+ })
208
+ .then((data) => {
209
+ config.remoteOpenEnabled = data["UNLOCK_PHONE_REMOTE"] === "true";
210
+ config.remoteOpenPermission =
211
+ permissions[+data["REMOTE_UNLOCK_AUTH"] || 0];
212
+ })
213
+ .catch(() => {
214
+ });
321
215
  };
322
-
323
- /**
324
- * 处理 MQTT 消息
325
- * @param param0
326
- */
327
- const handleMqttMessage = _ref3 => {
328
- let {
329
- deviceId,
330
- messageData,
331
- protocol
332
- } = _ref3;
333
- const {
334
- devId
335
- } = config.devInfo;
336
- if (devId === deviceId) {
337
- switch (+protocol) {
338
- case 23:
339
- {
340
- var _messageData$data, _messageData$data2;
341
- // 处理信号强度变化
342
- if (messageData !== null && messageData !== void 0 && (_messageData$data = messageData.data) !== null && _messageData$data !== void 0 && _messageData$data.signal || (messageData === null || messageData === void 0 || (_messageData$data2 = messageData.data) === null || _messageData$data2 === void 0 ? void 0 : _messageData$data2.signal) === 0) {
343
- const signal = messageData.data.signal;
344
- let signalText = "bad";
345
- if (signal > -50) {
346
- signalText = "good";
347
- } else if (signal <= -50 && signal > -65) {
348
- signalText = "normal";
349
- } else if (signal <= -65 && signal > -75) {
350
- signalText = "weak";
216
+ const handleMqttMessage = ({ deviceId, messageData, protocol }) => {
217
+ const { devId } = config.devInfo;
218
+ if (devId === deviceId) {
219
+ switch (+protocol) {
220
+ case 23: {
221
+ if (messageData?.data?.signal || messageData?.data?.signal === 0) {
222
+ const signal = messageData.data.signal;
223
+ let signalText = "bad";
224
+ if (signal > -50) {
225
+ signalText = "good";
226
+ }
227
+ else if (signal <= -50 && signal > -65) {
228
+ signalText = "normal";
229
+ }
230
+ else if (signal <= -65 && signal > -75) {
231
+ signalText = "weak";
232
+ }
233
+ emitter.emit("onWiFiSignalChange", {
234
+ signal,
235
+ level: signalText,
236
+ });
237
+ }
238
+ break;
351
239
  }
352
- emitter.emit("onWiFiSignalChange", {
353
- signal,
354
- level: signalText
355
- });
356
- }
357
- break;
358
240
  }
359
241
  }
360
- }
361
242
  };
362
243
  let hasEvent = false;
363
- const handleBleChange = _ref4 => {
364
- let {
365
- deviceId,
366
- status
367
- } = _ref4;
368
- if (status === "CONNECTED") {
369
- // 当蓝牙连接成功时,尝试同步密钥
370
- syncRemoteSerectKey();
371
- // 近场通讯时,尝试临时密码同步
372
- autoSyncTemp();
373
- // TODO 尝试同步 T0 时间
374
- syncT0();
375
- }
244
+ const handleBleChange = ({ deviceId, status }) => {
245
+ if (status === "CONNECTED") {
246
+ syncRemoteSerectKey();
247
+ autoSyncTemp();
248
+ syncT0();
249
+ }
376
250
  };
377
-
378
- /**
379
- * 添加事件
380
- */
381
251
  export const addEvents = () => {
382
- if (hasEvent) {
383
- return;
384
- }
385
- const {
386
- devId
387
- } = config.devInfo;
388
- hasEvent = true;
389
- registerDevice([devId]);
390
- registerBleStatus(devId);
391
- registerMQTTDeviceListener(devId);
392
- registerMQTTProtocolListener(23);
393
- onMqttMessage(handleMqttMessage);
394
- onDpDataChange(handleDeviceDpsChange);
395
- onDeviceInfoUpdated(handleDeviceInfoChange);
396
- onOnlineChange(updateOnlineType);
397
- onBleChange(handleBleChange);
252
+ if (hasEvent) {
253
+ return;
254
+ }
255
+ const { devId } = config.devInfo;
256
+ hasEvent = true;
257
+ registerDevice([devId]);
258
+ registerBleStatus(devId);
259
+ registerMQTTDeviceListener(devId);
260
+ registerMQTTProtocolListener(23);
261
+ onMqttMessage(handleMqttMessage);
262
+ onDpDataChange(handleDeviceDpsChange);
263
+ onDeviceInfoUpdated(handleDeviceInfoChange);
264
+ onOnlineChange(updateOnlineType);
265
+ onBleChange(handleBleChange);
398
266
  };
399
-
400
- /**
401
- * 添加事件
402
- */
403
267
  export const removeEvents = () => {
404
- if (hasEvent) {
405
- return;
406
- }
407
- hasEvent = false;
408
- offDpDataChange(handleDeviceDpsChange);
409
- offDeviceInfoUpdated(handleDeviceInfoChange);
410
- offOnlineChange(updateOnlineType);
411
- offBleChange(handleBleChange);
412
- offMqttMessage(handleMqttMessage);
268
+ if (hasEvent) {
269
+ return;
270
+ }
271
+ hasEvent = false;
272
+ offDpDataChange(handleDeviceDpsChange);
273
+ offDeviceInfoUpdated(handleDeviceInfoChange);
274
+ offOnlineChange(updateOnlineType);
275
+ offBleChange(handleBleChange);
276
+ offMqttMessage(handleMqttMessage);
277
+ };
278
+ export const onDeviceStatusChange = (callback) => {
279
+ emitter.on(DEVICE_STATUS_CHANGE_EVENT, callback);
413
280
  };
414
-
415
- /**
416
- * 注册设备状态信息变更事件
417
- * @param callback 回调函数
418
- */
419
- export const onDeviceStatusChange = callback => {
420
- emitter.on(DEVICE_STATUS_CHANGE_EVENT, callback);
281
+ export const offDeviceStatusChange = (callback) => {
282
+ emitter.off(DEVICE_STATUS_CHANGE_EVENT, callback);
421
283
  };
422
-
423
- /**
424
- * 注销设备状态信息变更事件
425
- * @param callback 回调函数
426
- */
427
- export const offDeviceStatusChange = callback => {
428
- emitter.off(DEVICE_STATUS_CHANGE_EVENT, callback);
429
- };
@@ -1,6 +1,2 @@
1
- /**
2
- * 同步密钥
3
- * @param force 是否强制同步
4
- */
5
1
  declare const syncRemoteSerectKey: (force?: boolean) => Promise<void>;
6
2
  export default syncRemoteSerectKey;