-
Notifications
You must be signed in to change notification settings - Fork 2
/
node_helper.js
61 lines (51 loc) · 1.5 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
* Node Helper: MMM-AC-aseag
*
* By joshua-martius
* MIT Licensed.
*/
var NodeHelper = require("node_helper");
var axios = require("axios").default;
var moment = require("moment");
module.exports = NodeHelper.create({
updateTimetable: async function(accessID, stopID) {
var self = this;
var options = {
method: 'GET',
url: 'https://demo.hafas.de/avv-aachen/restproxy/departureBoard',
params: {
id: stopID,
accessId: accessID,
format: 'json',
lang: 'de',
maxJourneys: '100'
}
};
let busses = [];
await axios.request(options).then(function (response) {
let raw = response.data;
let departures = raw.Departure;
departures.forEach(e => {
if(!e.reachable) return;
let bus = {};
bus.arrival = moment(e.date + " " + e.time).diff(moment(), "minutes")
bus.track = e.track.substring(0,3);
if(bus.arrival < 5 || bus.arrival > 15) return;
bus.name = e.ProductAtStop.name.replace(/ +/g, ' ') + " (" + bus.track + ")";
bus.direction = e.direction.replace(/ - /g, "-");
busses.push(bus)
})
//console.debug("Sending back " + busses.length + " Busses!")
}).catch(function (error) {
console.log("Error in AVV-API Request! " + error);
console.error(options)
});
self.sendSocketNotification("BUSSES", busses);
},
socketNotificationReceived: function(notification, payload) {
const self = this;
if (notification == "GET_BUSSES") {
self.updateTimetable(payload.accessID, payload.stopID);
}
},
});