-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·176 lines (143 loc) · 4.38 KB
/
app.js
File metadata and controls
executable file
·176 lines (143 loc) · 4.38 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env node
'use strict';
var noble = require('noble');
var log = require('loglevel');
var settings = require('./settings.js');
var BluzDKModule = require('./bluz-dk-module.js');
var EOL = require('os').EOL;
var debuglevel = settings.get('DEBUG');
var loggingLevels = ['trace', 'debug', 'info', 'warn', 'error'];
if (loggingLevels.indexOf(debuglevel) > -1) {
log.setDefaultLevel(debuglevel);
} else {
log.setDefaultLevel('error');
}
var peripheralList = {};
var shuttingDown = false;
var scanning = false;
var BLUZ_SERVICE_UUID = '871e022338ff77b1ed419fb3aa142db2';
noble.on('stateChange', function (state) {
if (state === 'poweredOn') {
startScanHelper();
} else {
stopScanHelper();
}
});
function startScanHelper() {
if (shuttingDown || scanning)
return;
scanning = true;
if (noble.state === 'poweredOn')
noble.startScanning([], true);
}
function stopScanHelper() {
if (!scanning)
return;
scanning = false;
noble.stopScanning();
}
noble.on('warning', function (message) {
log.warn('Master: noble warning:', message)
});
var server = require('./info-server.js')(peripheralList);
function deletePeripheral(id) {
// TODO: Replace with event based deletion
stopScanHelper(); // Not sure if necessary, but probably doesn't hurt
if (peripheralList[id]) {
log.info('Master: Removing peripheral:', id);
delete peripheralList[id].dkModule;
delete peripheralList[id];
}
startScanHelper();
}
function checkForDeadPeripherals() {
for (var id in peripheralList) {
var peripheral = peripheralList[id];
if (settings.get('deviceGroups:whitelist').indexOf(peripheral.name) > -1 &&
(peripheral.dkModule == null || !peripheral.dkModule.connected)) {
deletePeripheral(id);
}
}
setTimeout(checkForDeadPeripherals, 5000);
}
// checkForDeadPeripherals();
// Discovery
noble.on('discover', function (peripheral) {
if (!peripheralList[peripheral.id] && settings.get('blacklist').indexOf(peripheral.id) < 0 &&
settings.get('deviceGroups:whitelist').indexOf(peripheral.advertisement.localName) > -1) {
stopScanHelper(); // turn off scanning while connecting HACK?
log.info('Master: Found peripheral with ID: ' + peripheral.id + ' and Name: ' + peripheral.advertisement.localName);
peripheralList[peripheral.id] = {
found: true,
dkModule: null,
name: peripheral.advertisement.localName
};
peripheral.connect(function (error) {
peripheral.discoverServices([BLUZ_SERVICE_UUID], function (error, services) {
if (error)
log.error('Master: Discover services error:', error);
log.trace(services);
if (services.length > 0) {
log.info('Master: Peripheral a bluz');
var bluzMod = new BluzDKModule(peripheral, function () {
deletePeripheral(peripheral.id);
});
peripheralList[peripheral.id].dkModule = bluzMod;
} else {
log.info('Master: Peripheral not a Bluz');
peripheral.disconnect();
}
startScanHelper(); // connected, resume scanning
});
});
}
});
// Program Exit Handling
if (process.platform === "win32") {
var rl = require("readline").createInterface({
input: process.stdin,
output: process.stdout
});
rl.on("SIGINT", function () {
process.emit("SIGINT");
});
}
process.on("SIGINT", processExitHandler);
process.on("SIGTERM", processExitHandler);
process.stdout.on('error', function (err) {
// Handle Ctrl-C (SIGINT) when piped to another command
if (err.code == "EPIPE") {
processExitHandler();
}
});
function processExitHandler() {
//graceful shutdown
log.warn();
log.warn('Master: Shutting Down');
shuttingDown = true;
stopScanHelper();
for (var key in peripheralList) {
if (peripheralList[key].dkModule != null) {
log.info('Master: Shutting Down', key);
peripheralList[key].dkModule.shutDown();
} else {
delete(peripheralList[key]);
}
}
server.close();
setTimeout(processExitChecker, 1000); // graceful check
setTimeout(function () {
log.warn('Master: Hard Shutdown');
process.exit();
}, 10000); // hard shutdown failsafe
}
function processExitChecker() {
var numLeft = Object.keys(peripheralList).length;
log.info('Master: ', numLeft, 'peripherals left');
if (numLeft == 0) {
log.warn('Master: Shutdown');
process.exit()
} else {
setTimeout(processExitChecker, 1000);
}
}