From fda33bf18e8c0aa0ba53cd3e1514ad77aa07efe6 Mon Sep 17 00:00:00 2001 From: Ben Burwell Date: Thu, 5 Sep 2013 11:54:37 -0400 Subject: Init --- LICENSE.md | 13 ++++++ README.md | 133 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ active911.js | 106 +++++++++++++++++++++++++++++++++++++++++++++++ package.json | 18 ++++++++ 4 files changed, 270 insertions(+) create mode 100644 LICENSE.md create mode 100644 README.md create mode 100644 active911.js create mode 100644 package.json diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..a77e2bf --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,13 @@ +Copyright 2013 Ben Burwell + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/README.md b/README.md new file mode 100644 index 0000000..7122176 --- /dev/null +++ b/README.md @@ -0,0 +1,133 @@ +Active911 for Node.js +===================== + +by Ben Burwell + +Installation +------------ + +Add `active911` to your dependencies and run `npm install`. You can use it in your project by using + + var active911 = require('active911')('YOUR_APP_KEY', 'YOUR_API_KEY'); + + active911.ping(function (err) { + if (err) { + return console.log('Could not connect to Active911: ' + err); + } + + console.log('Connection to Active911 established.'); + }); + +Usage +----- + +The `active911` package provides support for the current Active911 API functions. We follow the standard Node.js pattern of `function(options, callback(error, data))`. If an error occurs, the first parameter of the callback will contain the error message. Otherwise, the first parameter will be false and the second parameter will contain the API response. + +### `ping(callback)` + +The `ping` command checks that your API and app keys are valid on the server. It takes only a callback function with a parameter for errors. + +### `getDevice(device_id, callback)` + +Returns some data about the device associated with the `device_id`. + +Example response: + + { + "response_action" : "watch", + "response_alert_id" : 4681099, + "lat" : 44.541404, + "name" : "Joseph Sullivan", + "position_accuracy" : 30, + "response_stamp" : 1371315741, + "device_id" : 2435, + "position_stamp" : 1371463606, + "device_type" : "smartphone", + "lon" : -123.364192 + } + +### `getAlert(alert_id, callback)` + +Returns data about the alert associated with the `alert_id`. + +Example response: + + { + "source" : "", + "priority" : "F3", + "cad_code" : "", + "lat" : 44.5379997, + "place" : "", + "agency_id" : 3, + "state" : "OR", + "map_code" : "", + "city" : "Philomath", + "timestamp" : 1369781684, + "address" : "100 S. 16th ST", + "description" : "Cat in tree", + "details" : "", + "unit" : "", + "lon" : -123.3634479, + "cross_street" : "", + "alert_id" : 4264162, + "units" : "" + } + +### `getLocations(north, south, east, west, callback)` + +Returns an array of locations that exist within the area bounded by `north`, `south`, `east`, and `west`. The parameters should be floating point numbers representing the latitude or longitude, whichever is appropriate for the compass direction. + +For example, + + active911.getLocations(46.12345, 44.54321, -122.00021, -124.132132, function (err, locations) { + if (err) { + return console.log('Unable to retrieve locations: ' + err); + } + + console.log('Returned ' + locations.length + ' locations.'); + }); + +Example response: + + [ + { + "resources" : [], + "lat" : 44.540121, + "name" : "Pinehurst Memorial", + "agency_id" : 3, + "icon_filename" : "icon-flag.png", + "description" : "Main Station Hosp.\nNear the depot\n5 entrances", + "id" : 175, + "lon" : -123.367601, + "icon_color" : "blue" + }, + { + "resources" : [ + { + "extension" : "jpg", + "title" : "Photo", + "id" : 5, + "details" : "", + "size" : 963979 + } + ], + "lat" : 44.541252, + "name" : "Hydrant", + "agency_id" : 3, + "icon_filename" : "icon-hydrant.png", + "description" : "15th & College", + "id" : 700, + "lon" : -123.364689, + "icon_color" : "red" + } + ] + +Contributing +------------ + +Contributions are encouraged. For a list of open issues, see . + +More Information +---------------- + +More information about the API is available on [the Active911 Github wiki](https://github.com/active911/open-api/wiki). 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); + } + }); + } + + }; +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..9cd0089 --- /dev/null +++ b/package.json @@ -0,0 +1,18 @@ +{ + "name": "active911", + "version": "0.1.0", + "author": "Ben Burwell ", + "description": "A simple interface to Active911", + "tags": [ "active911", "cad" ], + "main": "./active911.js", + "repository": { + "type": "git", + "url": "https://github.com/bburwell/active911.git" + }, + "bugs": { + "url": "https://github.com/bburwell/active911/issues" + }, + "engines": { + "node": ">=0.10" + } +} -- cgit v1.2.3