-
Notifications
You must be signed in to change notification settings - Fork 2
/
agent.nut
51 lines (44 loc) · 1.07 KB
/
agent.nut
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
report <- array(32, null);
http.onrequest(function(request, response) {
if ("payload" in request.query) {
device.send("payload", request.query.payload);
}
// adds new pins to the report group
if ("report" in request.query) {
device.send("report", request.query.report);
}
// All "non-connect" requests receive the current
// reporting state as a response.
if (device.isconnected()) {
response.send(200, serialize(report));
} else {
response.send(500, "Internal Server Error: Device not connected");
}
});
device.on("update", function(data) {
local bytes = toBytes(data);
local index = null;
local flag = 0;
foreach (byte in bytes) {
if (flag == 0) {
index = byte;
} else {
report[index] = byte;
}
flag = flag ^ 1;
}
});
function toBytes(data) {
return split(data, ",").map(function(value) {
return value.tointeger();
});
}
function serialize(array) {
local accum = "";
foreach (index, val in array) {
if (val != null) {
accum = accum + index + "," + val + ",";
}
}
return accum;
}