forked from joel-wehr/electric_imp_garage_door
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno_sensors_agent.nut
60 lines (59 loc) · 2.62 KB
/
no_sensors_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
//*************************TWILIO***********************************************
//The Twilio code is used to alert you via SMS if an unauthorized access to your
//Imp is made. It is not necessary for door control, and is just an added feature.
const TWILIO_ACCOUNT_SID = "" //Your SID
const TWILIO_AUTH_TOKEN = "" //Your Auth Token
const TWILIO_FROM_NUMBER = "+17175551212" // your phone no goes here
const TWILIO_TO_NUMBER = "+17175551212" // destination phone no
function send_sms(number, message) {
local twilio_url = format("https://api.twilio.com/2010-04-01/Accounts/%s/SMS/Messages.json", TWILIO_ACCOUNT_SID);
local auth = "Basic " + http.base64encode(TWILIO_ACCOUNT_SID+":"+TWILIO_AUTH_TOKEN);
local body = http.urlencode({From=TWILIO_FROM_NUMBER, To=number, Body=message});
local req = http.post(twilio_url, {Authorization=auth}, body);
local res = req.sendsync();
if(res.statuscode != 201) {
server.log("error sending message: "+res.body);
}
}
//*****************************END TWILIO***************************************
apiKey <- "" //Your API Key
doorState <- "Unknown.";
// Respond to incoming HTTP commands
http.onrequest(function(request, response) {
try {
local data = http.jsondecode(request.body);
server.log("Received: " + request.body);
if ("api-key" in request.headers && request.headers["api-key"] == apiKey) {
server.log(request.headers["api-key"]);
if (data.action == "status") {
local json = "{ \"status\" : { \"doorState\" : \"" + doorState + "\" }}";
server.log("Response: " + json);
response.send(200, json);
}
else if (data.action == "toggle") {
device.send("toggleDoor", data.action);
device.on("doorToggled", function(data) {
doorState = data;
local json = "{ \"status\" : { \"doorState\" : \"" + doorState + "\" }}";
server.log("Response: " + json);
response.send(200, json);
});
}
else {
server.log(request.body);
local json = "{ \"status\" : { \"doorState\" : \"Missing Data in Body\" }}";
response.send(500, json);
}
}
else {
local json = "{ \"status\" : { \"doorState\" : \"Unauthorized\" }}";
response.send(401, json);
//Uncomment the line below if you have a Twilio account set up
//and wish to use it to monitor unauthorized access.
//send_sms(TWILIO_TO_NUMBER, "Unauthorized access to Security System attempted.");
}
}
catch (ex) {
response.send(500, "Internal Server Error: " + ex);
}
});