1
0
mirror of https://github.com/KubaPro010/fm-dx-webserver.git synced 2026-02-26 22:13:53 +01:00
This commit is contained in:
NoobishSVK
2024-07-15 22:21:47 +02:00
7 changed files with 223 additions and 41 deletions

View File

@@ -22,24 +22,41 @@ function parsePluginConfig(filePath) {
// Check if pluginConfig has frontEndPath defined
if (pluginConfig.frontEndPath) {
const sourcePath = path.join(path.dirname(filePath), pluginConfig.frontEndPath);
const destinationDir = path.join(path.dirname(filePath), '../web/js/plugins', pluginConfig.frontEndPath, '..');
const destinationDir = path.join(__dirname, '../web/js/plugins', path.dirname(pluginConfig.frontEndPath));
// Check if the source path exists
if (!fs.existsSync(sourcePath)) {
console.error(`Error: source path ${sourcePath} does not exist.`);
return pluginConfig;
}
// Check if the destination directory exists, if not, create it
if (!fs.existsSync(destinationDir)) {
fs.mkdirSync(destinationDir, { recursive: true }); // Create directory recursively
}
// Copy the file to the destination directory
const destinationFile = path.join(destinationDir, path.basename(sourcePath));
fs.copyFileSync(sourcePath, destinationFile);
setTimeout(function() {
consoleCmd.logInfo(`Plugin ${pluginConfig.name} ${pluginConfig.version} initialized successfully.`);
}, 500)
// Platform-specific handling for symlinks/junctions
if (process.platform !== 'win32') {
// On Linux, create a symlink
try {
if (fs.existsSync(destinationFile)) {
fs.unlinkSync(destinationFile); // Remove existing file/symlink
}
fs.symlinkSync(sourcePath, destinationFile);
setTimeout(function() {
consoleCmd.logInfo(`Plugin ${pluginConfig.name} ${pluginConfig.version} initialized successfully.`);
}, 500)
} catch (err) {
console.error(`Error creating symlink at ${destinationFile}: ${err.message}`);
}
}
} else {
console.error(`Error: frontEndPath is not defined in ${filePath}`);
}
} catch (err) {
console.error(`Error parsing plugin config from ${filePath}: ${err}`);
console.error(`Error parsing plugin config from ${filePath}: ${err.message}`);
}
return pluginConfig;
@@ -62,9 +79,37 @@ function collectPluginConfigs() {
return pluginConfigs;
}
// Ensure the web/js/plugins directory exists
const webJsPluginsDir = path.join(__dirname, '../web/js/plugins');
if (!fs.existsSync(webJsPluginsDir)) {
fs.mkdirSync(webJsPluginsDir, { recursive: true });
}
// Main function to create symlinks/junctions for plugins
function createLinks() {
const pluginsDir = path.join(__dirname, '../plugins');
const destinationPluginsDir = path.join(__dirname, '../web/js/plugins');
if (process.platform === 'win32') {
// On Windows, create a junction
try {
if (fs.existsSync(destinationPluginsDir)) {
fs.rmSync(destinationPluginsDir, { recursive: true });
}
fs.symlinkSync(pluginsDir, destinationPluginsDir, 'junction');
setTimeout(function() {
consoleCmd.logInfo(`Plugin ${pluginConfig.name} ${pluginConfig.version} initialized successfully.`);
}, 500)
} catch (err) {
console.error(`Error creating junction at ${destinationPluginsDir}: ${err.message}`);
}
}
}
// Usage example
const allPluginConfigs = collectPluginConfigs();
createLinks();
module.exports = {
allPluginConfigs
}
};

View File

@@ -6,6 +6,9 @@ let cachedData = {};
let lastFetchTime = 0;
const fetchInterval = 3000;
const esSwitchCache = {"lastCheck":0, "esSwitch":false};
const esFetchInterval = 300000;
// Fetch data from maps
function fetchTx(freq, piCode, rdsPs) {
const now = Date.now();
@@ -46,6 +49,7 @@ function processData(data, piCode, rdsPs) {
let maxScore = -Infinity; // Initialize maxScore with a very low value
let txAzimuth;
let maxDistance;
let esMode = checkEs();
for (const cityId in data.locations) {
const city = data.locations[cityId];
@@ -53,7 +57,11 @@ function processData(data, piCode, rdsPs) {
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 score = (10*Math.log10(station.erp*1000)) / distance.distanceKm; // Calculate score
let weightDistance = distance.distanceKm
if (esMode && (distance.distanceKm > 200)) {
weightDistance = Math.abs(distance.distanceKm-1500);
}
const score = (10*Math.log10(station.erp*1000)) / weightDistance; // Calculate score
if (score > maxScore) {
maxScore = score;
txAzimuth = distance.azimuth;
@@ -82,6 +90,37 @@ function processData(data, piCode, rdsPs) {
}
}
function checkEs() {
const now = Date.now();
const url = "https://fmdx.org/includes/tools/get_muf.php";
let esSwitch = false;
if (now - esSwitchCache.lastCheck < esFetchInterval) {
esSwitch = esSwitchCache.esSwitch;
} else {
esSwitchCache.lastCheck = now;
fetch(url)
.then(response => response.json())
.then(data => {
if (serverConfig.identification.lon < -32) {
if (data.north_america.max_frequency != "No data") {
esSwitch = true;
}
} else {
if (data.europe.max_frequency != "No data") {
esSwitch = true;
}
}
esSwitchCache.esSwitch = esSwitch;
})
.catch(error => {
console.error("Error fetching data:", error);
});
}
return esSwitch;
}
function haversine(lat1, lon1, lat2, lon2) {
const R = 6371; // Earth radius in kilometers
const dLat = deg2rad(lat2 - lat1);