// Debug const { DebugBuilder } = require("../utilities/debugBuilder.js"); const log = new DebugBuilder("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; 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 ("154875000" = 154.875MHz) */ 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 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 { 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() { 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); } }