-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnode_helper.js
61 lines (49 loc) · 1.41 KB
/
node_helper.js
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
/* Magic Mirror
* Module: MMM-KVV
*
* By yo-less
* MIT Licensed.
*/
var parseString = require('xml2js').parseString;
const request = require('request');
const NodeHelper = require("node_helper");
module.exports = NodeHelper.create({
start: function() {
console.log("Starting node helper for: " + this.name);
},
/* getParams
* Generates an url with api parameters based on the config.
*
* return String - URL params.
*/
getParams: function() {
var params = "?city=";
params += this.config.cityID;
params += "&place=";
params += this.config.stationID;
return params;
},
socketNotificationReceived: function(notification, payload) {
if(notification === 'CONFIG'){
this.config = payload;
var nextbike_url = this.config.apiBase + this.getParams();
this.getData(nextbike_url, this.config.stationID);
}
},
parseData: function(input) {
var nextbikeData = "";
parseString(input, function (err, result) {
nextbikeData = JSON.parse(JSON.stringify(result));
});
return nextbikeData;
},
getData: function(options, stationID) {
request(options, (error, response, body) => {
if (response.statusCode === 200) {
this.sendSocketNotification("BIKES" + stationID, this.parseData(body));
} else {
console.log("Error getting nextbike data " + response.statusCode);
}
});
}
});