-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLock.js
91 lines (78 loc) · 2.25 KB
/
Lock.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// NOT UPGRADED
/**class Lock {
constructor(alarm) {
this.alarmCode = alarm.alarmCode;
this.installation = alarm.installation;
this.lockSerial = alarm.lockSerial;
}
getDoorLockState() {
const request = {
url: "/doorlockstate/search",
};
return this.installation
.client(request)
.then((doorLocks) =>
doorLocks.find((doorLock) => doorLock.deviceLabel === this.lockSerial)
)
.then((doorLock) => {
if (!doorLock) {
throw Error(`Could not find lock state for ${this.lockSerial}.`);
}
return doorLock;
});
}
getCurrentLockState(callback) {
this.log("Getting current lock state.");
this.getDoorLockState()
.then((doorLock) => {
callback(doorLock);
})
.catch(callback);
}
getTargetLockState(callback) {
this.log('Getting target lock state.');
this.getDoorLockState().then((doorLock) => {
const { pendingLockState, currentLockState } = doorLock;
const targetLockState = pendingLockState === 'NONE'
? currentLockState : pendingLockState;
callback(null, targetLockState === 'LOCKED'
? "LOCKED" : "UNLOCKED");
}).catch(callback);
}
setTargetLockState(value, callback) {
this.log(`Setting target lock state to: ${value}`);
const request = {
method: "PUT",
url: `/device/${this.lockSerial}/${value ? "lock" : "unlock"}`,
data: { code: this.alarmCode },
};
this.installation
.client(request)
.then(({ doorLockStateChangeTransactionId }) =>
this.resolveChangeResult(
`/doorlockstate/change/result/${doorLockStateChangeTransactionId}`
)
)
.then((result) => {
callback(result);
})
.catch(callback);
}
resolveChangeResult(url) {
this.log(`Resolving: ${url}`);
return this.installation.client({ url }).then(({ result }) => {
this.log(`Got "${result}" back from: ${url}`);
if (typeof result === "undefined" || result === "NO_DATA") {
return new Promise((resolve) =>
setTimeout(() => resolve(this.resolveChangeResult(url)), 200)
);
}
return result;
});
}
log(message) {
return console.log(`${message}`);
}
}
module.exports = Lock;
**/