-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathElectric Imp Agent Code.nut
75 lines (60 loc) · 1.98 KB
/
Electric Imp Agent Code.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
//local isconnectedFlag = device.isconnected();
// HTTP handler function
function httpHandler(req, resp) {
if (device.isconnected())
{
try {
local d = http.jsondecode(req.body);
//server.log(d.c);
if (d.c == "btn") {
//server.log(d.val);
device.send("btn", d.val);
resp.send(200, "OK");
}
} catch(ex) {
// If there was an error, send it back in the response
server.log("error:" + ex);
resp.send(500, "Internal Server Error: " + ex);
}
} else {
deviceStateChangeHandler(0);
}
}
// Register HTTP handler
http.onrequest(httpHandler);
// GateStateChange handler function
function gateStateChangeHandler(data) {
// URL to web service
local url = "http://projects.ajillion.com/save_gate_state";
// Set Content-Type header to json
local headers = { "Content-Type" : "application/json" };
// Encode received data and log
local body = http.jsonencode(data);
server.log(body);
// Send the data to your web service
http.post(url, headers, body).sendsync();
}
// Register gateStateChange handler
device.on("gateStateChange", gateStateChangeHandler);
// Register deviceStateChange handler
device.onconnect(function() {
deviceStateChangeHandler(1);
//isconnectedFlag = false;
});
device.ondisconnect(function() {
deviceStateChangeHandler(0);
});
function deviceStateChangeHandler(data){
//if (isconnectedFlag == false && data == 0) return;
//if (data == 1) isconnectedFlag = true;
local json = { "devicestate" : data };
// URL to web service
local url = "http://projects.ajillion.com/save_device_state";
// Set Content-Type header to json
local headers = { "Content-Type" : "application/json" };
// Encode received data and log
local body = http.jsonencode(json);
server.log(body);
// Send the data to web service
http.post(url, headers, body).sendsync();
}