aboutsummaryrefslogtreecommitdiff
path: root/active911.js
blob: df2e2c634128ba21a5da9275b723e062840da87a (plain)
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
var http = require('https');
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: 'access.active911.com',
            port: '443',
            path: '/interface/open_api.php',
            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);
                }
            });
        }

    };
};