164 lines
6.6 KiB
JavaScript
164 lines
6.6 KiB
JavaScript
// Debug
|
|
const { DebugBuilder } = require("../utilities/debugBuilder.js");
|
|
const log = new DebugBuilder("client", "clientController");
|
|
// Configs
|
|
require('dotenv').config();
|
|
const modes = require("../config/modes");
|
|
// Modules
|
|
const { executeAsyncConsoleCommand } = require("../utilities/executeConsoleCommands.js");
|
|
// Utilities
|
|
const { updateId, updateConfig } = require("../utilities/updateConfig");
|
|
const updatePreset = require("../utilities/updatePresets");
|
|
const requests = require("../utilities/httpRequests");
|
|
const { nodeObject } = require("../utilities/recordHelper.js");
|
|
|
|
var runningClientConfig = new nodeObject({_id: process.env.CLIENT_ID, _ip: process.env.CLIENT_IP, _name: process.env.CLIENT_NAME, _port: process.env.CLIENT_PORT, _location: process.env.CLIENT_LOCATION, _nearbySystems: process.env.CLIENT_NEARBY_SYSTEMS, _online: process.env.CLIENT_ONLINE});
|
|
|
|
/**
|
|
* 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();
|
|
}
|
|
|
|
async function checkLocalIP() {
|
|
let ipAddr;
|
|
if (process.platform === "win32") {
|
|
// Windows
|
|
var networkConfig = await executeAsyncConsoleCommand("ipconfig");
|
|
log.DEBUG('Network Config: ', networkConfig);
|
|
var networkConfigLines = await networkConfig.split("\n").filter(line => {
|
|
if (!line.includes(":")) return false;
|
|
|
|
line = line.split(":");
|
|
|
|
if (!line.length === 2) return false;
|
|
|
|
return true;
|
|
}).map(line => {
|
|
line = String(line).split(':', 2);
|
|
line[0] = String(line[0]).replace(/[.]|[\s]/g, "").trim();
|
|
line[1] = String(line[1]).replace(/(\\r|\\n)/g, "").trim();
|
|
return line;
|
|
});
|
|
networkConfig = Object.fromEntries(networkConfigLines);
|
|
log.DEBUG("Parsed IP Config Results: ", networkConfig);
|
|
log.DEBUG("Local IP address: ", networkConfig['IPv4Address']);
|
|
return networkConfig['IPv4Address'];
|
|
}
|
|
else {
|
|
// Linux
|
|
var networkConfig = await executeAsyncConsoleCommand("ip addr");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Checks the config file for all required fields or gets and updates the required fields
|
|
*/
|
|
exports.checkConfig = async function checkConfig() {
|
|
if (!runningClientConfig.ip) {
|
|
const ipAddr = await checkLocalIP();
|
|
updateConfig('ip', ipAddr);
|
|
runningClientConfig.ip = ipAddr;
|
|
}
|
|
|
|
if(!runningClientConfig.name) {
|
|
const lastOctet = await String(checkLocalIP()).spit('.')[-1];
|
|
const name = `Radio-Node-${lastOctet}`;
|
|
updateConfig('name', name);
|
|
runningClientConfig.name = name;
|
|
}
|
|
|
|
if(!runningClientConfig.port) {
|
|
const port = 3010;
|
|
updateConfig('port', port);
|
|
runningClientConfig.port = port;
|
|
}
|
|
|
|
}
|
|
|
|
/** 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;
|
|
await this.checkConfig();
|
|
// 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 (clientId === 0) {
|
|
// ID was not found in the config, creating a new node
|
|
reqOptions = new requests.requestOptions("/nodes/newNode", "POST");
|
|
requests.sendHttpRequest(reqOptions, JSON.stringify(), (responseObject) => {
|
|
// Update the client's ID if the server accepted it
|
|
if (responseObject.statusCode === 202) {
|
|
runningClientConfig.id = responseObject.body.nodeId;
|
|
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(runningClientConfig), (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);
|
|
});
|
|
}
|
|
|
|
|