1
0
mirror of https://github.com/KubaPro010/fm-dx-webserver.git synced 2026-02-26 22:13:53 +01:00

bugfixes, global url params, rds endpoint

This commit is contained in:
NoobishSVK
2024-07-31 16:29:16 +02:00
parent 7c2a71978b
commit e9d389fef0
21 changed files with 376 additions and 251 deletions

View File

@@ -205,12 +205,13 @@ const updateInterval = 75;
var dataToSend = {
pi: '?',
freq: 87.500.toFixed(3),
previousFreq: 87.500.toFixed(3),
signal: 0,
highestSignal: -Infinity,
prevFreq: 87.500.toFixed(3),
sig: 0,
sigRaw: '',
sigTop: -Infinity,
bw: 0,
st: false,
st_forced: false,
stForced: false,
rds: false,
ps: '',
tp: 0,
@@ -224,13 +225,14 @@ var dataToSend = {
eq: 0,
ant: 0,
txInfo: {
station: '',
tx: '',
pol: '',
erp: '',
city: '',
itu: '',
distance: '',
azimuth: ''
dist: '',
azi: '',
id: ''
},
country_name: '',
country_iso: 'UN',
@@ -251,7 +253,7 @@ const initialData = { ...dataToSend };
const resetToDefault = dataToSend => Object.assign(dataToSend, initialData);
function handleData(wss, receivedData) {
function handleData(wss, receivedData, rdsWss) {
// Retrieve the last update time for this client
const currentTime = Date.now();
@@ -350,6 +352,10 @@ function handleData(wss, receivedData) {
modifiedData += errorsNew.toString(16).padStart(2, '0');
}
rdsWss.clients.forEach((client) => { // Send to /rds endpoint via WebSocket
client.send(modifiedData);
});
rdsparser.parse_string(rds, modifiedData);
legacyRdsPiBuffer = null;
break;
@@ -360,13 +366,14 @@ function handleData(wss, receivedData) {
const currentTx = fetchTx(parseFloat(dataToSend.freq).toFixed(1), dataToSend.pi, dataToSend.ps);
if(currentTx && currentTx.station !== undefined) {
dataToSend.txInfo = {
station: currentTx.station,
tx: currentTx.station,
pol: currentTx.pol,
erp: currentTx.erp,
city: currentTx.city,
itu: currentTx.itu,
distance: currentTx.distance,
azimuth: currentTx.azimuth
dist: currentTx.distance,
azi: currentTx.azimuth,
id: currentTx.id
}
}
@@ -389,20 +396,22 @@ function processSignal(receivedData, st, stForced) {
const modifiedData = receivedData.substring(2);
const parsedValue = parseFloat(modifiedData);
dataToSend.st = st;
dataToSend.st_forced = stForced;
dataToSend.stForced = stForced;
initialData.st = st;
initialData.st_forced = stForced;
initialData.stForced = stForced;
if (!isNaN(parsedValue)) {
// Convert parsedValue to a number
var signal = parseFloat(parsedValue.toFixed(2));
dataToSend.signal = signal;
initialData.signal = signal;
dataToSend.sig = signal;
initialData.sig = signal;
dataToSend.sigRaw = receivedData;
initialData.sigRaw = receivedData;
// Convert highestSignal to a number for comparison
var highestSignal = parseFloat(dataToSend.highestSignal);
var highestSignal = parseFloat(dataToSend.sigTop);
if (signal > highestSignal) {
dataToSend.highestSignal = signal.toString(); // Convert back to string for consistency
dataToSend.sigTop = signal.toString(); // Convert back to string for consistency
}
}

View File

@@ -22,6 +22,8 @@ router.get('/', (req, res) => {
return;
}
const noPlugins = req.query.noPlugins === 'true';
if (configExists() === false) {
let serialPorts;
@@ -57,6 +59,7 @@ router.get('/', (req, res) => {
tuningUpperLimit: serverConfig.webserver.tuningUpperLimit,
chatEnabled: serverConfig.webserver.chatEnabled,
device: serverConfig.device,
noPlugins,
plugins: serverConfig.plugins,
bwSwitch: serverConfig.bwSwitch ? serverConfig.bwSwitch : false
});

View File

@@ -1,4 +1,3 @@
const WebSocket = require('ws');
const dataHandler = require('./datahandler');
const storage = require('./storage');
const consoleCmd = require('./console');
@@ -55,7 +54,7 @@ function formatUptime(uptimeInSeconds) {
let incompleteDataBuffer = '';
function resolveDataBuffer(data, wss) {
function resolveDataBuffer(data, wss, rdsWss) {
var receivedData = incompleteDataBuffer + data.toString();
const isIncomplete = (receivedData.slice(-1) != '\n');
@@ -73,7 +72,7 @@ function resolveDataBuffer(data, wss) {
}
if (receivedData.length) {
dataHandler.handleData(wss, receivedData);
dataHandler.handleData(wss, receivedData, rdsWss);
};
}

View File

@@ -11,6 +11,7 @@ const httpServer = http.createServer(app);
const WebSocket = require('ws');
const wss = new WebSocket.Server({ noServer: true });
const chatWss = new WebSocket.Server({ noServer: true });
const rdsWss = new WebSocket.Server({ noServer: true });
const path = require('path');
const net = require('net');
const client = new net.Socket();
@@ -109,7 +110,7 @@ if (serverConfig.xdrd.wirelessConnection === false) {
}, 3000);
serialport.on('data', (data) => {
helpers.resolveDataBuffer(data, wss);
helpers.resolveDataBuffer(data, wss, rdsWss);
});
serialport.on('error', (error) => {
@@ -185,7 +186,7 @@ function connectToXdrd() {
};
client.on('data', (data) => {
helpers.resolveDataBuffer(data, wss);
helpers.resolveDataBuffer(data, wss, rdsWss);
if (authFlags.authMsg == true && authFlags.messageCount > 1) {
// If the limit is reached, remove the 'data' event listener
client.off('data', authDataHandler);
@@ -437,6 +438,15 @@ chatWss.on('connection', (ws, request) => {
});
});
rdsWss.on('connection', (ws, request) => {
ws.on('message', function incoming(message) {
});
ws.on('close', function close() {
});
});
// Websocket register for /text, /audio and /chat paths
httpServer.on('upgrade', (request, socket, head) => {
if (request.url === '/text') {
@@ -453,6 +463,12 @@ httpServer.on('upgrade', (request, socket, head) => {
chatWss.emit('connection', ws, request);
});
});
} else if (request.url === '/rds') {
sessionMiddleware(request, {}, () => {
rdsWss.handleUpgrade(request, socket, head, (ws) => {
rdsWss.emit('connection', ws, request);
});
});
} else {
socket.destroy();
}

View File

@@ -83,6 +83,7 @@ function processData(data, piCode, rdsPs) {
itu: matchingCity.itu,
distance: maxDistance.toFixed(0),
azimuth: txAzimuth.toFixed(0),
id: matchingStation.id,
foundStation: true
};
} else {