Repository navigation
Expand file tree
/
Copy pathnode_helper.js
More file actions
179 lines (153 loc) · 5.77 KB
/
Copy pathnode_helper.js
File metadata and controls
179 lines (153 loc) · 5.77 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* Resrobot - Timetable for ResRobot Module */
/* Magic Mirror
* Module: MMM-ResRobot
*
* By Johan Alvinger https://github.com/Alvinger
* based on a script by Benjamin Angst http://www.beny.ch which is
* based on a script from Michael Teeuw http://michaelteeuw.nl
* MIT Licensed.
*/
const NodeHelper = require("node_helper");
const unirest = require("unirest");
const moment = require("moment");
module.exports = NodeHelper.create({
// Define start sequence.
start: function() {
console.log("Starting node_helper for module: " + this.name);
// Set locale.
moment.locale(config.language);
this.started = false;
},
// Receive notification
socketNotificationReceived: function(notification, payload) {
console.log("node_helper for " + this.name + " received a socket notification: " + notification + " - Payload: " + payload);
if (notification === "CONFIG" && this.started == false) {
this.config = payload;
this.started = true;
this.scheduleUpdate(this.config.initialLoadDelay);
}
},
/* updateTimetable()
* Requests new departure data from ResRobot.
* Calls processDepartures on successful response.
*/
updateTimetable: function() {
var self = this;
// Reset all departures
this.departures = [];
// Save list of froms and tos in arrays
var destfrom = this.config.from.split(",");
var destto = this.config.to.split(",");
// Process each from and to pair
for (var d in destfrom) {
// Get current list of departures between from and to
var url = this.getURL() + "&id=" + destfrom[d];
if (typeof destto[d] === "string" && destto[d] !== "") {
url += "&direction=" + destto[d];
}
unirest.get(url)
.send()
.end(function (r) {
if (r.error) {
console.log(self.name + " : " + r.error);
self.scheduleUpdate();
} else {
// console.log("body: ", JSON.stringify(r.body));
self.processDepartures(r.body);
}
});
}
},
/* getURL()
* Generates a base url with api parameters based on the config.
*
* return String - URL.
*/
getURL: function() {
var url = this.config.apiBase;
url +="&key=" + this.config.apiKey;
if (this.config.maximumEntries !== "") {
url += "&maxJourneys=" + this.config.maximumEntries;
}
return url;
},
/* processDepartures(data)
* Uses the received data to set the various values.
*
* argument data object - Departure information received from ResRobot.
*/
processDepartures: function(data) {
var now = moment();
for (var i in data.Departure) {
var departure = data.Departure[i];
var departureTime = moment(departure.date + " " + departure.time);
var actualdepartureTime = moment(departure.rtDate + " " + departure.rtTime);
var waitingTime = moment.duration(departureTime.diff(now));
var actualwaitingTime = moment.duration(actualdepartureTime.diff(now));
var departureTo = departure.direction;
var departureType = departure.Product.catOutS;
// If truncation is requested, truncate ending station at first word break after n characters
if (this.config.truncateAfter > 0) {
if (departureTo.indexOf(" ",this.config.truncateAfter) > 0) {
departureTo = departureTo.substring(0, departureTo.indexOf(" ",this.config.truncateAfter));
}
}
// Only save departures that occurs in the future (silently skip the past ones)
if (actualwaitingTime.get("minutes") > this.config.skipMinutes) {
this.departures.push({
timestamp: actualdepartureTime, // Departure timestamp, used for sorting
departuretime: departureTime.format("HH:mm"), // Departure time in HH:mm, used for display
actualdeparturetime: actualdepartureTime.format("HH:mm"), // Actual departure time in HH:mm, if delay
waitingtime: actualwaitingTime.get("minutes"), // Time until departure, in minutes
line: departure.transportNumber, // Line number/name of departure
type: departureType, // Short category code for departure
to: departureTo // Destination/Direction
});
}
else
if (waitingTime.get("minutes") > this.config.skipMinutes) {
this.departures.push({
timestamp: departureTime, // Departure timestamp, used for sorting
departuretime: departureTime.format("HH:mm"), // Departure time in HH:mm, used for display
actualdeparturetime: actualdepartureTime.format("HH:mm"), // Actual departure time in HH:mm, used for display
waitingtime: waitingTime.get("minutes"), // Time until departure, in minutes
line: departure.transportNumber, // Line number/name of departure
type: departureType, // Short category code for departure
to: departureTo // Destination/Direction
});
}
}
// Sort the departures and schedule an update for the first departure
// If no departures, use standard update interval
this.departures.sort(function(a, b) {
if (a.timestamp < b.timestamp) return -1;
if (a.timestamp > b.timestamp) return 1;
return 0;
});
if (typeof this.departures[0] !== "undefined" && this.departures.length > 0) {
// Set delay to the lowest of time until next departure and one hour
var delay = Math.min(this.departures[0].timestamp - now - (this.config.skipMinutes * 60 * 1000), 60 * 60 * 1000);
this.scheduleUpdate(delay);
} else {
this.scheduleUpdate();
}
// Tell the main module that we have a new list of departures
this.sendSocketNotification("DEPARTURES", this.departures);
},
/* scheduleUpdate()
* Schedule next update.
*
* argument delay number - Milliseconds before next update. If empty, this.config.updateInterval is used.
*/
scheduleUpdate: function(delay) {
var self = this;
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
clearTimeout(this.updateTimer);
this.updateTimer = setTimeout(function() {
self.updateTimetable();
}, nextLoad);
}
});