-
Notifications
You must be signed in to change notification settings - Fork 4
/
node_helper.js
231 lines (204 loc) · 7.65 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
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
/**
* @file node_helper.js
*
* @author fewieden
* @license MIT
*
* @see https://github.com/fewieden/MMM-WienerLinien
*/
/**
* @external node-fetch
* @see https://www.npmjs.com/package/node-fetch
*/
const fetch = require('node-fetch');
/**
* @external node_helper
* @see https://github.com/MichMich/MagicMirror/blob/master/js/node_helper.js
*/
const NodeHelper = require('node_helper');
/**
* @module node_helper
* @description Backend for the module to query data from the API provider.
*
* @requires external:node-fetch
* @requires external:node_helper
*/
module.exports = NodeHelper.create({
baseUrl: 'https://www.wienerlinien.at/ogd_realtime',
/**
* @function socketNotificationReceived
* @description Receives socket notifications from the module.
* @async
* @override
*
* @param {string} notification - Notification name
* @param {*} payload - Detailed payload of the notification.
*
* @returns {void}
*/
socketNotificationReceived(notification, payload) {
if (notification === 'CONFIG') {
this.config = payload;
this.getData();
setInterval(async () => {
await this.getData();
}, this.config.updateInterval);
}
},
/**
* @function getData
* @description Wrapper for all required data set requests.
* @async
*
* @returns {void}
*/
async getData() {
await this.getMonitoringData();
if (this.config.elevatorStations.length > 0) {
const url = `${this.baseUrl}/trafficInfoList?name=aufzugsinfo&relatedStop=${this.config.elevatorStations.join('&relatedStop=')}`;
await this.getAdditionalData(url, 'Elevator');
}
if (this.config.incidentLines.length > 0) {
const incidentType = this.config.incidentShort ? 'stoerungkurz' : 'stoerunglang';
const url = `${this.baseUrl}/trafficInfoList?name=${incidentType}&relatedLine=${this.config.incidentLines.join('&relatedLine=')}`;
await this.getAdditionalData(url, 'Incident');
}
},
/**
* @function getMonitoringData
* @description Requests departures of stations from monitoring service.
* @async
*
* @returns {void}
*/
async getMonitoringData() {
try {
const response = await fetch(`${this.baseUrl}/monitor?rbl=${this.config.stations.join('&rbl=')}`);
const parsedBody = await response.json();
if (parsedBody.message.value === 'OK') {
this.handleData(parsedBody.data.monitors, parsedBody.message.serverTime);
} else {
throw new Error('No WienerLinien data');
}
} catch (e) {
console.log('Error getting WienerLinien station data:', e);
}
},
/**
* @function getAdditionalData
* @description Requests additional data for incidents lines/elevators.
* @async
*
* @param {string} url - URL to API endpoint
* @param {string} type - Incident type
*
* @returns {void}
*/
async getAdditionalData(url, type) {
try {
const response = await fetch(url);
const parsedBody = await response.json();
if (Object.prototype.hasOwnProperty.call(parsedBody.data, 'trafficInfos')) {
this[`handle${type}Data`](parsedBody.data.trafficInfos);
} else {
throw new Error('No WienerLinien data');
}
} catch (e) {
console.log(`Error getting WienerLinien data for type ${type}:`, e);
}
},
/**
* @function handleElevatorData
* @description Maps elevator response and sends it to the module via socket notification.
*
* @param {object[]} data - Response from API
*
* @returns {void}
*/
handleElevatorData(data = []) {
const elevators = data
.map(({ title, description }) => `${title}: ${description}`)
.sort();
this.sendSocketNotification('ELEVATORS', elevators);
},
/**
* @function handleIncidentData
* @description Maps incident response and sends it to the module via socket notification.
*
* @param {object[]} data - Response from API
*
* @returns {void}
*/
handleIncidentData(data = []) {
const incidents = data
.map(({ relatedLines, description }) => ({
description,
lines: relatedLines.join(', ')
}))
.sort((a, b) => a.lines.localeCompare(b.lines));
this.sendSocketNotification('INCIDENTS', incidents);
},
/**
* @function handleData
* @description Maps station departure response and sends it to the module via socket notification.
*
* @param {object[]} data - Response from API
* @param {string} time - server time
*
* @returns {void}
*/
handleData(data, time) {
const stations = {};
for (let i = 0; i < data.length; i += 1) {
if (!Object.prototype.hasOwnProperty.call(stations, data[i].locationStop.properties.name)) {
stations[data[i].locationStop.properties.name] = {
name: data[i].locationStop.properties.title,
departures: []
};
}
for (let n = 0; n < data[i].lines.length; n += 1) {
let metroFlag = false;
for (let x = 0; x < data[i].lines[n].departures.departure.length; x += 1) {
if (Object.keys(data[i].lines[n].departures.departure[x].departureTime).length === 0) {
metroFlag = true;
break;
}
const { departureTime } = data[i].lines[n].departures.departure[x];
stations[data[i].locationStop.properties.name].departures.push({
time: departureTime[Object.prototype.hasOwnProperty.call(departureTime, 'timeReal') ? 'timeReal' : 'timePlanned'],
towards: data[i].lines[n].towards,
line: data[i].lines[n].name,
type: data[i].lines[n].type
});
}
if (metroFlag) {
const departureTimePattern = /[0-9]+/g;
const departureTimeMatches = data[i].lines[n].towards.match(departureTimePattern).toString()
.split(',');
const towardsPattern = /^[a-zäöüß ]+/i;
const towardsMatch = data[i].lines[n].towards.match(towardsPattern).toString()
.replace(/ {2,}/g, ' ')
.trim();
for (let x = 0; x < departureTimeMatches.length; x += 1) {
const datetime = new Date(time);
datetime.setSeconds(0);
datetime.setMinutes(datetime.getMinutes() + departureTimeMatches[x]);
stations[data[i].locationStop.properties.name].departures.push({
time: datetime,
towards: towardsMatch,
line: data[i].lines[n].name,
type: data[i].lines[n].type
});
}
}
}
}
const keys = Object.keys(stations);
for (let i = 0; i < keys.length; i += 1) {
stations[keys[i]].departures = stations[keys[i]].departures.sort((a, b) => {
return a.time < b.time ? -1 : (a.time > b.time ? 1 : 0);
});
}
this.sendSocketNotification('STATIONS', stations);
}
});