-
Notifications
You must be signed in to change notification settings - Fork 5
/
collector.js
211 lines (166 loc) · 6.2 KB
/
collector.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//
// Copyright (c) 2017 Cisco Systems
// Licensed under the MIT License
//
/**
* Collects PeopleCount analystics from a collection of RoomKits
* with a moving window interval
*/
const debug = require("debug")("collector");
const fine = require("debug")("collector:fine");
// Connects to a Room Device
const jsxapi = require("jsxapi");
function connect(device) {
return new Promise(function (resolve, reject) {
// Connect via SSH
const xapi = jsxapi.connect(`ssh://${device.ipAddress}`, {
username: device.username,
password: device.password
});
xapi.on('error', (err) => {
debug(`connexion failed for device: ${device.id}, ip address: ${device.ipAddress}, with err: ${err}`)
reject(err)
});
xapi.on('ready', () => {
fine(`connexion successful for device: ${device.id}`);
resolve(xapi);
});
});
}
// Time series storage
const stores = {};
function createStore(device) {
stores[device.id] = [];
}
function addCounter(device, date, count) {
fine(`adding count: ${count}, for device: ${device.id}`)
const store = stores[device.id];
store.push([date.toISOString(), count]);
}
// Initialize listeners for each device
const devices = require("./devices.json");
devices.forEach(device => {
fine(`connecting to device: ${device.id}`)
connect(device)
.then(xapi => {
debug(`connected to device: ${device.id}`);
// Check devices can count
xapi.status
.get('RoomAnalytics PeopleCount')
.then((counter) => {
fine(`fetched PeopleCount for device: ${device.id}`);
// Abort if device does not count
var count = counter.Current;
if (count == -1) {
debug(`device is not counting: ${device.id}`);
return;
}
// Store first TimeSeries
createStore(device);
addCounter(device, new Date(Date.now()), count);
// Listen to events
fine(`adding feedback listener to device: ${device.id}`);
xapi.feedback.on('/Status/RoomAnalytics/PeopleCount', (counter) => {
fine(`new PeopleCount: ${counter.Current}, for device: ${device.id}`);
// fetch PeopleCount value
var count = parseInt(counter.Current); // turn from string to integer
if (count == -1) {
debug(`WARNING: device '${device.id}' has stopped counting`);
return;
}
// register new TimeSeries
addCounter(device, new Date(Date.now()), count);
});
})
.catch((err) => {
debug(`Failed to retrieve PeopleCount status for device: ${device.id}, err: ${err.message}`);
console.log(`Please check your configuration: seems that '${device.id}' is NOT a Room device.`);
xapi.close();
});
})
.catch(err => {
debug(`Could not connect device: ${device.id}`)
})
});
//
// Clean TimeSeries
//
// Collect interval (moving window of collected time series)
var window = process.env.WINDOW ? process.env.WINDOW : 15 * 60; // in seconds
debug(`collecting window: ${window} seconds`);
// Individual store cleaner
function cleanStore(store) {
const oldest = Date.now() - window*1000;
const lowestDate = new Date(oldest).toISOString();
store.forEach(serie => {
if (serie[0] < lowestDate) {
store.shift()
}
});
}
// Dump time series in a store
function dumpSeries(store) {
store.forEach(serie => {
fine(`time: ${serie[0]}, count: ${serie[1]}`);
});
}
// Run cleaner
setInterval(function () {
Object.keys(stores).forEach((key) => {
fine(`cleaning TimeSeries for device: ${key}`);
const store = stores[key];
cleanStore(store);
if ("production" !== process.env.NODE_ENV) {
fine(`dumping stored series for device: ${key}`);
dumpSeries(store);
}
})
}, window * 1000); // in milliseconds
//
// Return people count for the device and averaged on the period (in seconds)
//
const { computeBarycentre } = require("./util/barycentre");
module.exports.averageOnPeriod = function (device, period) {
fine(`searching store for device: ${device}`);
const store = stores[device];
if (!store) {
fine(`could not find store for device: ${device}`);
return undefined;
}
fine(`found store for device: ${device}`);
// Compute average
const to = new Date(Date.now()).toISOString();
const from = new Date(Date.now() - period*1000).toISOString();
const avg = computeBarycentre(store, from, to);
fine(`computed avg: ${avg}, over last: ${period} seconds, for device: ${device}`);
return avg;
}
module.exports.latest = function (device) {
fine(`searching store for device: ${device}`);
const store = stores[device];
if (!store) {
fine(`could not find store for device: ${device}`);
return undefined;
}
fine(`found store for device: ${device}`);
// Looking for last serie
const lastSerie = store[store.length - 1];
fine(`found last serie with value: ${lastSerie[1]}, date: ${lastSerie[0]}, for device: ${device}`);
return lastSerie[1];
}
const { max } = require("./util/max");
module.exports.max = function (device) {
fine(`searching store for device: ${device}`);
const store = stores[device];
if (!store) {
fine(`could not find store for device: ${device}`);
return undefined;
}
fine(`found store for device: ${device}`);
// Looking for max value in series
const to = new Date(Date.now()).toISOString();
const from = new Date(Date.now() - period*1000).toISOString();
const maxSerie = computeMax(store, from, to);
fine(`found max value: ${maxSerie[1]}, date: ${maxSerie[0]}, for device: ${device}`);
return maxSerie;
}