-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode_helper.js
More file actions
executable file
·133 lines (124 loc) · 4 KB
/
Copy pathnode_helper.js
File metadata and controls
executable file
·133 lines (124 loc) · 4 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
/*
* Magic Mirror
* Node Helper: MMM-MVG
*
* By Simon Kobler
* MIT Licensed
*
*/
const NodeHelper = require("node_helper");
const globals = {
"User-Agent":
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36",
"Content-Type": "application/x-www-form-urlencoded",
Accept: "application/json, text/javascript, */*; q=0.01",
"X-Requested-With": "XMLHttpRequest",
"X-MVG-Authorization-Key": "5af1beca494712ed38d313714d4caff6",
Referer: "https://www.mvg.de/dienste/abfahrtszeiten.html",
"Accept-Encoding": "gzip",
"Accept-Language": "en-US,en;q=0.9,de;q=0.8",
};
const fibAPI = "https://www.mvg.de/api/bgw-pt/v3";
module.exports = NodeHelper.create({
socketNotificationReceived: function (notification, payload) {
const self = this;
switch (notification) {
case "GET_STATION_INFO":
self.getStationInfo(payload);
self.getInterruptionsInfo();
break;
case "GET_DEPARTURE_DATA":
self.getDepartureInfo(payload);
break;
default:
console.error("Switch item {} is missing", notification);
}
},
getDepartureInfo: function (payload) {
const self = this;
if (payload.stationId == 0) {
console.warn("Station ID for the specified station (" + payload.station + ") is not available yet. Awaiting retrieval from the API.");
return;
}
const args = {
globalId: payload.stationId,
limit: payload.maxEntries || 10,
offsetInMinutes: 0,
transportTypes: 'UBAHN,REGIONAL_BUS,BUS,TRAM,SBAHN'
};
const query = new URLSearchParams(args).toString();
fetch(`${fibAPI}/departures?${query}`, {
method: "GET",
headers: globals,
})
.then(async (response) => {
if (!response.ok) {
throw new Error(`Received unexpected response (${response.statusText}) from MVG api while retrieving departure data: ${await response.text()}`);
}
return response.json();
})
.then((data) => {
payload.transport = data;
self.sendSocketNotification("UPDATE_DEPARTURE_INFO", payload);
})
.catch((error) => {
console.error("Error while reading departure data", error);
self.sendSocketNotification(
"ERROR",
"COULD_NOT_GET_DEPARTURE_DATA"
);
});
},
getStationInfo(payload) {
const self = this;
const args = {
query: payload.station
}
const query = new URLSearchParams(args).toString();
fetch(`${fibAPI}/locations?${query}`, {
method: "GET",
headers: globals,
})
.then(async (response) => {
if (!response.ok) {
throw new Error(`Received unexpected response (${response.statusText}) from MVG api while retrieving station info: ${await response.text()}`);
}
return response.json();
})
.then((data) => {
if (data?.[0]?.globalId === undefined) {
self.sendSocketNotification("ERROR", "NO_STATION");
} else {
payload.stationId = data[0].globalId;
payload.stationName = data[0].name;
self.sendSocketNotification("UPDATE_STATION", payload);
}
})
.catch((error) => {
console.error("Error while reading station data", error);
self.sendSocketNotification(
"ERROR",
"COULD_NOT_GET_STATION_DATA"
);
});
},
getInterruptionsInfo: function () {
const self = this;
fetch(`${fibAPI}/messages`, {
method: "GET",
headers: globals,
})
.then(async (response) => {
if (!response.ok) {
throw new Error(`Received unexpected response (${response.statusText}) from MVG api while retrieving interruption data: ${await response.text()}`);
}
return response.json();
})
.then((data) => {
self.sendSocketNotification("UPDATE_INTERRUPTION_DATA", data);
})
.catch((error) => {
console.error("Error while reading interuption data", error);
});
},
});