-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathlisten.js
97 lines (87 loc) · 3 KB
/
listen.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
92
93
94
95
96
97
'use strict';
const { TTLockClient, LogOperate, LockedStatus } = require('../dist');
const settingsFile = "lockData.json";
async function doStuff() {
let lockData = await require("./common/loadData")(settingsFile);
let options = require("./common/options")(lockData);
const client = new TTLockClient(options);
await client.prepareBTService();
client.startMonitor();
console.log("Scan started");
client.on("foundLock", async (lock) => {
console.log(lock.toJSON());
console.log();
lock.on("locked", (l) => {
console.log("Lock was locked");
});
lock.on("unlocked", (l) => {
console.log("Lock was unlocked");
});
lock.on("updated", async (l, p) => {
if (p.newEvents && l.hasNewEvents()) {
if (!l.isConnected()) {
console.log("Reconnecting to lock to read log");
const res = await l.connect();
if (res == false) {
console.log("Unable to connect to lock");
process.exit(2);
}
}
let operations = await l.getOperationLog();
console.log(operations);
for (let op of operations) {
switch (op.recordType) {
case LogOperate.OPERATE_TYPE_MOBILE_UNLOCK:
case LogOperate.OPERATE_TYPE_KEYBOARD_PASSWORD_UNLOCK:
case LogOperate.OPERATE_TYPE_IC_UNLOCK_SUCCEED:
case LogOperate.OPERATE_TYPE_BONG_UNLOCK_SUCCEED:
case LogOperate.OPERATE_TYPE_FR_UNLOCK_SUCCEED:
case LogOperate.OPERATE_KEY_UNLOCK:
case LogOperate.GATEWAY_UNLOCK:
case LogOperate.ILLAGEL_UNLOCK:
case LogOperate.DOOR_SENSOR_UNLOCK:
// unlock event
console.log(">>>>>> Lock was unlocked <<<<<<");
break;
default:
// not unlock event
}
}
const status = await l.getLockStatus();
if (status == LockedStatus.LOCKED) {
console.log(">>>>>> Lock is now locked <<<<<<");
} else if (status == LockedStatus.UNLOCKED) {
console.log(">>>>>> Lock is now unlocked <<<<<<");
}
}
if (p.lockedStatus) {
const status = await l.getLockStatus();
if (status == LockedStatus.LOCKED) {
console.log(">>>>>> Lock is now locked from new event <<<<<<");
}
}
await l.disconnect();
});
if (lock.isInitialized() && lock.isPaired()) {
lock.on("disconnected", () => {
// setTimeout(() => {
// lock.connect();
// }, 3000);
// client.startScanLock();
console.log("Disconnected from a known lock");
client.startMonitor();
});
await lock.connect();
console.log("Connected to a known lock");
// make sure operation log is up to date
await lock.getOperationLog();
console.log();
console.log();
}
});
// save lock data changes
client.on("updatedLockData", async () => {
await require("./common/saveData")(settingsFile, client.getLockData());
});
}
doStuff();