@ray-js/lock-sdk 1.0.4 → 1.1.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 (52) hide show
  1. package/lib/api/index.d.ts +25 -0
  2. package/lib/api/index.js +59 -3
  3. package/lib/api/linkage.js +7 -7
  4. package/lib/api/lock.js +16 -16
  5. package/lib/api/log.js +6 -6
  6. package/lib/api/request.d.ts +6 -0
  7. package/lib/api/request.js +57 -0
  8. package/lib/api/setting.d.ts +1 -1
  9. package/lib/api/setting.js +3 -3
  10. package/lib/api/temp.js +20 -20
  11. package/lib/api/user.d.ts +21 -0
  12. package/lib/api/user.js +26 -7
  13. package/lib/api/video.js +2 -2
  14. package/lib/capability.d.ts +4 -0
  15. package/lib/capability.js +95 -0
  16. package/lib/config/dp-code/index.d.ts +6 -1
  17. package/lib/config/dp-code/index.js +6 -1
  18. package/lib/config/dp-map/normal.d.ts +2 -3
  19. package/lib/config/dp-map/normal.js +2 -9
  20. package/lib/config/dp-map/unlock-method.d.ts +489 -0
  21. package/lib/config/dp-map/unlock-method.js +17 -0
  22. package/lib/config/index.d.ts +6 -1
  23. package/lib/config/index.js +17 -1
  24. package/lib/index.d.ts +6 -2
  25. package/lib/index.js +8 -3
  26. package/lib/interface.d.ts +33 -1
  27. package/lib/media.d.ts +1 -0
  28. package/lib/media.js +9 -1
  29. package/lib/offline-dps.d.ts +9 -0
  30. package/lib/offline-dps.js +84 -0
  31. package/lib/open.d.ts +3 -0
  32. package/lib/open.js +56 -10
  33. package/lib/other.d.ts +16 -2
  34. package/lib/other.js +174 -31
  35. package/lib/sleep.d.ts +3 -3
  36. package/lib/sleep.js +40 -25
  37. package/lib/state.js +116 -57
  38. package/lib/sync/offline-dps.d.ts +1 -0
  39. package/lib/sync/offline-dps.js +34 -0
  40. package/lib/sync/remote-serect-key.js +19 -23
  41. package/lib/sync/t0.js +2 -2
  42. package/lib/sync/unlock-method.d.ts +2 -0
  43. package/lib/sync/unlock-method.js +42 -0
  44. package/lib/sync/user.d.ts +2 -0
  45. package/lib/sync/user.js +76 -0
  46. package/lib/utils/device.d.ts +7 -0
  47. package/lib/utils/device.js +39 -0
  48. package/lib/utils/errors.js +3 -0
  49. package/lib/utils/index.js +6 -6
  50. package/lib/utils/ipc.d.ts +11 -0
  51. package/lib/utils/ipc.js +26 -0
  52. package/package.json +2 -2
@@ -0,0 +1,34 @@
1
+ import { clearOfflineDpData } from "../api";
2
+ import { getOfflineDps } from "../offline-dps";
3
+ import config from "../config";
4
+ import { isUseNearChannel } from "../utils";
5
+ import { publishDpsOnly } from "../utils/publishDps";
6
+ export const syncOfflineDps = async () => {
7
+ if (config.supportOfflineDps) {
8
+ const useNear = isUseNearChannel();
9
+ if (useNear) {
10
+ const offlineDps = await getOfflineDps();
11
+ try {
12
+ if (Object.keys(offlineDps).length) {
13
+ const hasTask = Object.keys(offlineDps).some((code) => offlineDps[code].pushStatus === false);
14
+ if (hasTask) {
15
+ try {
16
+ await clearOfflineDpData(config.devInfo.devId);
17
+ }
18
+ catch (error) {
19
+ return;
20
+ }
21
+ const dpData = Object.keys(offlineDps).reduce((acc, code) => {
22
+ acc[code] = offlineDps[code].value;
23
+ return acc;
24
+ }, {});
25
+ publishDpsOnly(dpData);
26
+ }
27
+ }
28
+ }
29
+ catch (error) {
30
+ console.warn(error);
31
+ }
32
+ }
33
+ }
34
+ };
@@ -8,36 +8,32 @@ import { decrypt } from "../utils/device";
8
8
  import { publishDpsOnly } from "../utils/publishDps";
9
9
  import dpUtils from "@ray-js/tuya-dp-transform";
10
10
  import { reportSetKey as reportSetKeyMap, setKey as setKeyMap, } from "../config/dp-map/open";
11
- import { ProductCommunicationType } from "../constant";
12
11
  const syncRemoteSerectKey = async (force) => {
13
12
  const useNear = isUseNearChannel();
14
13
  if (useNear) {
15
14
  try {
16
- const isSupportBle = config.communication.includes(ProductCommunicationType.BLUETOOTH);
17
- if (config.communication.length > 1 && isSupportBle) {
18
- if (!force) {
19
- const keyValue = getDpValue(dpCodes.remoteNoPdSetkey);
20
- if (keyValue) {
21
- const keyData = dpUtils.parse(getDpValue(dpCodes.remoteNoPdSetkey), reportSetKeyMap);
22
- if (keyData.status === 0) {
23
- return;
24
- }
15
+ if (!force) {
16
+ const keyValue = getDpValue(dpCodes.remoteNoPdSetkey);
17
+ if (keyValue) {
18
+ const keyData = dpUtils.parse(getDpValue(dpCodes.remoteNoPdSetkey), reportSetKeyMap);
19
+ if (keyData.status === 0) {
20
+ return;
25
21
  }
26
22
  }
27
- const { lockUserId, userId } = await getCurrentUser();
28
- const { password, invalidTime, effectiveTime } = await checkRemoteKey(config.devInfo.devId);
29
- const pw = await decrypt(config.devInfo.devId, password);
30
- publishDpsOnly({
31
- [dpCodes.remoteNoPdSetkey]: dpUtils.format({
32
- valid: true,
33
- memberId: lockUserId !== 0 ? lockUserId : userId,
34
- startTime: effectiveTime,
35
- endTime: invalidTime,
36
- validNum: 100,
37
- key: stringToBytes(pw),
38
- }, setKeyMap),
39
- });
40
23
  }
24
+ const { lockUserId, userId } = await getCurrentUser();
25
+ const { password, invalidTime, effectiveTime } = await checkRemoteKey(config.devInfo.devId);
26
+ const pw = await decrypt(config.devInfo.devId, password);
27
+ publishDpsOnly({
28
+ [dpCodes.remoteNoPdSetkey]: dpUtils.format({
29
+ valid: true,
30
+ memberId: lockUserId !== 0 ? lockUserId : userId,
31
+ startTime: effectiveTime,
32
+ endTime: invalidTime,
33
+ validNum: 100,
34
+ key: stringToBytes(pw),
35
+ }, setKeyMap),
36
+ });
41
37
  }
42
38
  catch (e) {
43
39
  console.warn(e);
package/lib/sync/t0.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { getActiveTime } from "../api";
2
- import config from "../config";
2
+ import config, { hasCapability } from "../config";
3
3
  import dpCodes from "../config/dp-code";
4
4
  import { ProductCommunicationType } from "../constant";
5
5
  import { isUseNearChannel } from "../utils";
@@ -9,7 +9,7 @@ const isNeedSync = () => {
9
9
  const near = isUseNearChannel();
10
10
  return (near ||
11
11
  (config.devInfo.isOnline &&
12
- config.communication.includes(ProductCommunicationType.THREAD)));
12
+ hasCapability(ProductCommunicationType.THREAD)));
13
13
  };
14
14
  const syncT0 = async () => {
15
15
  if (hasSysncT0) {
@@ -0,0 +1,2 @@
1
+ export declare const syncUnlockMethod: () => Promise<void>;
2
+ export declare const autoSyncUnlockMethod: () => Promise<void>;
@@ -0,0 +1,42 @@
1
+ import { fetchSyncUnlockMethodInfo } from "../api/lock";
2
+ import config from "../config";
3
+ import dpCodes from "../config/dp-code";
4
+ import { getCurrentUser } from "../state";
5
+ import { publishDps } from "../utils/publishDps";
6
+ import dpUtils from "@ray-js/tuya-dp-transform";
7
+ import { reportSync as reportSyncMap } from "../config/dp-map/unlock-method";
8
+ import { reportSync as reportSyncBigMap } from "../config/dp-map/unlock-method-big";
9
+ let syncUnlockMethodFirst = true;
10
+ export const syncUnlockMethod = async () => {
11
+ syncUnlockMethodFirst = false;
12
+ const { allOpenDps = "" } = await getCurrentUser();
13
+ const { ins = "", distributed = false } = await fetchSyncUnlockMethodInfo({
14
+ devId: config.devInfo.devId,
15
+ dpIds: allOpenDps.split(","),
16
+ });
17
+ if (ins && distributed) {
18
+ const dpCode = config.supportBigData
19
+ ? dpCodes.synchMethodW
20
+ : dpCodes.synchMethod;
21
+ await publishDps({ [dpCode]: ins }, {
22
+ checkReport: (dpData) => {
23
+ if (typeof dpData[dpCode] !== "undefined") {
24
+ const result = dpUtils.parse(dpData[dpCode], config.supportBigData ? reportSyncBigMap : reportSyncMap);
25
+ if (result.stage === 1) {
26
+ return true;
27
+ }
28
+ }
29
+ return false;
30
+ },
31
+ });
32
+ }
33
+ };
34
+ export const autoSyncUnlockMethod = async () => {
35
+ try {
36
+ if (syncUnlockMethodFirst) {
37
+ await syncUnlockMethod();
38
+ }
39
+ }
40
+ catch (e) {
41
+ }
42
+ };
@@ -0,0 +1,2 @@
1
+ declare const syncDeleteUsers: () => Promise<void>;
2
+ export default syncDeleteUsers;
@@ -0,0 +1,76 @@
1
+ import { getDeviceStatus } from "../state";
2
+ import { ProductCommunicationType } from "../constant";
3
+ import config, { hasCapability } from "../config";
4
+ import { getUsersSyncLockData, removeUser } from "../api/user";
5
+ import dpCodes from "../config/dp-code";
6
+ import dpUtils from "@ray-js/tuya-dp-transform";
7
+ import { remove as removeBigMap, reportRemove as reportRemoveBigMap, } from "../config/dp-map/unlock-method-big";
8
+ import { remove as removeMap, reportRemove as reportRemoveMap, } from "../config/dp-map/unlock-method";
9
+ import { publishDps } from "../utils/publishDps";
10
+ const deleteUser = async (data) => {
11
+ const { admin, lockUserId, userId } = data;
12
+ const dpCode = config.supportBigData
13
+ ? dpCodes.unlockMethodDelW
14
+ : dpCodes.unlockMethodDel;
15
+ const dpData = {
16
+ type: 0,
17
+ stage: 0,
18
+ admin: !!admin,
19
+ memberId: lockUserId,
20
+ unlockId: config.supportBigData ? 0xffff : 0xff,
21
+ category: 0,
22
+ };
23
+ await publishDps({
24
+ [dpCode]: dpUtils.format(dpData, (config.supportBigData ? removeBigMap : removeMap)),
25
+ }, {
26
+ checkReport: (dps) => {
27
+ if (dps[dpCode]) {
28
+ const dpValue = dpUtils.parse(dps[dpCode], (config.supportBigData
29
+ ? reportRemoveBigMap
30
+ : reportRemoveMap));
31
+ if (dpValue.type === 0 &&
32
+ dpValue.status == 255 &&
33
+ dpValue.memberId === lockUserId) {
34
+ return true;
35
+ }
36
+ }
37
+ return false;
38
+ },
39
+ });
40
+ await removeUser(config.devInfo.devId, userId);
41
+ };
42
+ let isSyncDone = false;
43
+ const syncDeleteUsers = async () => {
44
+ const deviceStatus = getDeviceStatus();
45
+ if (deviceStatus.isWifiActive) {
46
+ return;
47
+ }
48
+ const hasThread = hasCapability(ProductCommunicationType.THREAD);
49
+ if (hasThread &&
50
+ (deviceStatus.onlineType === "local" || deviceStatus.onlineType === "cloud")) {
51
+ return;
52
+ }
53
+ if (isSyncDone)
54
+ return;
55
+ if (deviceStatus.type === "online" && deviceStatus.onlineType === "ble") {
56
+ isSyncDone = true;
57
+ let count = 0;
58
+ const handleSync = async () => {
59
+ try {
60
+ const { removedUser = [] } = await getUsersSyncLockData(config.devInfo.devId);
61
+ await Promise.all(removedUser.map((item) => deleteUser(item).catch(() => { })));
62
+ isSyncDone = false;
63
+ }
64
+ catch (e) {
65
+ if (e?.innerError?.errorCode === "DEVICE_KEY_NOT_FOUND" && count < 3) {
66
+ count++;
67
+ setTimeout(async () => {
68
+ await handleSync();
69
+ }, 2000);
70
+ }
71
+ }
72
+ };
73
+ handleSync();
74
+ }
75
+ };
76
+ export default syncDeleteUsers;
@@ -168,4 +168,11 @@ export declare const getHomeInfo: () => Promise<{
168
168
  }>;
169
169
  export declare const navigateToMiniProgram: (params: ParamType<typeof ty.navigateToMiniProgram>) => void;
170
170
  export declare const navigateTo: (params: ParamType<typeof ty.navigateTo>) => void;
171
+ export declare const checkShowMatterMutilpleShare: (deviceId: string) => Promise<unknown>;
172
+ export declare const getBLEOnlineState: (deviceId: string) => Promise<boolean>;
173
+ export declare const connectBLEDevice: (deviceId: string) => Promise<void>;
174
+ export declare const onAppShow: (cb: () => void) => void;
175
+ export declare const offAppShow: (cb: () => void) => void;
176
+ export declare const onAppHide: (cb: () => void) => void;
177
+ export declare const offAppHide: (cb: () => void) => void;
171
178
  export {};
@@ -325,3 +325,42 @@ export const navigateToMiniProgram = (params) => {
325
325
  export const navigateTo = (params) => {
326
326
  return ty.navigateTo(params);
327
327
  };
328
+ export const checkShowMatterMutilpleShare = (deviceId) => {
329
+ return new Promise((resolve, reject) => {
330
+ return ty.device.checkShowMatterMutilpleShare({
331
+ deviceId,
332
+ success: resolve,
333
+ fail: reject,
334
+ });
335
+ });
336
+ };
337
+ export const getBLEOnlineState = (deviceId) => {
338
+ return new Promise((resolve, reject) => {
339
+ ty.device.getBLEOnlineState({
340
+ deviceId,
341
+ success: (res) => resolve(res.isOnline),
342
+ fail: reject,
343
+ });
344
+ });
345
+ };
346
+ export const connectBLEDevice = (deviceId) => {
347
+ return new Promise((resolve, reject) => {
348
+ ty.device.connectBLEDevice({
349
+ deviceId,
350
+ success: () => resolve(),
351
+ fail: reject,
352
+ });
353
+ });
354
+ };
355
+ export const onAppShow = (cb) => {
356
+ ty.onAppShow(cb);
357
+ };
358
+ export const offAppShow = (cb) => {
359
+ ty.offAppShow(cb);
360
+ };
361
+ export const onAppHide = (cb) => {
362
+ ty.onAppShow(cb);
363
+ };
364
+ export const offAppHide = (cb) => {
365
+ ty.offAppShow(cb);
366
+ };
@@ -60,6 +60,9 @@ const errors = {
60
60
  1059: "Doorbell notification service not enabled",
61
61
  1060: "Does not support adding normal members",
62
62
  1061: "Hardware ID does not match When update face",
63
+ 1062: "Device does not support active connection",
64
+ 1063: "Password length only supports 4-6 digits",
65
+ 1064: "Non-thread device, not supported in LAN connection to open and close locks",
63
66
  };
64
67
  export const getError = (code, ...places) => {
65
68
  if (errors[code]) {
@@ -1,6 +1,6 @@
1
- import config from "../config";
1
+ import config, { hasCapability } from "../config";
2
2
  import dpCodes from "../config/dp-code";
3
- import { ProductCommunicationType, } from "../constant";
3
+ import { ProductCommunicationType } from "../constant";
4
4
  import { LoopTypes } from "./constant";
5
5
  import { getBackgroundFetchData, getCurrentHomeInfo, onBackgroundFetchData, } from "./device";
6
6
  import { getError } from "./errors";
@@ -98,11 +98,11 @@ export const validateEffectiveConfig = (effective) => {
98
98
  }
99
99
  };
100
100
  export const isUseNearChannel = () => {
101
- const { onlineType, devInfo, communication } = config;
101
+ const { onlineType, devInfo } = config;
102
102
  const isBleOnline = isOnlineByType(onlineType, 2);
103
- const isSupportWifi = communication.includes(ProductCommunicationType.WIFI);
104
- const isSupportBle = communication.includes(ProductCommunicationType.BLUETOOTH);
105
- const isSupportThread = communication.includes(ProductCommunicationType.THREAD);
103
+ const isSupportWifi = hasCapability(ProductCommunicationType.WIFI);
104
+ const isSupportBle = hasCapability(ProductCommunicationType.BLUETOOTH);
105
+ const isSupportThread = hasCapability(ProductCommunicationType.THREAD);
106
106
  if (isSupportWifi && isSupportBle) {
107
107
  return isBleOnline;
108
108
  }
@@ -0,0 +1,11 @@
1
+ declare const _default: {
2
+ wakeUpDoorBell: (deviceId: string) => Promise<unknown>;
3
+ isConnected: (deviceId: string) => Promise<{
4
+ isConnected: boolean;
5
+ }>;
6
+ isConnecting: (deviceId: string) => Promise<{
7
+ isConnecting: boolean;
8
+ }>;
9
+ connect: (deviceId: string) => Promise<unknown>;
10
+ };
11
+ export default _default;
@@ -0,0 +1,26 @@
1
+ const wakeUpDoorBell = (deviceId) => {
2
+ return new Promise((resolve, reject) => {
3
+ ty.ipc.wakeUpDoorBell({ deviceId, success: resolve, fail: reject });
4
+ });
5
+ };
6
+ const isConnected = (deviceId) => {
7
+ return new Promise((resolve, reject) => {
8
+ ty.ipc.isConnected({ deviceId, success: resolve, fail: reject });
9
+ });
10
+ };
11
+ const isConnecting = (deviceId) => {
12
+ return new Promise((resolve, reject) => {
13
+ ty.ipc.isConnecting({ deviceId, success: resolve, fail: reject });
14
+ });
15
+ };
16
+ const connect = (deviceId) => {
17
+ return new Promise((resolve, reject) => {
18
+ ty.ipc.connect({ deviceId, success: resolve, fail: reject });
19
+ });
20
+ };
21
+ export default {
22
+ wakeUpDoorBell,
23
+ isConnected,
24
+ isConnecting,
25
+ connect,
26
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/lock-sdk",
3
- "version": "1.0.4",
3
+ "version": "1.1.0-beta.1",
4
4
  "files": [
5
5
  "lib",
6
6
  "LICENSE.md"
@@ -31,7 +31,7 @@
31
31
  "react": "^19.0.0"
32
32
  },
33
33
  "dependencies": {
34
- "@ray-js/tuya-dp-transform": "^0.0.6",
34
+ "@ray-js/tuya-dp-transform": "^0.0.7",
35
35
  "js-base64": "^3.7.7"
36
36
  }
37
37
  }