-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbt.js
More file actions
135 lines (115 loc) · 3.03 KB
/
bt.js
File metadata and controls
135 lines (115 loc) · 3.03 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
var Firebase = require("firebase");
var FbRef = new Firebase(<url to your Firebase>);
var btSerial = new (require('bluetooth-serial-port')).BluetoothSerialPort();
var devicesRef;
var passedDevicesRef;
var devicesFoundInThisLastInquire=[];
//
// Add a device to the currently present list
//
addDevice = function(address, name){
if(devicesRef){
devicesRef.orderByChild("address").equalTo(address).once("value", function(snapshot) {
if(!snapshot.exists()){
var newDeviceRef = devicesRef.push();
newDeviceRef.set({ 'address': address, 'name': name, 'timestampIn': Firebase.ServerValue.TIMESTAMP });
console.log("Added");
}
});
}
}
//
// Start searching for nearby bluetooth devices
//
searchDevices = function(){
console.log("searching...");
devicesFoundInThisLastInquire.length = 0;
btSerial.inquire();
}
//
// Move to history the devices that are not nearby anymore
//
updateNonPresentDevices = function(){
devicesRef.once('value', function(dataSnapshot) {
dataSnapshot.forEach(function(childSnapshot) {
var device = childSnapshot.val();
if(!device.timestampOut && devicesFoundInThisLastInquire.indexOf(device.address) < 0){
console.log(device.address, " not present anymore");
var newPassedDeviceRef = passedDevicesRef.push();
newPassedDeviceRef.set(device);
newPassedDeviceRef.update({
"timestampOut": Firebase.ServerValue.TIMESTAMP
});
var deviceRef = devicesRef.child(childSnapshot.key())
deviceRef.remove();
}
});
});
}
//
// Stop the server/scanner after a defined time period
//
var tobeClosed = false;
closeTimeoutObject = setTimeout(function() {
tobeClosed = true;
console.log("to be closed");
}, 1200000); // 20 minutes
//
// authenticate using email/password
//
FbRef.authWithPassword({
//
// NEVER DO THIS!!! ALWAYS KEEP YOUR USER/PASS CRYPTED
//
"email": "you@yourserver.org",
"password": "yourpass"
}, function(error, authData) {
if (error) {
console.log("Login Failed!", error);
} else {
devicesRef = FbRef.child("devices");
passedDevicesRef = FbRef.child("passedDevices");
// All data is removed
devicesRef.remove();
passedDevicesRef.remove();
devicesRef.once('value', function(dataSnapshot) {
dataSnapshot.forEach(function(childSnapshot) {
console.log("Moving devices to passed");
var device = childSnapshot.val();
var newPassedDeviceRef = passedDevicesRef.push();
newPassedDeviceRef.set(device);
if(!device.timestampOut){
newPassedDeviceRef.update({
"timestampOut": 'unknown'
});
}
});
devicesRef.remove();
});
searchDevices();
}
});
//
// Bluetooth events
//
btSerial.on('found', function(address, name) {
console.log(address, name);
addDevice(address, name);
devicesFoundInThisLastInquire.push(address);
});
btSerial.on('finished', function() {
console.log("finished");
updateNonPresentDevices();
if(tobeClosed)
btSerial.close();
else
setTimeout(searchDevices, 5000);
});
btSerial.on('closed', function() {
console.log("closed");
process.exit(0);
});
//
// Running...
//
console.log("running...");