You've already forked fm-dx-webserver
mirror of
https://github.com/KubaPro010/fm-dx-webserver.git
synced 2026-02-27 14:33:52 +01:00
Local database cache for TX ID
Locally cache transmitter database for TX ID and visually show when multiple matches on frontend
This commit is contained in:
@@ -416,7 +416,8 @@ function handleData(wss, receivedData, rdsWss) {
|
|||||||
azi: currentTx.azimuth,
|
azi: currentTx.azimuth,
|
||||||
id: currentTx.id,
|
id: currentTx.id,
|
||||||
pi: currentTx.pi,
|
pi: currentTx.pi,
|
||||||
reg: currentTx.reg
|
reg: currentTx.reg,
|
||||||
|
otherMatches: currentTx.others
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ const fetch = require('node-fetch');
|
|||||||
const { serverConfig } = require('./server_config');
|
const { serverConfig } = require('./server_config');
|
||||||
const consoleCmd = require('./console');
|
const consoleCmd = require('./console');
|
||||||
|
|
||||||
let cachedData = {};
|
let localDb = {};
|
||||||
let lastFetchTime = 0;
|
let lastFetchTime = 0;
|
||||||
const fetchInterval = 1000;
|
const fetchInterval = 1000;
|
||||||
const esSwitchCache = {"lastCheck":0, "esSwitch":false};
|
const esSwitchCache = {"lastCheck":0, "esSwitch":false};
|
||||||
@@ -28,6 +28,19 @@ if (typeof algorithms[algoSetting] !== 'undefined') {
|
|||||||
weightedDist = algorithms[algoSetting][1];
|
weightedDist = algorithms[algoSetting][1];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IIFE to build the local TX DB cache from the endpoint.
|
||||||
|
(async () => {
|
||||||
|
try {
|
||||||
|
consoleCmd.logInfo('Fetching transmitter database...');
|
||||||
|
const response = await fetch(`https://maps.fmdx.org/api?qth=${serverConfig.identification.lat},${serverConfig.identification.lon}`);
|
||||||
|
if (!response.ok) throw new Error(`HTTP error! Status: ${response.status}`);
|
||||||
|
localDb = await response.json();
|
||||||
|
consoleCmd.logInfo('Transmitter database successfully loaded.');
|
||||||
|
} catch (error) {
|
||||||
|
consoleCmd.logError("Failed to fetch transmitter database:", error);
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
// Load the US states GeoJSON data
|
// Load the US states GeoJSON data
|
||||||
async function loadUsStatesGeoJson() {
|
async function loadUsStatesGeoJson() {
|
||||||
if (!usStatesGeoJson) {
|
if (!usStatesGeoJson) {
|
||||||
@@ -118,8 +131,28 @@ function validPsCompare(rdsPs, stationPs) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function evaluateStation(station) {
|
||||||
|
let esMode = checkEs();
|
||||||
|
let weightDistance = station.distanceKm;
|
||||||
|
if (esMode && station.distanceKm > 500) {
|
||||||
|
weightDistance = Math.abs(station.distanceKm - 1500);
|
||||||
|
}
|
||||||
|
let erp = station.erp && station.erp > 0 ? station.erp : 1;
|
||||||
|
let extraWeight = erp > 30 && station.distanceKm <= 500 ? 0.3 : 0;
|
||||||
|
let score = 0;
|
||||||
|
// If ERP is 1W, use a simpler formula to avoid zero-scoring.
|
||||||
|
if (erp === 0.001) {
|
||||||
|
score = erp / station.distanceKm;
|
||||||
|
} else {
|
||||||
|
score = ((10 * (Math.log10(erp * 1000))) / weightDistance) + extraWeight;
|
||||||
|
}
|
||||||
|
return score;
|
||||||
|
}
|
||||||
|
|
||||||
// Fetch data from maps
|
// Fetch data from maps
|
||||||
async function fetchTx(freq, piCode, rdsPs) {
|
async function fetchTx(freq, piCode, rdsPs) {
|
||||||
|
let match = null;
|
||||||
|
let multiMatches = [];
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
freq = parseFloat(freq);
|
freq = parseFloat(freq);
|
||||||
|
|
||||||
@@ -127,161 +160,74 @@ async function fetchTx(freq, piCode, rdsPs) {
|
|||||||
if (now - lastFetchTime < fetchInterval
|
if (now - lastFetchTime < fetchInterval
|
||||||
|| serverConfig.identification.lat.length < 2
|
|| serverConfig.identification.lat.length < 2
|
||||||
|| freq < 87
|
|| freq < 87
|
||||||
|
|| Object.keys(localDb).length === 0
|
||||||
|| (currentPiCode == piCode && currentRdsPs == rdsPs)) {
|
|| (currentPiCode == piCode && currentRdsPs == rdsPs)) {
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
lastFetchTime = now;
|
lastFetchTime = now;
|
||||||
|
|
||||||
if (cachedData[freq]) {
|
|
||||||
return processData(cachedData[freq], piCode, rdsPs);
|
|
||||||
}
|
|
||||||
|
|
||||||
const url = "https://maps.fmdx.org/api/?freq=" + freq;
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Try POST first
|
|
||||||
const postResponse = await fetch(url, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({ freq }), // You can omit or customize this
|
|
||||||
redirect: 'manual'
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!postResponse.ok) throw new Error(`POST failed: ${postResponse.status}`);
|
|
||||||
const data = await postResponse.json();
|
|
||||||
cachedData[freq] = data;
|
|
||||||
if (serverConfig.webserver.rdsMode === true) await loadUsStatesGeoJson();
|
if (serverConfig.webserver.rdsMode === true) await loadUsStatesGeoJson();
|
||||||
return processData(data, piCode, rdsPs);
|
|
||||||
} catch (postError) {
|
|
||||||
console.warn("POST failed, trying GET:", postError);
|
|
||||||
|
|
||||||
try {
|
const filteredLocations = Object.values(localDb.locations)
|
||||||
const getResponse = await fetch(url, { redirect: 'manual' });
|
.map(locData => ({
|
||||||
if (!getResponse.ok) throw new Error(`GET failed: ${getResponse.status}`);
|
...locData,
|
||||||
const data = await getResponse.json();
|
stations: locData.stations.filter(station =>
|
||||||
cachedData[freq] = data;
|
station.freq === freq &&
|
||||||
if (serverConfig.webserver.rdsMode === true) await loadUsStatesGeoJson();
|
(station.pi === piCode.toUpperCase() || station.pireg === piCode.toUpperCase() ) &&
|
||||||
return processData(data, piCode, rdsPs);
|
|
||||||
} catch (getError) {
|
|
||||||
console.error("GET also failed:", getError);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function processData(data, piCode, rdsPs) {
|
|
||||||
let matchingStation = null;
|
|
||||||
let matchingCity = null;
|
|
||||||
let maxScore = -Infinity;
|
|
||||||
let txAzimuth;
|
|
||||||
let maxDistance;
|
|
||||||
let esMode = checkEs();
|
|
||||||
let detectedByPireg = false;
|
|
||||||
currentPiCode = piCode;
|
|
||||||
currentRdsPs = rdsPs;
|
|
||||||
|
|
||||||
// First, collect all stations that match the piCode (without PS comparison).
|
|
||||||
let stationsForPi = [];
|
|
||||||
for (const cityId in data.locations) {
|
|
||||||
const city = data.locations[cityId];
|
|
||||||
if (city.stations) {
|
|
||||||
for (const station of city.stations) {
|
|
||||||
if (station.pi === piCode.toUpperCase() && !station.extra) {
|
|
||||||
stationsForPi.push({ station, city });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stationsForPi.length > 0) {
|
|
||||||
for (const { station, city } of stationsForPi) {
|
|
||||||
if (station.ps && validPsCompare(rdsPs, station.ps)) {
|
|
||||||
const distance = haversine(serverConfig.identification.lat, serverConfig.identification.lon, city.lat, city.lon);
|
|
||||||
evaluateStation(station, city, distance);
|
|
||||||
detectedByPireg = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fallback: Check using pireg if no match was found using the piCode (with valid PS comparison)
|
|
||||||
if (!matchingStation) {
|
|
||||||
for (const cityId in data.locations) {
|
|
||||||
const city = data.locations[cityId];
|
|
||||||
if (city.stations) {
|
|
||||||
for (const station of city.stations) {
|
|
||||||
if (
|
|
||||||
station.pireg &&
|
|
||||||
station.pireg.toUpperCase() === piCode.toUpperCase() &&
|
|
||||||
!station.extra &&
|
|
||||||
station.ps &&
|
|
||||||
validPsCompare(rdsPs, station.ps)
|
validPsCompare(rdsPs, station.ps)
|
||||||
) {
|
)
|
||||||
const distance = haversine(serverConfig.identification.lat, serverConfig.identification.lon, city.lat, city.lon);
|
}))
|
||||||
evaluateStation(station, city, distance);
|
.filter(locData => locData.stations.length > 0); // Ensure locations with at least one matching station remain
|
||||||
detectedByPireg = true;
|
|
||||||
}
|
for (loc of filteredLocations) {
|
||||||
}
|
loc = Object.assign(loc, loc.stations[0]);
|
||||||
}
|
delete loc.stations;
|
||||||
}
|
const dist = haversine(serverConfig.identification.lat, serverConfig.identification.lon, loc.lat, loc.lon);
|
||||||
|
loc = Object.assign(loc, dist);
|
||||||
|
loc.detectedByPireg = (loc.pireg === piCode.toUpperCase());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine the state if the city is in the USA
|
if (filteredLocations.length > 1) {
|
||||||
if (matchingStation && matchingCity.itu === 'USA') {
|
for (loc of filteredLocations) {
|
||||||
const state = getStateForCoordinates(matchingCity.lat, matchingCity.lon);
|
loc.score = evaluateStation(loc);
|
||||||
|
}
|
||||||
|
match = filteredLocations.reduce((max, obj) => obj.score > max.score ? obj : max, filteredLocations[0]);
|
||||||
|
multiMatches = filteredLocations.filter(obj => obj !== match);
|
||||||
|
} else if (filteredLocations.length === 1) {
|
||||||
|
match = filteredLocations[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (match) {
|
||||||
|
if (match.itu === 'USA') {
|
||||||
|
const state = getStateForCoordinates(match.lat, match.lon);
|
||||||
if (state) {
|
if (state) {
|
||||||
matchingCity.state = state; // Add state to matchingCity
|
match.state = state; // Add state to matchingCity
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (matchingStation) {
|
|
||||||
return {
|
return {
|
||||||
station: detectedByPireg
|
station: match.detectedByPireg
|
||||||
? `${matchingStation.station.replace("R.", "Radio ")}${matchingStation.regname ? ' ' + matchingStation.regname : ''}`
|
? `${match.station.replace("R.", "Radio ")}${match.regname ? ' ' + match.regname : ''}`
|
||||||
: matchingStation.station.replace("R.", "Radio "),
|
: match.station.replace("R.", "Radio "),
|
||||||
pol: matchingStation.pol.toUpperCase(),
|
pol: match.pol.toUpperCase(),
|
||||||
erp: matchingStation.erp && matchingStation.erp > 0 ? matchingStation.erp : '?',
|
erp: match.erp && match.erp > 0 ? match.erp : '?',
|
||||||
city: matchingCity.name,
|
city: match.name,
|
||||||
itu: matchingCity.state ? matchingCity.state + ', ' + matchingCity.itu : matchingCity.itu,
|
itu: match.state ? match.state + ', ' + match.itu : match.itu,
|
||||||
distance: maxDistance.toFixed(0),
|
distance: match.distanceKm.toFixed(0),
|
||||||
azimuth: txAzimuth.toFixed(0),
|
azimuth: match.azimuth.toFixed(0),
|
||||||
id: matchingStation.id,
|
id: match.id,
|
||||||
pi: matchingStation.pi,
|
pi: match.pi,
|
||||||
foundStation: true,
|
foundStation: true,
|
||||||
reg: detectedByPireg,
|
reg: match.detectedByPireg,
|
||||||
|
others: multiMatches.length,
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
return;
|
return Promise.resolve();
|
||||||
}
|
|
||||||
|
|
||||||
function evaluateStation(station, city, distance) {
|
|
||||||
let weightDistance = distance.distanceKm;
|
|
||||||
if (esMode && distance.distanceKm > 500) {
|
|
||||||
weightDistance = Math.abs(distance.distanceKm - 1500);
|
|
||||||
}
|
|
||||||
let erp = station.erp && station.erp > 0 ? station.erp : 1;
|
|
||||||
let extraWeight = erp >= weightedErp && distance.distanceKm <= weightedDist ? 0.3 : 0;
|
|
||||||
let score = 0;
|
|
||||||
// If ERP is 1W, use a simpler formula to avoid zero-scoring.
|
|
||||||
if (erp === 0.001) {
|
|
||||||
score = erp / distance.distanceKm;
|
|
||||||
} else {
|
|
||||||
score = ((10 * Math.log10(erp * 1000)) / weightDistance) + extraWeight;
|
|
||||||
}
|
|
||||||
if (score > maxScore) {
|
|
||||||
maxScore = score;
|
|
||||||
txAzimuth = distance.azimuth;
|
|
||||||
matchingStation = station;
|
|
||||||
matchingCity = city;
|
|
||||||
maxDistance = distance.distanceKm;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkEs() {
|
function checkEs() {
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
const url = "https://fmdx.org/includes/tools/get_muf.php";
|
const url = "https://fmdx.org/includes/tools/get_muf.php";
|
||||||
let esSwitch = false;
|
|
||||||
|
|
||||||
if (now - esSwitchCache.lastCheck < esFetchInterval) {
|
if (now - esSwitchCache.lastCheck < esFetchInterval) {
|
||||||
return esSwitchCache.esSwitch;
|
return esSwitchCache.esSwitch;
|
||||||
|
|||||||
@@ -119,6 +119,14 @@ label {
|
|||||||
font-size: 20px;
|
font-size: 20px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#data-station-others span {
|
||||||
|
color: var(--color-main);
|
||||||
|
background: var(--color-4);
|
||||||
|
margin-left: 4px;
|
||||||
|
padding: 0 5px;
|
||||||
|
border-radius: 3px;
|
||||||
|
}
|
||||||
|
|
||||||
.highest-signal-container {
|
.highest-signal-container {
|
||||||
margin-bottom: -20px !important;
|
margin-bottom: -20px !important;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -287,7 +287,7 @@
|
|||||||
<span id="data-station-city" style="font-size: 16px;"></span> <span class="text-small">[<span id="data-station-itu"></span>]</span>
|
<span id="data-station-city" style="font-size: 16px;"></span> <span class="text-small">[<span id="data-station-itu"></span>]</span>
|
||||||
</h4>
|
</h4>
|
||||||
<span class="text-small">
|
<span class="text-small">
|
||||||
<span id="data-station-erp"></span> kW [<span id="data-station-pol"></span>] <span class="text-gray">•</span> <span id="data-station-distance"></span> <span class="text-gray">•</span> <span id="data-station-azimuth"></span>
|
<span id="data-station-erp"></span> kW [<span id="data-station-pol"></span>] <span class="text-gray">•</span> <span id="data-station-distance"></span> <span class="text-gray">•</span> <span id="data-station-azimuth"></span> <span id="data-station-others"></span>
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -976,6 +976,7 @@ const updateDataElements = throttle(function(parsedData) {
|
|||||||
updateTextIfChanged($('#data-station-itu'), parsedData.txInfo.itu);
|
updateTextIfChanged($('#data-station-itu'), parsedData.txInfo.itu);
|
||||||
updateTextIfChanged($('#data-station-pol'), parsedData.txInfo.pol);
|
updateTextIfChanged($('#data-station-pol'), parsedData.txInfo.pol);
|
||||||
updateHtmlIfChanged($('#data-station-azimuth'), parsedData.txInfo.azi + '°');
|
updateHtmlIfChanged($('#data-station-azimuth'), parsedData.txInfo.azi + '°');
|
||||||
|
updateHtmlIfChanged($('#data-station-others'), parsedData.txInfo.otherMatches > 0 ? ('<span>+' + parsedData.txInfo.otherMatches +'</span>') : '');
|
||||||
const txDistance = localStorage.getItem('imperialUnits') == "true" ? (Number(parsedData.txInfo.dist) * 0.621371192).toFixed(0) + " mi" : parsedData.txInfo.dist + " km";
|
const txDistance = localStorage.getItem('imperialUnits') == "true" ? (Number(parsedData.txInfo.dist) * 0.621371192).toFixed(0) + " mi" : parsedData.txInfo.dist + " km";
|
||||||
updateTextIfChanged($('#data-station-distance'), txDistance);
|
updateTextIfChanged($('#data-station-distance'), txDistance);
|
||||||
$dataStationContainer.css('display', 'block');
|
$dataStationContainer.css('display', 'block');
|
||||||
|
|||||||
Reference in New Issue
Block a user