-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode_helper.js
More file actions
513 lines (431 loc) · 18 KB
/
node_helper.js
File metadata and controls
513 lines (431 loc) · 18 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/* Magic Mirror
* Module: MMM-RNV
*
* By jupadin
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
const { ApolloClient } = require('apollo-client');
const { InMemoryCache } = require('apollo-cache-inmemory');
const { HttpLink } = require('apollo-link-http');
const { setContext } = require('apollo-link-context');
const gql = require('graphql-tag');
const moment = require('moment');
const Log = require('logger');
const FIFTEEN_MINUTES = 15 * 60 * 1000;
const THIRTY_MINUTES = 30 * 60 * 1000;
const MAX_SERVER_BACKOFF = 3;
class Fetcher {
constructor(client, url, reloadInterval, numJourneys, stationID, identifier) {
this.client = client;
this.url = url;
this.reloadInterval = reloadInterval;
this.numJourneys = numJourneys;
this.stationID = stationID;
this.identifier = identifier;
this.data;
this.color;
this.reloadTimer = null;
this.serverErrorCount = 0;
this.lastFetch = null;
this.fetchFailedCallback = () => {};
this.dataReceivedCallback = () => {};
}
/**
* Clears any pending reload timer
*/
clearReloadTimer () {
if (this.reloadTimer) {
clearTimeout(this.reloadTimer);
this.reloadTimer = null;
}
}
/**
* Schedules the next fetch respecting MagicMirror test mode
* @param {number} delay - Delay in milliseconds
*/
scheduleNextFetch (delay) {
const nextDelay = Math.max(delay || this.reloadInterval, this.reloadInterval);
if (process.env.mmTestMode === "true") {
return;
}
this.reloadTimer = setTimeout(() => this.fetchData(), nextDelay);
}
/**
* Parses the Retry-After header value
* @param {string} retryAfter - The Retry-After header value
* @returns {number|null} Milliseconds to wait or null if parsing failed
*/
parseRetryAfter (retryAfter) {
const seconds = Number(retryAfter);
if (!Number.isNaN(seconds) && seconds >= 0) {
return seconds * 1000;
}
const retryDate = Date.parse(retryAfter);
if (!Number.isNaN(retryDate)) {
return Math.max(0, retryDate - Date.now());
}
return null;
}
/**
* Determines the retry delay for a non-ok response
* @param {Error} error - The error object
* @returns { {delay: number, error: Error} } Error describing the issue and computed retry delay
*/
getDelayForError (error) {
let delay = this.reloadInterval;
if (!error?.networkError?.statusCode) return delay;
let status = error?.networkError?.statusCode ?? 0;
if (status === 401 || status === 403) {
delay = Math.max(this.reloadInterval * 5, THIRTY_MINUTES);
Log.error(`${this.url} - Authentication failed (${status}). Waiting ${Math.round(delay / 60000)} minutes before retry.`);
} else if (status === 429) {
const retryAfter = response.headers.get("retry-after");
const parsed = retryAfter ? this.parseRetryAfter(retryAfter) : null;
delay = parsed !== null ? Math.max(parsed, this.reloadInterval) : Math.max(this.reloadInterval * 2, FIFTEEN_MINUTES);
Log.warn(`${this.url} - Rate limited (429). Retrying in ${Math.round(delay / 60000)} minutes.`);
} else if (status >= 500) {
this.serverErrorCount = Math.min(this.serverErrorCount + 1, MAX_SERVER_BACKOFF);
delay = this.reloadInterval * Math.pow(2, this.serverErrorCount);
Log.error(`${this.url} - Server error (${status}). Retry #${this.serverErrorCount} in ${Math.round(delay / 60000)} minutes.`);
} else if (status >= 400) {
delay = Math.max(this.reloadInterval * 2, FIFTEEN_MINUTES);
Log.error(`${this.url} - Client error (${status}). Retrying in ${Math.round(delay / 60000)} minutes.`);
} else {
Log.error(`${this.url} - GraphQL request failed: ${status}.`);
}
return delay
}
/**
* Check if enough time has passed since the last fetch to warrant a new one.
* Uses reloadInterval as the threshold to respect user's configured fetchInterval.
* @returns {boolean} True if a new fetch should be performed
*/
shouldRefetch () {
if (!this.lastFetch) {
return true;
}
const timeSinceLastFetch = Date.now() - this.lastFetch;
return timeSinceLastFetch >= this.reloadInterval;
}
/**
* Broadcasts the current data to listeners
*/
broadcastData() {
let numJourneys;
if (this.data?.data?.station?.journeys?.elements) numJourneys = this.data.data.station.journeys.elements.length;
else numJourneys = null;
// Log.debug(`Broadcasting ${numJourneys} journeys to ${this.identifier}`);
this.dataReceivedCallback(this);
}
/**
* Sets the callback for successful data fetches
* @param {( fetcher: Fetcher) => void} callback - Called when data is received
*/
onReceive (callback) {
this.dataReceivedCallback = callback;
}
/**
* Sets the callback for fetch failures
* @param {( fetcher: Fetcher, error: Error) => void} callback - Called when a fetch fails
*/
onError (callback) {
this.fetchFailedCallback = callback
}
/**
* Fetches and processes data
*/
async fetchData() {
Log.debug(`Fetching data from RNV-Server for ${this.identifier}...`);
const now = new Date().toISOString();
const later = new Date();
later.setHours(new Date().getHours() + 10);
const then = later.toISOString();
const numJourneys = this.numJourneys;
const stationID = this.stationID;
const query = `query {
station(id:"${stationID}") {
hafasID
longName
journeys(startTime: "${now}" first: ${numJourneys} endTime: "${then}") {
totalCount
elements {
... on Journey {
line {
id
style {
primary {
hex
}
}
}
type
stops(onlyHafasID: "${stationID}") {
pole {
platform {
type
label
barrierFreeType
}
}
destinationLabel
plannedArrival {
isoString
}
realtimeArrival {
isoString
}
plannedDeparture {
isoString
}
realtimeDeparture {
isoString
}
}
}
}
}
}
}`;
this.clearReloadTimer();
let nextDelay = this.reloadInterval;
try {
const response = await this.client.query({ query: gql(query) });
this.serverErrorCount = 0;
this.data = response;
// Set flag to check whether a previous fetch was successful
this.previousFetchOk = true;
this.lastFetch = Date.now();
this.broadcastData();
} catch (error) {
Log.error(error);
const delay = this.getDelayForError(error);
nextDelay = delay;
this.fetchFailedCallback(this, error);
}
this.scheduleNextFetch(nextDelay);
}
// async fetchColor() {
// Log.debug(`Fetching color from RNV-Server for module ${this.identifier}...`);
// const url = "https://rnvopendataportalpublic.blob.core.windows.net/public/openDataPortal/liniengruppen-farben.json";
// try {
// const response = await fetch(url);
// if (response.status != 200) {
// throw new Error(`Could not fetch color data from RNV-Server with status code ${response.status}.`)
// }
// const data = await response.json();
// this.color = data.lineGroups;
// } catch(error) {
// Log.error(`${error}`);
// }
// }
}
module.exports = NodeHelper.create({
start: function() {
this.config = null;
this.client = null;
// this.previousFetchOk = false;
// this.colorTimer = null;
// this.dataTimer = null;
this.fetchers = [];
},
socketNotificationReceived: async function(notification, payload) {
if (notification == "SET_CONFIG") {
let apiKey;
moment.updateLocale(config.language, payload.timeFormat);
const clientAPIURL = payload.clientAPIURL;
if (!payload.apiKey) {
const clientID = payload.clientID;
const clientSecret = payload.clientSecret;
const resourceID = payload.resourceID;
const oAuthURL = payload.oAuthURL;
// Create apiKey from given credentials
apiKey = await this.createToken(oAuthURL, clientID, clientSecret, resourceID);
}
// Authenticate by OAuth
this.client = this.authenticate(apiKey, clientAPIURL);
this.getOrCreateFetcher(this.client, payload.clientAPIURL, payload.identifier, payload.updateInterval, payload.numJourneys, payload.stationID);
}
},
/**
*
* @param {*} client
* @param {*} url
* @param {*} identifier
* @param {*} fetchInterval
* @param {*} numJourneys
* @param {*} stationID
* @returns
*/
getOrCreateFetcher: function(client, url, identifier, fetchInterval, numJourneys, stationID) {
try {
new URL(url);
} catch (error) {
Log.error(`Malformed API-URL (${url}): ${error}`)
this.sendSocketNotification("ERROR", error);
return;
}
let fetcher = null;
let fetchIntervalCorrected;
if (typeof this.fetchers[identifier + url] === "undefined") {
if (fetchInterval < 60 * 1000) {
Log.warn(`fetchInterval for url ${url} must be >= 60.000`)
fetchIntervalCorrected = 60000;
}
Log.debug(`Create new fetcher for url "${url}" and "${identifier}" - Interval: ${fetchIntervalCorrected || fetchInterval}`);
fetcher = new Fetcher(client, url, fetchIntervalCorrected || fetchInterval, numJourneys, stationID, identifier);
// Log.log(`Setting callback function of fetcher ${fetcher.identifier} for *onReceive*.`)
fetcher.onReceive((fetcher) => {
// Call the local *broadcastData* method of this module
this.broadcastData(fetcher, identifier);
})
// Log.log(`Setting callback function of fetcher ${fetcher.identifier} for *onError*.`)
fetcher.onError((fetcher, error) => {
Log.error(`Fetcher error - Could provide data for module ${fetcher.identifier}: ${error}`)
//let errorType = NodeHelper.checkFetchError(error);
this.sendSocketNotification("ERROR", {id: identifier, data: 4});
})
this.fetchers[identifier + url] = fetcher;
fetcher.fetchData();
//fetcher.fetchColor();
} else {
Log.debug(`Use existing fetcher for url ${url} and identifier ${identifier}`)
fetcher = this.fetchers[identifier + url];
// Check if data is stale and needs refresh
if (fetcher.shouldRefetch()) {
Log.debug(`Data is stale, fetching fresh data for url ${url}`)
fetcher.fetchData();
} else {
// Calling the onRecieveCallbackMethod of the fetcher object
fetcher.broadcastData(this, identifier);
}
}
},
// Create access token if there is none given in the configuration file
createToken: async function(OAUTH_URL, CLIENT_ID, CLIENT_SECRET, RESOURCE_ID) {
const response = await fetch(OAUTH_URL, {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: 'grant_type=client_credentials&client_id=' + CLIENT_ID + '&client_secret=' + CLIENT_SECRET + '&resource=' + RESOURCE_ID
});
if (!response.ok) {
Log.error(`${this.name}: Error while creating access token: ${response.error}`);
return null;
}
const json = await response.json();
return json["access_token"];
},
// Authenticate with given token
authenticate: function(token, clientAPIURL) {
var httpLink = new HttpLink({uri: clientAPIURL, credentials: 'same-origin', fetch: fetch});
var middlewareAuthLink = setContext(async (_, { headers }) => {
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : null,
},
};
});
var client = new ApolloClient({
link: middlewareAuthLink.concat(httpLink),
cache: new InMemoryCache()
})
return client;
},
/**
* Broadcasts the current data to listeners
*/
broadcastData(fetcher, identifier) {
let d = [];
if (fetcher.data) d = this.transformData(fetcher.data.data.station.journeys.elements);
const response = {
id: identifier,
data: d,
stationName: fetcher.data.data.station.longName,
stationID: fetcher.data.data.station.hafasID,
lastFetch: fetcher.lastFetch,
}
if (fetcher.data) this.sendSocketNotification("DATA", response);
// if (fetcher.color) this.sendSocketNotification("COLOR", {id: identifier, data: fetcher.color});
},
transformData(data) {
let elements = data || [];
// Neue Kopie für Darstellung
const transformedJourneys = elements
.filter(j => j.stops?.[0]?.plannedDeparture?.isoString)
.map(j => {
const stop = j.stops[0];
// Planned Departure
const planned = moment.utc(stop.plannedDeparture.isoString).local();
// Realtime Departure
const realtime = stop.realtimeDeparture?.isoString
? moment.utc(stop.realtimeDeparture.isoString).local()
: null;
// Delay in minutes
const delay = realtime ? realtime.diff(planned, "minutes") : 0;
return {
line: j.line.id.split('-')[1],
line_color: j.line.style.primary.hex,
depature: planned.format("HH:mm"),
direction: stop.destinationLabel,
delay: delay,
platform: stop.pole.platform.label,
type: j.type
};
});
return transformedJourneys;
// // Remove elements where its depature time is equal to null
// // Iteration from end of array since the command *splice* might reduce its size.
// for (let i = elements.length - 1; i >= 0; i--) {
// if (elements[i].stops[0].plannedDeparture.isoString == null) {
// elements.splice(i, 1);
// }
// }
// // Sorting fetched data based on the departure times
// elements.sort((a, b) => {
// let depA = a.stops[0].plannedDeparture.isoString;
// let depB = b.stops[0].plannedDeparture.isoString;
// return (depA < depB) ? -1 : ((depA > depB) ? 1 : 0);
// });
// const numDepartures = elements.length;
// const delayFactor = 60 * 1000;
// // Delay
// for (let i = 0; i < numDepartures; i++) {
// // Create new key-value pair, representing the current delay of the departure
// elements[i].stops[0].delay = 0;
// // If there is no realtime departure data avaialble, skip delay calculation and continue with next departure
// if (elements[i].stops[0].realtimeDeparture.isoString == null) {
// continue;
// }
// let currentDepartureTimes = elements[i].stops[0];
// // Planned Departure
// let plannedDepartureIsoString = currentDepartureTimes.plannedDeparture.isoString;
// let plannedDepartureDate = new Date(plannedDepartureIsoString);
// // Realtime Departure
// let realtimeDepartureIsoString = currentDepartureTimes.realtimeDeparture.isoString;
// let realtimeDepartureDate = new Date(realtimeDepartureIsoString);
// // Delay calculation
// let delayms = Math.abs(plannedDepartureDate - realtimeDepartureDate);
// let delay = Math.floor(delayms / delayFactor);
// // Assign calculated delay to new introduced key-value pair
// elements[i].stops[0].delay = delay;
// }
// // const { journeys } = fetchedData.data.station;
// // journeys.elements = journeys.elements.map(j => this.mapDepartures(j));
// return elements;
},
mapDepartures(departures) {
let transformedDepartures = {
line: departures.line.id.split('-')[1],
type: departures.type,
destination: departures.stops[0].destinationLabel,
platform: departures.stops[0].pole.platform.label,
delay: departures.stops[0].delay,
// TODO: Fix TimeZone-Part
// plannedDeparture: new Date(departures.stops[0].plannedDeparture.isoString).toLocaleTimeString('de-DE', {hour: '2-digit', minute: '2-digit', hour12: false}),
depature: moment().utc(departures.stops[0].plannedDeparture.isoString).local().format("HH:mm"),
direction: departures.stops[0].destinationLabel
}
return transformedDepartures;
},
});