@ray-js/lock-sdk 1.0.0-beta-1

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 (95) hide show
  1. package/lib/api/index.d.ts +5 -0
  2. package/lib/api/index.js +18 -0
  3. package/lib/api/linkage.d.ts +161 -0
  4. package/lib/api/linkage.js +74 -0
  5. package/lib/api/lock.d.ts +195 -0
  6. package/lib/api/lock.js +129 -0
  7. package/lib/api/log.d.ts +166 -0
  8. package/lib/api/log.js +44 -0
  9. package/lib/api/setting.d.ts +14 -0
  10. package/lib/api/setting.js +36 -0
  11. package/lib/api/temp.d.ts +211 -0
  12. package/lib/api/temp.js +130 -0
  13. package/lib/api/user.d.ts +154 -0
  14. package/lib/api/user.js +59 -0
  15. package/lib/api/video.d.ts +9 -0
  16. package/lib/api/video.js +17 -0
  17. package/lib/config/dp-code/index.d.ts +175 -0
  18. package/lib/config/dp-code/index.js +224 -0
  19. package/lib/config/dp-map/common.d.ts +102 -0
  20. package/lib/config/dp-map/common.js +120 -0
  21. package/lib/config/dp-map/normal.d.ts +6 -0
  22. package/lib/config/dp-map/normal.js +51 -0
  23. package/lib/config/dp-map/open.d.ts +21 -0
  24. package/lib/config/dp-map/open.js +62 -0
  25. package/lib/config/dp-map/unlock-method-big.d.ts +559 -0
  26. package/lib/config/dp-map/unlock-method-big.js +237 -0
  27. package/lib/config/dp-map/unlock-method.d.ts +551 -0
  28. package/lib/config/dp-map/unlock-method.js +231 -0
  29. package/lib/config/index.d.ts +46 -0
  30. package/lib/config/index.js +56 -0
  31. package/lib/constant.d.ts +101 -0
  32. package/lib/constant.js +136 -0
  33. package/lib/dp-interface.d.ts +7 -0
  34. package/lib/dp-interface.js +1 -0
  35. package/lib/event.d.ts +8 -0
  36. package/lib/event.js +9 -0
  37. package/lib/index.d.ts +25 -0
  38. package/lib/index.js +92 -0
  39. package/lib/interface.d.ts +807 -0
  40. package/lib/interface.js +1 -0
  41. package/lib/linkage.d.ts +26 -0
  42. package/lib/linkage.js +175 -0
  43. package/lib/log.d.ts +91 -0
  44. package/lib/log.js +314 -0
  45. package/lib/media.d.ts +43 -0
  46. package/lib/media.js +80 -0
  47. package/lib/open.d.ts +48 -0
  48. package/lib/open.js +247 -0
  49. package/lib/other.d.ts +36 -0
  50. package/lib/other.js +178 -0
  51. package/lib/parse/index.d.ts +6 -0
  52. package/lib/parse/index.js +22 -0
  53. package/lib/signal.d.ts +26 -0
  54. package/lib/signal.js +38 -0
  55. package/lib/sleep.d.ts +61 -0
  56. package/lib/sleep.js +121 -0
  57. package/lib/state.d.ts +54 -0
  58. package/lib/state.js +429 -0
  59. package/lib/sync/remote-serect-key.d.ts +5 -0
  60. package/lib/sync/remote-serect-key.js +60 -0
  61. package/lib/sync/t0.d.ts +5 -0
  62. package/lib/sync/t0.js +33 -0
  63. package/lib/sync/temp.d.ts +7 -0
  64. package/lib/sync/temp.js +88 -0
  65. package/lib/sync/unlock-mothod.d.ts +5 -0
  66. package/lib/sync/unlock-mothod.js +54 -0
  67. package/lib/temporary.d.ts +226 -0
  68. package/lib/temporary.js +637 -0
  69. package/lib/unlock-method.d.ts +269 -0
  70. package/lib/unlock-method.js +723 -0
  71. package/lib/user.d.ts +108 -0
  72. package/lib/user.js +361 -0
  73. package/lib/utils/base64-to-hex.d.ts +1 -0
  74. package/lib/utils/base64-to-hex.js +12 -0
  75. package/lib/utils/byte.d.ts +19 -0
  76. package/lib/utils/byte.js +74 -0
  77. package/lib/utils/constant.d.ts +11 -0
  78. package/lib/utils/constant.js +17 -0
  79. package/lib/utils/device.d.ts +207 -0
  80. package/lib/utils/device.js +353 -0
  81. package/lib/utils/errors.d.ts +2 -0
  82. package/lib/utils/errors.js +125 -0
  83. package/lib/utils/event.d.ts +23 -0
  84. package/lib/utils/event.js +144 -0
  85. package/lib/utils/hex-to-base64.d.ts +1 -0
  86. package/lib/utils/hex-to-base64.js +8 -0
  87. package/lib/utils/hex-to-bytes.d.ts +6 -0
  88. package/lib/utils/hex-to-bytes.js +16 -0
  89. package/lib/utils/index.d.ts +169 -0
  90. package/lib/utils/index.js +419 -0
  91. package/lib/utils/log.d.ts +5 -0
  92. package/lib/utils/log.js +78 -0
  93. package/lib/utils/publishDps.d.ts +11 -0
  94. package/lib/utils/publishDps.js +91 -0
  95. package/package.json +37 -0
package/lib/sleep.d.ts ADDED
@@ -0,0 +1,61 @@
1
+ /**
2
+ * 判断锁是否处于休眠状态
3
+ * @returns true 表示为休眠中
4
+ */
5
+ export declare const isSleep: () => boolean;
6
+ /**
7
+ * 判断锁是否支持休眠功能
8
+ * @returns true 表示支持休眠功能
9
+ */
10
+ export declare const hasSleepAbility: () => boolean;
11
+ /**
12
+ * 判断休眠功能是否已启用
13
+ * @returns true 表示休眠功能已启用
14
+ * false 表示休眠功能已禁用
15
+ */
16
+ export declare const isEnabledSleep: () => import("./interface").DpValue;
17
+ /**
18
+ * 启用休眠
19
+ * @returns Promise<void>
20
+ */
21
+ export declare const enableSleep: () => Promise<Record<string, import("./interface").DpValue>>;
22
+ /**
23
+ * 禁用休眠
24
+ * @returns Promise<void>
25
+ */
26
+ export declare const disableSleep: () => Promise<Record<string, import("./interface").DpValue>>;
27
+ /**
28
+ * 获取休眠时间段
29
+ * @returns 返回休眠时间段
30
+ * start: 开始时间(分钟)
31
+ * end: 结束时间(分钟)
32
+ */
33
+ export declare const getSleepPeriod: () => {
34
+ start: number;
35
+ end: number;
36
+ };
37
+ /**
38
+ * 设置休眠时间段
39
+ * @param params 休眠时间段
40
+ * startTime: 开始时间(分钟)
41
+ * endTime: 结束时间(分钟)
42
+ * @returns Promise<void>
43
+ */
44
+ export declare const updateSleepPeriod: (params: {
45
+ start: number;
46
+ end: number;
47
+ }) => Promise<Record<string, import("./interface").DpValue>>;
48
+ /**
49
+ * 注册休眠状态变更事件
50
+ * @param callback 休眠状态变更回调
51
+ */
52
+ export declare const onSleepStatusChange: (callback: (args: {
53
+ sleep: boolean;
54
+ }) => void) => void;
55
+ /**
56
+ * 注销休眠状态变更事件
57
+ * @param callback 休眠状态变更回调
58
+ */
59
+ export declare const offSleepStatusChange: (callback: (args: {
60
+ sleep: boolean;
61
+ }) => void) => void;
package/lib/sleep.js ADDED
@@ -0,0 +1,121 @@
1
+ import config from "./config";
2
+ import { getDpValue } from "./state";
3
+ import { getError } from "./utils/errors";
4
+ import { publishDps } from "./utils/publishDps";
5
+ import emitter from "./utils/event";
6
+ import { SLEEP_EVENT } from "./event";
7
+ import dpCodes from "./config/dp-code";
8
+ import { getDpPeriodTime, updateDpPeriodTime } from "./other";
9
+
10
+ /**
11
+ * 判断锁是否处于休眠状态
12
+ * @returns true 表示为休眠中
13
+ */
14
+ export const isSleep = () => {
15
+ const {
16
+ dpSchema
17
+ } = config;
18
+ if (typeof dpSchema[dpCodes.linkMode] === "undefined") {
19
+ return false;
20
+ }
21
+ const linkMode = getDpValue(dpCodes.linkMode);
22
+ return linkMode === "sleep" || linkMode === "lock_sleep";
23
+ };
24
+
25
+ /**
26
+ * 判断锁是否支持休眠功能
27
+ * @returns true 表示支持休眠功能
28
+ */
29
+ export const hasSleepAbility = () => {
30
+ const {
31
+ dpSchema
32
+ } = config;
33
+ return (typeof dpSchema[dpCodes.dormantSwitch] !== "undefined" || typeof dpSchema[dpCodes.onlineSwitch] !== "undefined") && typeof dpSchema[dpCodes.dormantSwitch] === "undefined";
34
+ };
35
+ const setSleepStatus = async status => {
36
+ const {
37
+ dpSchema
38
+ } = config;
39
+ if (typeof dpSchema[dpCodes.onlineSwitch] !== "undefined") {
40
+ return publishDps({
41
+ [dpCodes.onlineSwitch]: status
42
+ });
43
+ }
44
+ if (typeof dpSchema[dpCodes.dormantSwitch] !== "undefined") {
45
+ return publishDps({
46
+ [dpCodes.dormantSwitch]: status
47
+ });
48
+ }
49
+ throw getError(1014);
50
+ };
51
+
52
+ /**
53
+ * 判断休眠功能是否已启用
54
+ * @returns true 表示休眠功能已启用
55
+ * false 表示休眠功能已禁用
56
+ */
57
+ export const isEnabledSleep = () => {
58
+ const {
59
+ dpSchema
60
+ } = config;
61
+ if (typeof dpSchema[dpCodes.onlineSwitch] !== "undefined") {
62
+ return getDpValue(dpCodes.onlineSwitch);
63
+ }
64
+ if (typeof dpSchema[dpCodes.dormantSwitch] !== "undefined") {
65
+ return getDpValue(dpCodes.dormantSwitch);
66
+ }
67
+ throw getError(1014);
68
+ };
69
+
70
+ /**
71
+ * 启用休眠
72
+ * @returns Promise<void>
73
+ */
74
+ export const enableSleep = async () => {
75
+ return setSleepStatus(true);
76
+ };
77
+
78
+ /**
79
+ * 禁用休眠
80
+ * @returns Promise<void>
81
+ */
82
+ export const disableSleep = async () => {
83
+ return setSleepStatus(false);
84
+ };
85
+
86
+ /**
87
+ * 获取休眠时间段
88
+ * @returns 返回休眠时间段
89
+ * start: 开始时间(分钟)
90
+ * end: 结束时间(分钟)
91
+ */
92
+ export const getSleepPeriod = () => {
93
+ return getDpPeriodTime(dpCodes.dormantTimeSet);
94
+ };
95
+
96
+ /**
97
+ * 设置休眠时间段
98
+ * @param params 休眠时间段
99
+ * startTime: 开始时间(分钟)
100
+ * endTime: 结束时间(分钟)
101
+ * @returns Promise<void>
102
+ */
103
+ export const updateSleepPeriod = params => {
104
+ return updateDpPeriodTime(dpCodes.dormantTimeSet, params);
105
+ };
106
+
107
+ /**
108
+ * 注册休眠状态变更事件
109
+ * @param callback 休眠状态变更回调
110
+ */
111
+ export const onSleepStatusChange = callback => {
112
+ emitter.on(SLEEP_EVENT, callback);
113
+ };
114
+
115
+ /**
116
+ * 注销休眠状态变更事件
117
+ * @param callback 休眠状态变更回调
118
+ */
119
+ export const offSleepStatusChange = callback => {
120
+ emitter.off(SLEEP_EVENT, callback);
121
+ };
package/lib/state.d.ts ADDED
@@ -0,0 +1,54 @@
1
+ import { DpValue, CurrentUser, RemotePermission, DeviceStatus } from "./interface";
2
+ export declare const clearState: () => void;
3
+ export declare const getDpValue: (code: string) => DpValue;
4
+ /**
5
+ * 获取设备状态信息
6
+ * @returns
7
+ */
8
+ export declare const getDeviceStatus: () => DeviceStatus;
9
+ /**
10
+ * 获取当前用户信息
11
+ * @param isForce 是否强制刷新
12
+ * @returns
13
+ */
14
+ export declare const getCurrentUser: (isForce?: boolean) => Promise<CurrentUser>;
15
+ /**
16
+ * 获取当前用户信息
17
+ * 注意:此方法只能在初始化后使用
18
+ * @returns
19
+ */
20
+ export declare const getCurrentUserSync: () => CurrentUser | null;
21
+ /**
22
+ * 获取设备旋转角度
23
+ * @returns
24
+ */
25
+ export declare const getMediaRotate: () => {
26
+ imageAngle: number;
27
+ videoAngle: number;
28
+ };
29
+ /**
30
+ * 远程开关门权限列表
31
+ */
32
+ export declare const permissions: RemotePermission[];
33
+ /**
34
+ * 初始化数据状态
35
+ */
36
+ export declare const initState: () => Promise<void>;
37
+ /**
38
+ * 添加事件
39
+ */
40
+ export declare const addEvents: () => void;
41
+ /**
42
+ * 添加事件
43
+ */
44
+ export declare const removeEvents: () => void;
45
+ /**
46
+ * 注册设备状态信息变更事件
47
+ * @param callback 回调函数
48
+ */
49
+ export declare const onDeviceStatusChange: (callback: (status: DeviceStatus) => void) => void;
50
+ /**
51
+ * 注销设备状态信息变更事件
52
+ * @param callback 回调函数
53
+ */
54
+ export declare const offDeviceStatusChange: (callback: (status: DeviceStatus) => void) => void;
package/lib/state.js ADDED
@@ -0,0 +1,429 @@
1
+ import "core-js/modules/esnext.iterator.constructor.js";
2
+ import "core-js/modules/esnext.iterator.reduce.js";
3
+ /**
4
+ * 数据状态管理
5
+ */
6
+ 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";
8
+ import { getDeviceProperties } from "./api/setting";
9
+ import { parseCapabilities } from "./parse";
10
+ import emitter from "./utils/event";
11
+ import { DPCHANGE, LOGCHANGE } from "./utils/constant";
12
+ import dpCodes, { latLogDpCodes } from "./config/dp-code";
13
+ import { getCurrentUserInfo } from "./api/user";
14
+ import { equels, getDataWithPreFetch, isOnlineByType } from "./utils";
15
+ import { getRotateInfo } from "./api/video";
16
+ import syncT0 from "./sync/t0";
17
+ import syncRemoteSerectKey from "./sync/remote-serect-key";
18
+ import { autoSyncUnlockMethod } from "./sync/unlock-mothod";
19
+ import { autoSyncTemp } from "./sync/temp";
20
+ import { DEVICE_STATUS_CHANGE_EVENT, SLEEP_EVENT } from "./event";
21
+ import { getSleepPeriod, isSleep } from "./sleep";
22
+ import { ProductCommunicationType } from "./constant";
23
+
24
+ // 设备详细信息
25
+ let dpState = {};
26
+ let logRefreshTimer = 0;
27
+ export const clearState = () => {
28
+ dpState = {};
29
+ };
30
+ export const getDpValue = code => {
31
+ return dpState[code];
32
+ };
33
+
34
+ /**
35
+ * 更新设备在线类型
36
+ */
37
+ 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();
42
+ };
43
+
44
+ /**
45
+ * 获取设备状态信息
46
+ * @returns
47
+ */
48
+ 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";
88
+ }
89
+ break;
90
+ }
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;
107
+ }
108
+ // 判断是否设备是否休眠
109
+ const sleep = isSleep();
110
+ status.type = sleep ? "sleep" : "offline";
111
+ } else {
112
+ status.type = "online";
113
+ status.onlineType = "cloud";
114
+ }
115
+ break;
116
+ }
117
+ // metter + ble
118
+ if (communication.includes(ProductCommunicationType.THREAD) && communication.includes(ProductCommunicationType.BLUETOOTH)) {
119
+ // TODO 后继支持
120
+ }
121
+ break;
122
+ }
123
+ }
124
+ if (status.type === "sleep") {
125
+ if (getDpValue(dpCodes.dormantSwitch) || getDpValue(dpCodes.onlineSwitch)) {
126
+ status.sleepPeriod = getSleepPeriod();
127
+ }
128
+ }
129
+ return status;
130
+ };
131
+ 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
+ }
138
+ };
139
+
140
+ /**
141
+ * 处理dp上报
142
+ * @param dps
143
+ */
144
+ 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
+ }
154
+ }
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
+ });
170
+ }
171
+
172
+ // 是否触发日志更新
173
+ if (refleshLogsEnabled) {
174
+ clearTimeout(logRefreshTimer);
175
+ // 2 秒后触发刷新
176
+ // @ts-expect-error
177
+ logRefreshTimer = setTimeout(() => {
178
+ emitter.emit(LOGCHANGE, dpData);
179
+ }, 2000);
180
+ }
181
+ }
182
+ };
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
+ // 更新设备状态
198
+ 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
+ };
221
+ let currentUser = null;
222
+
223
+ /**
224
+ * 获取当前用户信息
225
+ * @param isForce 是否强制刷新
226
+ * @returns
227
+ */
228
+ export const getCurrentUser = async isForce => {
229
+ if (currentUser && !isForce) {
230
+ 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
+ };
240
+
241
+ /**
242
+ * 获取当前用户信息
243
+ * 注意:此方法只能在初始化后使用
244
+ * @returns
245
+ */
246
+ export const getCurrentUserSync = () => {
247
+ return currentUser;
248
+ };
249
+
250
+ /**
251
+ * 获取设备旋转角度
252
+ * @returns
253
+ */
254
+ export const getMediaRotate = () => {
255
+ return {
256
+ imageAngle: config.imageAngle,
257
+ videoAngle: config.videoAngle
258
+ };
259
+ };
260
+
261
+ /**
262
+ * 远程开关门权限列表
263
+ */
264
+ export const permissions = ["adminsOnly", "everyOne", "noOne"];
265
+
266
+ /**
267
+ * 初始化数据状态
268
+ */
269
+ 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
+ });
321
+ };
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";
351
+ }
352
+ emitter.emit("onWiFiSignalChange", {
353
+ signal,
354
+ level: signalText
355
+ });
356
+ }
357
+ break;
358
+ }
359
+ }
360
+ }
361
+ };
362
+ 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
+ }
376
+ };
377
+
378
+ /**
379
+ * 添加事件
380
+ */
381
+ 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);
398
+ };
399
+
400
+ /**
401
+ * 添加事件
402
+ */
403
+ 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);
413
+ };
414
+
415
+ /**
416
+ * 注册设备状态信息变更事件
417
+ * @param callback 回调函数
418
+ */
419
+ export const onDeviceStatusChange = callback => {
420
+ emitter.on(DEVICE_STATUS_CHANGE_EVENT, callback);
421
+ };
422
+
423
+ /**
424
+ * 注销设备状态信息变更事件
425
+ * @param callback 回调函数
426
+ */
427
+ export const offDeviceStatusChange = callback => {
428
+ emitter.off(DEVICE_STATUS_CHANGE_EVENT, callback);
429
+ };
@@ -0,0 +1,5 @@
1
+ /**
2
+ * 同步密钥
3
+ */
4
+ declare const syncRemoteSerectKey: () => Promise<void>;
5
+ export default syncRemoteSerectKey;