-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathnode_helper.js
More file actions
75 lines (66 loc) · 2.35 KB
/
node_helper.js
File metadata and controls
75 lines (66 loc) · 2.35 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
74
75
var NodeHelper = require("node_helper");
var request = require("request");
module.exports = NodeHelper.create({
start: function() {
console.log(this.name + " is started!");
},
//Subclass socketNotificationReceived received.
socketNotificationReceived: function(notification, payload) {
if (notification === "GET_REALTIME_SL") {
this.config = payload;
console.log("Retrieving SL realtime data");
for (var i = 0; i < this.config.siteids.length; i++) {
var siteId = this.config.siteids[i];
var apiUrl = this.config.apiBase + this.config.realTimeEndpoint + this.getParams(siteId);
this.makeRequest(siteId.id, apiUrl);
}
} else if (notification === "DECREMENT_SL") {
console.log("Decrementing SL time until departure");
this.sendSocketNotification("SL_DECREMENT_TIMERS");
}
},
makeRequest: function(siteId, apiUrl) {
var self = this;
request({
url: apiUrl,
method: "GET"
}, function(error, response, body) {
if (!error && response.statusCode === 200) {
var id = siteId;
var newBody = JSON.parse(body);
var tmp = {
id: id,
result: newBody
};
// console.log(id+" " + self.name + ": ",tmp);
self.sendSocketNotification("SL_REALTIME_DATA",tmp);
} else {
console.log(self.name + ": ", error);
}
});
},
getParams: function(siteId) {
//?key=<DIN API NYCKEL>&siteid=<SITEID>&timewindow=<TIMEWINDOW>
var params = "?";
params += "key=" + this.config.realtimeappid;
params += "&siteid=" + siteId.id;
if (siteId.type !== undefined) {
for (var i = 0; i < this.config.types.length; i++) {
var type = this.config.types[i];
if (siteId.type.includes(type)) {
params += "&" + type + "=true";
} else {
params += "&" + type + "=false";
}
}
}
//Timewindow between 1 - 60 minutes
if (siteId.timewindow !== undefined && siteId.timewindow > 0 && siteId.timewindow < 60) {
params += "&timewindow=" + siteId.timewindow;
} else {
params += "&timewindow=" + (((this.config.timewindow < 1) || (this.config.timewindow > 60)) ? 15 : this.config.timewindow);
}
console.log("params: " + params);
return params;
},
});