-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathnmbs-routeboard-card.js
116 lines (104 loc) · 3.54 KB
/
nmbs-routeboard-card.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
// Attribution for included font: https://www.fontsquirrel.com/license/BPdots
class NMBSRouteBoardCard extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
}
setConfig(config) {
const root = this.shadowRoot;
if (root.lastChild) root.removeChild(root.lastChild);
const cardConfig = Object.assign({}, config);
function getApiData() {
return fetch('https://api.irail.be/connections/?from=' + cardConfig.departureStation + '&to=' + cardConfig.arrivalStation + '&format=json&lang='+ cardConfig.lang )
.then(response => response.json())
.then((responseData) => {
return responseData;
})
.catch(function(error){
console.log(error);
})
}
function processData() {
getApiData().then(function(data){
let table;
if (cardConfig.lang == 'nl') {
table = `
<tr>
<th>Vertrek</th>
<th>Spoor</th>
<th>Richting</th>
</tr>
`
}
if (cardConfig.lang == 'fr') {
table = `
<tr>
<th>Départ</th>
<th>Voie</th>
<th>Direction</th>
</tr>
`
}
if (cardConfig.lang == 'en') {
table = `
<tr>
<th>Departure</th>
<th>Track</th>
<th>Direction</th>
</tr>
`
}
if (cardConfig.lang == 'de') {
table = `
<tr>
<th>Abfahrt</th>
<th>Gleis</th>
<th>Bestimmungsort</th>
</tr>
`
}
let y = data.connection.length;
if (cardConfig.show) {y= cardConfig.show;}
for (let i=0 ; i < y ; i++) {
let scheduledTime = new Date(data.connection[i].departure.time * 1000);
let hours = scheduledTime.getHours();
let minutes = ('0' + scheduledTime.getMinutes()).slice(-2);
let platform = data.connection[i].departure.platform;
let direction = data.connection[i].arrival.direction.name;
let delay = data.connection[i].departure.delay;
let delayString;
if (delay == 0) {delayString = '';}
else {delay = delay / 60; delayString = "+" + delay + "'";}
let row = '<tr><td class="time">'+ hours + ':' + minutes + '<span class="delay"> ' + delayString + '</span></td><td class="platform"><span class="platformNumber">' + platform + '</span></td><td class="direction">' + direction + '</td></tr>';
table = table + row;
}
root.getElementById("trains").innerHTML = table;
root.getElementById("title").innerHTML = cardConfig.departureStation + " - " + cardConfig.arrivalStation;
});
}
processData();
setInterval(processData, 60000);
const card = document.createElement('ha-card');
const content = document.createElement('div');
content.innerHTML = `
<div id="title" class="cardHeader"></div>
<div class="container">
<div class="tableContainer">
<table id="trains"></table>
</div>
</div>
`;
card.appendChild(content);
root.appendChild(card);
this._config = cardConfig;
}
set hass(hass) {
const config = this._config;
const root = this.shadowRoot;
root.lastChild.hass = hass;
}
getCardSize() {
return 1;
}
}
customElements.define('nmbs-routeboard', NMBSRouteBoardCard);