-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.js
235 lines (206 loc) · 8.88 KB
/
data.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
const SensorDataSet = require('./mongo');
const SECONDS_IN_MINUTE = 60;
const MINUTES_IN_HOUR = 60;
const ACCUMULATION_PERIOD = SECONDS_IN_MINUTE * MINUTES_IN_HOUR * 1000;
const NUM_VALUES = 24;
const PRECISIONS = {
temperature: 1,
light: 0,
moisture: 0
};
function calculateAccumulationPeriod() {
return ACCUMULATION_PERIOD - (Date.now() % ACCUMULATION_PERIOD);
}
module.exports = {
/**
* Gets the current sensor values for all sensors.
* @param callback The function to call with the values.
*/
getCurrentValues: callback => {
SensorDataSet.find({}, 'name currentValue', (err, entries) => {
if (err) {
console.log(err);
}
const data = {};
if (entries) {
for (let i = 0; i < entries.length; i++) {
data[entries[i].name] = entries[i].currentValue;
}
}
callback(data);
});
},
getPreferredValues: callback => {
SensorDataSet.find({}, 'name preferredValue', (err, entries) => {
if (err) {
console.log(err);
}
const data = {};
for (let i = 0; i < entries.length; i++) {
data[entries[i].name] = entries[i].preferredValue;
}
callback(data);
});
},
setPreferredValues: (values, callback) => {
let status = 200;
for (const name in values) {
if (values.hasOwnProperty(name)) {
const newValue = parseFloat(values[name]);
if (!isNaN(newValue)) {
// get current data to update
SensorDataSet.findOne(
{name: name},
(err, entry) => {
if (err) {
console.log(err);
return;
}
if (entry) {
entry.preferredValue = newValue;
// save updated document
entry.save(err => {
if (err) {
console.log(err);
}
});
} else {
// create new entry
const sensorDataSet = new SensorDataSet({
name: name,
currentValue: newValue,
preferredValue: newValue,
values: [],
accumulationPeriod: calculateAccumulationPeriod(),
accumulator: 0,
timeAccumulatorStart: Date.now(),
timeCurrentValueLastUpdated: Date.now(),
timeValuesLastModified: 0
});
// save to database
sensorDataSet.save(err => {
console.log(err);
});
}
});
} else {
console.log('Got malformed value ' + values[name]);
}
} else {
status = 403;
}
}
callback(status);
},
/**
* Gets the time each sensor value set was last modified. Note that this
* time applies to the graph data and NOT the current values.
* @param callback The function to call with the results.
*/
getTimeLastModified: callback => {
SensorDataSet.find({}, 'name timeValuesLastModified', (err, entries) => {
if (err) {
console.log(err);
}
let max = 0;
for (let i = 0; i < entries.length; i++) {
max = Math.max(max, entries[i].timeValuesLastModified);
}
callback({lastModified: max});
});
},
/**
* Gets all sensor data.
* @param callback Function to call with results.
*/
getAllData: callback => {
SensorDataSet.find({}, (err, entries) => {
if (err) {
console.log(err);
callback([]);
} else {
callback(entries);
}
});
},
addValues: (values, callback) => {
let status = 200;
for (const name in values) {
if (values.hasOwnProperty(name)) {
const newValue = parseFloat(values[name]);
if (!isNaN(newValue)) {
// get current data to update
SensorDataSet.findOne(
{name: name},
(err, data) => {
if (err) {
console.log(err);
return;
}
const currentTime = Date.now();
// if the entry already exists (usual case)
if (data) {
// update average
const duration = currentTime - data.timeCurrentValueLastUpdated;
data.accumulator = data.accumulator + duration * data.currentValue;
// update current values
data.currentValue = newValue;
data.timeCurrentValueLastUpdated = currentTime;
// if the accumulation period is over, store a new value
const totalDuration = currentTime - data.timeAccumulatorStart;
if (totalDuration >= data.accumulationPeriod) {
console.log('Seconds since last update: ---------------------' +
'' + totalDuration / 1000);
// correct for missed intervals (so we don't misplace a time label)
for (let i = 1; i < Math.floor(totalDuration / ACCUMULATION_PERIOD); i++) {
data.values.push(null);
}
// calculate final average and add
const average = (data.accumulator / totalDuration).toFixed(PRECISIONS[name]);
data.values.push(average);
// remove oldest value if needed
while (data.values.length > NUM_VALUES) {
data.values.shift();
}
// reset times
data.accumulator = 0;
data.timeValuesLastModified = currentTime;
data.timeAccumulatorStart = currentTime;
// adjust accumulation period for next cycle if we went over
data.accumulationPeriod = calculateAccumulationPeriod();
}
// save updated document
data.save(err => {
if (err) {
console.log(err);
}
});
} else {
// create new entry
const sensorDataSet = new SensorDataSet({
name: name,
currentValue: newValue,
preferredValue: 50,
values: [],
accumulationPeriod: calculateAccumulationPeriod(),
accumulator: 0,
timeAccumulatorStart: currentTime,
timeCurrentValueLastUpdated: currentTime,
timeValuesLastModified: 0
});
// save to database
sensorDataSet.save(err => {
console.log(err);
});
}
});
} else {
console.log('Got malformed value ' + values[name]);
}
} else {
status = 403;
}
}
callback(status);
}
};