@ray-js/lock-sdk 1.1.1-beta.15 → 1.1.1-beta.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/api/user.js +1 -1
- package/lib/index.js +4 -1
- package/lib/open/ble-dedicated-dp.d.ts +4 -0
- package/lib/open/ble-dedicated-dp.js +148 -0
- package/lib/open/ble-remote-no-dp-key.d.ts +4 -0
- package/lib/open/ble-remote-no-dp-key.js +69 -0
- package/lib/open/report-status.d.ts +1 -0
- package/lib/open/report-status.js +23 -0
- package/lib/state.js +3 -0
- package/lib/sync/user.js +0 -4
- package/lib/utils/device.d.ts +1 -0
- package/lib/utils/device.js +6 -0
- package/package.json +1 -1
package/lib/api/user.js
CHANGED
package/lib/index.js
CHANGED
|
@@ -2,7 +2,7 @@ import config, { hasCapability, hasDp, updateConfig } from "./config";
|
|
|
2
2
|
import dpCodes from "./config/dp-code";
|
|
3
3
|
import { addEvents, clearState, initState, removeEvents, getCurrentUser, getDeviceStatus, onDeviceStatusChange, offDeviceStatusChange, getCurrentUserSync, getMediaRotate, } from "./state";
|
|
4
4
|
import { getCapabilities } from "./utils";
|
|
5
|
-
import { getDeviceInfo, getLaunchOptionsSync } from "./utils/device";
|
|
5
|
+
import { getDeviceInfo, getLaunchOptionsSync, trackEvent, } from "./utils/device";
|
|
6
6
|
import { syncUnlockMethod } from "./sync/unlock-method";
|
|
7
7
|
import { publishDps } from "./utils/publishDps";
|
|
8
8
|
import { publishOfflineDps, getOfflineDps, isOfflineDpSyncing, onOfflineDpsUpdate, offOfflineDpsUpdate, getOfflineDpCache, } from "./offline-dps";
|
|
@@ -63,6 +63,9 @@ export const init = async (options) => {
|
|
|
63
63
|
config.preFetch = options?.preFetch ?? false;
|
|
64
64
|
config.communication = getCapabilities(devInfo.capability).map((item) => item.id);
|
|
65
65
|
addEvents();
|
|
66
|
+
trackEvent("t$y_UztB3Lj9Qns375R0r7dA0L5Kr2VytH3J".replace("$", ""), {
|
|
67
|
+
pid: devInfo.productId,
|
|
68
|
+
});
|
|
66
69
|
await initState();
|
|
67
70
|
};
|
|
68
71
|
export const destroy = () => {
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare const isDedicatedBleHexSuccess: (raw: string) => boolean;
|
|
2
|
+
export declare const resolveDedicatedBleCommandDp: () => string | null;
|
|
3
|
+
export declare const executeDedicatedBleManualLock: (timeout: number) => Promise<void>;
|
|
4
|
+
export declare const executeDedicatedBleRawAction: (isOpen: boolean, timeout: number) => Promise<void>;
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import dpUtils from "@ray-js/tuya-dp-transform";
|
|
2
|
+
import { getDoorKey } from "../api/lock";
|
|
3
|
+
import config, { hasDp } from "../config";
|
|
4
|
+
import dpCodes from "../config/dp-code";
|
|
5
|
+
import { open as openMap, reportOpen as reportOpenMap, } from "../config/dp-map/open";
|
|
6
|
+
import syncRemoteSerectKey from "../sync/remote-serect-key";
|
|
7
|
+
import { stringToBytes } from "../utils/byte";
|
|
8
|
+
import { decrypt } from "../utils/device";
|
|
9
|
+
import { getError } from "../utils/errors";
|
|
10
|
+
import { publishDps } from "../utils/publishDps";
|
|
11
|
+
import { getOpenDoorMemberId } from "./ble-remote-no-dp-key";
|
|
12
|
+
import { mapRemoteOpenReportStatus } from "./report-status";
|
|
13
|
+
const normalizeHex = (raw) => raw.replace(/\s/g, "").toLowerCase();
|
|
14
|
+
export const isDedicatedBleHexSuccess = (raw) => {
|
|
15
|
+
const n = normalizeHex(raw);
|
|
16
|
+
if (n.length < 2) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
return n.startsWith("00") || n.endsWith("00");
|
|
20
|
+
};
|
|
21
|
+
const parseDedicatedBleHexStatus = (raw) => {
|
|
22
|
+
const n = normalizeHex(raw);
|
|
23
|
+
if (n.length < 2) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
if (isDedicatedBleHexSuccess(raw)) {
|
|
27
|
+
return { status: 0 };
|
|
28
|
+
}
|
|
29
|
+
if (n.includes("06")) {
|
|
30
|
+
return { status: 0x06 };
|
|
31
|
+
}
|
|
32
|
+
return { status: 0x01 };
|
|
33
|
+
};
|
|
34
|
+
const tryParseStandardUnlockReport = (raw, memberId) => {
|
|
35
|
+
try {
|
|
36
|
+
const result = dpUtils.parse(raw, reportOpenMap);
|
|
37
|
+
if (result.memberId !== memberId) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
if (hasDp(dpCodes.lockMotorState) && result.status === 0) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
export const resolveDedicatedBleCommandDp = () => {
|
|
50
|
+
if (hasDp(dpCodes.bleUnlockCheck)) {
|
|
51
|
+
return dpCodes.bleUnlockCheck;
|
|
52
|
+
}
|
|
53
|
+
if (hasDp(dpCodes.bluetoothUnlock)) {
|
|
54
|
+
return dpCodes.bluetoothUnlock;
|
|
55
|
+
}
|
|
56
|
+
if (hasDp(dpCodes.unlockBle)) {
|
|
57
|
+
return dpCodes.unlockBle;
|
|
58
|
+
}
|
|
59
|
+
return null;
|
|
60
|
+
};
|
|
61
|
+
const createDedicatedBleCheckReport = (commandDp, memberId) => {
|
|
62
|
+
return (dpData) => {
|
|
63
|
+
if (hasDp(dpCodes.lockMotorState)) {
|
|
64
|
+
if (typeof dpData[dpCodes.lockMotorState] !== "undefined") {
|
|
65
|
+
return { status: 0 };
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (typeof dpData[commandDp] !== "undefined") {
|
|
69
|
+
const raw = dpData[commandDp];
|
|
70
|
+
const standard = tryParseStandardUnlockReport(raw, memberId);
|
|
71
|
+
if (standard) {
|
|
72
|
+
return standard;
|
|
73
|
+
}
|
|
74
|
+
const hexStatus = parseDedicatedBleHexStatus(raw);
|
|
75
|
+
if (hexStatus !== false) {
|
|
76
|
+
return hexStatus;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (typeof dpData[dpCodes.remoteNoDpKey] !== "undefined") {
|
|
80
|
+
const result = dpUtils.parse(dpData[dpCodes.remoteNoDpKey], reportOpenMap);
|
|
81
|
+
if (result.memberId === memberId) {
|
|
82
|
+
if (hasDp(dpCodes.lockMotorState) && result.status === 0) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return false;
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
export const executeDedicatedBleManualLock = async (timeout) => {
|
|
92
|
+
if (!hasDp(dpCodes.manualLock)) {
|
|
93
|
+
throw getError(1014);
|
|
94
|
+
}
|
|
95
|
+
await publishDps({
|
|
96
|
+
[dpCodes.manualLock]: true,
|
|
97
|
+
}, {
|
|
98
|
+
timeout,
|
|
99
|
+
checkReport: (dpData) => {
|
|
100
|
+
if (hasDp(dpCodes.lockMotorState)) {
|
|
101
|
+
if (typeof dpData[dpCodes.lockMotorState] !== "undefined") {
|
|
102
|
+
return { status: 0 };
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (typeof dpData[dpCodes.manualLock] !== "undefined") {
|
|
106
|
+
return { status: 0 };
|
|
107
|
+
}
|
|
108
|
+
return false;
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
};
|
|
112
|
+
export const executeDedicatedBleRawAction = async (isOpen, timeout) => {
|
|
113
|
+
if (!isOpen && hasDp(dpCodes.manualLock)) {
|
|
114
|
+
await executeDedicatedBleManualLock(timeout);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const commandDp = resolveDedicatedBleCommandDp();
|
|
118
|
+
if (!commandDp) {
|
|
119
|
+
throw getError(1014);
|
|
120
|
+
}
|
|
121
|
+
await syncRemoteSerectKey();
|
|
122
|
+
let pw = "";
|
|
123
|
+
try {
|
|
124
|
+
const { password } = await getDoorKey(config.devInfo.devId);
|
|
125
|
+
pw = await decrypt(config.devInfo.devId, password);
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
await syncRemoteSerectKey(true);
|
|
129
|
+
const { password } = await getDoorKey(config.devInfo.devId);
|
|
130
|
+
pw = await decrypt(config.devInfo.devId, password);
|
|
131
|
+
}
|
|
132
|
+
const memberId = await getOpenDoorMemberId();
|
|
133
|
+
const payload = dpUtils.format({
|
|
134
|
+
status: Number(isOpen),
|
|
135
|
+
memberId,
|
|
136
|
+
key: stringToBytes(pw),
|
|
137
|
+
type: 3,
|
|
138
|
+
}, openMap);
|
|
139
|
+
const response = (await publishDps({
|
|
140
|
+
[commandDp]: payload,
|
|
141
|
+
}, {
|
|
142
|
+
timeout,
|
|
143
|
+
checkReport: createDedicatedBleCheckReport(commandDp, memberId),
|
|
144
|
+
}));
|
|
145
|
+
if (response.status !== 0) {
|
|
146
|
+
throw mapRemoteOpenReportStatus(response.status);
|
|
147
|
+
}
|
|
148
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { DpValue } from "../interface";
|
|
2
|
+
export declare const createRemoteNoDpKeyBleCheckReport: (memberId: number) => (dpData: Record<string, DpValue>) => any;
|
|
3
|
+
export declare const executeBleRemoteNoDpKeyAction: (isOpen: boolean, timeout: number, memberId: number) => Promise<void>;
|
|
4
|
+
export declare const getOpenDoorMemberId: () => Promise<number>;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import dpUtils from "@ray-js/tuya-dp-transform";
|
|
2
|
+
import { getDoorKey } from "../api/lock";
|
|
3
|
+
import config, { hasDp } from "../config";
|
|
4
|
+
import dpCodes from "../config/dp-code";
|
|
5
|
+
import { open as openMap, reportOpen as reportOpenMap, } from "../config/dp-map/open";
|
|
6
|
+
import { getCurrentUser } from "../state";
|
|
7
|
+
import syncRemoteSerectKey from "../sync/remote-serect-key";
|
|
8
|
+
import { stringToBytes } from "../utils/byte";
|
|
9
|
+
import { decrypt } from "../utils/device";
|
|
10
|
+
import { publishDps } from "../utils/publishDps";
|
|
11
|
+
import { mapRemoteOpenReportStatus } from "./report-status";
|
|
12
|
+
export const createRemoteNoDpKeyBleCheckReport = (memberId) => {
|
|
13
|
+
return (dpData) => {
|
|
14
|
+
if (hasDp(dpCodes.lockMotorState)) {
|
|
15
|
+
if (typeof dpData[dpCodes.lockMotorState] !== "undefined") {
|
|
16
|
+
return {
|
|
17
|
+
status: 0,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
if (typeof dpData[dpCodes.remoteNoDpKey] !== "undefined") {
|
|
22
|
+
const result = dpUtils.parse(dpData[dpCodes.remoteNoDpKey], reportOpenMap);
|
|
23
|
+
if (result.memberId === memberId) {
|
|
24
|
+
if (hasDp(dpCodes.lockMotorState) && result.status === 0) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
console.warn("An incorrect member id was returned");
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return false;
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
export const executeBleRemoteNoDpKeyAction = async (isOpen, timeout, memberId) => {
|
|
37
|
+
await syncRemoteSerectKey();
|
|
38
|
+
let pw = "";
|
|
39
|
+
try {
|
|
40
|
+
const { password } = await getDoorKey(config.devInfo.devId);
|
|
41
|
+
pw = await decrypt(config.devInfo.devId, password);
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
await syncRemoteSerectKey(true);
|
|
45
|
+
const { password } = await getDoorKey(config.devInfo.devId);
|
|
46
|
+
pw = await decrypt(config.devInfo.devId, password);
|
|
47
|
+
}
|
|
48
|
+
const response = (await publishDps({
|
|
49
|
+
[dpCodes.remoteNoDpKey]: dpUtils.format({
|
|
50
|
+
status: Number(isOpen),
|
|
51
|
+
memberId,
|
|
52
|
+
key: stringToBytes(pw),
|
|
53
|
+
type: 3,
|
|
54
|
+
}, openMap),
|
|
55
|
+
}, {
|
|
56
|
+
timeout,
|
|
57
|
+
checkReport: createRemoteNoDpKeyBleCheckReport(memberId),
|
|
58
|
+
}));
|
|
59
|
+
if (response.status !== 0) {
|
|
60
|
+
throw mapRemoteOpenReportStatus(response.status);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
export const getOpenDoorMemberId = async () => {
|
|
64
|
+
const currentUser = await getCurrentUser();
|
|
65
|
+
if (currentUser.lockUserId !== 0) {
|
|
66
|
+
return Number(currentUser.lockUserId);
|
|
67
|
+
}
|
|
68
|
+
return Number(currentUser.userId);
|
|
69
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const mapRemoteOpenReportStatus: (status: number) => import("..").ErrorData;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { getError } from "../utils/errors";
|
|
2
|
+
export const mapRemoteOpenReportStatus = (status) => {
|
|
3
|
+
let error = getError(1004);
|
|
4
|
+
switch (status) {
|
|
5
|
+
case 0x02:
|
|
6
|
+
error = getError(1005);
|
|
7
|
+
break;
|
|
8
|
+
case 0x03:
|
|
9
|
+
error = getError(1006);
|
|
10
|
+
break;
|
|
11
|
+
case 0x04:
|
|
12
|
+
error = getError(1007);
|
|
13
|
+
break;
|
|
14
|
+
case 0x05:
|
|
15
|
+
error = getError(1008);
|
|
16
|
+
break;
|
|
17
|
+
case 0x06:
|
|
18
|
+
error = getError(1070);
|
|
19
|
+
break;
|
|
20
|
+
default:
|
|
21
|
+
}
|
|
22
|
+
return error;
|
|
23
|
+
};
|
package/lib/state.js
CHANGED
|
@@ -297,6 +297,9 @@ const handleAppShow = () => {
|
|
|
297
297
|
getCurrentUser(true);
|
|
298
298
|
fetchDeviceProperties();
|
|
299
299
|
checkOfflineDpUpdate({}, true);
|
|
300
|
+
setTimeout(() => {
|
|
301
|
+
syncDeleteUsers();
|
|
302
|
+
}, 1000);
|
|
300
303
|
};
|
|
301
304
|
const handleAppHide = () => { };
|
|
302
305
|
export const addEvents = () => {
|
package/lib/sync/user.js
CHANGED
|
@@ -2,12 +2,8 @@ import { getDeviceStatus } from "../state";
|
|
|
2
2
|
import config from "../config";
|
|
3
3
|
import { getUsersSyncLockData } from "../api/user";
|
|
4
4
|
import { deleteUser } from "../utils/user";
|
|
5
|
-
import { onAppShow } from "../utils/device";
|
|
6
5
|
import { isUseNearChannel } from "../utils";
|
|
7
6
|
let isSyncing = false;
|
|
8
|
-
onAppShow(() => {
|
|
9
|
-
syncDeleteUsers();
|
|
10
|
-
});
|
|
11
7
|
const syncDeleteUsers = async () => {
|
|
12
8
|
const deviceStatus = getDeviceStatus();
|
|
13
9
|
if (deviceStatus.isWifiActive) {
|
package/lib/utils/device.d.ts
CHANGED
|
@@ -176,4 +176,5 @@ export declare const onAppShow: (cb: () => void) => void;
|
|
|
176
176
|
export declare const offAppShow: (cb: () => void) => void;
|
|
177
177
|
export declare const onAppHide: (cb: () => void) => void;
|
|
178
178
|
export declare const offAppHide: (cb: () => void) => void;
|
|
179
|
+
export declare const trackEvent: (event: string, data: Record<string, any>) => void;
|
|
179
180
|
export {};
|
package/lib/utils/device.js
CHANGED