-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImpAlarm.agent.nut
425 lines (376 loc) · 16.2 KB
/
ImpAlarm.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
class Firebase {
// General
baseUrl = null; // Firebase base url
firebase = null; // the name of your firebase
auth = null; // Auth key (if auth is enabled)
// For REST calls:
defaultHeaders = { "Content-Type": "application/json" };
// For Streaming:
streamingHeaders = { "accept": "text/event-stream" };
streamingRequest = null; // The request object of the streaming request
data = null; // Current snapshot of what we're streaming
callbacks = null; // List of callbacks for streaming request
/***************************************************************************
* Constructor
* Returns: FirebaseStream object
* Parameters:
* baseURL - the base URL to your Firebase (https://username.firebaseio.com)
* auth - the auth token for your Firebase
**************************************************************************/
constructor(_firebase, _auth) {
this.firebase = _firebase;
this.baseUrl = "https://" + firebase + ".firebaseio.com";
this.auth = _auth;
this.data = {};
this.callbacks = {};
}
/***************************************************************************
* Attempts to open a stream
* Returns:
* false - if a stream is already open
* true - otherwise
* Parameters:
* path - the path of the node we're listending to (without .json)
* autoReconnect - set to false to close stream after first timeout
* onError - custom error handler for streaming API
**************************************************************************/
function stream(path = "", autoReconnect = true, onError = null) {
// if we already have a stream open, don't open a new one
if (streamingRequest) return false;
if (onError == null) onError = _defaultErrorHandler.bindenv(this);
local request = http.get(_buildUrl(path), streamingHeaders);
this.streamingRequest = request.sendasync(
function(resp) {
server.log("Stream Closed (" + resp.statuscode + ": " + resp.body +")");
// if we timed out and have autoreconnect set
if (resp.statuscode == 28 && autoReconnect) {
stream(path, autoReconnect, onError);
return;
}
if (resp.statuscode == 307) {
if("location" in resp.headers) {
// set new location
local location = resp.headers["location"];
local p = location.find(".firebaseio.com")+16;
this.baseUrl = location.slice(0, p);
stream(path, autoReconnect, onError);
return;
}
}
}.bindenv(this),
function(messageString) {
//try {
server.log("MessageString: " + messageString);
local message = _parseEventMessage(messageString);
if (message) {
local changedRoot = _setData(message);
_findAndExecuteCallback(message.path, changedRoot);
}
//} catch(ex) {
// if an error occured, invoke error handler
//onError([{ message = "Squirrel Error - " + ex, code = -1 }]);
//}
}.bindenv(this)
);
// Return true if we opened the stream
return true;
}
/***************************************************************************
* Returns whether or not there is currently a stream open
* Returns:
* true - streaming request is currently open
* false - otherwise
**************************************************************************/
function isStreaming() {
return (streamingRequest != null);
}
/***************************************************************************
* Closes the stream (if there is one open)
**************************************************************************/
function closeStream() {
if (streamingRequest) {
streamingRequest.cancel();
streamingRequest = null;
}
}
/***************************************************************************
* Registers a callback for when data in a particular path is changed.
* If a handler for a particular path is not defined, data will change,
* but no handler will be called
*
* Returns:
* nothing
* Parameters:
* path - the path of the node we're listending to (without .json)
* callback - a callback function with one parameter (data) to be
* executed when the data at path changes
**************************************************************************/
function on(path, callback) {
callbacks[path] <- callback;
}
/***************************************************************************
* Reads data from the specified path, and executes the callback handler
* once complete.
*
* NOTE: This function does NOT update firebase.data
*
* Returns:
* nothing
* Parameters:
* path - the path of the node we're reading
* callback - a callback function with one parameter (data) to be
* executed once the data is read
**************************************************************************/
function read(path, callback = null) {
http.get(_buildUrl(path), defaultHeaders).sendasync(function(res) {
if (res.statuscode != 200) {
server.log("Read: Firebase response: " + res.statuscode + " => " + res.body)
} else {
local data = null;
try {
data = http.jsondecode(res.body);
} catch (err) {
server.log("Read: JSON Error: " + res.body);
return;
}
if (callback) callback(data);
}
}.bindenv(this));
}
/***************************************************************************
* Pushes data to a path (performs a POST)
* This method should be used when you're adding an item to a list.
*
* NOTE: This function does NOT update firebase.data
* Returns:
* nothing
* Parameters:
* path - the path of the node we're pushing to
* data - the data we're pushing
**************************************************************************/
function push(path, data) {
http.post(_buildUrl(path), defaultHeaders, http.jsonencode(data)).sendasync(function(res) {
if (res.statuscode != 200) {
server.log("Push: Firebase response: " + res.statuscode + " => " + res.body)
}
}.bindenv(this));
}
/***************************************************************************
* Writes data to a path (performs a PUT)
* This is generally the function you want to use
*
* NOTE: This function does NOT update firebase.data
*
* Returns:
* nothing
* Parameters:
* path - the path of the node we're writing to
* data - the data we're writing
**************************************************************************/
function write(path, data) {
http.put(_buildUrl(path), defaultHeaders, http.jsonencode(data)).sendasync(function(res) {
if (res.statuscode != 200) {
server.log("Write: Firebase response: " + res.statuscode + " => " + res.body)
}
}.bindenv(this));
}
/***************************************************************************
* Updates a particular path (performs a PATCH)
* This method should be used when you want to do a non-destructive write
*
* NOTE: This function does NOT update firebase.data
*
* Returns:
* nothing
* Parameters:
* path - the path of the node we're patching
* data - the data we're patching
**************************************************************************/
function update(path, data) {
http.request("PATCH", _buildUrl(path), defaultHeaders, http.jsonencode(data)).sendasync(function(res) {
if (res.statuscode != 200) {
server.log("Update: Firebase response: " + res.statuscode + " => " + res.body)
}
}.bindenv(this));
}
/***************************************************************************
* Deletes the data at the specific node (performs a DELETE)
*
* NOTE: This function does NOT update firebase.data
*
* Returns:
* nothing
* Parameters:
* path - the path of the node we're deleting
**************************************************************************/
function remove(path) {
http.httpdelete(_buildUrl(path), defaultHeaders).sendasync(function(res) {
if (res.statuscode != 200) {
server.log("Delete: Firebase response: " + res.statuscode + " => " + res.body)
}
});
}
/************ Private Functions (DO NOT CALL FUNCTIONS BELOW) ************/
// Builds a url to send a request to
function _buildUrl(path) {
local url = baseUrl + path + ".json";
url += "?ns=" + firebase;
if (auth != null) url = url + "&auth=" + auth;
return url;
}
// Default error handler
function _defaultErrorHandler(errors) {
foreach(error in errors) {
server.log("ERROR " + error.code + ": " + error.message);
}
}
// parses event messages
function _parseEventMessage(text) {
// split message into parts
local lines = split(text, "\n");
// get the event
local eventLine = lines[0];
local event = eventLine.slice(7);
if(event.tolower() == "keep-alive") return null;
// get the data
local dataLine = lines[1];
local dataString = dataLine.slice(6);
// pull interesting bits out of the data
local d = http.jsondecode(dataString);
local path = d.path;
local messageData = d.data;
// return a useful object
return { "event": event, "path": path, "data": messageData };
}
// Sets data and returns root of changed data
function _setData(message) {
// base case - refresh everything
if (message.event == "put" && message.path =="/") {
data = (message.data != null) ? message.data : {};
return data
}
local pathParts = split(message.path, "/");
local currentData = data;
local parent = data;
foreach(part in pathParts) {
parent=currentData;
if (part in currentData) currentData = currentData[part];
else {
currentData[part] <- {};
currentData = currentData[part];
}
}
local key = pathParts.len() > 0 ? pathParts[pathParts.len()-1] : null;
if (message.event == "put") {
if (message.data == null) {
if (key != null) delete parent[key];
else data = {};
return null;
}
else {
if (key != null) parent[key] <- message.data;
else data[key] <- message.data;
}
}
if (message.event == "patch") {
foreach(k,v in message.data) {
if (key != null) parent[key][k] <- v
else data[k] <- v;
}
}
return (key != null) ? parent[key] : data;
}
// finds and executes a callback after data changes
function _findAndExecuteCallback(path, callbackData) {
local pathParts = split(path, "/");
local key = "";
for(local i = pathParts.len() - 1; i >= 0; i--) {
key = "";
for (local j = 0; j <= i; j++) key = key + "/" + pathParts[j];
if (key in callbacks || key + "/" in callbacks) break;
}
if (key + "/" in callbacks) key = key + "/";
if (key in callbacks) callbacks[key](callbackData);
}
}
//////////////////////////////////////////////////////////////////////
const MYTEXTNUMBER = "+xxxxxxxxxxx"; //Gloria cell phone number
const MYNUMBER = "+xxxxxxxxxxx"; //Gloria's cell phone number
const MYNAME = "Gloria";
const TWILIO_SID = "AC026c50ec54694ee238be87d0235b0bc9";
const TWILIO_PWD = "fdc79c638bced48e2da4c28b182f909d";
const TWILIOURL = "https://api.twilio.com/2010-04-01";
const MEDIASRC = "Url=http://3.bp.blogspot.com/-ytku256TJv8/UDwgMfLrbpI/AAAAAAAAFkI/ZcUXIbw1AYw/s1600/Fat+Cat+Sleepy.jpg";
//Create CALLSRC
const TWIMLETSURL = "http://twimlets.com/echo"
const TWIMLOPEN = "Twiml=%253CResponse%253E%253CSay%253E";
const TWIMLCLOSE = "%253C/Say%253E%253C/Response%253E";
const CAMPUSPOLICEPHONE = "+xxxxxxxxxxx"; //police number
const FRIENDNUMBER = "+xxxxxxxxxxx"; //Fay's cell phone number
server.log("Agent started.");
class Twilio {
constructor();
function call(dest, msgs, callback = null) {
local url = TWILIOURL+"/Accounts/"+TWILIO_SID+"/Calls.json"
local auth = http.base64encode(TWILIO_SID + ":" + TWILIO_PWD);
local headers = {"Authorization": "Basic " + auth};
local message = "Hi%20my%20name%20is%20" + MYNAME + "%20and%20I%20have%20just%20activated%20my%20Imp%20Alarm.%20I%20am%20in%20trouble%20on%20campus.%20Please%20help%20me.";
server.log("Is about to call");
local twimletsmsg = TWIMLETSURL+"?"+TWIMLOPEN+message+TWIMLCLOSE;
local request = http.post(url, headers, "From="+MYNUMBER+"&To="+CAMPUSPOLICEPHONE+"&"+twimletsmsg);
local response = request.sendsync();
server.log(response);
server.log("abc");
return response;
}
function text(dest, msgs, callback = null) {
local message = "Hi, this is " + MYNAME + ". Come pick me up, please. I'm in trouble.";
local data = { From = MYTEXTNUMBER, To = FRIENDNUMBER, Body = message };
local auth = http.base64encode(TWILIO_SID + ":" + TWILIO_PWD);
local headers = {"Authorization": "Basic " + auth};
http.post(TWILIOURL + "/Accounts/"+ TWILIO_SID + "/SMS/Messages.json", headers, http.urlencode(data)).sendasync(function(res) {
if (res.statuscode == 200 || res.statuscode == 201) {
server.log("Twilio SMS sent to: " + FRIENDNUMBER);
} else {
server.log("Twilio error: " + res.statuscode + " => " + res.body);
};
})
}
function sendmms(dest, msg, callback = null) {
local url = TWILIOURL+"/Accounts/"+TWILIO_SID+"/Messages.json"
local auth = http.base64encode(TWILIO_SID + ":" + TWILIO_PWD);
local headers = {"Authorization": "Basic " + auth};
local request = http.post(url, headers, "From="+MYTEXTNUMBER+"&To="+FRIENDNUMBER+"&"+MEDIASRC);
local response = request.sendsync();
server.log(response.body);
return response;
}
}
function find_location() {
//Call Google maps API to figure out location (GPS coordinates)
//return string address
}
function twilio() {
local mytwilio = Twilio();
server.log("Calling.");
mytwilio.call(0,0);
server.log("Call complete.");
}
device.on("BUTTON_FRIENDS", function(val) {
local mytwilio = Twilio();
server.log("Texting friend.");
mytwilio.text(0,0);
server.log("Text complete.");
});
device.on("BUTTON_CAMPUS_POLICE", function(val) {
local mytwilio = Twilio();
server.log("Calling campus police.");
mytwilio.call(0,0);
server.log("Call complete.");
});
device.on("BUTTON_FOR_FUN", function(val) {
local mytwilio = Twilio();
server.log("Send cat picture to friend.");
mytwilio.sendmms(0,0);
server.log("Message complete.");
});