You've already forked fm-dx-webserver
mirror of
https://github.com/KubaPro010/fm-dx-webserver.git
synced 2026-02-27 06:23:53 +01:00
TX ID, UI fixes
This commit is contained in:
127
tx_search.js
Normal file
127
tx_search.js
Normal file
@@ -0,0 +1,127 @@
|
||||
const fetch = require('node-fetch');
|
||||
var fs = require('fs');
|
||||
|
||||
let cachedData = {};
|
||||
|
||||
let serverConfig = {
|
||||
identification: {
|
||||
lat: 0,
|
||||
lon: 0
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
if(fs.existsSync('config.json')) {
|
||||
const configFileContents = fs.readFileSync('config.json', 'utf8');
|
||||
serverConfig = JSON.parse(configFileContents);
|
||||
}
|
||||
|
||||
let lastFetchTime = 0;
|
||||
const fetchInterval = 3000;
|
||||
|
||||
// Fetch data from maps
|
||||
function fetchTx(freq, piCode, rdsPs) {
|
||||
const now = Date.now();
|
||||
|
||||
// Check if it's been at least 3 seconds since the last fetch and if the QTH is correct
|
||||
if (now - lastFetchTime < fetchInterval || serverConfig.identification.lat.length < 2) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
lastFetchTime = now;
|
||||
|
||||
// Check if data for the given frequency is already cached
|
||||
if (cachedData[freq]) {
|
||||
return processData(cachedData[freq], piCode, rdsPs);
|
||||
}
|
||||
|
||||
const url = "https://maps.fmdx.pl/controller.php?freq=" + freq;
|
||||
|
||||
return fetch(url)
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
// Cache the fetched data for the specific frequency
|
||||
cachedData[freq] = data;
|
||||
return processData(data, piCode, rdsPs);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error("Error fetching data:", error);
|
||||
});
|
||||
}
|
||||
|
||||
function processData(data, piCode, rdsPs) {
|
||||
let matchingStation = null;
|
||||
let matchingCity = null;
|
||||
let minDistance = Infinity;
|
||||
let txAzimuth;
|
||||
|
||||
for (const cityId in data.locations) {
|
||||
const city = data.locations[cityId];
|
||||
if (city.stations) {
|
||||
for (const station of city.stations) {
|
||||
if (station.pi === piCode && station.ps.includes(rdsPs.replace(/ /g, '_'))) {
|
||||
const distance = haversine(serverConfig.identification.lat, serverConfig.identification.lon, city.lat, city.lon);
|
||||
if (distance.distanceKm < minDistance) {
|
||||
minDistance = distance.distanceKm;
|
||||
txAzimuth = distance.azimuth;
|
||||
matchingStation = station;
|
||||
matchingCity = city;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (matchingStation) {
|
||||
return {
|
||||
station: matchingStation.station.replace("R.", "Radio "),
|
||||
pol: matchingStation.pol.toUpperCase(),
|
||||
erp: matchingStation.erp,
|
||||
city: matchingCity.name,
|
||||
itu: matchingCity.itu,
|
||||
distance: minDistance.toFixed(0),
|
||||
azimuth: txAzimuth.toFixed(0),
|
||||
foundStation: true
|
||||
};
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
function haversine(lat1, lon1, lat2, lon2) {
|
||||
const R = 6371; // Earth radius in kilometers
|
||||
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));
|
||||
|
||||
// Distance in kilometers
|
||||
const distance = R * c;
|
||||
|
||||
// Azimuth calculation
|
||||
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);
|
||||
|
||||
// Convert azimuth from radians to degrees
|
||||
const azimuthDegrees = (azimuth * 180 / Math.PI + 360) % 360;
|
||||
|
||||
return {
|
||||
distanceKm: distance,
|
||||
azimuth: azimuthDegrees
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
function deg2rad(deg) {
|
||||
return deg * (Math.PI / 180);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
fetchTx
|
||||
};
|
||||
Reference in New Issue
Block a user