-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreq.js
214 lines (174 loc) · 5.84 KB
/
req.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
var apiKey = "c2kg7EGqKUAunjI3JPIaKXOGVhf5DEAZ";
var gmapsApiKey = "AIzaSyAcZBN476sRwEC3O98ED4Hr1IZTq2jptR8";
function getTSATimes(airport, callback){
var reqUrl = "https://demo30-test.apigee.net/v1/hack/tsa?airport=" + airport + "&apikey=" + apiKey;
httpGetAsync(reqUrl, callback);
}
function httpGetAsync(theUrl, callback){//from http://stackoverflow.com/a/4033310/2009336
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(xmlHttp.responseText);
}
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.send(null);
}
function timeRangeToMinMax(timeRange){//e.g. 1-10 min
return timeRange.replace("min", "").split("-").map(function(e){return parseInt(e)})
}
function handleTSATimes(res){
//console.log(res)
res = JSON.parse(res)
var lines = res.WaitTimeResult;
var waitTimeMap = {}
for(var i = 0; i < lines.length; i++){
var line = lines[i]
if(!waitTimeMap[line.checkpointID]){
waitTimeMap[line.checkpointID] = line
line.waitTimeRange = timeRangeToMinMax(line.waitTime)
}
}
var checkpoints = res.AirportResult[0].airport.checkpoints
for(var i = 0; i < checkpoints.length; i++){
var checkpoint = checkpoints[i]
waitTimeMap[checkpoint.id].checkpointData = checkpoint
}
return waitTimeMap
}
// getTSATimes("ATL", function(res){
// var waitTimeMap = handleTSATimes(res)
// console.log(waitTimeMap)
// document.getElementById("wait").innerHTML = waitTimeMap;
// })
//---Main Times stuff
document.getElementById("submit").onclick = function() {
fltnum = document.getElementById("fltnum").value;
getFltInfo(fltnum, function(res){
var airport = JSON.parse(res).flightStatusResponse.statusResponse.flightStatusTO.flightStatusLegTOList.departureAirportCode;
document.getElementById("airport").innerHTML = " at "+airport+":";
//var duration = getTime(arrivalAirport, "www.google.com");
//console.log(duration);
//document.getElementById(duration).innerHTML = duration;
printFltTimes();
getTSATimes(airport, function(tsaTimes){
var waitTimeMap = handleTSATimes(tsaTimes)
console.log(waitTimeMap)
var output = "<ul style='list-style-type:none'>"
for(waitObjectKey in waitTimeMap){//just assume there's no CSS injection taking place here
output += "<li>" + waitObjectToString(waitTimeMap[waitObjectKey]) + "</li>"
}
output += "</ul>"
document.getElementById("waitTimes").innerHTML = output
// getTime(document.getElementById('address').value || "711 techwood drive", arrivalAirport + " airport", "driving", function(e,r){
// console.log(e,r)
// })
})
duration = 36;
waitTime = 20;
totalTime = duration + waitTime + 60;
document.getElementById("totalTime").innerHTML = totalTime;
console.log(totalTime+"total");
leaveTime(totalTime);
})
// document.getElementById("wait").innerHTML = getTSATimes("ATL");
// console.log(fltnum);
}
function leaveTime(totalTime) {
fltnum = document.getElementById("fltnum").value;
getFltInfo(fltnum, function(res){
var departureTime = JSON.parse(res).flightStatusResponse.statusResponse.flightStatusTO.flightStatusLegTOList.departureLocalTimeScheduled;
d = Date.parse(departureTime);
//var d = new Date(+d);
tt = totalTime*60000;
t = new Date(d-tt);
if(t.getHours()>12) {
var h = t.getHours()-12;
var x = "PM";
} else {
h = t.getHours();
x = "AM";
}
if(t.getMinutes()<10) {
var m = "0"+t.getMinutes();
} else {
m = t.getMinutes();
}
document.getElementById("leaveTime").innerHTML = h+":"+m+" "+x;
})
}
function printFltTimes() {
fltnum = document.getElementById("fltnum").value;
getFltInfo(fltnum, function(res){
var departureTime = JSON.parse(res).flightStatusResponse.statusResponse.flightStatusTO.flightStatusLegTOList.departureLocalTimeScheduled;
var t = new Date(departureTime);
if(t.getHours()>12) {
var h = t.getHours()-12;
var x = "PM";
} else {
h = t.getHours();
x = "AM";
}
if(t.getMinutes()<10) {
var m = "0"+t.getMinutes();
} else {
m = t.getMinutes();
}
document.getElementById("departureTime").innerHTML = h+":"+m+" "+x;
})
}
function waitObjectToString(waitObject){
return waitObject.checkpointData.longname + " has a projected wait of " + waitObject.waitTime
}
function getDate(res){
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) {
dd='0'+dd
}
if(mm<10) {
mm='0'+mm
}
return yyyy + "-" + mm + "-" + dd;
}
//print current time
var d = new Date();
if(d.getHours()>12) {
var h = d.getHours()-12;
var x = "PM";
} else {
h = d.getHours();
x = "AM";
}
if(d.getMinutes()<10) {
var m = "0"+d.getMinutes();
} else {
m = d.getMinutes();
}
document.getElementById("time").innerHTML = h+":"+m+" "+x;
function getFltInfo(fltnum, callback){
var reqUrl = "https://demo30-test.apigee.net/v1/hack/status?flightNumber=" + fltnum +"&flightOriginDate=" + getDate() + "&apikey=" + apiKey;
httpGetAsync(reqUrl, callback);
}
///----Location stuff
document.getElementById("location").onclick = function() {
var x = document.getElementById("currentLocation");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(document.getElementById("address"));
console.log(navigator.geolocation);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
}
document.getElementById("location").onclick = function() {
return 1
}
function getTime(airport, callback){
//destination = airport+" airport";
destination = "ATL Airport"
mode = document.getElementById("method").value;
httpGetAsync('https://maps.googleapis.com/maps/api/directions/json?origin=' + encodeURI(location) + '&destination=' + encodeURI(destination) + '&key=' + gmapsApiKey + '&mode=' + mode + "?", callback);
}