forked from ekantola/infodisplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
214 lines (186 loc) · 7.23 KB
/
scripts.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
/*globals jQuery */
(function($, config) {
var now = new Date();
/*
* Time formatting, displaying and friends
*/
function repeat(str, count) {
var outStr = '';
for (var i=0; i<count; ++i) {
outStr += str;
}
return outStr;
}
function lpad(obj, padding, minCount) {
var str = '' + obj;
return (str.length < minCount) ? (repeat(padding, minCount-str.length) + str) : str;
}
function toTimeElem(number) {
return lpad(number, '0', 2);
}
function formatTime(hours, minutes, seconds) {
var str = toTimeElem(hours);
// Using '!=' to compare with null is intentional. The following comparisons are functionally equivalent:
//
// minutes != null
// (function(undefined) { return minutes === undefined || minutes === null; }())
if (minutes != null) {
str += ':' + toTimeElem(minutes);
}
if (seconds != null) {
str += ':' + toTimeElem(seconds);
}
return str;
}
function formatHourMinTime(date) {
return formatTime(date.getHours(), date.getMinutes());
}
function updateTime() {
now = new Date();
$('#date').text(('' + now).substring(0, 10));
$('#time').text(formatHourMinTime(now));
}
/*
* Bus schedules
*/
function diffMinutesToStr(diffMinutes) {
var hours = Math.floor(diffMinutes / 60);
var minStr = ' min'; //(diffMinutes%60 === 1) ? " min" : " mins";
if (false /*hours > 0*/) {
return hours + 'h' + diffMinutes%60 + minStr;
} else {
return diffMinutes + minStr;
}
}
function populateTable(stopId, arrivalData) {
var tableId = "#stop_" + stopId;
$(tableId).empty();
var total = 0;
for (var i=0; i<arrivalData.length && total < config.ARRIVAL_ITEMS; i++) {
var item = arrivalData[i];
var stopTime = formatHourMinTime(item.time);
var diffTime = Math.round((item.time - now) / (1000*60));
if (diffTime > 0) {
$(tableId).append(
'<tr><td class="line">' + item.busLineName + '</td>' +
'<td class="destination">' + /*item.destination +*/ (item.busLineName == 501 ? 'Espoo' : 'Helsinki') + '</td>' +
'<td class="diffTime">' + diffMinutesToStr(diffTime) + '</td>' +
'<td class="stopTime">' + stopTime + '</td></tr>');
total++;
}
if (i >= arrivalData.length) {
break;
}
}
}
function isTrueData(evaluateItem, savedItems) {
if (!savedItems || !savedItems.length) {
return true;
}
for (var i=0; i<savedItems.length; ++i) {
var item = savedItems[i];
var timeDiff = (evaluateItem.time.getHours()*60 + evaluateItem.time.getMinutes()) - (item.time.getHours()*60 + item.time.getMinutes());
if (evaluateItem.busLineName === item.busLineName && (timeDiff <= config.TIME_LIMIT)) {
return false;
}
}
return true;
}
function getReittiopasStopData(apiData) {
var lines = apiData.split('\n'),
returnData = [];
for (var i=1; i<lines.length; ++i) {
var line = lines[i];
if (line && line.length && line.length > 1) {
var data = line.split('|');
var time = lpad(data[0], '0', 4);
var hour = parseInt(time.substring(0, 2), 10);
var minute = parseInt(time.substring(2, 4), 10);
var isTomorrow = 0;
if (hour > 23) {
isTomorrow = 1;
hour = hour - 24;
}
var atStop = new Date(now.getFullYear(), now.getMonth(), now.getDate()+isTomorrow, hour, minute, 0);
var diffMinutes = (atStop.getTime() - now.getTime()) / (1000*60);
data = {
minutes: diffMinutes,
time: atStop,
busLineName: data[1],
destination: data[2]
};
if (isTrueData(data, returnData)) {
returnData[returnData.length] = data;
}
}
}
return returnData;
}
function refreshBusStops(stops) {
for(var i = 0; i < stops.length; i++) {
$.get(config.reittiopas.baseUrl + stops[i].id, function(data) {
populateTable(this.url.substr(this.url.lastIndexOf("=") + 1), getReittiopasStopData(data));
});
}
}
/*
* Weather
*/
function refreshWeatherForecastForCity(location) {
$.get(config.weatherbug.BASE_URL, {
ACode: config.weatherbug.API_CODE,
unittype: 1,
citycode: location.code
}, function(data) {
var desc = $(data).find('item:first').find('description:first');
// Turn the CDATA content into jQuery DOM object, wrapping it inside a div first to make find() work
var descDOM = $('<div>' + desc.text() + '</div>');
var img = descDOM.find('img:first');
var imgUrl = img.length && img[0].src;
// Extract temperatures
var temp;
var childNodes = descDOM[0].childNodes; // argh, need to use DOM here :(
for (var i=0, length=childNodes.length, element; i<length; i++) {
element = childNodes[i];
if (element.data && ('' + element.data).indexOf('°C') >= 0) {
temp = parseInt(element.data, 10);
break;
}
}
// Update image and temp in UI
var forecast = $('#weather' + location.name);
forecast.find('.conditions img').attr('src', imgUrl);
forecast.find('.temp .value').text(temp);
});
}
function refreshWeatherForecasts(locations) {
for(var i = 0; i < locations.length; i++) {
refreshWeatherForecastForCity(locations[i]);
}
}
/*
* Other
*/
$(document).ready(function() {
setInterval(updateTime, config.CLOCK_UPDATE_TIMEOUT_MILLIS);
refreshWeatherForecasts(config.weatherbug.locations);
setInterval(refreshWeatherForecasts, config.WEATHER_REFRESH_TIMEOUT_MILLIS, config.weatherbug.locations);
refreshBusStops(config.reittiopas.stops);
setInterval(refreshBusStops, config.REITTIOPAS_REFRESH_TIMEOUT_MILLIS, config.reittiopas.stops);
if (config.taxi) {
if (config.taxi.sms) {
$('#taxi_address').text(config.taxi.sms.address);
$('#taxi_smsnumber').text(config.taxi.sms.number);
$('#taxi_sms').show();
}
if (config.taxi.phone) {
if (config.taxi.sms) {
$('#taxi_or').show();
}
$('#taxi_phonenumber').text(config.taxi.phone);
$('#taxi_phone').show();
}
$('#taxi').show();
}
});
}(jQuery, infodisplayConfig));