Files
DRB-CnC/Client/controllers/clientController.js
Logan Cusano 4e1b82c557 Init WIP Bot
2022-12-11 06:09:25 -05:00

102 lines
4.3 KiB
JavaScript

// Debug output
const debug = require('debug')('client:clientController');
// Configs
const config = require("../config/clientConfig");
const modes = require("../config/modes");
// Utilities
const updateConfig = require("../utilities/updateConfig");
const updatePreset = require("../utilities/updatePresets");
const requests = require("../utilities/httpRequests");
/**
* Check the body for the required fields to update or add a preset
* @param req Express req from the endpoint controller
* @param res Express res from the endpoint controller
* @param callback The callback function to call when this function completes
* @returns {*}
*/
function checkBodyForPresetFields(req, res, callback) {
if (!req.body?.systemName) return res.status(403).json({"message": "No system in the request"});
if (!req.body?.frequencies && Array.isArray(req.body.frequencies)) return res.status(403).json({"message": "No frequencies in the request or type is not an array"});
if (!req.body?.mode && typeof req.body.mode === "string") return res.status(403).json({"message": "No mode in the request"});
if (!req.body?.trunkFile) {
if (modes.digitalModes.includes(req.body.mode)) return res.status(403).json({"message": "No trunk file in the request but digital mode specified. If you are not using a trunk file for this frequency make sure to specify 'none' for trunk file in the request"})
// If there is a value keep it but if not, add nothing so the system can update that key (if needed)
req.body.trunkFile = req.body.trunkFile ?? "none";
}
return callback();
}
/** Check in with the server
* If the bot has a saved ID, check in with the server to update any information or just check back in
* If the bot does not have a saved ID, it will attempt to request a new ID from the server
*/
exports.checkIn = async () => {
let reqOptions;
// Check if there is an ID found, if not add the node to the server. If there was an ID, check in with the server to make sure it has the correct information
if (config.clientConfig.id === 0) {
// ID was not found in the config, creating a new node
reqOptions = new requests.requestOptions("/nodes/newNode", "POST");
delete config.clientConfig.id;
requests.sendHttpRequest(reqOptions, JSON.stringify(config.clientConfig), (responseObject) => {
// Update the client's ID if the server accepted it
if (responseObject.statusCode === 202) {
config.clientConfig.id = responseObject.body.nodeId;
updateConfig.updateId(responseObject.body.nodeId);
}
});
}
else {
// ID is in the config, checking in with the server
reqOptions = new requests.requestOptions("/nodes/nodeCheckIn", "POST");
requests.sendHttpRequest(reqOptions, JSON.stringify(config.clientConfig), (responseObject) => {
if (responseObject.statusCode === 202) {
// Server accepted an update
}
if (responseObject.statusCode === 200) {
// Server accepted the response but there were no keys to be updated
}
});
}
}
/** Controller for the /client/requestCheckIn endpoint
* This is the endpoint wrapper to queue a check in
*/
exports.requestCheckIn = async (req, res) => {
this.checkIn();
return res.sendStatus(200);
}
/** Controller for the /client/presets endpoint
* This is the endpoint wrapper to get the presets object
*/
exports.getPresets = async (req, res) => {
return res.status(200).json(updatePreset.getPresets());
}
/** Controller for the /client/updatePreset endpoint
* This is the endpoint wrapper to update the selected preset (must include the whole object for that preset otherwise it will be rejected)
*/
exports.updatePreset = async (req, res) => {
checkBodyForPresetFields(req, res, () => {
updatePreset.updatePreset(req.body.systemName, () => {
return res.sendStatus(200);
}, {frequencies: req.body.frequencies, mode: req.body.mode, trunkFile: req.body.trunkFile});
})
}
/**
* Adds a new preset to the client
*/
exports.addNewPreset = async (req, res) => {
checkBodyForPresetFields(req, res, () => {
updatePreset.addNewPreset(req.body.systemName, req.body.frequencies, req.body.mode, () => {
return res.sendStatus(200);
}, req.body.trunkFile);
});
}