-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (102 loc) · 2.88 KB
/
index.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// setup
const readLine = require('readline');
require('dotenv').config();
// Bluetooth lib
const noble = require('@abandonware/noble');
// Database lib
const DB = require('./database');
const noop = () => {};
if (!process.env.DEBUG) {
console.log = noop;
}
const { LEFT_BLIND, RIGHT_BLIND, TIMEOUT_S } = process.env;
const TIMEOUT_MS = TIMEOUT_S * 60 * 1000;
const left = LEFT_BLIND.replaceAll(':', '').toLowerCase();
const right = RIGHT_BLIND.replaceAll(':', '').toLowerCase();
const blinds = [left, right];
let discoveredDevices = 0;
const discoveredBlinds = [];
const batteryService = '180f';
const batteryLevelUUID = '2a19';
const batteryStatus = {
[left]: {
position: 'left',
id: left,
},
[right]: {
position: 'right',
id: right,
},
};
async function startDB() {
DB.setHost(process.env.DB_HOST, 'localhost');
await DB.init();
}
startDB();
startTimeout();
noble.on('stateChange', function (state) {
state === 'poweredOn' ? startScan(true) : stopScan();
});
noble.on('discover', async function (peripheral) {
const { id } = peripheral;
printProgress(++discoveredDevices);
const isBlindDevice = blinds.includes(id);
const hasAlreadySeen = discoveredBlinds.includes(id);
if (isBlindDevice && !hasAlreadySeen) {
noble.stopScanning();
await peripheral.connectAsync();
const {
characteristics,
} = await peripheral.discoverSomeServicesAndCharacteristicsAsync(
[batteryService],
[batteryLevelUUID]
);
const batteryLevel = (await characteristics[0].readAsync())[0];
batteryStatus[id].value = batteryLevel;
resetLine();
printLine(`Blind with ID: ${id} found - batteryLevel: ${batteryLevel}\n`);
discoveredBlinds.push(peripheral);
if (blinds.length === discoveredBlinds.length) {
stopScan();
console.log(`Found ${discoveredBlinds.length} blinds`);
console.log('writing to database');
DB.addToDatabase(batteryStatus);
printLine('\nUpdated');
await DB.readFromDatabase();
process.exit(0);
} else {
startScan();
}
}
});
noble.on('connect', function (state) {
console.log({ state });
});
noble.on('warning', function (message) {
console.warn({ message });
});
async function startScan(initial = false) {
initial && printLine(`Scanning...\n`);
await noble.startScanningAsync();
}
async function stopScan() {
await noble.stopScanningAsync();
}
function printLine(text, withDate = true) {
process.stdout.write(`${withDate ? new Date() : ''} ${text}`);
}
function printProgress(devices) {
resetLine();
printLine(devices + ' scanned', false);
}
function resetLine() {
readLine.clearLine(process.stdout, 0);
readLine.cursorTo(process.stdout, 0, null);
}
function startTimeout() {
setTimeout(async () => {
await noble.stopScanningAsync();
process.stderr.write(`\n\nTimeout after ${TIMEOUT_MS / 1000 / 60} mins`);
process.exit(1);
}, TIMEOUT_MS);
}