Init WIP Bot
This commit is contained in:
48
Client/utilities/httpRequests.js
Normal file
48
Client/utilities/httpRequests.js
Normal file
@@ -0,0 +1,48 @@
|
||||
// Debug
|
||||
const debug = require('debug')('client:httpRequests');
|
||||
// Config
|
||||
const config = require("../config/clientConfig");
|
||||
// Modules
|
||||
const http = require("http");
|
||||
|
||||
exports.requestOptions = class requestOptions {
|
||||
constructor(path, method, hostname = undefined, headers = undefined, port = undefined) {
|
||||
if (method === "POST"){
|
||||
this.hostname = hostname ?? config.serverConfig.hostname
|
||||
this.path = path
|
||||
this.port = port ?? config.serverConfig.port
|
||||
this.method = method
|
||||
this.headers = headers ?? {
|
||||
'Content-Type': 'application/json',
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the HTTP request to the server
|
||||
* @param requestOptions
|
||||
* @param data
|
||||
* @param callback
|
||||
*/
|
||||
exports.sendHttpRequest = function sendHttpRequest(requestOptions, data, callback){
|
||||
debug("Sending a request to: ", requestOptions.hostname, requestOptions.port)
|
||||
// Create the request
|
||||
const req = http.request(requestOptions, res => {
|
||||
res.on('data', (data) => {
|
||||
const responseObject = {
|
||||
"statusCode": res.statusCode,
|
||||
"body": JSON.parse(data)
|
||||
};
|
||||
debug("Response Object: ", responseObject);
|
||||
callback(responseObject);
|
||||
})
|
||||
}).on('error', err => {
|
||||
debug('Error: ', err.message)
|
||||
// TODO need to handle if the server is down
|
||||
})
|
||||
|
||||
// Write the data to the request and send it
|
||||
req.write(data)
|
||||
req.end()
|
||||
}
|
||||
41
Client/utilities/updateConfig.js
Normal file
41
Client/utilities/updateConfig.js
Normal file
@@ -0,0 +1,41 @@
|
||||
// Debug
|
||||
const debug = require('debug')('client:updateConfig');
|
||||
// Modules
|
||||
const replace = require('replace-in-file');
|
||||
|
||||
class Options {
|
||||
constructor(key, updatedValue) {
|
||||
this.files = "./config/clientConfig.js";
|
||||
// A regex of the line containing the key in the config file
|
||||
this.from = new RegExp(`"${key}": (.+),`, "g");
|
||||
// Check to see if the value is a string and needs to be wrapped in double quotes
|
||||
if (typeof updatedValue === "string") this.to = `"${key}": "${updatedValue}",`;
|
||||
else this.to = `"${key}": ${updatedValue},`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper to update the client's saved ID
|
||||
* @param updatedId The updated ID assigned to the bot
|
||||
*/
|
||||
exports.updateId = (updatedId) => {
|
||||
const options = new Options("id", updatedId);
|
||||
|
||||
updateConfigFile(options, (updatedFiles) => {
|
||||
// Do Something
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Wrapper to write changes to the file
|
||||
* @param options An instance of the Objects class specified to the key being updated
|
||||
* @param callback Callback when the files have been modified
|
||||
*/
|
||||
function updateConfigFile(options, callback){
|
||||
replace(options, (error, changedFiles) => {
|
||||
if (error) return console.error('Error occurred:', error);
|
||||
debug('Modified files:', changedFiles);
|
||||
callback(changedFiles);
|
||||
});
|
||||
}
|
||||
125
Client/utilities/updatePresets.js
Normal file
125
Client/utilities/updatePresets.js
Normal file
@@ -0,0 +1,125 @@
|
||||
// Debug
|
||||
const debug = require('debug')('client:updatePresets');
|
||||
// Modules
|
||||
const fs = require('fs');
|
||||
|
||||
/**
|
||||
* Write the given presets to the JSON file
|
||||
* @param presets The preset or presets to be written
|
||||
* @param {function} callback The function to be called when this wrapper completes
|
||||
*/
|
||||
function writePresets(presets, callback = undefined) {
|
||||
fs.writeFile("../config/radioPresets.json", JSON.stringify(presets), (err) => {
|
||||
// Error checking
|
||||
if (err) throw err;
|
||||
debug("Write Complete");
|
||||
if (callback) callback()
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper to ensure each value in the array is in Hz format
|
||||
* @param frequenciesArray
|
||||
* @returns {*[]}
|
||||
*/
|
||||
function sanitizeFrequencies(frequenciesArray) {
|
||||
let sanitizedFrequencyArray = [];
|
||||
|
||||
for (const freq of frequenciesArray) {
|
||||
sanitizedFrequencyArray.push(convertFrequencyToHertz(freq));
|
||||
}
|
||||
|
||||
debug(sanitizedFrequencyArray);
|
||||
return sanitizedFrequencyArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to convert a string or a float into the integer type needed to be saved
|
||||
* @param frequency Could be a string, number or float,
|
||||
* @returns {number|number|*} Return the value to be saved in Hz format ("154875000" = 154.875MHz)
|
||||
*/
|
||||
function convertFrequencyToHertz(frequency){
|
||||
// check if the passed value is a number
|
||||
if(typeof frequency == 'number' && !isNaN(frequency)){
|
||||
if (Number.isInteger(frequency)) {
|
||||
debug(`${frequency} is integer.`);
|
||||
// Check to see if the frequency has the correct length
|
||||
if (frequency.toString().length >= 7 && frequency.toString().length <= 9) return frequency
|
||||
}
|
||||
else {
|
||||
debug(`${frequency} is a float value.`);
|
||||
// Convert to a string to remove the decimal in place and then correct the length
|
||||
frequency = frequency.toString();
|
||||
// One extra digit checked for the '.' included in the string
|
||||
if (frequency.length >= 8 && frequency.length <= 10) return parseInt(frequency.replace(".", ""));
|
||||
else if (frequency.length <= 7) {
|
||||
// Check to see if the frequency is 1s, 10s or 100s of MHz
|
||||
let zerosToBeRemoved = 3 - frequency.split(".")[0].length;
|
||||
// Need to add more 0s since it was in MHz format
|
||||
let neededZeros = (9 - frequency.length) - zerosToBeRemoved;
|
||||
frequency = frequency.replace(".", "") + '0'.repeat(neededZeros)
|
||||
|
||||
return parseInt(frequency);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
debug(`${frequency} is not a number`);
|
||||
frequency = convertFrequencyToHertz(parseFloat(frequency));
|
||||
|
||||
return parseInt(frequency)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the saved presets and returns a preset object
|
||||
* @returns {any} The object containing the different systems the bot is near
|
||||
*/
|
||||
exports.getPresets = function getPresets() {
|
||||
return JSON.parse(fs.readFileSync("../config/radioPresets.json"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new preset to the radioPresets JSON file
|
||||
*
|
||||
* @param {string} systemName The name of the system being added
|
||||
* @param {Array} frequencies The frequency or frequencies the SDR should tune to for this system
|
||||
* @param {string} mode The listening mode the SDR should be using when listening to this frequency
|
||||
* @param {function} callback The callback function to call when completed
|
||||
* @param {string} trunkFile The file that contains all trunking information (if applicable to the selected listening mode)
|
||||
*/
|
||||
exports.addNewPreset = (systemName, frequencies, mode, callback, trunkFile = undefined) => {
|
||||
const presets = this.getPresets();
|
||||
// Create the preset for the new system
|
||||
presets[systemName] = {
|
||||
"frequencies": sanitizeFrequencies(frequencies),
|
||||
"mode": mode,
|
||||
"trunkFile": trunkFile ?? "none"
|
||||
}
|
||||
// Write the changes to the preset config file
|
||||
writePresets(presets, callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the specified system
|
||||
*
|
||||
* @param {string} systemName The name of the system being modified
|
||||
* @param {function} callback The callback function to be called when the function completes
|
||||
* @param {Array} frequencies The frequency or frequencies the SDR should tune to for this system
|
||||
* @param {string} mode The listening mode the SDR should be using when listening to this frequency
|
||||
* @param {string} trunkFile The file that contains all trunking information (if applicable to the selected listening mode)
|
||||
*/
|
||||
exports.updatePreset = (systemName, callback, { frequencies = undefined, mode = undefined, trunkFile = undefined }) => {
|
||||
const presets = this.getPresets();
|
||||
// Check if a system name was passed
|
||||
if (systemName in presets) {
|
||||
// System name exists, checking to see if the keys are different
|
||||
if(frequencies && frequencies !== presets[systemName].frequencies) presets[systemName].frequencies = sanitizeFrequencies(frequencies);
|
||||
if(mode && mode !== presets[systemName].mode) presets[systemName].mode = mode;
|
||||
if(trunkFile && trunkFile !== presets[systemName].trunkFile || trunkFile === "") presets[systemName].trunkFile = trunkFile ?? "none";
|
||||
// Write the changes
|
||||
writePresets(presets, callback);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user