129 lines
5.2 KiB
JavaScript
129 lines
5.2 KiB
JavaScript
// Config
|
|
const discordConfig = require("../config/discordConfig");
|
|
// Debug
|
|
const { DebugBuilder } = require("../utilities/debugBuilder.js");
|
|
const log = new DebugBuilder("server", "adminController");
|
|
// Utilities
|
|
const mysqlHandler = require("../utilities/mysqlHandler");
|
|
const utils = require("../utilities/utils");
|
|
const requests = require("../utilities/httpRequests");
|
|
|
|
/** Get the presets of all online nodes, can be used for functions
|
|
*
|
|
* @param callback Callback function
|
|
* @returns {*} A list of the systems online
|
|
*/
|
|
async function getPresetsOfOnlineNodes(callback) {
|
|
mysqlHandler.getOnlineNodes((onlineNodes) => {
|
|
let systems = {};
|
|
onlineNodes.forEach(onlineNode => {
|
|
systems[onlineNode.id] = utils.BufferToJson(onlineNode.nearbySystems);
|
|
});
|
|
|
|
callback(systems);
|
|
});
|
|
}
|
|
|
|
async function requestNodeListenToPreset(preset, nodeId, callback) {
|
|
mysqlHandler.getNodeInfoFromId(nodeId, (nodeObject) =>{
|
|
reqOptions = new requests.requestOptions("/bot/join", "POST", nodeObject.ip, nodeObject.port);
|
|
requests.sendHttpRequest(reqOptions, JSON.stringify({
|
|
"channelID": discordConfig.channelID,
|
|
"presetName": preset
|
|
}), (responseObject) => {
|
|
callback(responseObject)
|
|
});
|
|
})
|
|
}
|
|
|
|
async function getNodeBotStatus(nodeId, callback) {
|
|
mysqlHandler.getNodeInfoFromId(nodeId, (nodeObject) =>{
|
|
reqOptions = new requests.requestOptions("/bot/status", "GET", nodeObject.ip, nodeObject.port, undefined, 5);
|
|
requests.sendHttpRequest(reqOptions, JSON.stringify({}), (responseObject) => {
|
|
if (responseObject === false) {
|
|
// Bot is joined
|
|
}
|
|
else {
|
|
// Bot is free
|
|
}
|
|
callback(responseObject);
|
|
});
|
|
});
|
|
}
|
|
|
|
async function requestNodeLeaveServer(nodeId, callback) {
|
|
getNodeBotStatus(nodeId, (responseObject) => {
|
|
if (responseObject === false) {
|
|
// Bot is joined
|
|
mysqlHandler.getNodeInfoFromId(nodeId, (nodeObject) =>{
|
|
reqOptions = new requests.requestOptions("/bot/leave", "POST", nodeObject.ip, nodeObject.port);
|
|
requests.sendHttpRequest(reqOptions, JSON.stringify({}), (responseObject) => {
|
|
callback(responseObject);
|
|
});
|
|
});
|
|
}
|
|
else {
|
|
// Bot is free
|
|
callback(false);
|
|
}
|
|
})
|
|
}
|
|
|
|
|
|
/** Return to requests for the presets of all online nodes, cannot be used in functions
|
|
*
|
|
* @param {*} req Express request parameter
|
|
* @param {*} res Express response parameter
|
|
*/
|
|
exports.getAvailablePresets = async (req, res) => {
|
|
await getPresetsOfOnlineNodes((systems) => {
|
|
res.status(200).json({
|
|
"systemsOnline": systems
|
|
});
|
|
})
|
|
}
|
|
|
|
/** Request a node to join the server listening to a specific preset
|
|
*
|
|
* @param {*} req Express request parameter
|
|
* @var {*} req.body.preset The preset to join (REQ)
|
|
* @var {*} req.body.nodeId The specific node to join (OPT/REQ if more than one node has the preset)
|
|
* @param {*} res Express response parameter
|
|
*/
|
|
exports.joinPreset = async (req, res) => {
|
|
if (!req.body.preset) return res.status(400).json("No preset specified");
|
|
await getPresetsOfOnlineNodes((systems) => {
|
|
const systemsWithSelectedPreset = Object.values(systems).filter(nodePresets => nodePresets.includes(req.body.preset)).length
|
|
if (!systemsWithSelectedPreset) return res.status(400).json("No system online with that preset");
|
|
if (systemsWithSelectedPreset > 1) {
|
|
if (!req.body.nodeId) return res.status(175).json("Multiple locations with the selected channel, please specify a nodeID (nodeId)")
|
|
requestNodeListenToPreset(req.body.preset, req.body.nodeId, (responseObject) => {
|
|
if (responseObject === false) return res.status(400).json("Timeout reached");
|
|
return res.sendStatus(responseObject.statusCode);
|
|
});
|
|
}
|
|
else {
|
|
let nodeId;
|
|
if (!req.body.nodeId) nodeId = utils.getKeyByArrayValue(systems, req.body.preset);
|
|
else nodeId = req.body.nodeId;
|
|
requestNodeListenToPreset(req.body.preset, nodeId, (responseObject) => {
|
|
if (responseObject === false) return res.status(400).json("Timeout reached");
|
|
return res.sendStatus(responseObject.statusCode);
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
/** Request a node to join the server listening to a specific preset
|
|
*
|
|
* @param {*} req Express request parameter
|
|
* @param {*} res Express response parameter
|
|
*/
|
|
exports.leaveServer = async (req, res) => {
|
|
if (!req.body.nodeId) return res.status(400).json("No nodeID specified");
|
|
|
|
requestNodeLeaveServer(req.body.nodeId, (responseObject) => {
|
|
if (responseObject === false) return res.status(400).json("Bot not joined to server");
|
|
return res.sendStatus(responseObject.statusCode);
|
|
});
|
|
} |