2018-05-05 02:59:47 +08:00
|
|
|
//
|
|
|
|
|
|
|
|
const geolib = require('geolib');
|
2018-03-28 07:18:25 +08:00
|
|
|
const leaflet = require('leaflet');
|
2018-05-04 04:07:16 +08:00
|
|
|
const rivets = require('rivets');
|
2018-03-13 06:30:52 +08:00
|
|
|
|
2018-03-14 22:07:41 +08:00
|
|
|
import draw_base_map from './base_map'
|
2018-05-05 02:59:47 +08:00
|
|
|
import { ACTUAL_ROUTE_COLOR } from './config'
|
2018-03-13 06:30:52 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Render the live map
|
|
|
|
* @param opts
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
export default (opts) => {
|
|
|
|
|
2018-05-03 04:14:18 +08:00
|
|
|
opts = Object.assign({
|
|
|
|
update_uri: '/api/acars',
|
|
|
|
pirep_uri: '/api/pireps/{id}',
|
2018-05-03 04:28:57 +08:00
|
|
|
pirep_link_uri: '/pireps/{id}',
|
2018-05-03 04:14:18 +08:00
|
|
|
positions: null,
|
|
|
|
render_elem: 'map',
|
|
|
|
aircraft_icon: '/assets/img/acars/aircraft.png',
|
|
|
|
units: 'nmi',
|
|
|
|
}, opts);
|
|
|
|
|
|
|
|
const map = draw_base_map(opts);
|
|
|
|
const aircraftIcon = leaflet.icon({
|
|
|
|
iconUrl: opts.aircraft_icon,
|
|
|
|
iconSize: [42, 42],
|
|
|
|
iconAnchor: [21, 21],
|
|
|
|
});
|
|
|
|
|
2018-05-05 02:59:47 +08:00
|
|
|
/**
|
|
|
|
* Hold the markers
|
|
|
|
* @type {{}}
|
|
|
|
*/
|
|
|
|
let markers_list = {};
|
|
|
|
|
2018-05-03 04:14:18 +08:00
|
|
|
let pannedToCenter = false;
|
2018-05-05 02:59:47 +08:00
|
|
|
|
2018-05-03 04:14:18 +08:00
|
|
|
let layerFlights = null;
|
|
|
|
let layerSelFlight = null;
|
|
|
|
let layerSelFlightFeature = null;
|
|
|
|
let layerSelFlightLayer = null;
|
2018-05-05 02:59:47 +08:00
|
|
|
let layerSelArr = null;
|
|
|
|
let layerSelDep = null;
|
2018-03-13 06:30:52 +08:00
|
|
|
|
2018-05-05 02:59:47 +08:00
|
|
|
/**
|
|
|
|
* Controller for two-way bindings
|
|
|
|
* @type {{focusMarker: focusMarker}}
|
|
|
|
*/
|
|
|
|
const mapController = {
|
|
|
|
/**
|
|
|
|
* Focus on a specific marker
|
|
|
|
* @param e
|
|
|
|
* @param model
|
|
|
|
*/
|
|
|
|
focusMarker: (e, model) => {
|
|
|
|
if(!(model.pirep.id in markers_list)) {
|
|
|
|
console.log('marker not found in list');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const marker = markers_list[model.pirep.id];
|
|
|
|
onFlightClick(marker[0], marker[1]);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const r_map_view = rivets.bind($('#map-info-box'), {pirep: {}, controller: mapController});
|
|
|
|
const r_table_view = rivets.bind($('#live_flights'), {pireps: [], controller: mapController});
|
2018-05-04 04:07:16 +08:00
|
|
|
|
2018-03-13 06:30:52 +08:00
|
|
|
/**
|
2018-05-03 04:14:18 +08:00
|
|
|
* When a flight is clicked on, show the path, etc for that flight
|
|
|
|
* @param feature
|
|
|
|
* @param layer
|
2018-03-13 06:30:52 +08:00
|
|
|
*/
|
2018-05-03 04:14:18 +08:00
|
|
|
const onFlightClick = (feature, layer) => {
|
|
|
|
|
|
|
|
const pirep_uri = opts.pirep_uri.replace('{id}', feature.properties.pirep_id);
|
|
|
|
const geojson_uri = opts.pirep_uri.replace('{id}', feature.properties.pirep_id) + "/acars/geojson";
|
|
|
|
|
|
|
|
const pirep_info = $.ajax({
|
|
|
|
url: pirep_uri,
|
|
|
|
dataType: 'json',
|
|
|
|
error: console.log
|
|
|
|
});
|
|
|
|
|
|
|
|
const flight_route = $.ajax({
|
|
|
|
url: geojson_uri,
|
|
|
|
dataType: 'json',
|
|
|
|
error: console.log
|
|
|
|
});
|
|
|
|
|
|
|
|
// Load up the PIREP info
|
2018-05-05 02:59:47 +08:00
|
|
|
$.when(flight_route).done((rte) => {
|
2018-05-03 04:14:18 +08:00
|
|
|
if (layerSelFlight !== null) {
|
|
|
|
map.removeLayer(layerSelFlight);
|
2018-05-05 02:59:47 +08:00
|
|
|
//map.removeLayer(layerSelArr);
|
|
|
|
//map.removeLayer(layerSelDep);
|
2018-05-03 04:14:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
layerSelFlight = leaflet.geodesic([], {
|
2018-05-03 04:28:57 +08:00
|
|
|
weight: 5,
|
2018-05-03 04:14:18 +08:00
|
|
|
opacity: 0.9,
|
|
|
|
color: ACTUAL_ROUTE_COLOR,
|
|
|
|
wrap: false,
|
|
|
|
}).addTo(map);
|
|
|
|
|
2018-05-05 02:59:47 +08:00
|
|
|
layerSelFlight.geoJson(rte.line);
|
2018-05-03 04:14:18 +08:00
|
|
|
layerSelFlightFeature = feature;
|
|
|
|
layerSelFlightLayer = layer;
|
|
|
|
|
2018-05-05 02:59:47 +08:00
|
|
|
/*const dptIcon = leaflet.divIcon({
|
|
|
|
html: '<div class="map-info-label"><h5>' + rte.airports.d.icao + '</h5></div>'
|
|
|
|
});
|
|
|
|
|
|
|
|
layerSelDep = leaflet.marker([rte.airports.d.lat, rte.airports.d.lon], {icon:dptIcon}).addTo(map);
|
|
|
|
*/
|
|
|
|
|
2018-05-03 04:14:18 +08:00
|
|
|
// Center on it, but only do it once, in case the map is moved
|
|
|
|
if(!pannedToCenter) {
|
2018-05-05 02:59:47 +08:00
|
|
|
// find center
|
|
|
|
const c = geolib.getCenter([
|
|
|
|
{latitude: rte.airports.a.lat, longitude: rte.airports.a.lon},
|
|
|
|
{latitude: rte.airports.d.lat, longitude: rte.airports.d.lon},
|
|
|
|
]);
|
|
|
|
|
|
|
|
//map.panTo({lat: c.latitude, lng: c.longitude});
|
|
|
|
map.panTo({lat: rte.position.lat, lng: rte.position.lon});
|
2018-05-03 04:14:18 +08:00
|
|
|
pannedToCenter = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
//
|
|
|
|
// When the PIREP info is done loading, show the bottom bar
|
|
|
|
//
|
2018-05-04 04:07:16 +08:00
|
|
|
$.when(pirep_info).done(pirep => {
|
|
|
|
r_map_view.update({pirep:pirep.data});
|
|
|
|
$('#map-info-box').show();
|
2018-05-03 04:14:18 +08:00
|
|
|
});
|
|
|
|
};
|
2018-03-13 06:30:52 +08:00
|
|
|
|
2018-05-03 04:14:18 +08:00
|
|
|
const updateMap = () => {
|
2018-03-13 06:30:52 +08:00
|
|
|
|
2018-05-03 04:14:18 +08:00
|
|
|
console.log('reloading flights from acars...');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AJAX UPDATE
|
|
|
|
*/
|
2018-05-04 04:07:16 +08:00
|
|
|
const pirep_uri = opts.pirep_uri.replace('{id}', '');
|
|
|
|
let pireps = $.ajax({
|
|
|
|
url: pirep_uri,
|
|
|
|
dataType: 'json',
|
|
|
|
error: console.log
|
|
|
|
});
|
2018-05-03 04:14:18 +08:00
|
|
|
|
|
|
|
let flights = $.ajax({
|
|
|
|
url: opts.update_uri,
|
|
|
|
dataType: 'json',
|
|
|
|
error: console.log
|
|
|
|
});
|
|
|
|
|
2018-05-04 04:07:16 +08:00
|
|
|
$.when(flights).done(flightGeoJson => {
|
2018-05-03 04:14:18 +08:00
|
|
|
|
|
|
|
if (layerFlights !== null) {
|
|
|
|
layerFlights.clearLayers()
|
2018-03-13 06:30:52 +08:00
|
|
|
}
|
2018-05-03 04:14:18 +08:00
|
|
|
|
|
|
|
layerFlights = leaflet.geoJSON(flightGeoJson, {
|
|
|
|
onEachFeature: (feature, layer) => {
|
|
|
|
layer.on({
|
|
|
|
click: (e) => {
|
|
|
|
pannedToCenter = false;
|
|
|
|
onFlightClick(feature, layer)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let popup_html = '';
|
|
|
|
if (feature.properties && (feature.properties.popup !== '' && feature.properties.popup !== undefined)) {
|
|
|
|
popup_html += feature.properties.popup;
|
|
|
|
layer.bindPopup(popup_html);
|
|
|
|
}
|
2018-05-05 02:59:47 +08:00
|
|
|
|
|
|
|
// add to the list
|
|
|
|
markers_list[feature.properties.pirep_id] = [feature, layer];
|
2018-05-03 04:14:18 +08:00
|
|
|
},
|
|
|
|
pointToLayer: function (feature, latlon) {
|
|
|
|
return leaflet.marker(latlon, {
|
|
|
|
icon: aircraftIcon,
|
|
|
|
rotationAngle: feature.properties.heading
|
|
|
|
})
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
layerFlights.addTo(map);
|
|
|
|
|
|
|
|
// Reload the clicked-flight information
|
|
|
|
if (layerSelFlight !== null) {
|
|
|
|
onFlightClick(layerSelFlightFeature, layerSelFlightLayer)
|
|
|
|
}
|
2018-05-04 04:07:16 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
$.when(pireps).done(pireps => {
|
|
|
|
r_table_view.update({
|
|
|
|
pireps: pireps.data,
|
|
|
|
has_data: (pireps.data.length > 0),
|
|
|
|
});
|
|
|
|
});
|
2018-05-03 04:14:18 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
updateMap();
|
|
|
|
setInterval(updateMap, 10000)
|
2018-03-13 06:30:52 +08:00
|
|
|
};
|