Debian Codegraph @

public_html/planeObject.js

raw
1 "use strict";
2
3 function PlaneObject(icao) {
4 // Info about the plane
5 this.icao = icao;
6 this.icaorange = findICAORange(icao);
7 this.flight = null;
8 this.squawk = null;
9 this.selected = false;
10 this.category = null;
11
12 // Basic location information
13 this.altitude = null;
14 this.speed = null;
15 this.track = null;
16 this.prev_position = null;
17 this.position = null;
18 this.position_from_mlat = false
19 this.sitedist = null;
20
21 // Data packet numbers
22 this.messages = null;
23 this.rssi = null;
24
25 // Track history as a series of line segments
26 this.track_linesegs = [];
27 this.history_size = 0;
28
29 // When was this last updated (receiver timestamp)
30 this.last_message_time = null;
31 this.last_position_time = null;
32
33 // When was this last updated (seconds before last update)
34 this.seen = null;
35 this.seen_pos = null;
36
37 // Display info
38 this.visible = true;
39 this.marker = null;
40 this.markerStyle = null;
41 this.markerIcon = null;
42 this.markerStaticStyle = null;
43 this.markerStaticIcon = null;
44 this.markerStyleKey = null;
45 this.markerSvgKey = null;
46
47 // start from a computed registration, let the DB override it
48 // if it has something else.
49 this.registration = registration_from_hexid(this.icao);
50 this.icaotype = null;
51
52 // request metadata
53 getAircraftData(this.icao).done(function(data) {
54 if ("r" in data) {
55 this.registration = data.r;
56 }
57
58 if ("t" in data) {
59 this.icaotype = data.t;
60 }
61
62 if (this.selected) {
63 refreshSelected();
64 }
65 }.bind(this));
66 }
67
68 // Appends data to the running track so we can get a visual tail on the plane
69 // Only useful for a long running browser session.
70 PlaneObject.prototype.updateTrack = function(estimate_time) {
71 if (!this.position)
72 return false;
73 if (this.position == this.prev_position)
74 return false;
75
76 var projHere = ol.proj.fromLonLat(this.position);
77 var projPrev;
78 if (this.prev_position === null) {
79 projPrev = projHere;
80 } else {
81 projPrev = ol.proj.fromLonLat(this.prev_position);
82 }
83
84 this.prev_position = this.position;
85
86 if (this.track_linesegs.length == 0) {
87 // Brand new track
88 //console.log(this.icao + " new track");
89 var newseg = { fixed: new ol.geom.LineString([projHere]),
90 feature: null,
91 head_update: this.last_position_time,
92 tail_update: this.last_position_time,
93 estimated: false,
94 ground: (this.altitude === "ground")
95 };
96 this.track_linesegs.push(newseg);
97 this.history_size ++;
98 return;
99 }
100
101 var lastseg = this.track_linesegs[this.track_linesegs.length - 1];
102 var elapsed = (this.last_position_time - lastseg.head_update);
103
104 var est_track = (elapsed > estimate_time);
105 var ground_track = (this.altitude === "ground");
106
107 if (est_track) {
108
109 if (!lastseg.estimated) {
110 // >5s gap in data, create a new estimated segment
111 //console.log(this.icao + " switching to estimated");
112 lastseg.fixed.appendCoordinate(projPrev);
113 this.track_linesegs.push({ fixed: new ol.geom.LineString([projPrev, projHere]),
114 feature: null,
115 head_update: this.last_position_time,
116 estimated: true });
117 this.history_size += 2;
118 } else {
119 // Keep appending to the existing dashed line; keep every point
120 lastseg.fixed.appendCoordinate(projPrev);
121 lastseg.head_update = this.last_position_time;
122 this.history_size++;
123 }
124
125 return true;
126 }
127
128 if (lastseg.estimated) {
129 // We are back to good data (we got two points close in time), switch back to
130 // solid lines.
131 lastseg = { fixed: new ol.geom.LineString([projPrev]),
132 feature: null,
133 head_update: this.last_position_time,
134 tail_update: this.last_position_time,
135 estimated: false,
136 ground: (this.altitude === "ground") };
137 this.track_linesegs.push(lastseg);
138 this.history_size ++;
139 // continue
140 }
141
142 if ( (lastseg.ground && this.altitude !== "ground") ||
143 (!lastseg.ground && this.altitude === "ground") ) {
144 //console.log(this.icao + " ground state changed");
145 // Create a new segment as the ground state changed.
146 // assume the state changed halfway between the two points
147 // FIXME needs reimplementing post-google
148
149 lastseg.fixed.appendCoordinate(projPrev);
150 this.track_linesegs.push({ fixed: new ol.geom.LineString([projPrev, projHere]),
151 feature: null,
152 head_update: this.last_position_time,
153 tail_update: this.last_position_time,
154 estimated: false,
155 ground: (this.altitude === "ground") });
156 this.history_size += 3;
157 return true;
158 }
159
160 // Add more data to the existing track.
161 // We only retain some historical points, at 5+ second intervals,
162 // plus the most recent point
163 if (this.last_position_time - lastseg.tail_update >= 5) {
164 // enough time has elapsed; retain the last point and add a new one
165 //console.log(this.icao + " retain last point");
166 lastseg.fixed.appendCoordinate(projHere);
167 lastseg.tail_update = lastseg.head_update;
168 this.history_size ++;
169 }
170
171 lastseg.head_update = this.last_position_time;
172 return true;
173 };
174
175 // This is to remove the line from the screen if we deselect the plane
176 PlaneObject.prototype.clearLines = function() {
177 for (var i = this.track_linesegs.length - 1; i >= 0 ; --i) {
178 var seg = this.track_linesegs[i];
179 if (seg.feature !== null) {
180 PlaneTrailFeatures.remove(seg.feature);
181 seg.feature = null;
182 }
183 }
184 };
185
186 PlaneObject.prototype.getMarkerColor = function() {
187 // Emergency squawks override everything else
188 if (this.squawk in SpecialSquawks)
189 return SpecialSquawks[this.squawk].markerColor;
190
191 var h, s, l;
192
193 if (this.altitude === null) {
194 h = ColorByAlt.unknown.h;
195 s = ColorByAlt.unknown.s;
196 l = ColorByAlt.unknown.l;
197 } else if (this.altitude === "ground") {
198 h = ColorByAlt.ground.h;
199 s = ColorByAlt.ground.s;
200 l = ColorByAlt.ground.l;
201 } else {
202 s = ColorByAlt.air.s;
203 l = ColorByAlt.air.l;
204
205 // find the pair of points the current altitude lies between,
206 // and interpolate the hue between those points
207 var hpoints = ColorByAlt.air.h;
208 h = hpoints[0].val;
209 for (var i = hpoints.length-1; i >= 0; --i) {
210 if (this.altitude > hpoints[i].alt) {
211 if (i == hpoints.length-1) {
212 h = hpoints[i].val;
213 } else {
214 h = hpoints[i].val + (hpoints[i+1].val - hpoints[i].val) * (this.altitude - hpoints[i].alt) / (hpoints[i+1].alt - hpoints[i].alt)
215 }
216 break;
217 }
218 }
219 }
220
221 // If we have not seen a recent position update, change color
222 if (this.seen_pos > 15) {
223 h += ColorByAlt.stale.h;
224 s += ColorByAlt.stale.s;
225 l += ColorByAlt.stale.l;
226 }
227
228 // If this marker is selected, change color
229 if (this.selected){
230 h += ColorByAlt.selected.h;
231 s += ColorByAlt.selected.s;
232 l += ColorByAlt.selected.l;
233 }
234
235 // If this marker is a mlat position, change color
236 if (this.position_from_mlat) {
237 h += ColorByAlt.mlat.h;
238 s += ColorByAlt.mlat.s;
239 l += ColorByAlt.mlat.l;
240 }
241
242 if (h < 0) {
243 h = (h % 360) + 360;
244 } else if (h >= 360) {
245 h = h % 360;
246 }
247
248 if (s < 5) s = 5;
249 else if (s > 95) s = 95;
250
251 if (l < 5) l = 5;
252 else if (l > 95) l = 95;
253
254 return 'hsl(' + (h/5).toFixed(0)*5 + ',' + (s/5).toFixed(0)*5 + '%,' + (l/5).toFixed(0)*5 + '%)'
255 }
256
257 PlaneObject.prototype.updateIcon = function() {
258 var col = this.getMarkerColor();
259 var opacity = (this.position_from_mlat ? 0.75 : 1.0);
260 var outline = (this.position_from_mlat ? OutlineMlatColor : OutlineADSBColor);
261 var baseMarker = getBaseMarker(this.category, this.icaotype);
262 var weight = ((this.selected ? 2 : 1) / baseMarker.scale).toFixed(1);
263 var rotation = (this.track === null ? 0 : this.track);
264
265 var svgKey = col + '!' + outline + '!' + baseMarker.key + '!' + weight;
266 var styleKey = opacity + '!' + rotation;
267
268 if (this.markerStyle === null || this.markerIcon === null || this.markerSvgKey != svgKey) {
269 //console.log(this.icao + " new icon and style " + this.markerSvgKey + " -> " + svgKey);
270
271 var icon = new ol.style.Icon({
272 anchor: baseMarker.anchor,
273 anchorXUnits: 'pixels',
274 anchorYUnits: 'pixels',
275 scale: baseMarker.scale,
276 imgSize: baseMarker.size,
277 src: svgPathToURI(baseMarker.path, baseMarker.size, outline, weight, col),
278 rotation: (baseMarker.noRotate ? 0 : rotation * Math.PI / 180.0),
279 opacity: opacity,
280 rotateWithView: (baseMarker.noRotate ? false : true)
281 });
282
283 if (baseMarker.noRotate) {
284 // the base marker won't be rotated
285 this.markerStaticIcon = icon;
286 this.markerStaticStyle = new ol.style.Style({
287 image: this.markerStaticIcon
288 });
289
290 // create an arrow that we will rotate around the base marker
291 // to indicate heading
292
293 var offset = baseMarker.markerRadius * baseMarker.scale + 6;
294 var size = offset * 2;
295
296 var arrowPath = "M " + offset + ",0 m 4,4 -8,0 4,-4 z";
297 this.markerIcon = new ol.style.Icon({
298 anchor: [offset, offset],
299 anchorXUnits: 'pixels',
300 anchorYUnits: 'pixels',
301 scale: 1.0,
302 imgSize: [size, size],
303 src: svgPathToURI(arrowPath, [size, size], outline, 1, outline),
304 rotation: rotation * Math.PI / 180.0,
305 opacity: opacity,
306 rotateWithView: true
307 });
308 this.markerStyle = new ol.style.Style({
309 image: this.markerIcon
310 });
311 } else {
312 this.markerIcon = icon;
313 this.markerStyle = new ol.style.Style({
314 image: this.markerIcon
315 });
316 this.markerStaticIcon = null;
317 this.markerStaticStyle = new ol.style.Style({});
318 }
319
320 this.markerStyleKey = styleKey;
321 this.markerSvgKey = svgKey;
322
323 if (this.marker !== null) {
324 this.marker.setStyle(this.markerStyle);
325 this.markerStatic.setStyle(this.markerStaticStyle);
326 }
327 }
328
329 if (this.markerStyleKey != styleKey) {
330 //console.log(this.icao + " new rotation");
331 this.markerIcon.setRotation(rotation * Math.PI / 180.0);
332 this.markerIcon.setOpacity(opacity);
333 if (this.staticIcon) {
334 this.staticIcon.setOpacity(opacity);
335 }
336 this.markerStyleKey = styleKey;
337 }
338
339 return true;
340 };
341
342 // Update our data
343 PlaneObject.prototype.updateData = function(receiver_timestamp, data) {
344 // Update all of our data
345 this.messages = data.messages;
346 this.rssi = data.rssi;
347 this.last_message_time = receiver_timestamp - data.seen;
348
349 if (typeof data.altitude !== "undefined")
350 this.altitude = data.altitude;
351 if (typeof data.vert_rate !== "undefined")
352 this.vert_rate = data.vert_rate;
353 if (typeof data.speed !== "undefined")
354 this.speed = data.speed;
355 if (typeof data.track !== "undefined")
356 this.track = data.track;
357 if (typeof data.lat !== "undefined") {
358 this.position = [data.lon, data.lat];
359 this.last_position_time = receiver_timestamp - data.seen_pos;
360
361 if (SitePosition !== null) {
362 var WGS84 = new ol.Sphere(6378137);
363 this.sitedist = WGS84.haversineDistance(SitePosition, this.position);
364 }
365
366 this.position_from_mlat = false;
367 if (typeof data.mlat !== "undefined") {
368 for (var i = 0; i < data.mlat.length; ++i) {
369 if (data.mlat[i] === "lat" || data.mlat[i] == "lon") {
370 this.position_from_mlat = true;
371 break;
372 }
373 }
374 }
375 }
376 if (typeof data.flight !== "undefined")
377 this.flight = data.flight;
378 if (typeof data.squawk !== "undefined")
379 this.squawk = data.squawk;
380 if (typeof data.category !== "undefined")
381 this.category = data.category;
382 };
383
384 PlaneObject.prototype.updateTick = function(receiver_timestamp, last_timestamp) {
385 // recompute seen and seen_pos
386 this.seen = receiver_timestamp - this.last_message_time;
387 this.seen_pos = (this.last_position_time === null ? null : receiver_timestamp - this.last_position_time);
388
389 // If no packet in over 58 seconds, clear the plane.
390 if (this.seen > 58) {
391 if (this.visible) {
392 //console.log("hiding " + this.icao);
393 this.clearMarker();
394 this.visible = false;
395 if (SelectedPlane == this.icao)
396 selectPlaneByHex(null,false);
397 }
398 } else {
399 this.visible = true;
400 if (this.position !== null && (this.selected || this.seen_pos < 60)) {
401 if (this.updateTrack(receiver_timestamp - last_timestamp + (this.position_from_mlat ? 30 : 5))) {
402 this.updateLines();
403 this.updateMarker(true);
404 } else {
405 this.updateMarker(false); // didn't move
406 }
407 } else {
408 this.clearMarker();
409 }
410 }
411 };
412
413 PlaneObject.prototype.clearMarker = function() {
414 if (this.marker) {
415 PlaneIconFeatures.remove(this.marker);
416 PlaneIconFeatures.remove(this.markerStatic);
417 /* FIXME google.maps.event.clearListeners(this.marker, 'click'); */
418 this.marker = this.markerStatic = null;
419 }
420 };
421
422 // Update our marker on the map
423 PlaneObject.prototype.updateMarker = function(moved) {
424 if (!this.visible || this.position == null) {
425 this.clearMarker();
426 return;
427 }
428
429 this.updateIcon();
430 if (this.marker) {
431 if (moved) {
432 this.marker.setGeometry(new ol.geom.Point(ol.proj.fromLonLat(this.position)));
433 this.markerStatic.setGeometry(new ol.geom.Point(ol.proj.fromLonLat(this.position)));
434 }
435 } else {
436 this.marker = new ol.Feature(new ol.geom.Point(ol.proj.fromLonLat(this.position)));
437 this.marker.hex = this.icao;
438 this.marker.setStyle(this.markerStyle);
439 PlaneIconFeatures.push(this.marker);
440
441 this.markerStatic = new ol.Feature(new ol.geom.Point(ol.proj.fromLonLat(this.position)));
442 this.markerStatic.hex = this.icao;
443 this.markerStatic.setStyle(this.markerStaticStyle);
444 PlaneIconFeatures.push(this.markerStatic);
445 }
446 };
447
448 // Update our planes tail line,
449 PlaneObject.prototype.updateLines = function() {
450 if (!this.selected)
451 return;
452
453 if (this.track_linesegs.length == 0)
454 return;
455
456 var estimateStyle = new ol.style.Style({
457 stroke: new ol.style.Stroke({
458 color: '#a08080',
459 width: 1.5,
460 lineDash: [3, 3]
461 })
462 });
463
464 var airStyle = new ol.style.Style({
465 stroke: new ol.style.Stroke({
466 color: '#000000',
467 width: 2
468 })
469 });
470
471 var groundStyle = new ol.style.Style({
472 stroke: new ol.style.Stroke({
473 color: '#408040',
474 width: 2
475 })
476 });
477
478 // create the new latest-position line
479 var lastseg = this.track_linesegs[this.track_linesegs.length - 1];
480 var lastfixed = lastseg.fixed.getCoordinateAt(1.0);
481 var geom = new ol.geom.LineString([lastfixed, ol.proj.fromLonLat(this.position)]);
482 var feature = new ol.Feature(geom);
483 feature.setStyle(this.altitude === 'ground' ? groundStyle : airStyle);
484
485 if (PlaneTrailFeatures.length == 0) {
486 PlaneTrailFeatures.push(feature);
487 } else {
488 PlaneTrailFeatures.setAt(0, feature);
489 }
490
491 // create any missing fixed line features
492 for (var i = 0; i < this.track_linesegs.length; ++i) {
493 var seg = this.track_linesegs[i];
494 if (seg.feature === null) {
495 seg.feature = new ol.Feature(seg.fixed);
496 if (seg.estimated) {
497 seg.feature.setStyle(estimateStyle);
498 } else if (seg.ground) {
499 seg.feature.setStyle(groundStyle);
500 } else {
501 seg.feature.setStyle(airStyle);
502 }
503
504 PlaneTrailFeatures.push(seg.feature);
505 }
506 }
507 };
508
509 PlaneObject.prototype.destroy = function() {
510 this.clearLines();
511 this.clearMarker();
512 };