-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
142 lines (128 loc) · 5.51 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Local Buses</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<table class="table">
<thead>
<tr><th></th><th>Origin/Destination</th><th>Time to leave</th></tr>
</thead>
<tbody id="times">
</tbody>
</table>
</div>
</div>
</body>
<script>
let searchParams = new URLSearchParams(window.location.search);
let compassOffset = searchParams.get('o');
let speechNotification = null;
if (!compassOffset) {
compassOffset = 0;
}
let stopParams = searchParams.getAll('s');
let stops = [];
for (let i = 0; i < stopParams.length; i++) {
let parts = stopParams[i].split('!');
stops.push({ id: parts[0], delay: parts[1] * 60});
}
function slowestFirst(a, b) {
return b.delay - a.delay;
}
// round down to half minute intervals, display as 0.5
function fmt(time_in_seconds) {
return Math.floor(time_in_seconds * 2 / 60) / 2;
}
function newNotifications(naptanId, vehicleId, delay) {
console.log("Clearing current notifications");
if (speechNotification != null)
window.clearTimeout(speechNotification);
notifications(naptanId, vehicleId, delay);
}
function notifications(naptanId, vehicleId, delay) {
console.log("Beginning notification pool for "+naptanId);
$.getJSON("https://api.tfl.gov.uk/StopPoint/"+naptanId+"/Arrivals",
function(data, textStatus, jqXHR) {
let utterance;
$.each(data,
function(index, value) {
if (value.vehicleId == vehicleId) {
timeToLeave = value.timeToStation - delay;
if (timeToLeave > 120) {
utterance = new SpeechSynthesisUtterance("Leave in "+fmt(timeToLeave)+" minutes for "+value.lineName+" to "+value.towards);
console.log("Setting 60s timeout for update");
speechNotification = window.setTimeout(notifications, 60*1000, naptanId, vehicleId, delay);
} else if (timeToLeave > 0) {
utterance = new SpeechSynthesisUtterance("Leave now for " + value.lineName+" to "+value.towards + " from " + value.stationName ); ;
} else {
utterance = new SpeechSynthesisUtterance("You are "+ 0-timeToLeave+" seconds late for " + value.lineName+ " to "+ value.towards);
}
} else {
}
}
);
if (utterance == null)
utterance = new SpeechSynthesisUtterance("Tracking Lost");
window.speechSynthesis.speak(utterance);
}
);
}
let transports = [];
$.each(stops, function(index, value) {
let stop = value;
$.getJSON("https://api.tfl.gov.uk/StopPoint/"+value.id+"/Arrivals",
function(data, textStatus, jqXHR) {
$.each(data,
function( index, value) {
let lineName = value.lineName;
let wait = Math.floor(value.timeToStation);
let destination = value.towards;
let color;
if (wait < stop.delay) {
color = "danger";
} else if (wait - 2 > stop.delay) {
color = "success";
} else {
color = "warning";
}
let loc;
if (value.modeName == "bus") {
loc = value.destinationName+"<br/>"+value.stationName+" <b>"+value.platformName+"";
} else {
loc = value.platformName+"<br/>"+value.stationName.replace(" Underground Station","")+" to "+destination;
}
let tr = "<tr data-bus='"+value.vehicleId+"' data-time='"+(wait-stop.delay)+"'><td>"+lineName+"</td><td>"+loc+"</td><td class='text-"+color+"'>"+fmt(wait-stop.delay)+ "min (+"+fmt(stop.delay)+"min walk)</td><td><img src='timer.png' height='16' onclick='newNotifications(\""+value.naptanId+"\",\""+value.vehicleId+"\","+stop.delay+");'></td></tr>";
let thing = {
vehicleId : value.vehicleId,
delay : (wait -stop.delay),
element : tr
};
transports.push(thing);
}
);
let sorted = transports.sort(slowestFirst);
let times = $('#times');
times.empty();
let foundBuses = new Map();
for (let i = 0; i < sorted.length; i++) {
let bus = sorted[i].vehicleId;
if (!foundBuses.has(bus)) {
times.append(sorted[i].element);
}
foundBuses.set(bus, sorted[i]);
}
}
);
});
</script>
</html>