-
Notifications
You must be signed in to change notification settings - Fork 1
/
livedata.html
709 lines (666 loc) · 23.6 KB
/
livedata.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
<!DOCTYPE html>
<html lang="en">
<head>
<title>
livedata - FG Tracker API = v2
</title>
<meta charset="utf-8">
<meta name="viewport"
content="width=device-width,user-scalable=no,minimum-scale=1.0,maximum-scale=1.0">
<script type="text/javascript"
src="js/jquery-2.0.3.js">
</script>
<script src="js/OpenLayers.js">
</script>
<script src="js/geodesic.js"
type="text/javascript">
</script>
<script src="js/utils.js"
type="text/javascript">
</script>
<link rel="stylesheet"
href="css/livedata.css"
type="text/css">
<style>
#wrapper{
position: relative;
top: 1em;
/* bottom: 22em; */
left: 2em;
z-index: 1000;
border: 0px solid #000000;
}
#header_flights {
position: absolute;
color: #ffffff;
background: #999999 ;/*url('images/background_trans.png');*/
/* width: 393px; */
width: 95%;
bottom: 0px;
line-height: 0px;
}
#header_flights td {
font-family: "DejaVuSansMonoBook", monospace;
font-size: 10px;
padding-right: 6px;
padding-left: 14px;
color: #ffffff;
}
#content_flights {
position: absolute;
background: url('images/background_trans.png');
padding-left: 15px;
padding-top: 5px;
padding-right: 5px;
overflow-y: scroll;
overflow-x: hidden;
height: 550px;
width: 94%;
}
#flights {
border-bottom: 1px solid #CCC;
padding: 0;
border: 0;
}
#flights td {
font-family: "DejaVuSansMonoBook", monospace;
font-size: 12px;
padding-right: 10px;
line-height: 0px;
}
.fid,.callsign,.time,.cs,.lon,.lat,.hdg,.alt_ft,.spd_kts,.model,.dist {
width: 180px;
border-top: 1px solid #CCCCCC;
}
/* align numbers to right */
.fid, .time, .lon, .lat, .hdg, .alt_ft, .spd_kts, .dist {
text-align:right;
}
/* text left padding */
.cs,.model {
padding-left: 2em;
}
.red {
color: #ff0000;
}
.gray666 {
color: #666666;
}
.grayCCC {
color: #CCCCCC;
}
.back_grayCCC {
background: #CCCCCC;
}
.back_yellowFF0 {
background: #FFFF00;
}
.back_greenB7F {
background: #b7f846;
}
.hdg_n {
background: url('images/plane_black_10x10n.png') no-repeat left center;
}
.hdg_ne {
background: url('images/plane_black_10x10ne.png') no-repeat left center;
}
.hdg_e {
background: url('images/plane_black_10x10e.png') no-repeat left center;
}
.hdg_se {
background: url('images/plane_black_10x10se.png') no-repeat left center;
}
.hdg_s {
background: url('images/plane_black_10x10s.png') no-repeat left center;
}
.hdg_sw {
background: url('images/plane_black_10x10sw.png') no-repeat left center;
}
.hdg_w {
background: url('images/plane_black_10x10w.png') no-repeat left center;
}
.hdg_nw {
background: url('images/plane_black_10x10nw.png') no-repeat left center;
}
#status_line {
position: absolute;
background: url('images/background_trans.png');
font-family: "DejaVuSansMonoBook", monospace;
font-size: 12px;
padding-left: 1em;
bottom: 0px;
height: 30px;
line-height: 30px;
width: 95%;
vertical-align: middle;
border: 1px solid #F00;
}
</style>
<script>
var use_proxy = true;
var live_to = 10000; // refresh each 10 seconds
var tr_url = 'proxy/livedata.php';
var yh_url = 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fmpserver15.flightgear.org%2Fmodules%2Ffgtracker%2Finterface.php%3Faction%3Dlivewaypoints%22&format=json&callback=';
var bgn_req;
var add_heading_img = true;
var currCount = 0;
var info_line = '';
var atcCount = 0;
var maxCount = 0;
var open_flights = [];
var removal = [];
var selected = []; // mouse selected flights
var mps2knots = 1.94384617;
var max_txt = 100; // limit text to console
var tableheader = "<table>"+
"<tr class='table_header'>"+
"<td class='fid'><p>FID<\/p><\/td>"+
"<td class='time' style='display: none'><p>Time<\/p><\/td>"+
"<td class='callsign'><p>Callsign<\/p><\/td>"+
"<td class='lon'><p>Longitude<\/p><\/td>"+
"<td class='lat'><p>Latitude<\/p><\/td>"+
"<td class='hdg'><p>Heading<\/p><\/td>"+
"<td class='alt_ft'><p>Altitude (ft)<\/p><\/td>"+
"<td class='spd_kts'><p>Speed (kt)<\/p><\/td>"+
"<td class='model'><p>Model<\/p><\/td>"+
"<td class='dist'><p>Dist (km)<\/p><\/td>"+
"<\/tr>";
$(document).ready(function() {
$(tableheader).appendTo('#header_flights')
$("<table cellspacing='0' cellpadding='0' id='flights' />")
.appendTo("#content_flights");
$('#flights').hide().show(300);
// $("#info_line").html(info_line);
});
function init(lon,lat,zoom,icao) { // init the map, etc
}
function tr_flight(cs,fid,model,lat,lon,alt,time) {
this.callsign = cs;
this.fid = fid;
this.model = model;
this.lat = lat;
this.lon = lon;
this.alt = parseInt(alt);
this.hdg = 0;
this.spd_kts = 0;
this.waypoints = [];
this.time_raw = time;
this.time_last = time;
this.status = 'OPEN';
this.dist = 0;
this.secs = 0;
}
//////////////////////////////////////////////////////////////////////////////////////
// DISPLAY OF DATA
// ===============
function addFlight(flight) {
// Add flight to the map
//var fid = flight.children('.fid').text()
//var callsign = flight.children('.callsign').text()
//var lon = flight.children('.lon').text()
//var lat = flight.children('.lat').text()
//var hdg = flight.children('.hdg').text()
//var spd_kts = flight.children('.spd_kts').text()
//var model = get_model(flight.children('.model').text());
//addPlanes(fid, callsign, lon, lat, hdg, spd_kts, model)
//addLabels(fid, callsign, lon, lat)
// Add flight to the table
$('#flights').append(flight);
}
function removeFlights() {
var delCnt = 0;
// Remove features from table
$.each(removal, function(i, val) {
$.each($('.flightrow'), function(i, row) {
if (($(row).get(0).id) == val) {
var fid = $(row).get(0).id;
// FIX: 20131103 - check and remove 'fid' from selected, if in selected
for(var j = selected.length-1; j >= 0; j--) {
if (selected[j] == fid) {
selected.splice(j,1); // remove item
}
}
$(row).hide(1000, function(){ $(row).remove(); });
delCnt++;
}
});
});
// Remove features from the map
//$.each(removal, function(i, val) {
// removePlanes(val)
//});
return delCnt;
}
// various guesses that it is ATC
function is_atc( callsign, model ) {
if (model == 'OpenRadar') return true;
var mod = model.toUpperCase();
var n = mod.indexOf('ATC');
if (n >= 0) return true;
n = callsign.indexOf('ATC');
if (n >= 0) return true;
n = callsign.indexOf('TWR');
if (n >= 0) return true;
return false;
}
// Check number between
function between(x, min, max) {
return x >= min && x <= max;
}
var tr_first = 1;
function tr_addFlights(msg) {
var flt;
var flightsdata = [];
var datafids = [];
var flightshere = [];
var newCnt = 0;
var updCnt = 0;
var remCnt = 0;
var delCnt = 0;
var alt,hfid;
removal = [];
var i, max = open_flights.length;
for (i = 0; i < max; i++) {
flt = open_flights[i];
if (flt.alt < 5000) {
alt = parseInt(flt.alt);
} else {
alt = 'FL' + parseInt(flt.alt / 100);
}
hfid = '<a href="tracker2.html?fid='+flt.fid;
if (use_proxy) {
hfid += '&f=p';
} else {
hfid += '&f=y';
}
hfid += '" target="_blank">'+flt.fid+'</a>';
var flightrow = "<tr class='flightrow' id='"+flt.fid+"'>"+
"<td class='fid'><p>"+hfid+
"<td class='time' style='display: none'><p>"+flt.secs+
"<td class='callsign'><p>"+flt.callsign+
"<td class='lon'><p>"+flt.lon+
"<td class='lat'><p>"+flt.lat+
"<td class='hdg'><p>"+flt.hdg+
"<td class='alt_ft'><p>"+alt+
"<td class='spd_kts'><p>"+parseInt(flt.spd_kts)+
"<td class='model'><p>"+flt.model+
"<td class='dist'><p>"+flt.dist.toFixed(2)+
"<\/tr>"
flightsdata.push($(flightrow))
//datafids.push(JSON.stringify(flt.fid))
datafids.push(parseInt(flt.fid))
} // end each flight
// We start to draw only when document is fully loaded
$(document).ready(function() {
// Getting all the flights already on screen
$('.flightrow').each( function() {
flightshere.push($(this).children('.fid').text())
});
if (tr_first > 0) {
tr_first = 0;
$.each(flightsdata, function () {
var callsign = $(this).children('.callsign').text();
var model = $(this).children('.model').text();
var an_ATC = is_atc(callsign,model);
if (an_ATC) {
$(this).addClass('back_yellowFF0');
atcCount++;
//} else if (spd_kts < 10) {
// $(this).addClass('gray666');
}
addFlight($(this));
newCnt++;
});
} else {
// not first
// ADD OR UPDATE: Not first load, so append only what's not already here
atcCount = 0; // start ATC (guess) counter
$.each(flightsdata, function(i) {
var checkfidel = $(this).children('.fid').text();
if ($.inArray(checkfidel, selected) >= 0) {
$(this).addClass('back_greenB7F');
}else{
$(this).removeClass('back_greenB7F');
}
// Update vars
var checkfid = $(this).children('.fid').text();
var callsign = $(this).children('.callsign').text();
var lat = $(this).children('.lat').text();
var lon = $(this).children('.lon').text();
var hdg = $(this).children('.hdg').text();
var spd_kts = $(this).children('.spd_kts').text();
var model = $(this).children('.model').text();
var an_ATC = is_atc(callsign,model);
if (an_ATC) {
$(this).addClass('back_yellowFF0');
atcCount++;
} else if (spd_kts < 10) {
$(this).addClass('gray666');
} else {
$(this).removeClass('gray666');
}
// Update check
if ($.inArray(checkfidel, flightshere) > -1) {
// Update the map
// For openlayers we have to change lat/lon order, grmbl
//var nhdg = updatePlanes(checkfid, callsign, lon, lat, hdg, spd_kts, model);
// if got a better heading
//if (nhdg != hdg) {
// // how to update 'this'
// var tmp = $(this).children('.hdg');
// tmp.innerText = nhdg;
// //$(this).hdg = nhdg;
//}
// Update table
var checkfid2 = "\[id=\""+checkfid+"\"\]";
$(checkfid2).replaceWith($(this));
updCnt++;
} else {
//console.log("Flight to add: "+flightsdata[i].children('.callsign').text())
// TODO: If a column sort is active, sortcolumn.length, then should
// add this at the correct place in the list, using maybe
// insertBefore, rather than just append it...
addFlight($(this));
newCnt++;
}
});
// REMOVE: Have a look what needs to be removed and add it to the removal array
$('.fid').each( function() {
var fidrem = parseInt($(this).children().text());
if (fidrem > 0) {
if ($.inArray(fidrem, datafids) < 0) {
removal.push(fidrem);
remCnt++;
}
}
});
delCnt = removeFlights();
} // first time y/n
var dt = new Date();
info_line = dt.toUTCString();
currCount = flightsdata.length;
info_line += " "+msg+" Active " + currCount
if (atcCount > 0) {
info_line += " (ATC " + atcCount + ")";
}
if (currCount > maxCount) {
maxCount = currCount;
}
if (newCnt > 0)
info_line += " new " + newCnt;
if (updCnt > 0)
info_line += " upd " + updCnt;
if (remCnt > 0)
info_line += " rem " + remCnt;
if (delCnt > 0)
info_line += " del " + delCnt;
if (maxCount > currCount)
info_line += " (max " + maxCount + ")";
//info_line += ' z='+curr_zoom;
//msg += info_line;
//setStatus(msg);
$("#status_line").html(info_line);
console.log(info_line);
// ========================
// Showing the heading with background image
if (add_heading_img) {
$('.hdg').each( function() {
var value = $(this).children('p').text()
if (between(value,0,22) || between(value,293,360)) {
$(this).addClass('hdg_n')
} else if (between(value,23,67)) {
$(this).addClass('hdg_ne')
} else if (between(value,68,112)) {
$(this).addClass('hdg_e')
} else if (between(value,113,157)) {
$(this).addClass('hdg_se')
} else if (between(value,158,202)) {
$(this).addClass('hdg_s')
} else if (between(value,203,247)) {
$(this).addClass('hdg_sw')
} else if (between(value,248,292)) {
$(this).addClass('hdg_w')
}
});
}
$('.flightrow').hover(
function() {
$(this).addClass('back_grayCCC')
},
function() {
$(this).removeClass('back_grayCCC')
}
);
// Select a flight in the list
$('.flightrow').click(
function() {
var check = $(this).children('.fid').text();
if ($.inArray(check, selected) < 0) {
$(this).addClass('back_greenB7F');
selected.push(check);
// zoomToBoundsSelected(selected);
} else {
$(this).removeClass('back_greenB7F');
var index = selected.indexOf($(this).children('.fid').text());
selected.splice(index,1);
$(this).removeClass('back_greenB7F');
}
} // function
); // CLICK FUNCTION
}); // document ready
}
function search_fid(fid, array) {
var i, len = array.length;
for ( i = 0; i < len; i++) {
if (array[i].fid === fid) {
return i;
}
}
return -1;
}
function collectData(data) {
var msg;
var dato = data.data;
var wpta = dato.wpt;
var max = wpta.length;
var i,obj,fid,stat,cs,alt,pair,index;
var model,lat,lon,flt,closed,ind,dist,last;
var callsigns = [];
var flights = [];
closed = 0;
for (i = 0; i < max; i++) {
obj = wpta[i];
fid = obj.flight_id;
stat = obj.current_status;
cs = obj.callsign;
alt = obj.alt;
pair = cs + fid;
index = callsigns.indexOf( pair );
if (index < 0) {
model = obj.model;
lat = obj.lat;
lon = obj.lon;
time = obj.time_raw;
callsigns.push(pair);
ind = search_fid(fid, open_flights);
if (ind >= 0) {
last = open_flights[ind].time_last; // get the last time
}
flt = new tr_flight(cs,fid,model,lat,lon,alt,time);
flt.waypoints.push(obj);
if (ind >= 0) {
// accumulate distance from last position to new position
dist = getDistance( open_flights[ind].lat, open_flights[ind].lon, lat, lon );
flt.dist += dist;
flt.time_raw = open_flights[ind].time_raw; // return time to original start time
flt.secs = time - flt.time_raw; // current minus first time
flt.time_last = time;
if ((dist > 0.001) && (time > last)) {
flt.spd_kts = ((dist * 1000) / (time - last)) * mps2knots; // meters per second
} else {
flt.spd_kts = 0;
}
}
flights.push(flt);
index = callsigns.indexOf( pair );
} else {
flt = flights[index];
flt.waypoints.push(obj);
}
if (stat != 'OPEN') {
if (index >= 0) {
if (flights[index].status != 'CLOSED') {
closed++;
flights[index].status = 'CLOSED'
}
}
}
}
max = flights.length;
var wpta,wpcnt,ocnt,j,wpts,tmr,lastrt,k;
var plat,plon,dist;
wpcnt = 0;
ocnt = 0;
///////////////////////////////////////////////////////////////////
open_flights = []; // trash last open_flights, and build a new set
// including ONLY open flights
for (i = 0; i < max; i++) {
flt = flights[i];
if (flt.status != 'OPEN') {
continue;
}
wpta = flt.waypoints;
wpts = wpta.length;
wpcnt += wpts;
ocnt++;
for (j = 0; j < wpts; j++) {
obj = wpta[j];
fid = obj.flight_id;
alt = obj.alt;
lat = obj.lat;
lon = obj.lon;
tmr = obj.time_raw;
if (j == 0) {
lastrt = tmr;
k = j;
} else {
if (tmr > lastrt) {
msg = "Out of order time "+lastrt+" at "+k+", now "+tmr+" at "+j;
console.log(msg);
k = j;
lastrt = tmr;
}
}
if (j == 1) {
dist = getDistance(plat,plon,lat,lon);
if (dist > 0.0) {
flt.hdg = getBearing(plat,plon,lat,lon);
}
}
plat = lat;
plon = lon;
}
open_flights.push(flt);
}
var elap = getElapsed(bgn_req);
msg = "Data: flights "+max+", open "+ocnt+", wpts "+wpcnt+" elap: "+elap;
//setStatus(msg);
console.log(msg);
tr_addFlights(msg);
}
function CB_livedata() {
if ((this.readyState == 4) && (this.status == "200")) {
var msg,text,file,data;
var dnerr = false;
var lto = live_to;
text = this.responseText;
if (use_proxy) {
data = JSON.parse(text);
} else {
try {
file = JSON.parse(text);
data = file.query.results.body.p;
data = JSON.parse( data );
} catch( err ) {
msg = "Got error: "+err.message;
console.log(msg);
//setStatus(msg);
if (text.length > max_txt) {
text = text.substr(0,max_txt);
}
msg = " data: "+text;
console.log(msg);
dnerr = true;
lto = 55;
}
}
if (data && data.data && data.data.wpt) {
collectData(data);
} else if (!dnerr) {
msg = "Invalid data!"
console.log(msg);
if (text.length > max_txt) {
text = text.substr(0,max_txt);
}
msg = " data: "+text;
console.log(msg);
ltp = 55;
}
if (use_proxy) {
setTimeout("sendRequest2('"+tr_url+"')", lto);
} else {
setTimeout("sendRequest2('"+yh_url+"')", lto);
}
}
}
function sendRequest2(url) {
bgn_req = Date.now();
getUrlwithCB( url, CB_livedata ); // CB_JsonLive
}
</script>
</head>
<body>
<div align="center">
<p align="center">livedata - FG Tracker API - v2</p>
</div>
<div id="wrapper">
<div id="header_flights">
</div>
<div id="content_flights">
</div>
</div>
<div id="status_line">
status:
</div>
<script type="text/javascript"
defer="defer">
var fetch = gup('f');
if (fetch.length) {
if (fetch == 'p') {
use_proxy = true;
console.log("Set to use proxy php/curl");
} else if (fetch == 'y') {
use_proxy = false;
console.log("Set to use Yahoo! API");
}
}
info_line = 'Fetching livedata... ';
if (use_proxy) {
info_line += 'Using '+tr_url;
sendRequest2(tr_url); // get the FGTracker livewaypoints
} else {
info_line += 'Using Yahoo!';
sendRequest2(yh_url); // get the FGTracker livewaypoints
}
info_line += ' Normally takes 1/2 secs...';
$("#status_line").html(info_line);
var lat = gup('lat');
var lon = gup('lon');
var zoom = gup('zoom');
var icao = gup('icao');
init(lon,lat,zoom,icao); // init - but no map here...
</script>
<!-- 20140910 - Get rid the header which just take space - use paragraph -->
<!-- 20140910 - Add a link to tracker2.html to follow a flight on the map -->
</body>
</html>