|
| 1 | +/* Copyright (c) 2016 Patrick Van Oosterwijck. See the file LICENSE for copying permission. */ |
| 2 | +/* |
| 3 | +This module provides an easy to use interface to send device updates and get |
| 4 | +device commands from the Losant cloud service. It was developed on the |
| 5 | +ESP8266 and does not use TLS, don't send private information with this module! |
| 6 | +*/ |
| 7 | + |
| 8 | +var http = require('http'); |
| 9 | + |
| 10 | +// Example device ID and device auth structure |
| 11 | +// |
| 12 | +// var deviceid = '5793f96ee34f6e0170669c09'; |
| 13 | +// var deviceauth = { |
| 14 | +// key: 'e659d275-84be-42e4-8319-389d13962b9f', |
| 15 | +// secret: 'cd5020a074ececf81d8a87175a0d22e00cb3dd1c26113d22e2232d93181b26dd' |
| 16 | +// }; |
| 17 | + |
| 18 | + |
| 19 | +/* Device constructor */ |
| 20 | +function LosantDevice(device, auth) { |
| 21 | + this.device = device; |
| 22 | + this.auth = auth; |
| 23 | +} |
| 24 | + |
| 25 | +/* Generic Losant request function */ |
| 26 | +function losantReq(method, path, authtoken, reqdata, callback) { |
| 27 | + if (typeof(callback) !== 'function') { |
| 28 | + callback = undefined; |
| 29 | + } |
| 30 | + var body = reqdata ? JSON.stringify(reqdata) : undefined; |
| 31 | + var reqopt = { |
| 32 | + host: 'api.losant.com', |
| 33 | + path: path, |
| 34 | + method: method, |
| 35 | + headers: { |
| 36 | + 'Content-Type': 'application/json', |
| 37 | + 'Accept' : 'application/json' |
| 38 | + } |
| 39 | + }; |
| 40 | + if (body) { |
| 41 | + reqopt.headers['Content-Length'] = body.length; |
| 42 | + } |
| 43 | + if (authtoken) { |
| 44 | + reqopt.headers.Authorization = 'Bearer ' + authtoken; |
| 45 | + } |
| 46 | + var resbody = ""; |
| 47 | + var req = http.request(reqopt, function (res) { |
| 48 | + res.on('data', function(resdata) { |
| 49 | + resbody += resdata; |
| 50 | + }); |
| 51 | + res.on('close', function() { |
| 52 | + if (callback) callback(undefined, JSON.parse(resbody)); |
| 53 | + }); |
| 54 | + res.on('end', function() { |
| 55 | + if (callback) callback(undefined, JSON.parse(resbody)); |
| 56 | + }); |
| 57 | + }); |
| 58 | + req.on('error', function (err) { |
| 59 | + if(callback) callback(err); |
| 60 | + }); |
| 61 | + req.end(body); |
| 62 | +} |
| 63 | + |
| 64 | +/** Get authentication token for the device */ |
| 65 | +LosantDevice.prototype.getDeviceAuthToken = function (callback) { |
| 66 | + var reqdata = { |
| 67 | + deviceId: this.device, |
| 68 | + key: this.auth.key, |
| 69 | + secret: this.auth.secret |
| 70 | + }; |
| 71 | + losantReq('POST', '/auth/device', undefined, reqdata, callback); |
| 72 | +} |
| 73 | + |
| 74 | +/** Update the device data */ |
| 75 | +LosantDevice.prototype.updateDeviceData = function (data, callback) { |
| 76 | + this.getDeviceAuthToken(function (err, auth) { |
| 77 | + if (err) { |
| 78 | + if (typeof(callback) === 'function') callback(err); |
| 79 | + } else { |
| 80 | + losantReq('POST', '/applications/' + auth.applicationId + |
| 81 | + '/devices/' + auth.deviceId + '/state', |
| 82 | + auth.token, { data: data }, callback); |
| 83 | + } |
| 84 | + }); |
| 85 | +} |
| 86 | + |
| 87 | +/** Get the last 20 device commands */ |
| 88 | +LosantDevice.prototype.getDeviceCommand = function (since, callback) { |
| 89 | + this.getDeviceAuthToken(function (err, auth) { |
| 90 | + if (err) { |
| 91 | + if (typeof(callback) === 'function') callback(err); |
| 92 | + } else { |
| 93 | + losantReq('GET', '/applications/' + auth.applicationId + |
| 94 | + '/devices/' + auth.deviceId + '/command?limit=20' + |
| 95 | + (since !== undefined ? '&since=' + since : ''), |
| 96 | + auth.token, undefined, callback); |
| 97 | + } |
| 98 | + }); |
| 99 | +} |
| 100 | + |
| 101 | +/** Setup function for the Losant device */ |
| 102 | +exports.setup = function (deviceId, auth) { |
| 103 | + return new LosantDevice(deviceId, auth); |
| 104 | +} |
| 105 | + |
0 commit comments