-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathagent.nut
137 lines (112 loc) · 4.06 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
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
commands <- {"clean":1,
"sleep":1,
"doc":1,
"spot":1,
"status":1
};
// hardcoded imp's mac address
mac <- "";
server.log("Agent Started");
// when device is connected
// request device mac
// device.onconnect(function(){
// // get imp's mac
// device.send("getImpMac", "test");
// });
// device.on("setMac", function(data){
// mac = data;
// server.log("Imp mac: " + mac);
// });
const TIMEOUT = 15; // close hanging async requests after 15 seconds
responses <- {};
// create unique keys for responses table
function generateKey() {
local key = math.rand();
while (key in responses) key = math.rand();
return key.tostring();
}
// check every second if response is waiting > TIMEOUT
// send timeout error if true
function responsesHandler() {
imp.wakeup(1, responsesHandler);
local t = time();
foreach(key, response in responses) {
if (t - response.t > TIMEOUT) {
local response = http.jsonencode({ "result": "error", "error": "Agent timeout"});
responses[key].resp.header("Content-type", "application/json");
responses[key].resp.send(408, response);
delete responses[key];
}
}
} responsesHandler();
// send response back when data received from device
device.on("asyncdata", function(data) {
//server.log("sending response");
if (!(data.k in responses)) {
return;
}
local response = responses[data.k].resp;
response.header("Content-type", "application/json");
response.send(200, http.jsonencode(data.d));
delete responses[data.k];
});
function httpHandler(req, resp) {
local path = split(req.path, "/");
local imp_mac = "";
local command = "";
//extract imp mac from a path if any
if (path.len() >= 2){
imp_mac = path[1];
}
if (imp_mac == mac){
// before processing any request need to check whether device is connected
if (device.isconnected()){
// get command from request
if (req.method == "GET"){
if ("status" in req.query){
command = "status";
}
if ("command" in req.query){
//server.log("Procesing command : " + req.query["command"]);
if (req.query["command"] in commands){
command = req.query["command"];
}
}
}
// send command to device if command is not empty
if (command != ""){
// generate key, and store the response object
local responseKey = generateKey();
responses[responseKey] <- { resp = resp, t = time() } ;
local data = { k = responseKey, r = command, d = null };
device.send("processCommand" data);
}
// wrong command need to send response about it
else {
local response = http.jsonencode({ "result": "error", "command": "unknown", "error": "unknown command"});
resp.header("Content-type", "application/json");
resp.send(200, response);
}
return;
}
// if imp device is disconnected response with error
else {
//if we can find command in request
local response = {};
if (req.query["command"]){
response = http.jsonencode({ "result": "error", "error": "Device offline", "command": req.query["command"]});
}
else{
response = http.jsonencode({ "result": "error", "error": "Device offline", "command": "unknown"});
}
resp.header("Content-type", "application/json");
resp.send(200, response);
}
}
// if mac address in request doesn't match, or something else is messed up
// response with 401
else{
resp.send(401, "Unauthorized");
}
}
http.onrequest(httpHandler);