-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode_helper.js
More file actions
73 lines (69 loc) · 2.32 KB
/
Copy pathnode_helper.js
File metadata and controls
73 lines (69 loc) · 2.32 KB
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
'use strict';
/* Magic Mirror
* Module: MMM-Smappee
*
* By Christopher Fenner http://github.com/CFenner
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
const oauth2 = require('simple-oauth2');
const request = require('request-promise');
const HOST = 'https://app1pub.smappee.net';
const ENDPOINT = '/dev/v1';
const AUTH_PATH = '/oauth2/token';
const SERVICE_PATH = '/servicelocation';
//const CONSUMPTION_PATH = '/{serviceId}/consumption';
module.exports = NodeHelper.create({
start: function () {
console.log("Starting node helper for " + this.name);
},
getLocations: function(token){
this.accessToken = this.auth.accessToken.create(token);
return request.get(
HOST + ENDPOINT + SERVICE_PATH, {
'auth': {'bearer': this.accessToken.token.access_token},
json: true
});
},
getConsumption: function(locations){
var location = locations.serviceLocations[0].serviceLocationId;
var to = new Date().getTime();
return request.get(
HOST + ENDPOINT + SERVICE_PATH + '/' + location + '/consumption', {
'auth': {'bearer': this.accessToken.token.access_token},
json: true,
qs: {
aggregation: 1,
from: to - 900000,
to: to,
}
});
},
socketNotificationReceived: function(notification, payload) {
if('SMAPPEE_LOAD' === notification){
var self = this;
if(!self.auth){
self.auth = oauth2.create({
client: {
id: payload.client.id,
secret: payload.client.secret
},
auth: {
tokenHost: HOST,
tokenPath: ENDPOINT + AUTH_PATH
}
});
}
self.auth.ownerPassword.getToken({
username: payload.user.id,
password: payload.user.password
}).then(
self.getLocations.bind(self)
).then(
self.getConsumption.bind(self)
).then((response) => {
self.sendSocketNotification('SMAPPEE_DATA', response.consumptions.pop());
}).catch(console.log);
}
}
});