-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbusinfo.js
151 lines (128 loc) · 4.88 KB
/
businfo.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
function HourMin(hms) {
var array = hms.split(":")
var h = parseInt(array[0])
var m = parseInt(array[1])
this.hour = h
this.min = m
}
HourMin.prototype.toString = function() {
return this.hour + ":" + ("0" + this.min).slice(-2)
};
function compareHourMin(a, b) {
if (a.hour < b.hour) {
return -1
} else if (a.hour > b.hour) {
return 1
} else {
if (a.min < b.min) {
return -1
} else if (a.min > b.min) {
return 1
} else {
return 0
}
}
}
function compareDepartureTime(a, b) {
return compareHourMin(a["departure_time"], b["departure_time"])
}
var stops = []
var trips = []
var stop_times = []
var calendar = []
var calendar_dates = []
function readTable(table, data) {
var lines = data.split(/\r\n|\r|\n/)
var heads = lines[0].split(",")
for (var i = 1; i < lines.length; ++i) {
cols = lines[i].split(",")
var item = {}
for (var j = 0; j < cols.length; ++j) {
item[heads[j]] = cols[j]
}
table.push(item)
}
}
function main() {
var stopDesc = {}
for (var i = 0; i < stops.length; ++i) {
stopDesc[stops[i]["stop_id"]] = stops[i]["stop_desc"]
}
var today = new Date()
var day = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"][today.getDay()]
var services = {}
for (var i = 0; i < calendar.length; ++i) {
if (calendar[i][day] == "1") {
services[calendar[i]["service_id"]] = true
}
}
var year = today.getFullYear()
var month = today.getMonth() + 1
var date = today.getDate()
var ymdStr = year.toString() + ("00" + month).slice(-2) + ("00" + date).slice(-2)
for (var i = 0; i < calendar_dates.length; ++i) {
if (calendar_dates[i]["date"] == ymdStr) {
if (calendar_dates[i]["exception_type"] == "1") {
services[calendar_dates[i]["service_id"]] = undefined
} else if (calendar_dates[i]["exception_type"] == "2") {
services[calendar_dates[i]["service_id"]] = true
}
}
}
var trip_headsign = {}
for (var i = 0; i < trips.length; ++i) {
if (services[trips[i]["service_id"]]) {
trip_headsign[trips[i]["trip_id"]] = trips[i]["trip_headsign"]
}
}
var southbound = []
var northbound = []
for (var i = 0; i < stop_times.length; ++i) {
if (stop_times[i]["stop_id"] == "TakaokaEkiMinamiGuchi1") {
if (trip_headsign[stop_times[i]["trip_id"]]) {
southbound.push({departure_time: new HourMin(stop_times[i]["departure_time"]), headsign: trip_headsign[stop_times[i]["trip_id"]], stop_desc: stopDesc[stop_times[i]["stop_id"]]})
}
} else if (stop_times[i]["stop_id"] == "ShinTakaokaEki1" || stop_times[i]["stop_id"] == "ShinTakaokaEki2") {
if (trip_headsign[stop_times[i]["trip_id"]]) {
northbound.push({departure_time: new HourMin(stop_times[i]["departure_time"]), headsign: trip_headsign[stop_times[i]["trip_id"]], stop_desc: stopDesc[stop_times[i]["stop_id"]]})
}
}
}
southbound.sort(compareDepartureTime)
northbound.sort(compareDepartureTime)
var nowTime = new HourMin("" + today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds())
var southboundFirst = southbound.length
for (var i = 0; i < southbound.length; ++i) {
if (compareHourMin(nowTime, southbound[i]["departure_time"]) < 0) {
southboundFirst = i
break
}
}
var northboundFirst = northbound.length
for (var i = 0; i < northbound.length; ++i) {
if (compareHourMin(nowTime, northbound[i]["departure_time"]) < 0) {
northboundFirst = i
break
}
}
document.getElementById("bus-sb-1-stop").textContent = southbound[southboundFirst]["stop_desc"]
document.getElementById("bus-sb-1-time").textContent = southbound[southboundFirst]["departure_time"]
document.getElementById("bus-sb-1-dest").textContent = southbound[southboundFirst]["headsign"]
document.getElementById("bus-sb-2-stop").textContent = southbound[southboundFirst + 1]["stop_desc"]
document.getElementById("bus-sb-2-time").textContent = southbound[southboundFirst + 1]["departure_time"]
document.getElementById("bus-sb-2-dest").textContent = southbound[southboundFirst + 1]["headsign"]
document.getElementById("bus-nb-1-stop").textContent = northbound[northboundFirst]["stop_desc"]
document.getElementById("bus-nb-1-time").textContent = northbound[northboundFirst]["departure_time"]
document.getElementById("bus-nb-1-dest").textContent = northbound[northboundFirst]["headsign"]
document.getElementById("bus-nb-2-stop").textContent = northbound[northboundFirst + 1]["stop_desc"]
document.getElementById("bus-nb-2-time").textContent = northbound[northboundFirst + 1]["departure_time"]
document.getElementById("bus-nb-2-dest").textContent = northbound[northboundFirst + 1]["headsign"]
}
window.onload = function() {
$.get("csv/stops.txt", readTable.bind(undefined, stops))
$.get("csv/trips.txt", readTable.bind(undefined, trips))
$.get("csv/stop_times.txt", readTable.bind(undefined, stop_times))
$.get("csv/calendar.txt", readTable.bind(undefined, calendar))
$.get("csv/calendar_dates.txt", readTable.bind(undefined, calendar_dates))
setTimeout(main, 1000)
}