-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtilt-hydrometer.js
52 lines (41 loc) · 1.61 KB
/
tilt-hydrometer.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
#!/usr/bin/env node
var path = require('path');
var pkg = require(path.join(__dirname, 'package.json'));
var Bleacon = require('bleacon');
var request = require('request');
const pickObject = (obj, keys) => Object.keys(obj)
.filter(key => keys.indexOf(key) >= 0)
.reduce((newObj, key) => Object.assign(newObj, { [key]: obj[key] }), {});
// Parse command line options
var program = require('commander');
program
.version(pkg.version)
.parse(process.argv);
function buildPayload(bleacon){
var deviceLabel = tilt[bleacon.uuid] + '-' + bleacon.uuid; // Assigns the device label based on the TILT identified
bleacon.timeStamp = Date.now(); // Set the actual timestamp
// Build the payload by default
var payload = {
"uuid": deviceLabel,
"temperature":{ "value": bleacon.major, "timestamp": bleacon.timeStamp },
"gravity": { "value": bleacon.minor/1000, "timestamp": bleacon.timeStamp },
"rssi": { "value": bleacon.rssi, "timestamp": bleacon.timeStamp }
};
return payload;
}
Bleacon.on('discover', function (bleacon) {
// Identifies the TILT Hydrometer available
tilt = {
"a495bb10c5b14b44b5121370f02d74de": "Red",
"a495bb20c5b14b44b5121370f02d74de": "Green",
"a495bb30c5b14b44b5121370f02d74de": "Black",
"a495bb40c5b14b44b5121370f02d74de": "Purple",
"a495bb50c5b14b44b5121370f02d74de": "Orange",
"a495bb60c5b14b44b5121370f02d74de": "Blue",
"a495bb70c5b14b44b5121370f02d74de": "Pink"
};
if (tilt[bleacon.uuid] != null) {
console.log(buildPayload(bleacon));
}
});
Bleacon.startScanning();