// Debug const { DebugBuilder } = require("../utilities/debugBuilder.js"); const log = new DebugBuilder("client", "radioController"); // Modules require('dotenv').config(); const { closeProcessWrapper, changeCurrentConfigWrapper, openRadioSessionWrapper } = require("../utilities/utilities"); let radioChildProcess; /** * Closes the radio executable if it's in one */ exports.closeRadioSession = async (req, res) => { if (!radioChildProcess || !req.body.radioSession) return res.sendStatus(204); if (radioChildProcess) radioChildProcess = await closeProcessWrapper(radioChildProcess); if (req.body.radioSession) req.body.radioSession = await closeProcessWrapper(req.body.radioSession); if (!radioChildProcess) return res.sendStatus(200); } /** * Change the current 'cfg.json' file to the preset specified * @param {string} presetName */ exports.changeCurrentConfig = async (req, res) => { const presetName = req.body.presetName; if (!presetName) return res.status(500).json("You must include the preset name") const updatedConfigObject = await changeCurrentConfigWrapper(presetName); // No change was made to the config if (!updatedConfigObject) return res.sendStatus(200); // Error was encountered if (typeof updatedConfigObject === "string") return res.status(500).json(updatedConfigObject); // There was a change made to the config, reopening the radio session if it was open if (radioChildProcess) { log.DEBUG("Radio session open, restarting to accept the new config"); const radioSessionResult = await openRadioSessionWrapper(radioChildProcess, presetName); // throw an error to the client if the wrapper ran into an error if (typeof radioSessionResult === "string") return res.status(500).json(updatedConfigObject); } return res.sendStatus(202); } /** * Open a new OP25 process tuned to the specified system */ exports.openRadioSession = async (req, res) => { const presetName = req.body.presetName; if(!presetName) return res.status(500).json({"message": "You must include the preset name to start the radio session with"}) radioChildProcess = await openRadioSessionWrapper(radioChildProcess, presetName); // throw an error to the client if the wrapper ran into an error if (typeof radioSessionResult === "string") return res.status(500).json(updatedConfigObject); return res.sendStatus(200); } /** * Attach the radio session to the request to be used elsewhere * * @param {*} req * @param {*} res */ exports.attachRadioSessionToRequest = async (req, res, next) => { req.body.radioSession = radioChildProcess; next(); }