Skip to content

Commit 5b8aaf6

Browse files
committed
Add simple Losant cloud service module
1 parent 280ab8f commit 5b8aaf6

File tree

2 files changed

+207
-0
lines changed

2 files changed

+207
-0
lines changed

modules/Losant.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+

modules/Losant.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<!--- Copyright (c) 2016 Patrick Van Oosterwijck. See the file LICENSE for copying permission. -->
2+
Losant
3+
======
4+
5+
* KEYWORDS: Losant, IoT, cloud, API
6+
7+
The [Losant module](/modules/Losant.js) provides an easy to use
8+
interface to send device updates to and get device commands from
9+
the [Losant](https://www.losant.com/) cloud IoT platform.
10+
It was developed on the ESP8266 and does not use TLS,
11+
don't send private information with this module!
12+
13+
API
14+
---
15+
16+
#### Constructor
17+
18+
The module exports a setup function to create a Losant device object:
19+
20+
```
21+
exports.setup = function (deviceId, auth)
22+
```
23+
24+
`deviceId` is the Losant assigned Id for your device, for example:
25+
```
26+
var losantDeviceId = '5793f96ebf2f6e0102349c09';
27+
```
28+
29+
`auth` is an object that contains a key/secret pair that allows
30+
access to the device, for example:
31+
```
32+
var losantDeviceAuth = {
33+
key: '88c8a5b7-3ce5-4e52-906e-e6f58b8647e7',
34+
secret: 'e39deb235e9c627a69b050566a6ace34ca93a12d31446e11eec68da0be66aabe'
35+
};
36+
```
37+
38+
The Losant device object exports the following methods:
39+
40+
#### updateDeviceData
41+
42+
```
43+
updateDeviceData = function (data, callback)
44+
```
45+
46+
`data` is an object containing key/value pairs
47+
```
48+
{
49+
key1: value1,
50+
key2: value2
51+
}
52+
```
53+
The keys and value types need to match the attributes set up for a specific
54+
device in the Losant device setup.
55+
56+
`callback` is an optional callback function of the following format:
57+
```
58+
function (err, resp) {}
59+
```
60+
`err` is a possible error object, `resp` is the parsed response from
61+
Losant.
62+
63+
#### getDeviceCommand
64+
65+
```
66+
getDeviceCommand = function (since, callback)
67+
```
68+
69+
`since` is an optional time stamp that indicates since when the commands
70+
should be returned. This can be used to filter out old commands already
71+
processed.
72+
73+
`callback` is an optional callback function of the following format:
74+
```
75+
function (err, resp) {}
76+
```
77+
`err` is a possible error object, `resp` is the parsed response from
78+
Losant.
79+
80+
Example
81+
-------
82+
83+
Below is an example of how to use this in practice:
84+
85+
```
86+
var losantDeviceId = '5793f96ebf2f6e0102349c09';
87+
var losantDeviceAuth = {
88+
key: '88c8a5b7-3ce5-4e52-906e-e6f58b8647e7',
89+
secret: 'e39deb235e9c627a69b050566a6ace34ca93a12d31446e11eec68da0be66aabe'
90+
};
91+
92+
var losant = require('Losant').setup(losantDeviceId, losantDeviceAuth);
93+
losant.updateDeviceData({
94+
temperature: 23.4,
95+
humidity: 45.2
96+
});
97+
```
98+
99+
Reference
100+
---------
101+
102+
* APPEND_JSDOC: Losant.js

0 commit comments

Comments
 (0)