-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
71 lines (63 loc) · 1.53 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
// Set authorized devices
acceptedDevices = [
'105.105.60.123.235.203'
];
// Local vars
var noneFound; // var name for timeout
var authorized = false;
// Set up hardware
require('tesselate') ({
modules: {
A: ['ble-ble113a', 'ble'],
B: ['relay-mono', 'relay']
}
}, function (tessel, modules) {
var ble = modules.ble;
var relay = modules.relay;
// Initial scan for devices
scan();
// When a device is discovered
ble.on('discover', function(peripheral) {
deviceID = peripheral.address._str;
if(acceptedDevices.indexOf(deviceID) > -1) {
console.log('Authorized device in range.', deviceID);
ble.stopScanning();
clearTimeout(noneFound);
if(!authorized) {
authorize();
}
// Check for changes
poll();
} else {
console.log('Unauthorized device discovered.', deviceID);
}
});
// Scan for devices regularly
function poll() {
setTimeout(scan, 5000);
}
// Check and see if authed devices in range
function scan () {
console.log('Scanning...');
ble.startScanning();
noneFound = setTimeout(function () {
ble.stopScanning();
console.log('No authorized BLE devices in range.');
if(authorized) {
deauthorize();
}
// Check for changes
poll();
}, 5000);
}
function authorize () {
authorized = true;
relay.turnOn(1);
console.log('authorized:', authorized);
}
function deauthorize () {
authorized = false;
relay.turnOff(1);
console.log('authorized:', authorized);
}
});