2020-09-04 23:50:05 +08:00
|
|
|
/**
|
|
|
|
* Before you edit these, read the documentation on how these files are compiled:
|
|
|
|
* https://docs.phpvms.net/customize/building-assets
|
|
|
|
*
|
|
|
|
* Edits here don't take place until you compile these assets and then upload them.
|
|
|
|
*/
|
2018-05-05 02:59:47 +08:00
|
|
|
|
2019-08-30 20:08:00 +08:00
|
|
|
import draw_base_map from './base_map';
|
|
|
|
|
|
|
|
import { ACTUAL_ROUTE_COLOR } from './config';
|
|
|
|
|
|
|
|
import request from '../request';
|
2020-05-20 05:41:55 +08:00
|
|
|
import {LatLng} from "leaflet/dist/leaflet-src.esm";
|
2019-08-30 20:08:00 +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
|
|
|
|
|
|
|
/**
|
|
|
|
* Render the live map
|
2019-08-30 20:08:00 +08:00
|
|
|
* @param _opts
|
2018-03-13 06:30:52 +08:00
|
|
|
* @private
|
|
|
|
*/
|
2019-08-30 20:08:00 +08:00
|
|
|
export default (_opts) => {
|
|
|
|
const opts = Object.assign({
|
|
|
|
center: [29.98139, -95.33374],
|
|
|
|
refresh_interval: 10, // seconds
|
|
|
|
zoom: 5,
|
2019-09-14 03:10:47 +08:00
|
|
|
acars_uri: '/api/acars',
|
2019-09-13 23:21:40 +08:00
|
|
|
update_uri: '/api/acars/geojson',
|
2019-08-30 20:08:00 +08:00
|
|
|
pirep_uri: '/api/pireps/{id}',
|
|
|
|
pirep_link_uri: '/pireps/{id}',
|
|
|
|
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],
|
|
|
|
});
|
|
|
|
|
2020-05-20 05:41:55 +08:00
|
|
|
const centerCoords = new LatLng(opts.center[0], opts.center[1]);
|
|
|
|
|
2019-08-30 20:08:00 +08:00
|
|
|
/**
|
|
|
|
* Hold the markers
|
|
|
|
* @type {{}}
|
|
|
|
*/
|
|
|
|
const markers_list = {};
|
2020-03-02 01:41:09 +08:00
|
|
|
let pannedToFlight = false;
|
2019-08-30 20:08:00 +08:00
|
|
|
let layerFlights = null;
|
|
|
|
let layerSelFlight = null;
|
|
|
|
let layerSelFlightFeature = null;
|
|
|
|
let layerSelFlightLayer = null;
|
|
|
|
|
|
|
|
const liveMapController = {
|
|
|
|
pirep: {},
|
|
|
|
pireps: [],
|
|
|
|
has_data: false,
|
|
|
|
controller: {
|
2020-02-01 02:57:21 +08:00
|
|
|
focusMarker: null, // assigned below
|
2019-08-30 20:08:00 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
rivets.bind($('#map-info-box'), liveMapController);
|
|
|
|
rivets.bind($('#live_flights'), liveMapController);
|
|
|
|
|
2019-09-14 03:10:47 +08:00
|
|
|
function drawRoute(feature, layer, route) {
|
2019-08-30 20:08:00 +08:00
|
|
|
if (layerSelFlight !== null) {
|
|
|
|
map.removeLayer(layerSelFlight);
|
|
|
|
}
|
|
|
|
|
2020-02-01 02:57:21 +08:00
|
|
|
layerSelFlight = new L.Geodesic([], {
|
2019-08-30 20:08:00 +08:00
|
|
|
weight: 5,
|
|
|
|
opacity: 0.9,
|
|
|
|
color: ACTUAL_ROUTE_COLOR,
|
|
|
|
wrap: false,
|
|
|
|
}).addTo(map);
|
|
|
|
|
2020-02-01 02:57:21 +08:00
|
|
|
layerSelFlight.fromGeoJson(route.line);
|
2019-08-30 20:08:00 +08:00
|
|
|
layerSelFlightFeature = feature;
|
|
|
|
layerSelFlightLayer = layer;
|
|
|
|
|
|
|
|
// Center on it, but only do it once, in case the map is moved
|
2020-03-02 01:41:09 +08:00
|
|
|
if (!pannedToFlight) {
|
2019-08-30 20:08:00 +08:00
|
|
|
map.panTo({
|
|
|
|
lat: route.position.lat,
|
|
|
|
lng: route.position.lon,
|
|
|
|
});
|
|
|
|
|
2020-03-02 01:41:09 +08:00
|
|
|
pannedToFlight = true;
|
2019-08-30 20:08:00 +08:00
|
|
|
}
|
2019-09-14 03:10:47 +08:00
|
|
|
}
|
2019-08-30 20:08:00 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* When a flight is clicked on, show the path, etc for that flight
|
|
|
|
* @param feature
|
|
|
|
* @param layer
|
|
|
|
*/
|
2019-09-14 03:10:47 +08:00
|
|
|
function onFlightClick(feature, layer) {
|
2019-08-30 20:08:00 +08:00
|
|
|
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`;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run these in parallel:
|
|
|
|
* 1. Get information about the PIREP and populate the bottom box/container
|
|
|
|
* 2. Draw out the flight route
|
|
|
|
*/
|
|
|
|
request(pirep_uri).then((response) => {
|
|
|
|
const pirep = response.data.data;
|
|
|
|
console.log(pirep);
|
|
|
|
|
|
|
|
liveMapController.pirep = pirep;
|
2018-05-03 04:14:18 +08:00
|
|
|
});
|
|
|
|
|
2019-08-30 20:08:00 +08:00
|
|
|
request(geojson_uri).then((response) => {
|
|
|
|
const route = response.data.data;
|
|
|
|
console.log(route);
|
2018-05-05 02:59:47 +08:00
|
|
|
|
2019-08-30 20:08:00 +08:00
|
|
|
drawRoute(feature, layer, route);
|
|
|
|
});
|
2019-09-14 03:10:47 +08:00
|
|
|
}
|
2018-05-05 02:59:47 +08:00
|
|
|
|
2020-02-01 02:57:21 +08:00
|
|
|
/**
|
|
|
|
* Focus on a specific marker
|
|
|
|
* @param e
|
|
|
|
* @param model
|
|
|
|
*/
|
|
|
|
function 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]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Assign functions to the controller
|
|
|
|
*/
|
|
|
|
liveMapController.controller.drawRoute = drawRoute;
|
|
|
|
liveMapController.controller.focusMarker = focusMarker;
|
|
|
|
liveMapController.controller.onFlightClick = onFlightClick;
|
|
|
|
|
2019-08-30 20:08:00 +08:00
|
|
|
const updateMap = () => {
|
2019-09-14 03:10:47 +08:00
|
|
|
request(opts.acars_uri).then((response) => {
|
2019-08-30 20:08:00 +08:00
|
|
|
const pireps = response.data.data;
|
|
|
|
liveMapController.pireps = pireps;
|
|
|
|
liveMapController.has_data = pireps.length > 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
request({ url: opts.update_uri }).then((response) => {
|
|
|
|
const flightGeoJson = response.data.data;
|
|
|
|
|
|
|
|
if (layerFlights !== null) {
|
|
|
|
layerFlights.clearLayers();
|
|
|
|
}
|
|
|
|
|
|
|
|
layerFlights = leaflet.geoJSON(flightGeoJson, {
|
|
|
|
onEachFeature: (feature, layer) => {
|
|
|
|
layer.on({
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
click: (e) => {
|
2020-03-02 01:41:09 +08:00
|
|
|
pannedToFlight = false;
|
2019-08-30 20:08:00 +08:00
|
|
|
liveMapController.controller.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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add to the list
|
|
|
|
markers_list[feature.properties.pirep_id] = [feature, layer];
|
|
|
|
},
|
|
|
|
pointToLayer(feature, latlon) {
|
|
|
|
return leaflet.marker(latlon, {
|
|
|
|
icon: aircraftIcon,
|
|
|
|
rotationAngle: feature.properties.heading,
|
|
|
|
});
|
2018-05-05 02:59:47 +08:00
|
|
|
},
|
2019-08-30 20:08:00 +08:00
|
|
|
});
|
2018-05-05 02:59:47 +08:00
|
|
|
|
2019-08-30 20:08:00 +08:00
|
|
|
layerFlights.addTo(map);
|
2018-05-04 04:07:16 +08:00
|
|
|
|
2019-08-30 20:08:00 +08:00
|
|
|
// Reload the clicked-flight information
|
|
|
|
if (layerSelFlight !== null) {
|
|
|
|
liveMapController.controller.onFlightClick(layerSelFlightFeature, layerSelFlightLayer);
|
2020-03-02 01:41:09 +08:00
|
|
|
} else {
|
|
|
|
// Center on active flights
|
|
|
|
// eslint-disable-next-line no-lonely-if
|
|
|
|
if (!pannedToFlight) {
|
2020-05-20 05:41:55 +08:00
|
|
|
try {
|
|
|
|
map.panTo(layerFlights.getBounds().getCenter());
|
|
|
|
} catch (e) {
|
|
|
|
map.panTo(centerCoords);
|
|
|
|
}
|
2020-03-02 01:41:09 +08:00
|
|
|
}
|
2019-08-30 20:08:00 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
updateMap();
|
|
|
|
setInterval(updateMap, opts.refresh_interval * 1000);
|
2018-03-13 06:30:52 +08:00
|
|
|
};
|