Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tx_search.js #109

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions server/tx_search.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,41 @@ const esSwitchCache = {"lastCheck":0, "esSwitch":false};
const esFetchInterval = 300000;
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
let Latitude = serverConfig.identification.lat;
let Longitude = serverConfig.identification.lon;

// Create WebSocket URL
const webserverPort = serverConfig.webserver.webserverPort || 8080; // Fallback to port 8080
const externalWsUrl = `ws://127.0.0.1:${webserverPort}/data_plugins`;
const WebSocket = require('ws');

// 5-second delay before activation
setTimeout(() => {
const websocket = new WebSocket(externalWsUrl);

// Event listener to receive data
websocket.on('message', (data) => {
try {
// Parse the received data
const parsedData = JSON.parse(data);

// Check if the dataset is of type GPS
if (parsedData.type === "GPS" && parsedData.value) {
const gpsData = parsedData.value;
const { status, time, lat, lon, alt, mode } = gpsData;

if (status === "active") {
Latitude = parseFloat(lat);
Longitude = parseFloat(lon);
}
}
} catch (error) {
logError("Error processing WebSocket data:", error);
}
});

}, 5000);


// Load the US states GeoJSON data
async function loadUsStatesGeoJson() {
Expand Down Expand Up @@ -67,7 +102,8 @@ async function fetchTx(freq, piCode, rdsPs) {
if (isNaN(freq)) {
return;
}
if (now - lastFetchTime < fetchInterval || serverConfig.identification.lat.length < 2 || freq < 87) {

if (now - lastFetchTime < fetchInterval || Latitude.length < 2 || freq < 87) {
return Promise.resolve();
}

Expand Down Expand Up @@ -124,7 +160,7 @@ async function processData(data, piCode, rdsPs) {
if (city.stations) {
for (const station of city.stations) {
if (station.pi === piCode.toUpperCase() && !station.extra && station.ps && station.ps.toLowerCase().includes(rdsPs.replace(/ /g, '_').replace(/^_*(.*?)_*$/, '$1').toLowerCase())) {
const distance = haversine(serverConfig.identification.lat, serverConfig.identification.lon, city.lat, city.lon);
const distance = haversine(Latitude, Longitude, city.lat, city.lon);
evaluateStation(station, city, distance);
detectedByPireg = false;
}
Expand All @@ -139,7 +175,7 @@ async function processData(data, piCode, rdsPs) {
if (city.stations) {
for (const station of city.stations) {
if (station.pireg && station.pireg.toUpperCase() === piCode.toUpperCase() && !station.extra && station.ps && station.ps.toLowerCase().includes(rdsPs.replace(/ /g, '_').replace(/^_*(.*?)_*$/, '$1').toLowerCase())) {
const distance = haversine(serverConfig.identification.lat, serverConfig.identification.lon, city.lat, city.lon);
const distance = haversine(Latitude, Longitude, city.lat, city.lon);
evaluateStation(station, city, distance);
detectedByPireg = true;
}
Expand Down Expand Up @@ -182,12 +218,12 @@ function checkEs() {

if (now - esSwitchCache.lastCheck < esFetchInterval) {
esSwitch = esSwitchCache.esSwitch;
} else if (serverConfig.identification.lat > 20) {
} else if (Latitude > 20) {
esSwitchCache.lastCheck = now;
fetch(url)
.then(response => response.json())
.then(data => {
if (serverConfig.identification.lon < -32) {
if (Longitude < -32) {
if (data.north_america.max_frequency != "No data") {
esSwitch = true;
}
Expand Down