1
0
mirror of https://github.com/KubaPro010/fm-dx-webserver.git synced 2026-02-27 06:23:53 +01:00

Improve TX DB fetch retry process

Improve process for retrying when TX DB download fails
This commit is contained in:
Adam Wisher
2025-07-05 16:23:40 +01:00
parent eb8dbf8fd5
commit 3cedab5248

View File

@@ -3,11 +3,9 @@ const { serverConfig } = require('./server_config');
const consoleCmd = require('./console'); const consoleCmd = require('./console');
let localDb = {}; let localDb = {};
let lastDownloadTime = 0; // Last DB download attempt time.
let lastFetchTime = 0; let lastFetchTime = 0;
let piFreqIndex = {}; // Indexing for speedier PI+Freq combinations let piFreqIndex = {}; // Indexing for speedier PI+Freq combinations
const fetchInterval = 1000; const fetchInterval = 1000;
const downloadInterval = 300000;
const esSwitchCache = {"lastCheck": null, "esSwitch": false}; const esSwitchCache = {"lastCheck": null, "esSwitch": false};
const esFetchInterval = 300000; const esFetchInterval = 300000;
var currentPiCode = ''; var currentPiCode = '';
@@ -38,12 +36,8 @@ if (typeof algorithms[algoSetting] !== 'undefined') {
weightedDist = algorithms[algoSetting][1]; weightedDist = algorithms[algoSetting][1];
} }
// IIFE to build the local TX DB cache from the endpoint. // Build the TX database.
(async () => { setTimeout(buildTxDatabase, 3000);
const now = Date.now();
lastDownloadTime = now;
await buildTxDatabase();
})();
if (serverConfig.identification.gpsMode) { if (serverConfig.identification.gpsMode) {
// 5-second delay before activation of GPS lat/lon websocket // 5-second delay before activation of GPS lat/lon websocket
@@ -76,15 +70,30 @@ if (serverConfig.identification.gpsMode) {
// Function to build local TX database from FMDX Maps endpoint. // Function to build local TX database from FMDX Maps endpoint.
async function buildTxDatabase() { async function buildTxDatabase() {
try { if (Latitude.length > 0 && Longitude.length > 0) {
consoleCmd.logInfo('Fetching transmitter database...'); let awaitingTxInfo = true;
const response = await fetch(`https://maps.fmdx.org/api?qth=${serverConfig.identification.lat},${serverConfig.identification.lon}`); while (awaitingTxInfo) {
if (!response.ok) throw new Error(`HTTP error! Status: ${response.status}`); try {
localDb = await response.json(); consoleCmd.logInfo('Fetching transmitter database...');
buildPiFreqIndex(); const response = await fetch(`https://maps.fmdx.org/api?qth=${serverConfig.identification.lat},${serverConfig.identification.lon}`, {
consoleCmd.logInfo('Transmitter database successfully loaded.'); method: 'GET',
} catch (error) { headers: {
consoleCmd.logError("Failed to fetch transmitter database:", error); 'Accept': 'application/json'
}
});
if (!response.ok) throw new Error(`HTTP error! Status: ${response.status}`);
localDb = await response.json();
buildPiFreqIndex();
consoleCmd.logInfo('Transmitter database successfully loaded.');
awaitingTxInfo = false;
} catch (error) {
consoleCmd.logError("Failed to fetch transmitter database:", error);
await new Promise(res => setTimeout(res, 30000));
consoleCmd.logInfo('Retrying transmitter database download...');
}
}
} else {
consoleCmd.logInfo('Server latitude and longitude must be set before transmitter database can be built');
} }
} }
@@ -230,21 +239,12 @@ async function fetchTx(freq, piCode, rdsPs) {
const now = Date.now(); const now = Date.now();
freq = parseFloat(freq); freq = parseFloat(freq);
// If we don't have a local database and the interval has passed, re-try download.
if (
Object.keys(localDb).length === 0 &&
now - lastDownloadTime > downloadInterval
) {
lastDownloadTime = now;
await buildTxDatabase();
}
if ( if (
isNaN(freq) || isNaN(freq) ||
now - lastFetchTime < fetchInterval || now - lastFetchTime < fetchInterval ||
Latitude.length < 2 || Latitude.length < 2 ||
freq < 87 || freq < 87 ||
Object.keys(localDb).length === 0 || Object.keys(piFreqIndex).length === 0 ||
(currentPiCode === piCode && currentRdsPs === rdsPs) (currentPiCode === piCode && currentRdsPs === rdsPs)
) return Promise.resolve(); ) return Promise.resolve();