From 956dc89107e1ae3da0907e520bf302d26f430d49 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sun, 3 Mar 2024 20:49:29 -0500 Subject: [PATCH] Adding functional usage of client self updater #10 - Added update command to the server - Server can request nodes update - Nodes have an 'endpoint' for updating - Fixes to the install script --- client/modules/selfUpdater.mjs | 2 ++ client/modules/socketClient.mjs | 5 +++- client/modules/socketClientWrappers.mjs | 20 +++++++++++++--- client/setup.sh | 4 ++-- server/discordBot/commands/update.mjs | 32 +++++++++++++++++++++++++ server/modules/socketServerWrappers.mjs | 15 ++++++++++++ 6 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 server/discordBot/commands/update.mjs diff --git a/client/modules/selfUpdater.mjs b/client/modules/selfUpdater.mjs index 7a84963..28dbabe 100644 --- a/client/modules/selfUpdater.mjs +++ b/client/modules/selfUpdater.mjs @@ -24,8 +24,10 @@ export const checkForUpdates = async () => { console.log('Update completed successfully. Restarting the application...'); // Restart the application to apply the updates restartApplication(); + return true } else { console.log('The application is up to date.'); + return false } } catch (error) { console.error('Error checking for updates:', error); diff --git a/client/modules/socketClient.mjs b/client/modules/socketClient.mjs index 84e3466..d41b766 100644 --- a/client/modules/socketClient.mjs +++ b/client/modules/socketClient.mjs @@ -1,5 +1,5 @@ import { io } from "socket.io-client"; -import { logIntoServerWrapper, nodeCheckStatus, nodeJoinServer, nodeLeaveServer, nodeGetUsername, nodeCheckDiscordClientStatus, nodeCheckCurrentSystem } from "./socketClientWrappers.mjs"; +import { logIntoServerWrapper, nodeCheckStatus, nodeJoinServer, nodeLeaveServer, nodeGetUsername, nodeCheckDiscordClientStatus, nodeCheckCurrentSystem, nodeUpdate } from "./socketClientWrappers.mjs"; /** * Initialize the socket connection with the server, this will handle disconnects within itself @@ -24,6 +24,9 @@ export const initSocketConnection = async (localNodeConfig) => { }); // Node events/commands + // Requested the node update itself + socket.on('node-update', nodeUpdate); + // Requested to join a discord guild and listen to a system socket.on('node-join', nodeJoinServer); diff --git a/client/modules/socketClientWrappers.mjs b/client/modules/socketClientWrappers.mjs index c8a2d83..1c01063 100644 --- a/client/modules/socketClientWrappers.mjs +++ b/client/modules/socketClientWrappers.mjs @@ -1,5 +1,17 @@ import { checkIfDiscordVCConnected, joinDiscordVC, leaveDiscordVC, getDiscordUsername, checkIfClientIsOpen } from '../discordAudioBot/dabWrappers.mjs'; import { getCurrentSystem } from '../op25Handler/op25Handler.mjs'; +import { checkForUpdates } from './selfUpdater.mjs'; + + +/** + * Check if the bot has an update available + * @param {any} socketCallback The callback function to return the result + * @callback {boolean} If the node has an update available or not + */ +export const nodeUpdate = async (socketCallback) => { + socketCallback(await checkForUpdates()); +} + /** * Wrapper to log into the server @@ -15,6 +27,7 @@ export const logIntoServerWrapper = async (socket, localNodeConfig) => { sendNodeUpdateWrapper(socket, localNodeConfig); } + /** * Send the server an update * @param {any} socket The socket connection with the server @@ -32,7 +45,7 @@ export const sendNodeUpdateWrapper = async (socket, localNodeConfig) => { * Join the requested server VC and listen to the requested system * @param {object} joinData The object containing all the information to join the server */ -export const nodeJoinServer = async (joinData) => { +export const nodeJoinServer = async (joinData) => { await joinDiscordVC(joinData); } @@ -52,7 +65,7 @@ export const nodeLeaveServer = async (guildId) => { * @callback {boolean} If the node is connected to VC in the given guild */ export const nodeCheckStatus = async (guildId, socketCallback) => { - socketCallback(await checkIfDiscordVCConnected(guildId)); + socketCallback(await checkIfDiscordVCConnected(guildId)); } @@ -64,7 +77,7 @@ export const nodeCheckStatus = async (guildId, socketCallback) => { * @callback {any} */ export const nodeGetUsername = async (guildId, socketCallback) => { - socketCallback(await getDiscordUsername(guildId)); + socketCallback(await getDiscordUsername(guildId)); } @@ -76,6 +89,7 @@ export const nodeCheckDiscordClientStatus = async (socketCallback) => { socketCallback(await checkIfClientIsOpen()); } + /** * Check what system the local node is currently listening to * @callback {boolean} If the node has an open discord client or not diff --git a/client/setup.sh b/client/setup.sh index c100d41..d26874a 100644 --- a/client/setup.sh +++ b/client/setup.sh @@ -49,7 +49,7 @@ prompt_nearby_system() { fi echo "\"$system_name\": { - \"frequencies\": [$(echo "$frequencies" | sed 's/,/","/g')], + \"frequencies\": [\"$(echo "$frequencies" | sed 's/,/","/g')\"], \"mode\": \"$mode\", \"trunkFile\": \"$trunk_file\", \"whitelistFile\": \"$whitelist_file\" @@ -113,7 +113,7 @@ systems_json="${systems_json%,}" # Remove trailing comma systems_json+="}" # Append the created systems to the presets file -mkdir ./config +mkdir -p ./config echo "$systems_json" >> "./config/radioPresets.json" echo "Systems added to radioPresets.json." diff --git a/server/discordBot/commands/update.mjs b/server/discordBot/commands/update.mjs new file mode 100644 index 0000000..122469b --- /dev/null +++ b/server/discordBot/commands/update.mjs @@ -0,0 +1,32 @@ +import { SlashCommandBuilder } from 'discord.js'; +import { requestNodeUpdate } from '../../modules/socketServerWrappers.mjs'; + +// Exporting data property that contains the command structure for discord including any params +export const data = new SlashCommandBuilder() + .setName('update') + .setDescription('Updates all nodes currently logged on'); + +// Exporting other properties +export const example = "/update"; // An example of how the command would be run in discord chat, this will be used for the help command +export const deferInitialReply = false; // If we the initial reply in discord should be deferred. This gives extra time to respond, however the method of replying is different. + +/** + * The function to run when the command is called by a discord user + * @param {any} nodeIo The nodeIO server for manipulation of sockets + * @param {any} interaction The interaction object + */ +export const execute = async (nodeIo, interaction) => { + try { + const sockets = await nodeIo.allSockets(); + console.log("All open sockets: ",sockets); + await sockets.map(openSocket => { + requestNodeUpdate(openSocket); + }) + //await interaction.reply(`**Online Sockets: '${sockets}'**`); + await interaction.reply('**Pong.**'); + //await interaction.channel.send('**Pong.**'); + } catch (err) { + console.error(err); + // await interaction.reply(err.toString()); + } +} \ No newline at end of file diff --git a/server/modules/socketServerWrappers.mjs b/server/modules/socketServerWrappers.mjs index 41e0df2..30efd76 100644 --- a/server/modules/socketServerWrappers.mjs +++ b/server/modules/socketServerWrappers.mjs @@ -282,4 +282,19 @@ export const requestNodeJoinSystem = async (socket, systemName, discordChanelId) export const requestBotLeaveServer = async (socket, guildId) => { // Send the command to the node await sendNodeCommand(socket, "node-leave", guildId); +} + + +/** + * Requset a given socket node to update themselves + * @param {any} socket The socket object of the node to request to update + */ +export const requestNodeUpdate = async (socket) => { + await sendNodeCommand(socket, 'node-update', (status) => { + if (status) { + console.log("Node is out of date, updating now", socket.node.name); + } else { + console.log("Node is up to date", socket.node.name); + } + }); } \ No newline at end of file