Files
DRB-CnC/Client/utilities/updatePresets.js
2023-03-26 15:32:52 -04:00

121 lines
4.8 KiB
JavaScript

// Debug
const { DebugBuilder } = require("../utilities/debugBuilder.js");
const log = new DebugBuilder("client", "updatePresets");
// Modules
const fs = require('fs');
const path = require("path");
const converter = require("convert-units");
/**
* 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) {
log.DEBUG(`${__dirname}`);
fs.writeFile("./config/radioPresets.json", JSON.stringify(presets), (err) => {
// Error checking
if (err) throw err;
log.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));
}
log.DEBUG("Sanitized Frequency Array", 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 ("154.875"MHz format = "154875000")
*/
function convertFrequencyToHertz(frequency){
// check if the passed value is a number
if(typeof frequency == 'number' && !isNaN(frequency)){
if (Number.isInteger(frequency)) {
log.DEBUG(`${frequency} is an integer.`);
// Check to see if the frequency has the correct length
if (frequency.toString().length >= 7 && frequency.toString().length <= 9) return frequency
}
else {
log.DEBUG(`${frequency} is a float value.`);
// Convert to a string to remove the decimal in place and then correct the length
return converter(frequency).from("MHz").to("Hz");
}
} else {
log.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() {
const presetDir = path.resolve("./config/radioPresets.json");
log.DEBUG(`Getting presets from directory: '${presetDir}'`);
return JSON.parse(fs.readFileSync(presetDir));
}
/**
* 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);
}
}