Added receiver position API to the Utils.js.

This commit is contained in:
Marat Fayzullin 2024-04-17 19:44:32 -04:00
parent cadb28b5ee
commit bbcb8fb66e
4 changed files with 25 additions and 18 deletions

View File

@ -58,18 +58,18 @@ function MapManager() {
//
MapManager.prototype.process = function(e) {
if (typeof e.data != 'string') {
console.error("unsupported binary data on websocket; ignoring");
console.error('unsupported binary data on websocket; ignoring');
return
}
if (e.data.substr(0, 16) == "CLIENT DE SERVER") {
if (e.data.substr(0, 16) == 'CLIENT DE SERVER') {
return
}
try {
var json = JSON.parse(e.data);
switch (json.type) {
case "update":
case 'update':
this.processUpdates(json.value);
break;
@ -79,9 +79,11 @@ MapManager.prototype.process = function(e) {
});
break;
case "config":
case 'config':
Object.assign(this.config, json.value);
if ('receiver_gps' in this.config) {
// Save receiver location
Utils.setReceiverPos(this.config.receiver_gps);
// Passing API key even if this particular map
// engine does not need it (Google Maps do)
this.initializeMap(

View File

@ -430,7 +430,6 @@ $.fn.hfdlMessagePanel = function() {
AdsbMessagePanel = function(el) {
MessagePanel.call(this, el);
this.clearButton.css('display', 'none');
this.receiver_pos = null;
}
AdsbMessagePanel.prototype = Object.create(MessagePanel.prototype);
@ -439,10 +438,6 @@ AdsbMessagePanel.prototype.supportsMessage = function(message) {
return message['mode'] === 'ADSB-LIST';
};
AdsbMessagePanel.prototype.setReceiverPos = function(pos) {
if (pos.lat && pos.lon) this.receiver_pos = pos;
};
AdsbMessagePanel.prototype.render = function() {
$(this.el).append($(
'<table>' +
@ -504,13 +499,14 @@ AdsbMessagePanel.prototype.pushMessage = function(msg) {
// Compute distance to the receiver
var distance = '';
if (this.receiver_pos && entry.lat && entry.lon) {
var receiver_pos = Utils.getReceiverPos();
if (receiver_pos && entry.lat && entry.lon) {
var id = entry.icao? entry.icao
: entry.aircraft? entry.aircraft
: entry.flight? entry.flight
: null;
distance = Utils.distanceKm(entry, this.receiver_pos) + '&nbsp;km';
distance = Utils.distanceKm(entry, receiver_pos) + '&nbsp;km';
if (id) distance = Utils.linkToMap(id, distance);
}

View File

@ -8,6 +8,17 @@ Utils.callsign_url = null;
Utils.vessel_url = null;
Utils.flight_url = null;
Utils.icao_url = null;
Utils.receiver_pos = null;
// Set receiver position
Utils.setReceiverPos = function(pos) {
if (pos.lat && pos.lon) this.receiver_pos = pos;
};
// Get receiver position
Utils.getReceiverPos = function() {
return this.receiver_pos;
};
// Set URL for linkifying callsigns
Utils.setCallsignUrl = function(url) {
@ -145,8 +156,11 @@ Utils.HHMMSS = function(t) {
return pad(t.getUTCHours()) + ':' + pad(t.getUTCMinutes()) + ':' + pad(t.getUTCSeconds());
};
// Compute distance, in kilometers, between two latlons.
// Compute distance, in kilometers, between two latlons. Use receiver
// location if the second latlon is not provided.
Utils.distanceKm = function(p1, p2) {
// Use receiver location if second latlon not given
if (p2 == null) p2 = this.receiver_pos;
// Convert from map objects to latlons
if ("lng" in p1) p1 = { lat : p1.lat(), lon : p1.lng() };
if ("lng" in p2) p2 = { lat : p2.lat(), lon : p2.lng() };

View File

@ -1069,19 +1069,14 @@ function on_ws_recv(evt) {
}
if ('receiver_gps' in config) {
var adsb_panel = $('#openwebrx-panel-adsb-message').adsbMessagePanel();
adsb_panel.setReceiverPos(config['receiver_gps']);
Utils.setReceiverPos(config['receiver_gps']);
}
if ('flight_url' in config) {
var hfdl_panel = $('#openwebrx-panel-hfdl-message').hfdlMessagePanel();
var adsb_panel = $('#openwebrx-panel-adsb-message').adsbMessagePanel();
Utils.setFlightUrl(config['flight_url']);
}
if ('modes_url' in config) {
var hfdl_panel = $('#openwebrx-panel-hfdl-message').hfdlMessagePanel();
var adsb_panel = $('#openwebrx-panel-adsb-message').adsbMessagePanel();
Utils.setIcaoUrl(config['modes_url']);
}