-
Notifications
You must be signed in to change notification settings - Fork 0
/
shelly-script.js
51 lines (50 loc) · 1.43 KB
/
shelly-script.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
let startIndex = 0;
let values = [];
let pending = false;
let notifyTimer = Timer.set(1000, true, function() {
if (pending)
return;
pending = true;
Shelly.call(
"Switch.GetStatus", { id: 0 },
function(res, err_code, err_msg, ud) {
pending = false;
if (res) {
if (values.length >= 600) {
values.splice(0, 1);
++startIndex;
}
values.push(res.apower);
}
}
);
});
HTTPServer.registerEndpoint("power", function(req, res) {
let data = {start_index: startIndex, power_values: values};
if (req.query) {
let alreadyReceived = parseInt(req.query);
if (alreadyReceived > startIndex &&
alreadyReceived <= startIndex + values.length) {
data = {
start_index: alreadyReceived,
power_values: values.slice(alreadyReceived - startIndex)
};
}
}
res.body = JSON.stringify(data);
/* version that removes the old data once it has been received
if (req.query) {
let alreadyReceived = parseInt(req.query);
if (alreadyReceived > startIndex &&
alreadyReceived <= startIndex + values.length) {
let toRemove = alreadyReceived - startIndex
values.splice(0, toRemove);
startIndex += toRemove;
}
}
res.body = JSON.stringify({start_index: startIndex, power_values: values});
*/
res.headers = [["Access-Control-Allow-Origin", "*"],
["Content-Type", "application/json"]];
res.send();
});