-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathztmGdanskDepartureBoardParser.js
165 lines (144 loc) · 6.96 KB
/
ztmGdanskDepartureBoardParser.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
var ztmGdanskDepartureBoardParser = {
setDepartureBoard: function (divTableId, stopId, stopName, displayCode) {
$.getJSON("https://ckan2.multimediagdansk.pl/delays?stopId=" + stopId, function (result) {
if (typeof result.error !== 'undefined') {
console.log(result.error);
}
else {
var table = document.getElementById(divTableId);
table.innerHTML = "";
var div1 = document.createElement('div');
var tr, th, span;
// Creating INFO row
var tb1 = document.createElement('table');
tb1.id = "infoDeparturesBoard";
tr = tb1.insertRow();
th = tr.insertCell();
th.id = "blank";
th = tr.insertCell();
th.id = "stopName";
th.innerText = stopName;
th = tr.insertCell();
th.style.textAlign = "right";
span = document.createElement('span');
span.id = "lastUpdateTime";
span.innerText = result.lastUpdate.split(" ")[1];
th.appendChild(span);
tr = tb1.insertRow();
th = tr.insertCell();
th = tr.insertCell();
th = tr.insertCell();
div1.appendChild(tb1);
// Creating HEADERS row
var tb2 = document.createElement('table');
tb2.id = "headerDeparturesBoard";
tb2.style.borderBottom = "0px";
tr = tb2.insertRow();
th = tr.insertCell();
th.className = "route";
th.innerText = "LINIA";
th = tr.insertCell();
th.className = "dir";
th.innerText = "KIERUNEK DOCELOWY";
th = tr.insertCell();
th.className = "time";
th.innerText = "ODJAZD";
div1.appendChild(tb2);
table.appendChild(div1);
// Creating TIMES rows
var div2 = document.createElement('div');
var tb3 = document.createElement('table');
tb3.id = "timesDeparturesBoard";
for (let index = 0; index < result.delay.length; index++) {
tr = tb3.insertRow();
th = tr.insertCell();
th.className = "route";
span = document.createElement('span');
span.innerText = result.delay[index].routeId > 400 && result.delay[index].routeId < 500 ? "N" + (result.delay[index].routeId - 400).toString() : result.delay[index].routeId.toString();
th.appendChild(span);
th = tr.insertCell();
th.className = "dir";
span = document.createElement('span');
span.innerText = result.delay[index].headsign;
th.appendChild(span);
th = tr.insertCell();
th.className = "time";
span = document.createElement('span');
if (result.delay[index].status === "REALTIME") {
if (result.delay[index].delayInSeconds > 300) //delay 5 min
{
span.style.color = "red";
}
else if (result.delay[index].delayInSeconds < -60) //advance 1 min
{
span.style.color = "lime";
}
}
else {
span.style.color = "yellow";
}
var temp = result.delay[index].estimatedTime.split(":");
var estimatedHours = parseInt(temp[0]);
var estimatedMinutes = parseInt(temp[1]);
var now = new Date();
var estimatedTime = new Date();
if (estimatedHours < now.getHours()) {
estimatedTime.setDay(estimatedTime.getDate() + 1);
estimatedTime.setHours(estimatedHours);
estimatedTime.setMinutes(estimatedMinutes);
}
else {
estimatedTime.setHours(estimatedHours);
estimatedTime.setMinutes(estimatedMinutes);
}
var calculatedTimeMinutes = (estimatedTime - now) / (1000 * 60);
if (calculatedTimeMinutes < 20) // 20 min
{
span.innerText = parseInt(calculatedTimeMinutes).toString() + " min (" + result.delay[index].estimatedTime + ")";
}
else {
span.innerText = result.delay[index].estimatedTime;
}
th.appendChild(span);
}
div2.appendChild(tb3);
table.appendChild(div2);
// Creating NEWS
$.getJSON("https://files.xyzgcm.pl/f/xml/bsk.json", function (result) {
if (typeof result.error !== 'undefined') {
console.log(result.error);
}
else {
if (result.komunikaty.length != 0)
table.innerHTML += "<div id=\"news\"></div>";
for (let index = 0; index < result.komunikaty.length; index++) {
news = document.getElementById("news");
news.innerHTML += result.komunikaty[index].tytul + "\n" + result.komunikaty[index].tresc;
}
}
});
// Creating SCROLLER
table.innerHTML += "<div id=\"scroller\"></div>";
$.getJSON("https://ckan2.multimediagdansk.pl/displayMessages", function (result) {
if (typeof result.error !== 'undefined') {
console.log(result.error);
}
else {
for (let index = 0; index < result.displaysMsg.length; index++) {
if (result.displaysMsg[index].displayCode == displayCode) {
scroller = document.getElementById("scroller");
p = document.createElement('p');
p.id = "scroller_content";
p.innerText = result.displaysMsg[index].messagePart1 + result.displaysMsg[index].messagePart2;
if (result.displaysMsg[index].msgType == 1) {
p.style.color = "red";
}
scroller.appendChild(p);
}
}
}
});
}
});
}
};