From 8ad8015ae7e49154d5c0172986e2a6f12a82730a Mon Sep 17 00:00:00 2001 From: NoobishSVK Date: Wed, 7 Feb 2024 20:18:38 +0100 Subject: [PATCH] logging improvements, connection changes, ui changes --- console.js | 15 +++- datahandler.js | 2 +- index.js | 153 +++++++++++++++++++++------------------- stream/index.js | 8 +-- web/css/breadcrumbs.css | 6 +- web/css/buttons.css | 7 -- web/index.ejs | 4 +- 7 files changed, 108 insertions(+), 87 deletions(-) diff --git a/console.js b/console.js index 6f36bc7..4fe0a0c 100644 --- a/console.js +++ b/console.js @@ -1,4 +1,5 @@ const verboseMode = process.argv.includes('--debug'); +const verboseModeFfmpeg = process.argv.includes('--ffmpegdebug'); const getCurrentTime = () => { const currentTime = new Date(); @@ -10,6 +11,7 @@ const getCurrentTime = () => { const MESSAGE_PREFIX = { DEBUG: "\x1b[36m[DEBUG]\x1b[0m", ERROR: "\x1b[31m[ERROR]\x1b[0m", + FFMPEG: "\x1b[36m[FFMPEG]\x1b[0m", INFO: "\x1b[32m[INFO]\x1b[0m", WARN: "\x1b[33m[WARN]\x1b[0m", }; @@ -38,6 +40,17 @@ const logError = (...messages) => { console.log(logMessage); }; +const logFfmpeg = (...messages) => { + if (verboseModeFfmpeg) { + const logMessage = `${getCurrentTime()} ${MESSAGE_PREFIX.FFMPEG} ${messages.join(' ')}`; + logs.push(logMessage); + if (logs.length > maxLogLines) { + logs.shift(); // Remove the oldest log if the array exceeds the maximum number of lines + } + console.log(logMessage); + } +}; + const logInfo = (...messages) => { const logMessage = `${getCurrentTime()} ${MESSAGE_PREFIX.INFO} ${messages.join(' ')}`; logs.push(logMessage); @@ -57,5 +70,5 @@ const logWarn = (...messages) => { }; module.exports = { - logError, logDebug, logInfo, logWarn, logs + logError, logDebug, logFfmpeg, logInfo, logWarn, logs }; diff --git a/datahandler.js b/datahandler.js index aceab51..9c86162 100644 --- a/datahandler.js +++ b/datahandler.js @@ -341,7 +341,7 @@ function handleData(ws, receivedData) { } // Get the received TX info - const currentTx = fetchTx(dataToSend.freq, dataToSend.pi, dataToSend.ps); + const currentTx = fetchTx(parseFloat(dataToSend.freq).toFixed(1), dataToSend.pi, dataToSend.ps); if(currentTx && currentTx.station !== undefined) { dataToSend.txInfo = { station: currentTx.station, diff --git a/index.js b/index.js index 212fe53..492a987 100644 --- a/index.js +++ b/index.js @@ -100,106 +100,117 @@ function authenticateWithXdrd(client, salt, password) { client.write('x\n'); } -if(serverConfig.identification.tunerName.includes('zvartoshu')) { - process.exit(1); -} +connectToXdrd(); // xdrd connection -if (serverConfig.xdrd.xdrdPassword.length > 1) { - client.connect(serverConfig.xdrd.xdrdPort, serverConfig.xdrd.xdrdIp, () => { - logInfo('Connection to xdrd established successfully.'); - - const authFlags = { - authMsg: false, - firstClient: false, - receivedPassword: false - }; - - const authDataHandler = (data) => { - const receivedData = data.toString(); - const lines = receivedData.split('\n'); +function connectToXdrd() { + if (serverConfig.xdrd.xdrdPassword.length > 1) { + client.connect(serverConfig.xdrd.xdrdPort, serverConfig.xdrd.xdrdIp, () => { + logInfo('Connection to xdrd established successfully.'); - for (const line of lines) { + const authFlags = { + authMsg: false, + firstClient: false, + receivedPassword: false + }; + + const authDataHandler = (data) => { + const receivedData = data.toString(); + const lines = receivedData.split('\n'); - if (!authFlags.receivedPassword) { - authFlags.receivedSalt = line.trim(); - authenticateWithXdrd(client, authFlags.receivedSalt, serverConfig.xdrd.xdrdPassword); - authFlags.receivedPassword = true; - } else { - if (line.startsWith('a')) { - authFlags.authMsg = true; - logWarn('Authentication with xdrd failed. Is your password set correctly?'); - } else if (line.startsWith('o1,')) { - authFlags.firstClient = true; - } else if (line.startsWith('T') && line.length <= 7) { - const freq = line.slice(1) / 1000; - dataHandler.dataToSend.freq = freq.toFixed(3); - } else if (line.startsWith('OK')) { - authFlags.authMsg = true; - logInfo('Authentication with xdrd successful.'); - } + for (const line of lines) { - if (authFlags.authMsg && authFlags.firstClient) { - client.write('T87500\n'); - client.write('A0\n'); - client.write('G11\n'); - client.off('data', authDataHandler); - return; + if (!authFlags.receivedPassword) { + authFlags.receivedSalt = line.trim(); + authenticateWithXdrd(client, authFlags.receivedSalt, serverConfig.xdrd.xdrdPassword); + authFlags.receivedPassword = true; + } else { + if (line.startsWith('a')) { + authFlags.authMsg = true; + logWarn('Authentication with xdrd failed. Is your password set correctly?'); + } else if (line.startsWith('o1,')) { + authFlags.firstClient = true; + } else if (line.startsWith('T') && line.length <= 7) { + const freq = line.slice(1) / 1000; + dataHandler.dataToSend.freq = freq.toFixed(3); + } else if (line.startsWith('OK')) { + authFlags.authMsg = true; + logInfo('Authentication with xdrd successful.'); + } + + if (authFlags.authMsg && authFlags.firstClient) { + client.write('T87500\n'); + client.write('A0\n'); + client.write('G11\n'); + client.off('data', authDataHandler); + return; + } } } - } - }; - - client.on('data', (data) => { - var receivedData = incompleteDataBuffer + data.toString(); - const isIncomplete = (receivedData.slice(-1) != '\n'); + }; - if (isIncomplete) { - const position = receivedData.lastIndexOf('\n'); - if (position < 0) { - incompleteDataBuffer = receivedData; - receivedData = ''; + client.on('data', (data) => { + var receivedData = incompleteDataBuffer + data.toString(); + const isIncomplete = (receivedData.slice(-1) != '\n'); + + if (isIncomplete) { + const position = receivedData.lastIndexOf('\n'); + if (position < 0) { + incompleteDataBuffer = receivedData; + receivedData = ''; + } else { + incompleteDataBuffer = receivedData.slice(position + 1); + receivedData = receivedData.slice(0, position + 1); + } } else { - incompleteDataBuffer = receivedData.slice(position + 1); - receivedData = receivedData.slice(0, position + 1); + incompleteDataBuffer = ''; } - } else { - incompleteDataBuffer = ''; - } + + if (receivedData.length) { + wss.clients.forEach((client) => { + if (client.readyState === WebSocket.OPEN) { + dataHandler.handleData(client, receivedData); + } + }); + } + }); - if (receivedData.length) { - wss.clients.forEach((client) => { - if (client.readyState === WebSocket.OPEN) { - dataHandler.handleData(client, receivedData); - } - }); - } + client.on('data', authDataHandler); }); - - client.on('data', authDataHandler); - }); + } } client.on('close', () => { - logWarn('Disconnected from xdrd.'); + logWarn('Disconnected from xdrd. Attempting to reconnect.'); + setTimeout(function () { + connectToXdrd(); + }, 2000) }); client.on('error', (err) => { + setTimeout(function () { + connectToXdrd(); + }, 2000) switch (true) { case err.message.includes("ECONNRESET"): - logError("Connection to xdrd lost. Exiting..."); - process.exit(1); + logError("Connection to xdrd lost. Reconnecting..."); + break; case err.message.includes("ETIMEDOUT"): - logError("Connection to xdrd @ " + serverConfig.xdrd.xdrdIp + ":" + serverConfig.xrd.xdrdPort + " timed out."); - process.exit(1); + logError("Connection to xdrd @ " + serverConfig.xdrd.xdrdIp + ":" + serverConfig.xdrd.xdrdPort + " timed out."); + break; case err.message.includes("ECONNREFUSED"): logError("Connection to xdrd @ " + serverConfig.xdrd.xdrdIp + ":" + serverConfig.xdrd.xdrdPort + " failed. Is xdrd running?"); break; + case err.message.includes("EINVAL"): + logError("Attempts to reconnect are failing repeatedly. Consider checking your settings or restarting xdrd."); + break; + default: logError("Unhandled error: ", err.message); + break; } }); diff --git a/stream/index.js b/stream/index.js index bd796e2..504801e 100644 --- a/stream/index.js +++ b/stream/index.js @@ -43,21 +43,21 @@ function enableAudioStream() { // Handle the output of the child process (optional) childProcess.stdout.on('data', (data) => { - consoleCmd.logDebug(`stdout: ${data}`); + consoleCmd.logFfmpeg(`stdout: ${data}`); }); childProcess.stderr.on('data', (data) => { - consoleCmd.logDebug(`stderr: ${data}`); + consoleCmd.logFfmpeg(`stderr: ${data}`); }); // Handle the child process exit event childProcess.on('close', (code) => { - consoleCmd.logDebug(`Child process exited with code ${code}`); + consoleCmd.logFfmpeg(`Child process exited with code ${code}`); }); // You can also listen for the 'error' event in case the process fails to start childProcess.on('error', (err) => { - consoleCmd.logError(`Error starting child process: ${err}`); + consoleCmd.logFfmpeg(`Error starting child process: ${err}`); }); } } diff --git a/web/css/breadcrumbs.css b/web/css/breadcrumbs.css index 9566987..bfbb35f 100644 --- a/web/css/breadcrumbs.css +++ b/web/css/breadcrumbs.css @@ -249,7 +249,7 @@ label { } } -@media only screen and (min-width: 960px) and (max-height: 860px) { +@media only screen and (min-width: 769px) and (max-height: 860px) { #rt-container { height: 90px !important; } @@ -268,4 +268,8 @@ label { float: right; text-align: right; } + h2 { + margin-bottom: 10px; + font-size: 18px; + } } \ No newline at end of file diff --git a/web/css/buttons.css b/web/css/buttons.css index f63dacd..4f573a9 100644 --- a/web/css/buttons.css +++ b/web/css/buttons.css @@ -84,13 +84,6 @@ input[type="range"] { background: transparent; } -input[type="range"]::after { - - width: 100px; - height: 100px; - background: blue; -} - /* Track: Mozilla Firefox */ input[type="range"]::-moz-range-track { height: 48px; diff --git a/web/index.ejs b/web/index.ejs index 26f4162..e33ea45 100644 --- a/web/index.ejs +++ b/web/index.ejs @@ -133,7 +133,7 @@
- +
@@ -253,7 +253,7 @@
 Support the developer!
-

FM-DX WebServer v1.0.2 [6/2/2024] by Noobish & the OpenRadio community.

+

FM-DX WebServer v1.0.3 [7/2/2024] by Noobish & the OpenRadio community.

This app works thanks to these amazing projects:
- librdsparser & maps.fmdx.pl by Konrad Kosmatka
- 3LAS by JoJoBond