generated from Dennis-Rosenbaum/MMM-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-MyBus.js
114 lines (100 loc) · 2.93 KB
/
MMM-MyBus.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
Module.register("MMM-MyBus", {
defaults: {
updateInterval: 600000,
departuremonitor: "7ceb8ad4ddbb52c8fb175944b4933e39",
exampleContent: "Hurra"
},
getStyles() {
return ["template.css"]
},
start() {
this.templateContent = this.config.exampleContent
this.loaded = false;
this.url = `https://www.vrs.de/index.php?eID=tx_vrsinfo_departuremonitor&i=${this.config.departuremonitor}`;
this.getData()
.then((data) => {
this.updateTime = data['updated'];
events = data['events'];
station = data['events'][0]['stopPoint']['name'];
console.log(station);
console.log(events);
this.convertJSONToTable(events);
})
.catch((error) => {
console.error('Eror: ', error);
});
setInterval(() => {
this.getData();
}, this.config.updateInterval);
},
getData: async function () {
const proxyUrl = 'https://cors-anywhere.herokuapp.com/';
const requestOptions = {
method: "GET",
redirect: "follow",
headers: {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:132.0) Gecko/20100101 Firefox/132.0"
}
};
try {
const response = await fetch(proxyUrl + this.url, requestOptions);
if (!response.ok) {
throw new Error('Network response was not ok ' + response.statusText);
}
const result = await response.json();
return result;
}
catch (error) {
console.log('Fetch error: ', error);
throw error;
}
},
convertJSONToTable: function(jsonData) {
table = '<table><thead><tr>';
table += '</tr></thead><tbody>';
table += '<tr>';
table += '<td>';
table += 'Geplant';
table += '</td>';
table += '<td>';
table += 'Erwartet';
table += '</td>';
table += '<td>';
table += 'Linie';
table += '</td>';
table += '<td>';
table += 'Richtung';
table += '</td>';
table += '</tr>';
jsonData.forEach(row => {
estimated = row['departure']['estimate'];
if (typeof (estimated) == "undefined") {
estimated = ''
}
table += '<tr>';
table += '<td>';
table += row['departure']['timetable'];
table += '</td>';
table += '<td>';
table += estimated;
table += '</td>';
table += '<td>';
table += row['line']['product'] + ' ' + row['line']['number'];
table += '</td>';
table += '<td>';
table += row['line']['direction'];
table += '</td>';
table += '</tr>';
});
table += '</tbody></table>';
return table;
},
/**
* Render the page we're on.
*/
getDom() {
const wrapper = document.createElement("div")
wrapper.innerHTML = '<b>Abfahrten ' + this.updateTime + ' Linie</b><br />${this.exampleContent}';
return wrapper
}
})