You've already forked fm-dx-webserver
mirror of
https://github.com/KubaPro010/fm-dx-webserver.git
synced 2026-02-26 22:13:53 +01:00
Additional third algorithm option plus more comprehensive PS checking (thanks to @Highpoint2000 )
339 lines
12 KiB
JavaScript
339 lines
12 KiB
JavaScript
const fetch = require('node-fetch');
|
|
const { serverConfig } = require('./server_config');
|
|
const consoleCmd = require('./console');
|
|
|
|
let cachedData = {};
|
|
let lastFetchTime = 0;
|
|
const fetchInterval = 1000;
|
|
const esSwitchCache = {"lastCheck":0, "esSwitch":false};
|
|
const esFetchInterval = 300000;
|
|
var currentPiCode = '';
|
|
var currentRdsPs = '';
|
|
const usStatesGeoJsonUrl = "https://raw.githubusercontent.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json";
|
|
let usStatesGeoJson = null; // To cache the GeoJSON data for US states
|
|
|
|
// Get weighting values based on algorithm setting.
|
|
// Defaults = algorithm 1
|
|
let weightedErp = 10;
|
|
let weightedDist = 400;
|
|
const algorithms = [
|
|
[10, 400],
|
|
[30, 500],
|
|
[5, 400]
|
|
];
|
|
const algoSetting = parseInt(serverConfig.webserver.txIdAlgorithm);
|
|
|
|
if (typeof algorithms[algoSetting] !== 'undefined') {
|
|
weightedErp = algorithms[algoSetting][0];
|
|
weightedDist = algorithms[algoSetting][1];
|
|
}
|
|
|
|
// Load the US states GeoJSON data
|
|
async function loadUsStatesGeoJson() {
|
|
if (!usStatesGeoJson) {
|
|
try {
|
|
const response = await fetch(usStatesGeoJsonUrl);
|
|
if (!response.ok) throw new Error(`HTTP error! Status: ${response.status}`);
|
|
usStatesGeoJson = await response.json();
|
|
} catch (error) {
|
|
console.error("Failed to load US States GeoJSON:", error);
|
|
usStatesGeoJson = null; // Ensure it's null so it can retry later
|
|
}
|
|
}
|
|
}
|
|
|
|
// Function to get bounding box of a state
|
|
function getStateBoundingBox(coordinates) {
|
|
let minLat = Infinity, maxLat = -Infinity, minLon = Infinity, maxLon = -Infinity;
|
|
|
|
// Check if it's a MultiPolygon or a Polygon
|
|
for (const polygon of coordinates) {
|
|
// If it's a Polygon, it won't have an extra level of arrays
|
|
const linearRings = Array.isArray(polygon[0][0]) ? polygon : [polygon];
|
|
|
|
for (const ring of linearRings) {
|
|
for (const [lon, lat] of ring) {
|
|
if (lat < minLat) minLat = lat;
|
|
if (lat > maxLat) maxLat = lat;
|
|
if (lon < minLon) minLon = lon;
|
|
if (lon > maxLon) maxLon = lon;
|
|
}
|
|
}
|
|
}
|
|
|
|
return { minLat, maxLat, minLon, maxLon };
|
|
}
|
|
|
|
// Function to check if a city (lat, lon) falls within the bounding box of a state
|
|
function isCityInState(lat, lon, boundingBox) {
|
|
return lat >= boundingBox.minLat && lat <= boundingBox.maxLat &&
|
|
lon >= boundingBox.minLon && lon <= boundingBox.maxLon;
|
|
}
|
|
|
|
// Function to check if a city (lat, lon) is inside any US state and return the state name
|
|
function getStateForCoordinates(lat, lon) {
|
|
if (!usStatesGeoJson) return null;
|
|
|
|
for (const feature of usStatesGeoJson.features) {
|
|
const boundingBox = getStateBoundingBox(feature.geometry.coordinates);
|
|
if (isCityInState(lat, lon, boundingBox)) {
|
|
return feature.properties.name; // Return the state's name if city is inside bounding box
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Compares the standardized rdsPs string with the station's PS value.
|
|
* The rdsPs string is standardized by replacing spaces with underscores and converting to lowercase.
|
|
* The station's PS value is split into tokens (e.g., "__mdr___ _kultur_" -> ["__mdr___", "_kultur_"]).
|
|
* The function iterates through all tokens and checks if any token yields at least three valid (non "_" ) matches.
|
|
* Only positions where rdsPs is not an underscore are compared.
|
|
* If at least three valid matches are found for any token, the function returns true.
|
|
*/
|
|
function validPsCompare(rdsPs, stationPs) {
|
|
// Standardize the rdsPs string: replace spaces with underscores and convert to lowercase.
|
|
const standardizedRdsPs = rdsPs.replace(/ /g, '_').toLowerCase();
|
|
|
|
// Split stationPs into tokens (e.g., "__mdr___ _kultur_" -> ["__mdr___", "_kultur_"])
|
|
const psTokens = stationPs.split(/\s+/).filter(token => token.length > 0).map(token => token.toLowerCase());
|
|
|
|
// Iterate through all tokens and check if any token yields at least three valid (non "_" ) matches.
|
|
for (let token of psTokens) {
|
|
// If the token's length does not match the standardized rdsPs length, skip this token.
|
|
if (token.length !== standardizedRdsPs.length) continue;
|
|
|
|
let matchCount = 0;
|
|
for (let i = 0; i < standardizedRdsPs.length; i++) {
|
|
// Skip this position if the character in standardizedRdsPs is an underscore.
|
|
if (standardizedRdsPs[i] === '_') continue;
|
|
if (token[i] === standardizedRdsPs[i]) {
|
|
matchCount++;
|
|
}
|
|
}
|
|
if (matchCount >= 3) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// Fetch data from maps
|
|
async function fetchTx(freq, piCode, rdsPs) {
|
|
const now = Date.now();
|
|
freq = parseFloat(freq);
|
|
|
|
if (isNaN(freq)) return;
|
|
if (now - lastFetchTime < fetchInterval
|
|
|| serverConfig.identification.lat.length < 2
|
|
|| freq < 87
|
|
|| (currentPiCode == piCode && currentRdsPs == rdsPs)) {
|
|
return Promise.resolve();
|
|
}
|
|
|
|
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();
|
|
return processData(data, piCode, rdsPs);
|
|
} catch (postError) {
|
|
console.warn("POST failed, trying GET:", postError);
|
|
|
|
try {
|
|
const getResponse = await fetch(url, { redirect: 'manual' });
|
|
if (!getResponse.ok) throw new Error(`GET failed: ${getResponse.status}`);
|
|
const data = await getResponse.json();
|
|
cachedData[freq] = data;
|
|
if (serverConfig.webserver.rdsMode === true) await loadUsStatesGeoJson();
|
|
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)
|
|
) {
|
|
const distance = haversine(serverConfig.identification.lat, serverConfig.identification.lon, city.lat, city.lon);
|
|
evaluateStation(station, city, distance);
|
|
detectedByPireg = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Determine the state if the city is in the USA
|
|
if (matchingStation && matchingCity.itu === 'USA') {
|
|
const state = getStateForCoordinates(matchingCity.lat, matchingCity.lon);
|
|
if (state) {
|
|
matchingCity.state = state; // Add state to matchingCity
|
|
}
|
|
}
|
|
|
|
if (matchingStation) {
|
|
return {
|
|
station: detectedByPireg
|
|
? `${matchingStation.station.replace("R.", "Radio ")}${matchingStation.regname ? ' ' + matchingStation.regname : ''}`
|
|
: matchingStation.station.replace("R.", "Radio "),
|
|
pol: matchingStation.pol.toUpperCase(),
|
|
erp: matchingStation.erp && matchingStation.erp > 0 ? matchingStation.erp : '?',
|
|
city: matchingCity.name,
|
|
itu: matchingCity.state ? matchingCity.state + ', ' + matchingCity.itu : matchingCity.itu,
|
|
distance: maxDistance.toFixed(0),
|
|
azimuth: txAzimuth.toFixed(0),
|
|
id: matchingStation.id,
|
|
pi: matchingStation.pi,
|
|
foundStation: true,
|
|
reg: detectedByPireg,
|
|
};
|
|
} else {
|
|
return;
|
|
}
|
|
|
|
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() {
|
|
const now = Date.now();
|
|
const url = "https://fmdx.org/includes/tools/get_muf.php";
|
|
let esSwitch = false;
|
|
|
|
if (now - esSwitchCache.lastCheck < esFetchInterval) {
|
|
return esSwitchCache.esSwitch;
|
|
}
|
|
|
|
if (serverConfig.identification.lat > 20) {
|
|
esSwitchCache.lastCheck = now;
|
|
fetch(url)
|
|
.then(response => {
|
|
if (!response.ok) throw new Error(`HTTP error! Status: ${response.status}`);
|
|
return response.json();
|
|
})
|
|
.then(data => {
|
|
if ((serverConfig.identification.lon < -32 && data.north_america.max_frequency !== "No data") ||
|
|
(serverConfig.identification.lon >= -32 && data.europe.max_frequency !== "No data")) {
|
|
esSwitchCache.esSwitch = true;
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error("Error fetching Es data:", error);
|
|
});
|
|
}
|
|
|
|
return esSwitchCache.esSwitch;
|
|
}
|
|
|
|
function haversine(lat1, lon1, lat2, lon2) {
|
|
const R = 6371;
|
|
const dLat = deg2rad(lat2 - lat1);
|
|
const dLon = deg2rad(lon2 - lon1);
|
|
const a =
|
|
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
|
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
|
|
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
|
const distance = R * c;
|
|
|
|
const y = Math.sin(dLon) * Math.cos(deg2rad(lat2));
|
|
const x = Math.cos(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) -
|
|
Math.sin(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(dLon);
|
|
const azimuth = Math.atan2(y, x);
|
|
const azimuthDegrees = (azimuth * 180 / Math.PI + 360) % 360;
|
|
|
|
return {
|
|
distanceKm: distance,
|
|
azimuth: azimuthDegrees
|
|
};
|
|
}
|
|
|
|
function deg2rad(deg) {
|
|
return deg * (Math.PI / 180);
|
|
}
|
|
|
|
module.exports = {
|
|
fetchTx
|
|
}; |