@ray-js/lock-sdk 1.2.0-beta.9 → 1.2.0

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 (231) hide show
  1. package/lib/api/fit.d.ts +55 -2
  2. package/lib/api/fit.js +43 -1
  3. package/lib/api/gesture.d.ts +11 -0
  4. package/lib/api/gesture.js +18 -0
  5. package/lib/api/log.d.ts +52 -4
  6. package/lib/api/log.js +22 -0
  7. package/lib/api/temp.d.ts +21 -0
  8. package/lib/api/temp.js +24 -0
  9. package/lib/config/dp-code/index.d.ts +1 -1
  10. package/lib/config/dp-code/index.js +1 -0
  11. package/lib/entries/main/bootstrap/bootstrap-lock-instance.d.ts +2 -1
  12. package/lib/entries/main/bootstrap/bootstrap-lock-instance.js +23 -8
  13. package/lib/entries/main/bootstrap/bootstrap.d.ts +3 -2
  14. package/lib/entries/main/bootstrap/bootstrap.js +10 -6
  15. package/lib/entries/main/bootstrap/primary-instance.d.ts +1 -2
  16. package/lib/entries/main/bootstrap/primary-instance.js +1 -1
  17. package/lib/entries/main/bootstrap/resolve-runtime-from-export-this.js +1 -1
  18. package/lib/entries/main/build-instance.d.ts +6 -3
  19. package/lib/entries/main/build-instance.js +13 -1
  20. package/lib/entries/main/exports/capability.d.ts +2 -0
  21. package/lib/entries/main/exports/capability.js +7 -1
  22. package/lib/entries/main/exports/fit-sdk/binding.d.ts +10 -0
  23. package/lib/entries/{fit/exports → main/exports/fit-sdk}/binding.js +6 -3
  24. package/lib/entries/{fit/exports → main/exports/fit-sdk}/device.d.ts +11 -10
  25. package/lib/entries/{fit/exports → main/exports/fit-sdk}/device.js +20 -20
  26. package/lib/entries/{fit/exports → main/exports/fit-sdk}/index.d.ts +1 -0
  27. package/lib/entries/{fit/exports → main/exports/fit-sdk}/index.js +1 -0
  28. package/lib/entries/main/exports/fit-sdk/log.d.ts +3 -0
  29. package/lib/entries/main/exports/fit-sdk/log.js +5 -0
  30. package/lib/entries/main/exports/fit-sdk/temp.d.ts +40 -0
  31. package/lib/entries/main/exports/fit-sdk/temp.js +59 -0
  32. package/lib/entries/main/exports/gesture.d.ts +6 -0
  33. package/lib/entries/main/exports/gesture.js +8 -0
  34. package/lib/entries/main/exports/index.d.ts +5 -0
  35. package/lib/entries/main/exports/index.js +5 -0
  36. package/lib/entries/main/exports/log.d.ts +2 -0
  37. package/lib/entries/main/exports/log.js +7 -1
  38. package/lib/entries/main/exports/misc.d.ts +4 -2
  39. package/lib/entries/main/exports/misc.js +2 -2
  40. package/lib/entries/main/index.d.ts +3 -3
  41. package/lib/entries/main/index.js +11 -5
  42. package/lib/feature/fit/bind-lock-to-fit.d.ts +1 -0
  43. package/lib/feature/fit/bind-lock-to-fit.js +53 -0
  44. package/lib/feature/fit/ble-key.d.ts +10 -4
  45. package/lib/feature/fit/ble-key.js +145 -4
  46. package/lib/feature/fit/device-bv.d.ts +2 -0
  47. package/lib/feature/fit/device-bv.js +15 -0
  48. package/lib/feature/fit/invalid-password.d.ts +10 -0
  49. package/lib/feature/fit/invalid-password.js +43 -0
  50. package/lib/feature/fit/list.d.ts +12 -2
  51. package/lib/feature/fit/list.js +54 -5
  52. package/lib/feature/fit/log.d.ts +7 -0
  53. package/lib/feature/fit/log.js +123 -0
  54. package/lib/feature/fit/method/add-flow.js +2 -2
  55. package/lib/feature/fit/method/add-password.js +2 -2
  56. package/lib/feature/fit/method/update-remove.js +40 -14
  57. package/lib/feature/fit/password-list.d.ts +37 -0
  58. package/lib/feature/fit/password-list.js +44 -1
  59. package/lib/feature/fit/security-channel.d.ts +3 -0
  60. package/lib/feature/fit/security-channel.js +43 -0
  61. package/lib/feature/fit/shared.d.ts +8 -0
  62. package/lib/feature/fit/shared.js +40 -1
  63. package/lib/feature/fit/sync.d.ts +4 -2
  64. package/lib/feature/fit/sync.js +63 -22
  65. package/lib/feature/fit/temp-password.d.ts +23 -0
  66. package/lib/feature/fit/temp-password.js +95 -3
  67. package/lib/feature/fit/users.d.ts +1 -0
  68. package/lib/feature/fit/users.js +1 -0
  69. package/lib/feature/gesture/gesture.d.ts +6 -0
  70. package/lib/feature/gesture/gesture.js +3 -0
  71. package/lib/feature/log/log.d.ts +5 -1
  72. package/lib/feature/log/log.js +53 -6
  73. package/lib/feature/misc/ble-control.js +2 -2
  74. package/lib/feature/misc/capability.d.ts +2 -0
  75. package/lib/feature/misc/capability.js +33 -31
  76. package/lib/feature/misc/connect-device.d.ts +1 -1
  77. package/lib/feature/misc/connect-device.js +7 -6
  78. package/lib/feature/misc/is-use-near-channel-for-runtime.js +8 -1
  79. package/lib/feature/password/temporary/offline-create.d.ts +14 -0
  80. package/lib/feature/password/temporary/offline-create.js +77 -1
  81. package/lib/feature/service/goto-service.d.ts +1 -1
  82. package/lib/feature/state/clear.js +1 -0
  83. package/lib/feature/state/device-status.js +17 -6
  84. package/lib/feature/state/init-state.js +17 -9
  85. package/lib/feature/state/platform-routing.js +12 -3
  86. package/lib/feature/state/remote-permissions.js +2 -1
  87. package/lib/feature/state/sync/remote-serect-key.js +7 -0
  88. package/lib/feature/storage/swr-cache.d.ts +7 -0
  89. package/lib/feature/storage/swr-cache.js +54 -0
  90. package/lib/feature/unlock/method/add-flow.js +2 -2
  91. package/lib/feature/unlock/method/add-password.js +2 -2
  92. package/lib/feature/user/sync-user.js +4 -2
  93. package/lib/feature/utils/index.d.ts +1 -0
  94. package/lib/feature/utils/index.js +1 -0
  95. package/lib/interface.d.ts +23 -1
  96. package/lib/platform/device.d.ts +4 -3
  97. package/lib/platform/ipc.d.ts +3 -0
  98. package/lib/platform/ipc.js +13 -0
  99. package/lib/platform/storage.d.ts +1 -1
  100. package/lib/runtime/LockDeviceRuntime.js +3 -0
  101. package/lib/runtime/lock-config.d.ts +3 -0
  102. package/lib/runtime/lock-config.js +5 -0
  103. package/lib/runtime/runtime-state-types.d.ts +6 -0
  104. package/lib/utils/constant.d.ts +1 -0
  105. package/lib/utils/constant.js +6 -0
  106. package/lib/utils/index.d.ts +2 -1
  107. package/lib/utils/index.js +1 -0
  108. package/package.json +1 -11
  109. package/lib/bootstrap/bootstrap-lock-instance.d.ts +0 -7
  110. package/lib/bootstrap/bootstrap-lock-instance.js +0 -16
  111. package/lib/bootstrap/bootstrap.d.ts +0 -4
  112. package/lib/bootstrap/bootstrap.js +0 -53
  113. package/lib/bootstrap/lock-instance-runtime-ref.d.ts +0 -3
  114. package/lib/bootstrap/lock-instance-runtime-ref.js +0 -7
  115. package/lib/bootstrap/primary-instance.d.ts +0 -6
  116. package/lib/bootstrap/primary-instance.js +0 -24
  117. package/lib/bootstrap/resolve-runtime-from-export-this.d.ts +0 -5
  118. package/lib/bootstrap/resolve-runtime-from-export-this.js +0 -29
  119. package/lib/bootstrap/top-level-runtime.d.ts +0 -2
  120. package/lib/bootstrap/top-level-runtime.js +0 -9
  121. package/lib/entries/ble/bootstrap/bootstrap-lock-instance.d.ts +0 -7
  122. package/lib/entries/ble/bootstrap/bootstrap-lock-instance.js +0 -12
  123. package/lib/entries/ble/bootstrap/bootstrap.d.ts +0 -4
  124. package/lib/entries/ble/bootstrap/bootstrap.js +0 -53
  125. package/lib/entries/ble/bootstrap/lock-instance-runtime-ref.d.ts +0 -3
  126. package/lib/entries/ble/bootstrap/lock-instance-runtime-ref.js +0 -7
  127. package/lib/entries/ble/bootstrap/primary-instance.d.ts +0 -6
  128. package/lib/entries/ble/bootstrap/primary-instance.js +0 -24
  129. package/lib/entries/ble/bootstrap/resolve-runtime-from-export-this.d.ts +0 -5
  130. package/lib/entries/ble/bootstrap/resolve-runtime-from-export-this.js +0 -29
  131. package/lib/entries/ble/bootstrap/top-level-runtime.d.ts +0 -2
  132. package/lib/entries/ble/bootstrap/top-level-runtime.js +0 -9
  133. package/lib/entries/ble/build-instance.d.ts +0 -9
  134. package/lib/entries/ble/build-instance.js +0 -50
  135. package/lib/entries/ble/exports/capability.d.ts +0 -6
  136. package/lib/entries/ble/exports/capability.js +0 -17
  137. package/lib/entries/ble/exports/config.d.ts +0 -3
  138. package/lib/entries/ble/exports/config.js +0 -5
  139. package/lib/entries/ble/exports/dp.d.ts +0 -9
  140. package/lib/entries/ble/exports/dp.js +0 -13
  141. package/lib/entries/ble/exports/index.d.ts +0 -16
  142. package/lib/entries/ble/exports/index.js +0 -16
  143. package/lib/entries/ble/exports/linkage.d.ts +0 -5
  144. package/lib/entries/ble/exports/linkage.js +0 -15
  145. package/lib/entries/ble/exports/log.d.ts +0 -9
  146. package/lib/entries/ble/exports/log.js +0 -21
  147. package/lib/entries/ble/exports/media.d.ts +0 -4
  148. package/lib/entries/ble/exports/media.js +0 -12
  149. package/lib/entries/ble/exports/misc.d.ts +0 -19
  150. package/lib/entries/ble/exports/misc.js +0 -30
  151. package/lib/entries/ble/exports/offline-dps.d.ts +0 -8
  152. package/lib/entries/ble/exports/offline-dps.js +0 -24
  153. package/lib/entries/ble/exports/open/do-open-close-action.d.ts +0 -2
  154. package/lib/entries/ble/exports/open/do-open-close-action.js +0 -36
  155. package/lib/entries/ble/exports/open/index.d.ts +0 -18
  156. package/lib/entries/ble/exports/open/index.js +0 -45
  157. package/lib/entries/ble/exports/signal.d.ts +0 -6
  158. package/lib/entries/ble/exports/signal.js +0 -14
  159. package/lib/entries/ble/exports/sleep.d.ts +0 -13
  160. package/lib/entries/ble/exports/sleep.js +0 -31
  161. package/lib/entries/ble/exports/state.d.ts +0 -6
  162. package/lib/entries/ble/exports/state.js +0 -19
  163. package/lib/entries/ble/exports/temporary.d.ts +0 -31
  164. package/lib/entries/ble/exports/temporary.js +0 -91
  165. package/lib/entries/ble/exports/unlock/sync.d.ts +0 -1
  166. package/lib/entries/ble/exports/unlock/sync.js +0 -5
  167. package/lib/entries/ble/exports/unlock-method.d.ts +0 -19
  168. package/lib/entries/ble/exports/unlock-method.js +0 -58
  169. package/lib/entries/ble/exports/user/index.d.ts +0 -23
  170. package/lib/entries/ble/exports/user/index.js +0 -22
  171. package/lib/entries/ble/exports/user/sync.d.ts +0 -1
  172. package/lib/entries/ble/exports/user/sync.js +0 -5
  173. package/lib/entries/ble/index.d.ts +0 -8
  174. package/lib/entries/ble/index.js +0 -39
  175. package/lib/entries/fit/bootstrap/bootstrap-lock-instance.d.ts +0 -7
  176. package/lib/entries/fit/bootstrap/bootstrap-lock-instance.js +0 -12
  177. package/lib/entries/fit/bootstrap/bootstrap.d.ts +0 -4
  178. package/lib/entries/fit/bootstrap/bootstrap.js +0 -69
  179. package/lib/entries/fit/bootstrap/lock-instance-runtime-ref.d.ts +0 -3
  180. package/lib/entries/fit/bootstrap/lock-instance-runtime-ref.js +0 -7
  181. package/lib/entries/fit/bootstrap/primary-instance.d.ts +0 -6
  182. package/lib/entries/fit/bootstrap/primary-instance.js +0 -24
  183. package/lib/entries/fit/bootstrap/resolve-runtime-from-export-this.d.ts +0 -5
  184. package/lib/entries/fit/bootstrap/resolve-runtime-from-export-this.js +0 -29
  185. package/lib/entries/fit/bootstrap/top-level-runtime.d.ts +0 -2
  186. package/lib/entries/fit/bootstrap/top-level-runtime.js +0 -9
  187. package/lib/entries/fit/build-instance.d.ts +0 -9
  188. package/lib/entries/fit/build-instance.js +0 -57
  189. package/lib/entries/fit/exports/binding.d.ts +0 -8
  190. package/lib/entries/fit/exports/temp.d.ts +0 -19
  191. package/lib/entries/fit/exports/temp.js +0 -18
  192. package/lib/entries/fit/index.d.ts +0 -8
  193. package/lib/entries/fit/index.js +0 -36
  194. package/lib/entries/main/exports/open.d.ts +0 -16
  195. package/lib/entries/main/exports/open.js +0 -44
  196. package/lib/entries/screen/bootstrap/bootstrap-lock-instance.d.ts +0 -7
  197. package/lib/entries/screen/bootstrap/bootstrap-lock-instance.js +0 -12
  198. package/lib/entries/screen/bootstrap/bootstrap.d.ts +0 -4
  199. package/lib/entries/screen/bootstrap/bootstrap.js +0 -69
  200. package/lib/entries/screen/bootstrap/lock-instance-runtime-ref.d.ts +0 -3
  201. package/lib/entries/screen/bootstrap/lock-instance-runtime-ref.js +0 -7
  202. package/lib/entries/screen/bootstrap/primary-instance.d.ts +0 -6
  203. package/lib/entries/screen/bootstrap/primary-instance.js +0 -24
  204. package/lib/entries/screen/bootstrap/resolve-runtime-from-export-this.d.ts +0 -5
  205. package/lib/entries/screen/bootstrap/resolve-runtime-from-export-this.js +0 -29
  206. package/lib/entries/screen/bootstrap/top-level-runtime.d.ts +0 -2
  207. package/lib/entries/screen/bootstrap/top-level-runtime.js +0 -9
  208. package/lib/entries/screen/build-instance.d.ts +0 -9
  209. package/lib/entries/screen/build-instance.js +0 -57
  210. package/lib/entries/screen/exports/bind-events.d.ts +0 -4
  211. package/lib/entries/screen/exports/bind-events.js +0 -8
  212. package/lib/entries/screen/exports/binding.d.ts +0 -6
  213. package/lib/entries/screen/exports/binding.js +0 -15
  214. package/lib/entries/screen/exports/device.d.ts +0 -16
  215. package/lib/entries/screen/exports/device.js +0 -41
  216. package/lib/entries/screen/exports/event.d.ts +0 -3
  217. package/lib/entries/screen/exports/event.js +0 -5
  218. package/lib/entries/screen/exports/index.d.ts +0 -5
  219. package/lib/entries/screen/exports/index.js +0 -5
  220. package/lib/entries/screen/exports/user.d.ts +0 -2
  221. package/lib/entries/screen/exports/user.js +0 -8
  222. package/lib/entries/screen/index.d.ts +0 -8
  223. package/lib/entries/screen/index.js +0 -36
  224. package/lib/feature/unlock/door/door-action.d.ts +0 -2
  225. package/lib/feature/unlock/door/door-action.js +0 -32
  226. package/lib/fit.d.ts +0 -4
  227. package/lib/fit.js +0 -3
  228. package/lib/runtime/device-status/compute-device-status.d.ts +0 -25
  229. package/lib/runtime/device-status/compute-device-status.js +0 -94
  230. package/lib/screen.d.ts +0 -4
  231. package/lib/screen.js +0 -3
@@ -1,18 +1,141 @@
1
+ import dpUtils from "@ray-js/tuya-dp-transform";
1
2
  import { getFitBleKeyListApi, addFitBleKeyApi, updateFitBleKeyApi, removeFitBleKeyApi, renameFitBleKeyApi, } from "../../api/fit";
3
+ import dpCodes from "../../config/dp-code";
4
+ import { fitAddBleKey, fitModifyBleKey } from "../../config/dp-map/ble-key-fit";
5
+ import { publishDpsForRuntime } from "../dp/publish-dps";
6
+ import { getError } from "../../utils/errors";
2
7
  import { sdkLog } from "../../utils/sdk-log";
3
- import { formatWeek } from "../../utils";
8
+ import { UserType } from "../../constant";
9
+ import { getFitUsersForRuntime } from "./users";
10
+ import { formatWeek, parseWeek, transformEffectiveConfigToValidConfig, validateEffectiveConfig, } from "../../utils";
11
+ function normalizeBleKey(item) {
12
+ const effectiveConfig = {
13
+ effectiveDate: item.effectiveTime,
14
+ expiredDate: item.invalidTime,
15
+ };
16
+ const schedule = item.scheduleDetails?.[0];
17
+ if (schedule) {
18
+ if (schedule.allDay === false) {
19
+ effectiveConfig.repeat = "week";
20
+ effectiveConfig.weeks = parseWeek(schedule.workingDay);
21
+ effectiveConfig.effectiveTime = schedule.effectiveTime;
22
+ effectiveConfig.expiredTime = schedule.invalidTime;
23
+ }
24
+ else {
25
+ effectiveConfig.repeat = "none";
26
+ }
27
+ }
28
+ return {
29
+ id: item.id,
30
+ unlockBindingId: item.unlockBindingId,
31
+ name: item.name,
32
+ opModeSubType: item.opModeSubType,
33
+ effectiveConfig,
34
+ sn: item.sn,
35
+ userName: item.userName,
36
+ devNameList: item.devNameList,
37
+ };
38
+ }
4
39
  export async function getFitBleKeyListForRuntime(runtime) {
5
40
  const startedAt = Date.now();
6
41
  sdkLog("info", "fit", "getBleKeyListStart", {
7
42
  devId: runtime.devId,
8
43
  });
9
44
  const list = await getFitBleKeyListApi(runtime.devId);
45
+ const result = (list || []).map(normalizeBleKey);
10
46
  sdkLog("info", "fit", "getBleKeyListSuccess", {
11
47
  devId: runtime.devId,
12
48
  duration: Date.now() - startedAt,
13
- data: { count: Array.isArray(list) ? list.length : 0 },
49
+ data: { count: result.length },
50
+ });
51
+ return result;
52
+ }
53
+ async function createFitBleKeyWithPayload(runtime, keyType, startedAt, payload, addToCloud) {
54
+ const report = await publishDpsForRuntime(runtime, { [dpCodes.bleKeyCreate]: payload }, {
55
+ checkReport: (dps) => {
56
+ const raw = dps[dpCodes.bleKeyCreate];
57
+ if (!raw || raw.length < 44)
58
+ return false;
59
+ return { sn: raw.slice(4, 6), status: raw.slice(42, 44) };
60
+ },
61
+ });
62
+ if (report.status !== "00") {
63
+ sdkLog("warn", "fit", "createBleKeyDeviceFail", {
64
+ devId: runtime.devId,
65
+ data: { type: keyType, status: report.status },
66
+ });
67
+ throw getError(1042);
68
+ }
69
+ try {
70
+ const result = await addToCloud(parseInt(report.sn, 16));
71
+ sdkLog("info", "fit", "createBleKeySuccess", {
72
+ devId: runtime.devId,
73
+ duration: Date.now() - startedAt,
74
+ data: { type: keyType },
75
+ });
76
+ return result;
77
+ }
78
+ catch (error) {
79
+ publishDpsForRuntime(runtime, { [dpCodes.bleKeyDelete]: "0000" }, { timeout: 5000 }).catch(() => {
80
+ });
81
+ sdkLog("error", "fit", "createBleKeyCloudFail", {
82
+ devId: runtime.devId,
83
+ duration: Date.now() - startedAt,
84
+ data: { type: keyType },
85
+ error,
86
+ });
87
+ throw error;
88
+ }
89
+ }
90
+ export async function createFitPermanentBleKeyForRuntime(runtime, userId) {
91
+ const startedAt = Date.now();
92
+ sdkLog("info", "fit", "createBleKeyStart", {
93
+ devId: runtime.devId,
94
+ data: { type: "permanent" },
95
+ });
96
+ const users = await getFitUsersForRuntime(runtime);
97
+ const user = users.find((item) => item.userId === userId);
98
+ if (!user) {
99
+ sdkLog("warn", "fit", "createBleKeyUserNotFound", {
100
+ devId: runtime.devId,
101
+ duration: Date.now() - startedAt,
102
+ data: { userCount: users.length },
103
+ });
104
+ throw getError(1048);
105
+ }
106
+ const admin = user.userType === UserType.ADMIN;
107
+ const payload = `000000${admin ? "01" : "00"}${user.userTimeSet}`;
108
+ return createFitBleKeyWithPayload(runtime, "permanent", startedAt, payload, (sn) => addFitBleKeyApi(runtime.devId, { sn, userId, timeSetType: 10 }));
109
+ }
110
+ export async function createFitTemporaryBleKeyForRuntime(runtime, params) {
111
+ const startedAt = Date.now();
112
+ sdkLog("info", "fit", "createBleKeyStart", {
113
+ devId: runtime.devId,
114
+ data: { type: "temporary" },
14
115
  });
15
- return list;
116
+ validateEffectiveConfig(params.effective);
117
+ const { effective } = params;
118
+ const payload = dpUtils.format({
119
+ slaveId: 0,
120
+ memberId: 0,
121
+ admin: false,
122
+ validConfig: transformEffectiveConfigToValidConfig(effective),
123
+ }, fitAddBleKey);
124
+ return createFitBleKeyWithPayload(runtime, "temporary", startedAt, payload, (sn) => addFitBleKeyApi(runtime.devId, {
125
+ sn,
126
+ effectiveTime: effective.effectiveDate,
127
+ invalidTime: effective.expiredDate,
128
+ schedule: [
129
+ {
130
+ allDay: effective.repeat !== "week",
131
+ effectiveTime: effective.effectiveTime ?? 0,
132
+ invalidTime: effective.expiredTime ?? 0,
133
+ workingDay: formatWeek(effective.weeks ?? [0]),
134
+ },
135
+ ],
136
+ name: params.name,
137
+ timeSetType: effective.repeat !== "week" ? 20 : 30,
138
+ }));
16
139
  }
17
140
  export async function addFitBleKeyForRuntime(runtime, params) {
18
141
  const startedAt = Date.now();
@@ -31,7 +154,7 @@ export async function addFitBleKeyForRuntime(runtime, params) {
31
154
  });
32
155
  return result;
33
156
  }
34
- export async function updateFitBleKeyForRuntime(runtime, params) {
157
+ export async function updateFitTemporaryBleKeyForRuntime(runtime, params) {
35
158
  const startedAt = Date.now();
36
159
  const logParams = params ?? {};
37
160
  sdkLog("info", "fit", "updateBleKeyStart", {
@@ -41,6 +164,24 @@ export async function updateFitBleKeyForRuntime(runtime, params) {
41
164
  hasId: typeof logParams["id"] === "string",
42
165
  },
43
166
  });
167
+ validateEffectiveConfig(params.effective);
168
+ const validConfig = transformEffectiveConfigToValidConfig(params.effective);
169
+ const payload = dpUtils.format({ slaveId: 0, memberId: 0, admin: false, validConfig }, fitModifyBleKey);
170
+ const report = await publishDpsForRuntime(runtime, { [dpCodes.bleKeyModify]: payload }, {
171
+ checkReport: (dps) => {
172
+ const raw = dps[dpCodes.bleKeyModify];
173
+ if (!raw || raw.length < 44)
174
+ return false;
175
+ return { status: raw.slice(42, 44) };
176
+ },
177
+ });
178
+ if (report.status !== "00") {
179
+ sdkLog("warn", "fit", "updateBleKeyDeviceFail", {
180
+ devId: runtime.devId,
181
+ data: { status: report.status },
182
+ });
183
+ throw getError(1042);
184
+ }
44
185
  const schedule = [
45
186
  {
46
187
  allDay: params.effective.repeat !== "week",
@@ -0,0 +1,2 @@
1
+ import type { LockDeviceRuntime } from "../../runtime/LockDeviceRuntime";
2
+ export declare const getFitDeviceBvForRuntime: (runtime: LockDeviceRuntime) => Promise<number>;
@@ -0,0 +1,15 @@
1
+ import { getFitDeviceBVApi } from "../../api/fit";
2
+ export const getFitDeviceBvForRuntime = (runtime) => {
3
+ const cached = runtime.cache.fitDeviceBvCache;
4
+ if (cached) {
5
+ return cached;
6
+ }
7
+ const pending = getFitDeviceBVApi(runtime.devId).catch((error) => {
8
+ if (runtime.cache.fitDeviceBvCache === pending) {
9
+ runtime.cache.fitDeviceBvCache = undefined;
10
+ }
11
+ throw error;
12
+ });
13
+ runtime.cache.fitDeviceBvCache = pending;
14
+ return pending;
15
+ };
@@ -0,0 +1,10 @@
1
+ import type { LockDeviceRuntime } from "../../runtime/LockDeviceRuntime";
2
+ import { type FitInvalidPwdQuery, type FitInvalidPwdListResult } from "../../api/fit";
3
+ export type { FitInvalidPwdQuery, FitInvalidPwdListResult };
4
+ export interface GetFitInvalidPwdListParams extends FitInvalidPwdQuery {
5
+ }
6
+ export interface ClearFitInvalidPwdParams {
7
+ opMode?: number | string;
8
+ }
9
+ export declare function getFitInvalidPwdListForRuntime(runtime: LockDeviceRuntime, params?: GetFitInvalidPwdListParams): Promise<FitInvalidPwdListResult>;
10
+ export declare function clearFitInvalidPwdForRuntime(runtime: LockDeviceRuntime, params?: ClearFitInvalidPwdParams): Promise<boolean>;
@@ -0,0 +1,43 @@
1
+ import { clearFitInvalidPwdApi, getFitInvalidPwdListApi, } from "../../api/fit";
2
+ import { getError } from "../../utils/errors";
3
+ import { sdkLog } from "../../utils/sdk-log";
4
+ export async function getFitInvalidPwdListForRuntime(runtime, params = {}) {
5
+ const startedAt = Date.now();
6
+ sdkLog("info", "fitInvalidPwd", "getListStart", {
7
+ devId: runtime.devId,
8
+ data: { hasOpMode: params.opMode !== undefined },
9
+ });
10
+ try {
11
+ const result = await getFitInvalidPwdListApi(runtime.devId, params);
12
+ return result;
13
+ }
14
+ catch (error) {
15
+ throw error;
16
+ }
17
+ }
18
+ export async function clearFitInvalidPwdForRuntime(runtime, params = {}) {
19
+ const startedAt = Date.now();
20
+ sdkLog("info", "fitInvalidPwd", "clearStart", {
21
+ devId: runtime.devId,
22
+ data: { hasOpMode: params.opMode !== undefined },
23
+ });
24
+ try {
25
+ const result = await clearFitInvalidPwdApi(runtime.devId, params.opMode);
26
+ sdkLog("info", "fitInvalidPwd", "clearSuccess", {
27
+ devId: runtime.devId,
28
+ duration: Date.now() - startedAt,
29
+ });
30
+ return result;
31
+ }
32
+ catch (error) {
33
+ sdkLog("error", "fitInvalidPwd", "clearFail", {
34
+ devId: runtime.devId,
35
+ duration: Date.now() - startedAt,
36
+ error,
37
+ });
38
+ if (typeof error?.errorCode === "number") {
39
+ throw error;
40
+ }
41
+ throw getError(1028);
42
+ }
43
+ }
@@ -1,3 +1,13 @@
1
+ import { type BoundFitLockItem, type UnbindLockItem } from "../../api/fit";
1
2
  import type { LockDeviceRuntime } from "../../runtime/LockDeviceRuntime";
2
- export declare function getBoundLockListForRuntime(runtime: LockDeviceRuntime): Promise<import("../../api/fit").BoundFitLockItem[]>;
3
- export declare function getUnbindLockListForRuntime(runtime: LockDeviceRuntime): Promise<import("../../api/fit").UnbindLockItem[]>;
3
+ export type FitLockUnbindableReason = "ble_version_mismatch" | "security_version_mismatch";
4
+ export interface UnbindLockListItem extends UnbindLockItem {
5
+ bindable: boolean;
6
+ unbindableReason?: FitLockUnbindableReason;
7
+ }
8
+ export interface BoundLockListItem extends BoundFitLockItem {
9
+ securityVersionMismatch: boolean;
10
+ securityNeedUpdate: boolean;
11
+ }
12
+ export declare function getBoundLockListForRuntime(runtime: LockDeviceRuntime): Promise<BoundLockListItem[]>;
13
+ export declare function getUnbindLockListForRuntime(runtime: LockDeviceRuntime): Promise<UnbindLockListItem[]>;
@@ -1,28 +1,77 @@
1
1
  import { getBoundLockListApi, getUnbindLockListApi, } from "../../api/fit";
2
2
  import { sdkLog } from "../../utils/sdk-log";
3
+ import { getFitDeviceBvForRuntime } from "./device-bv";
4
+ import { getSecurityUpdatedLockIds, isFitSecurityChannelEnabledForRuntime, } from "./security-channel";
5
+ const isSecurityVersionMismatch = (runtime, lock) => !!lock.isSupportScy && !isFitSecurityChannelEnabledForRuntime(runtime);
6
+ const resolveUnbindableReason = (runtime, lock, fitBv) => {
7
+ const lockBv = Number(lock.protocolVersion) || 0;
8
+ if (lockBv && fitBv && lockBv !== fitBv) {
9
+ return "ble_version_mismatch";
10
+ }
11
+ if (isSecurityVersionMismatch(runtime, lock)) {
12
+ return "security_version_mismatch";
13
+ }
14
+ return undefined;
15
+ };
3
16
  export async function getBoundLockListForRuntime(runtime) {
4
17
  const startedAt = Date.now();
5
18
  sdkLog("info", "fit", "getBoundLockListStart", {
6
19
  devId: runtime.devId,
7
20
  });
8
21
  const list = await getBoundLockListApi(runtime.devId);
22
+ const rawItems = Array.isArray(list) ? list : [];
23
+ const securityEnabled = isFitSecurityChannelEnabledForRuntime(runtime);
24
+ const securityUpdatedIds = securityEnabled && rawItems.some((lock) => lock.isSupportScy)
25
+ ? await getSecurityUpdatedLockIds(runtime.devId)
26
+ : [];
27
+ const items = rawItems.map((lock) => ({
28
+ ...lock,
29
+ securityVersionMismatch: isSecurityVersionMismatch(runtime, lock),
30
+ securityNeedUpdate: securityEnabled &&
31
+ !!lock.isSupportScy &&
32
+ !securityUpdatedIds.includes(lock.devId),
33
+ }));
9
34
  sdkLog("info", "fit", "getBoundLockListSuccess", {
10
35
  devId: runtime.devId,
11
36
  duration: Date.now() - startedAt,
12
- data: { count: Array.isArray(list) ? list.length : 0 },
37
+ data: {
38
+ count: items.length,
39
+ securityMismatchCount: items.filter((item) => item.securityVersionMismatch).length,
40
+ securityNeedUpdateCount: items.filter((item) => item.securityNeedUpdate)
41
+ .length,
42
+ },
13
43
  });
14
- return list;
44
+ return items;
15
45
  }
16
46
  export async function getUnbindLockListForRuntime(runtime) {
17
47
  const startedAt = Date.now();
18
48
  sdkLog("info", "fit", "getUnbindLockListStart", {
19
49
  devId: runtime.devId,
20
50
  });
21
- const list = await getUnbindLockListApi(runtime.devId);
51
+ const [list, fitBv] = await Promise.all([
52
+ getUnbindLockListApi(runtime.devId),
53
+ getFitDeviceBvForRuntime(runtime).catch((error) => {
54
+ sdkLog("warn", "fit", "getUnbindLockListBvFailed", {
55
+ devId: runtime.devId,
56
+ error,
57
+ });
58
+ return 0;
59
+ }),
60
+ ]);
61
+ const items = (Array.isArray(list) ? list : []).map((lock) => {
62
+ const unbindableReason = resolveUnbindableReason(runtime, lock, fitBv);
63
+ return unbindableReason
64
+ ? { ...lock, bindable: false, unbindableReason }
65
+ : { ...lock, bindable: true };
66
+ });
22
67
  sdkLog("info", "fit", "getUnbindLockListSuccess", {
23
68
  devId: runtime.devId,
24
69
  duration: Date.now() - startedAt,
25
- data: { count: Array.isArray(list) ? list.length : 0 },
70
+ data: {
71
+ count: items.length,
72
+ fitBv,
73
+ bindableCount: items.filter((item) => item.bindable).length,
74
+ },
26
75
  });
27
- return list;
76
+ return items;
28
77
  }
@@ -0,0 +1,7 @@
1
+ import { type GetLogsResult } from "../log/log";
2
+ import type { LockDeviceRuntime } from "../../runtime/LockDeviceRuntime";
3
+ export interface GetFitLogsParams {
4
+ page?: number;
5
+ pageSize?: number;
6
+ }
7
+ export declare function getFitLogsForRuntime(rt: LockDeviceRuntime, params?: GetFitLogsParams): Promise<GetLogsResult>;
@@ -0,0 +1,123 @@
1
+ import { getFitRecordListApi } from "../../api/log";
2
+ import { closeTypes, dpIdsForUsernameToUnlockName, unlockDp2Type, } from "../log/log";
3
+ import dpCodes from "../../config/dp-code";
4
+ import base64ToHex from "../../utils/base64-to-hex";
5
+ import { handleLocalOperation } from "../../utils/log";
6
+ import { getCurrentUserForRuntime } from "../user/current-user";
7
+ const fitRecordDps = [
8
+ dpCodes.bluetoothUnlockKit,
9
+ dpCodes.unlockPasswordKit,
10
+ dpCodes.unlockCardKit,
11
+ dpCodes.unlockFingerprintKit,
12
+ dpCodes.unlockFaceKit,
13
+ dpCodes.unlockEyeKit,
14
+ dpCodes.unlockHandKit,
15
+ dpCodes.unlockFingerVeinKit,
16
+ dpCodes.unlockTemporaryKit,
17
+ dpCodes.unlockDoubleKit,
18
+ dpCodes.unlockOfflinePd,
19
+ dpCodes.unlockDynamic,
20
+ dpCodes.lockRecord,
21
+ ];
22
+ function parseFitRecord(cfg, raw, currentUserId) {
23
+ const historyId = raw.historyId ?? raw.id;
24
+ const time = raw.gmtCreate ?? Number(raw.time);
25
+ const dpId = typeof raw.dpId === "string" ? Number(raw.dpId) : raw.dpId;
26
+ let closeValue = undefined;
27
+ let dpValue = raw.dpValue;
28
+ const dpCode = cfg.codesById[dpId];
29
+ let type = dpCode === dpCodes.lockRecord ? "close_record" : "unlock_record";
30
+ const dpSchema = cfg.dpSchema[dpCode];
31
+ if (dpSchema?.type === "raw" && typeof dpValue === "string") {
32
+ try {
33
+ dpValue = base64ToHex(dpValue);
34
+ }
35
+ catch {
36
+ }
37
+ }
38
+ if (type === "close_record") {
39
+ const rawCloseValue = typeof dpValue === "string" ? dpValue : "";
40
+ const closeType = parseInt(rawCloseValue.slice(0, 2), 16);
41
+ closeValue = closeTypes[closeType] || closeTypes[0];
42
+ }
43
+ if (dpCode === dpCodes.lockLocalRecord) {
44
+ type = "local_operation";
45
+ }
46
+ let userName = raw.userName;
47
+ let unlockName = raw.unlockName;
48
+ if (dpIdsForUsernameToUnlockName.includes(dpId)) {
49
+ unlockName = raw.userName;
50
+ userName = "";
51
+ }
52
+ const log = {
53
+ type,
54
+ logId: String(historyId),
55
+ userId: raw.userId,
56
+ dpId,
57
+ data: dpValue,
58
+ dpCode,
59
+ userName: userName || "",
60
+ unlockName: unlockName || "",
61
+ avatarUrl: raw.avatarUrl,
62
+ relateDevName: raw.relateDevName,
63
+ mediaInfo: raw.mediaInfoList?.map?.((item) => {
64
+ return {
65
+ mediaPath: item.mediaPath,
66
+ fileKey: item.fileKey,
67
+ fileUrl: item.fileUrl,
68
+ mediaBucket: item.mediaBucket,
69
+ mediaKey: item.mediaKey,
70
+ width: item.positionWidth,
71
+ height: item.positionHeight,
72
+ angle: item.positionAngle,
73
+ isVR: !!item.positionVr,
74
+ position: item.position,
75
+ };
76
+ }),
77
+ unionUnlockInfo: raw.unionUnlockInfo?.map((item) => ({
78
+ userId: item.userId,
79
+ userName: item.userName,
80
+ unlockName: item.unlockName,
81
+ sn: item.sn,
82
+ type: unlockDp2Type[item.opMode],
83
+ })),
84
+ closeValue,
85
+ time,
86
+ currentUser: +raw.userId === +currentUserId,
87
+ bindable: raw.memberBindableFlag === 1,
88
+ unlockMethod: type === "unlock_record" ? unlockDp2Type[dpCode] : undefined,
89
+ localRecord: handleLocalOperation(raw.localOperateRecordInfo, dpValue),
90
+ };
91
+ if (raw.matterRecordInfo) {
92
+ log.matterRecord = {
93
+ sn: raw.matterRecordInfo.matterSn,
94
+ type: raw.matterRecordInfo.matterType,
95
+ source: raw.matterRecordInfo.matterSource,
96
+ };
97
+ }
98
+ return log;
99
+ }
100
+ export async function getFitLogsForRuntime(rt, params) {
101
+ const cfg = rt.config;
102
+ const { page = 1, pageSize = 50 } = params || {};
103
+ const currentUser = await getCurrentUserForRuntime(rt);
104
+ const data = await getFitRecordListApi({
105
+ devId: cfg.devInfo.devId,
106
+ dpIds: fitRecordDps.reduce((res, item) => {
107
+ if (cfg.idsByCode[item]) {
108
+ res.push(cfg.idsByCode[item]);
109
+ }
110
+ return res;
111
+ }, []),
112
+ offset: (page - 1) * pageSize,
113
+ limit: pageSize,
114
+ });
115
+ if (!data?.datas?.length) {
116
+ return { list: [], hasMore: false, total: 0 };
117
+ }
118
+ return {
119
+ list: data.datas.map((raw) => parseFitRecord(cfg, raw, currentUser.userId)),
120
+ hasMore: data.hasNext,
121
+ total: data.totalCount,
122
+ };
123
+ }
@@ -1,6 +1,6 @@
1
1
  import dpUtils from "@ray-js/tuya-dp-transform";
2
2
  import { addFitOpenMode } from "../../../api/fit";
3
- import { sleep } from "../../../utils";
3
+ import { formatCloudUnlockId, sleep } from "../../../utils";
4
4
  import { DPCHANGE } from "../../../utils/constant";
5
5
  import { getError } from "../../../utils/errors";
6
6
  import emitter from "../../../utils/event";
@@ -195,7 +195,7 @@ const handleAddReport = async (dps) => {
195
195
  await addFitOpenMode({
196
196
  devIdList: [addUnlockMethodData.lockDevId],
197
197
  fittingId: rt.devId,
198
- unlockId: `${addUnlockMethodData.unlockDpId}-${result.unlockId}`,
198
+ unlockId: formatCloudUnlockId(addUnlockMethodData.unlockDpId, result.unlockId),
199
199
  unlockName: "",
200
200
  userId: addUnlockMethodData.userId,
201
201
  hijack: false,
@@ -1,6 +1,6 @@
1
1
  import dpUtils from "@ray-js/tuya-dp-transform";
2
2
  import { addFitOpenMode } from "../../../api/fit";
3
- import { sleep } from "../../../utils";
3
+ import { formatCloudUnlockId, sleep } from "../../../utils";
4
4
  import { getError, handleCloudError } from "../../../utils/errors";
5
5
  import { publishDpsForRuntime } from "../../dp/publish-dps";
6
6
  import { getFitUnlockMethodBase } from "./internal/base";
@@ -75,7 +75,7 @@ export async function fitAddPasswordForRuntime(rt, params) {
75
75
  await addFitOpenMode({
76
76
  devIdList: [lockDevId],
77
77
  fittingId,
78
- unlockId: `${dpId}-${res.unlockId}`,
78
+ unlockId: formatCloudUnlockId(dpId, res.unlockId),
79
79
  unlockName: params.name,
80
80
  userId: params.userId,
81
81
  hijack: !!params.hijack,
@@ -1,14 +1,40 @@
1
1
  import dpUtils from "@ray-js/tuya-dp-transform";
2
- import { removeFitOpenMode, updateFitOpenMode } from "../../../api/fit";
2
+ import { getFitOpenModeList, removeFitOpenMode, updateFitOpenMode, } from "../../../api/fit";
3
3
  import dpCodes from "../../../config/dp-code";
4
4
  import { fitRemove, fitReportRemove, } from "../../../config/dp-map/unlock-method-fit";
5
5
  import { isAdmin } from "../../../utils";
6
6
  import { getError, handleCloudError } from "../../../utils/errors";
7
7
  import { publishDpsForRuntime } from "../../dp/publish-dps";
8
- import { getUnlockMethodTypeByDpCodeForRuntime, parallelOnlyForRuntime, } from "../../utils";
9
- import { getUnlockMethodDetailForRuntime } from "../../unlock/method/detail";
10
- import { resolveSlaveId } from "../resolve-slave-id";
8
+ import { getUnlockMethodTypeByTypeForRuntime, parallelOnlyForRuntime, } from "../../utils";
11
9
  import { sdkLog } from "../../../utils/sdk-log";
10
+ const FIT_DELETABLE_UNLOCK_METHOD_TYPES = [
11
+ "card",
12
+ "finger",
13
+ "face",
14
+ "hand",
15
+ "fingerVein",
16
+ ];
17
+ async function findFitOpenModeRawById(rt, id) {
18
+ for (const type of FIT_DELETABLE_UNLOCK_METHOD_TYPES) {
19
+ let unlockMethodConfig;
20
+ try {
21
+ unlockMethodConfig = getUnlockMethodTypeByTypeForRuntime(rt, type);
22
+ }
23
+ catch {
24
+ continue;
25
+ }
26
+ const dpId = rt.config.idsByCode[unlockMethodConfig.code];
27
+ if (!dpId) {
28
+ continue;
29
+ }
30
+ const raw = await getFitOpenModeList(rt.devId, dpId);
31
+ const hit = (raw ?? []).find((item) => item.unlockBindingId === id);
32
+ if (hit) {
33
+ return { raw: hit, unlockMethodConfig };
34
+ }
35
+ }
36
+ return null;
37
+ }
12
38
  export async function fitUpdateUnlockMethodForRuntime(rt, params) {
13
39
  const startedAt = Date.now();
14
40
  sdkLog("info", "fitUnlockMethod", "updateStart", {
@@ -49,19 +75,20 @@ export const fitDeleteUnlockMethodForRuntime = parallelOnlyForRuntime(async func
49
75
  devId: rt.devId,
50
76
  data: { id, hasLockDevId: typeof lockDevId === "string" },
51
77
  });
52
- const detail = await getUnlockMethodDetailForRuntime(rt, id);
53
- const { lockUserId, unlockId, userType, dpId } = detail;
54
- const code = cfg.codesById[dpId];
55
- if (!code) {
56
- const error = getError(1014);
57
- sdkLog("warn", "fitUnlockMethod", "deleteMissingDpCode", {
78
+ const found = await findFitOpenModeRawById(rt, id);
79
+ if (!found) {
80
+ const error = getError(1040);
81
+ sdkLog("warn", "fitUnlockMethod", "deleteRecordNotFound", {
58
82
  devId: rt.devId,
59
- data: { id, dpId },
83
+ data: { id },
60
84
  error,
61
85
  });
62
86
  throw error;
63
87
  }
64
- const unlockMethodConfig = getUnlockMethodTypeByDpCodeForRuntime(rt, code);
88
+ const { raw, unlockMethodConfig } = found;
89
+ const lockUserId = raw.lockUserId;
90
+ const unlockId = raw.sn;
91
+ const slaveId = parseInt(raw.devUnlockId, 16);
65
92
  const removeDpCode = dpCodes.unlockMethodDel;
66
93
  if (!cfg.dpSchema[removeDpCode]) {
67
94
  const error = getError(1014);
@@ -72,12 +99,11 @@ export const fitDeleteUnlockMethodForRuntime = parallelOnlyForRuntime(async func
72
99
  });
73
100
  throw error;
74
101
  }
75
- const slaveId = await resolveSlaveId(rt, lockDevId);
76
102
  const dpData = {
77
103
  slaveId,
78
104
  type: unlockMethodConfig.id,
79
105
  stage: 0,
80
- admin: isAdmin(userType),
106
+ admin: isAdmin(raw.userType),
81
107
  memberId: lockUserId,
82
108
  unlockId,
83
109
  operation: 1,
@@ -1,3 +1,4 @@
1
+ import { type RawFitOpenModeScheduleItem } from "../../api/fit";
1
2
  import { EffectiveConfig, UnlockMethodType } from "../../interface";
2
3
  import { LockDeviceRuntime } from "../../runtime/LockDeviceRuntime";
3
4
  export interface FitPasswordItem {
@@ -22,3 +23,39 @@ export declare const getFitPasswordListForRuntime: (runtime: LockDeviceRuntime)
22
23
  devLockId: string;
23
24
  effective: EffectiveConfig;
24
25
  }[]>;
26
+ export interface FitOnlinePasswordItem {
27
+ id: number;
28
+ pwdId: number;
29
+ sn: number;
30
+ gmtCreate: number;
31
+ opModeType: number;
32
+ opModeSubType: number;
33
+ name: string;
34
+ userName: string;
35
+ mobile: string;
36
+ hijack: boolean;
37
+ operate: number;
38
+ effectiveTime: number;
39
+ invalidTime: number;
40
+ devNameList: string[];
41
+ scheduleDetails: RawFitOpenModeScheduleItem[];
42
+ }
43
+ export interface FitOfflinePasswordItem {
44
+ id: number | string;
45
+ pwdId: number;
46
+ sn: number;
47
+ gmtCreate: number;
48
+ opModeType: number;
49
+ opModeSubType: number;
50
+ pwdName: string;
51
+ mayExpired: boolean;
52
+ gmtStart: number;
53
+ gmtExpired: number;
54
+ devNameList: string[];
55
+ scheduleDetails: RawFitOpenModeScheduleItem[];
56
+ revokedPwdName: string;
57
+ revokedPwdEffectiveTime: number;
58
+ revokedPwdInvalidTime: number;
59
+ }
60
+ export declare const getFitOnlinePasswordListForRuntime: (runtime: LockDeviceRuntime) => Promise<FitOnlinePasswordItem[]>;
61
+ export declare const getFitOfflinePasswordListForRuntime: (runtime: LockDeviceRuntime) => Promise<FitOfflinePasswordItem[]>;