From fda33bf18e8c0aa0ba53cd3e1514ad77aa07efe6 Mon Sep 17 00:00:00 2001 From: Ben Burwell Date: Thu, 5 Sep 2013 11:54:37 -0400 Subject: Init --- active911.js | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 active911.js (limited to 'active911.js') diff --git a/active911.js b/active911.js new file mode 100644 index 0000000..577e47e --- /dev/null +++ b/active911.js @@ -0,0 +1,106 @@ +var http = require('http'); +var querystring = require('querystring'); + +module.exports = function (app_key, api_key) { + + // this is used to run all server commands + var server_command = function (data, callback) { + + // add keys to the POST request + data.app_key = app_key; + data.api_key = api_key; + + // convert the object to a querystring for POSTing + var post = querystring.stringify(data); + + // HTTP request configuration + var options = { + host: 'localhost', + port: '5000', + path: '/api', + method: 'POST', + headers: { + 'Content-Length': post.length; + } + }; + + // set up the request + var request = http.request(options, function (res) { + + res.setEncoding('utf8'); + + var json = ''; + res.on('data', function (chunk) { + json += chunk; + }); + + res.on('end', function () { + + var response = JSON.parse(json); + + if (response.result === 'error') { + callback(response.message); + } else { + return callback(false, response.message); + } + }); + + res.on('error', function (err) { + callback(err); + }); + }); + + request.write(post); + request.end(); + + }; + + return { + + ping: function (callback) { + var options = { + 'operation': 'ping' + }; + + server_command(options, callback); + }, + + getDevice: function (device_id, callback) { + var options = { + 'operation': 'get_device', + 'device_id': device_id + }; + + server_command(options, callback); + }, + + getAlert: function (alert_id, callback) { + var options = { + 'operation': 'get_alert', + 'alert_id': alert_id + }; + + server_command(options, callback); + }, + + getLocations: function (north, south, east, west, callback) { + var options = { + 'operation': 'get_locations', + 'north': north, + 'south': south, + 'east': east, + 'west': west + }; + + // use an intermediate callback to extract the location array + server_command(options, function (err, response) { + if (err) { + return callback(err); + } else { + return callback(err, response.locations); + } + }); + } + + }; +}; -- cgit v1.2.3