-
Notifications
You must be signed in to change notification settings - Fork 0
/
ble_wrapper.js
140 lines (122 loc) · 2.59 KB
/
ble_wrapper.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* Magic Mirror
* Module: MMM-MiFlora
*
* By Maikel Rehl https://github.com/rehlma
* MIT Licensed.
*/
/**
* Available Arguments
* --duration=30 Discovery duration in sec
* --interval=30 Sensordata update interval in min
* --address=c4:7c:8d:65:e7:22 Mac adress of the sensor. You can use it multiple times
* --address=c4:7c:8d:65:e7:23 Mac adress of the sensor. You can use it multiple times
*/
var miflora = require("miflora");
// Getting arguments
var args = require("minimist")(process.argv.slice(2));
var isConnected = false;
var connectedSensors = [];
const conf = {
interval: args.interval || 30 * 60 * 1000
};
const floraConf = {
duration: args.duration * 1000 || 60 * 1000,
addresses: setAdresses(),
ignoreUnknown: setAdresses().length !== 0
};
/**
* Set all addresses from arguments
*
* @returns {Array} Array of all MiFlora addresses
*/
function setAdresses() {
if (Array.isArray(args.address)) {
return args.address;
} else if (args.address) {
return [args.address];
} else {
return [];
}
}
/**
* Initialize connection
*/
function initConnection() {
console.log("Start discovery for " + floraConf.addresses);
isConnected = false;
connectedSensors = [];
miflora.discover(floraConf).then((devices) => {
console.log("Found " + devices.length + " devices");
if (devices.length === 0) {
console.log("Retry");
initConnection();
}
devices.forEach((device) => {
console.log(device.name + " [" + device.address + "]");
connect(device);
});
});
}
/**
* Connect to a device
*
* @param {device} device to connect to
*/
function connect(device) {
device
.connect()
.then((_) => {
console.log("Connected");
connectedSensors.push(device);
})
.then((_) => {
console.log("All devices connected");
isConnected = true;
// Initial data
publishData();
})
.catch((reason) => {
console.log(reason);
console.log("Retry after 3 sec");
setTimeout(() => {
connect(device);
}, 3000);
});
}
/**
* Gather all sensot data
*
* @returns {Promise} which includes all sensor data
*/
function getData() {
if (!isConnected) {
return;
}
var collect = [];
connectedSensors.forEach((sensor) => {
collect.push(sensor.query());
});
return Promise.all(collect);
}
/**
* Publish device data as JSON to stdout
*/
function publishData() {
if (!isConnected) {
return;
}
getData()
.then((data) => {
process.stdout.write(JSON.stringify(data));
})
.catch((reason) => {
console.log(reason);
});
}
(function () {
// Connect devices
initConnection();
setInterval(function () {
publishData();
}, conf.interval * 60 * 1000);
})();