-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.nut
65 lines (63 loc) · 2.55 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
//*************************TWILIO***********************************************
const TWILIO_ACCOUNT_SID = "YOUR SID" // your SID goes here
const TWILIO_AUTH_TOKEN = "YOUR TOKEN" // your token goes here
const TWILIO_FROM_NUMBER = "+19195551212" // your phone no goes here
const TWILIO_TO_NUMBER = "+19195551212" // 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);
}
}
device.on("Alert", function(v) {
send_sms(TWILIO_TO_NUMBER, v)
});
//*****************************END TWILIO***************************************
smsArmed <- "off";
sirenArmed <- "off";
sirenState <- "off";
sensorState <- "unset";
apiKey <- "" // Enter your API Key here
// Respond to incoming HTTP commands
http.onrequest(function(request, response) {
try {
local data = http.jsondecode(request.body);
server.log(request.body);
if ("api-key" in request.headers && request.headers["api-key"] == apiKey) {
server.log(request.headers["api-key"]);
if (data.action == "get") {
local json = "{ \"status\" : { \"auth\" : \"yes\", \"smsArmed\" : \"" + smsArmed + "\" , \"sirenArmed\" : \"" + sirenArmed + "\" , \"sirenState\" : \"" + sirenState + "\" , \"sensorState\" : \"" + sensorState + "\" }}";
response.send(200, json);
}
else if (data.action == "set") {
server.log(data);
smsArmed = (data.smsArmed);
sirenArmed = (data.sirenArmed);
sirenState = (data.sirenState);
device.send("setStatus", data);
device.on("statusResponse", function(data) {
response.send(200, data);
});
}
else {
server.log(request.body);
response.send(500, "Missing Data in Body");
}
}
else {
local json = "{ \"status\" : { \"auth\" : \"no\" }}";
response.send(401, json)
send_sms(TWILIO_TO_NUMBER, "Unauthorized access to Security System attempted.")
}
}
catch (ex) {
response.send(500, "Internal Server Error: " + ex);
}
});
device.on("sensorState", function (v) {
sensorState = v;
});