/
webex.js
243 lines (171 loc) · 5.55 KB
/
webex.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Sending an HTTP Request from a NodeJS server
//const http = require("http");
const https = require("https");
scheduled_flights = [];
const Stream = require("stream").Transform;
const fs = require("fs");
//Schedules a flight
let ID = "N4278H ";
let est_dep = 1609715691;
/*(async () => {
await scheduleFlight(ID, est_dep, est_dep + 10800);
updateFlights();
})();*/
//print out the flights that have been scheduled
for(let a = 0; a < scheduled_flights.length; a++) {
console.log(scheduled_flights[a]);
}
// Private
function getData(ID_in, est_dep, est_arr, flight) {
return new Promise((resolve, reject) => {
let found = false;
let begin = flight.departure;
let end = begin + 7200;
//search through api for the flight
let url = "https://opensky-network.org/api/flights/all?";
url += ("begin=" + begin);
url += "&";
url += ("end=" + end);
https
.get(url, resp => {
let data = "";
// A chunk of data has been recieved.
resp.on("data", chunk => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on("end", () => {
let parsed = JSON.parse(data);
console.log(ID_in);
//loop through the list of api flights, search for the flight ID
for (let i = 0; i < parsed.length; i++) {
if (parsed[i].callsign == ID_in) {
// console.log(parsed[i].estDepartureAirport);
// console.log(parsed[i].estArrivalAirport);
flight.dep_airport = parsed[i].estDepartureAirport;
flight.arr_airport = parsed[i].estArrivalAirport;
// console.log(flight.dep_airport);
// console.log(flight.arr_airport);
resolve([true, flight]);
break;
}
}
resolve([false, null]);
})
.on("error", err => {
console.log("Error: " + err.message);
reject(err);
});
});
});
}
function getHi(ID_in, est_dep, est_arr) {
console.log("hi");
}
//Creates flight object given a flight ID and an estimated depature time in UNIX time
//Adds flight object to array of flights (scheduled flights)
async function scheduleFlight(ID_in, est_dep, est_arr, displayName) {
let flight = {
ID: ID_in,
departure: est_dep,
arrival: est_arr,
name: displayName,
arr_airport: "",
dep_airport: ""
}/*
let found = false;
let begin = est_dep;
let end = est_dep + 7200;
//search through api for the flight
let url = "https://opensky-network.org/api/flights/all?";
url += ("begin=" + begin);
url += "&";
url += ("end=" + end);
console.log(url);
https
.get(url, resp => {
let data = "";
// A chunk of data has been recieved.
resp.on("data", chunk => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on("end", () => {
let parsed = JSON.parse(data);
//loop through the list of api flights, search for the flight ID
for (let i = 0; i < parsed.length; i++) {
if (parsed[i].callsign == ID) {
found = true;
console.log("found");
break;
}
}
})
.on("error", err => {
console.log("Error: " + err.message);
});*/
/*getData(ID_in, est_dep, est_arr)
.then((found) => console.log(found))
.catch((err) => console.log(err));*/
return new Promise((resolve, reject) => {
getData(ID_in, est_dep, est_arr, flight)
.then(found => {
if (found[0]) return resolve(found[1]);
reject();
})
.catch(err => reject(err));
});
//Add flight object to the array of flights
/*if(found) {
scheduled_flights.push(flight);
}else {
console.log("Flight ID: " + flight.ID + " not found");
}*/
}
//after adding flights, everytime user wants to see schedule, update is called
//makes sure that all flight times are "live"
function updateFlights() {
console.log(scheduled_flights.length);
for (let i = 0; i < scheduled_flights.length; i++) {
let found = false;
let url = "https://opensky-network.org/api/flights/all?";
url += ("begin=" + scheduled_flights[i].departure);
url += "&";
url += ("end=" + scheduled_flights[i].arrival);
https
.get(url, resp => {
let data = "";
// A chunk of data has been recieved.
resp.on("data", chunk => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on("end", () => {
let parsed = JSON.parse(data);
//loop through the list of api flights, search for the flight ID
for (let i = 0; i < parsed.length; i++) {
if (parsed[i].callsign == scheduled_flights[i].ID) {
found = true;
scheduled_flights[i].arrival = parsed[i].firstSeen;
scheduled_flights[i].departure = parsed[i].lastSeen;
break;
}
}
if (!found) {
scheduled_flights.splice(i, 1);
//item not there, remove it from schedule
//handles case where flight is missing
}
})
.on("error", err => {
console.log("Error: " + err.message);
});
//Add flight object to the array of flights
})
}
}
module.exports = {
scheduleFlight,
updateFlights,
scheduled_flights
}