// 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(); else return }); } /** * 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 >= 1000000) return frequency if (frequency >= 100 && frequency <= 999) return frequency * 1000000 log.WARN("Frequency hasn't matched filters: ", 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 parseInt(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 */ function getPresets() { const presetDir = path.resolve("./config/radioPresets.json"); log.DEBUG(`Getting presets from directory: '${presetDir}'`); if (fs.existsSync(presetDir)) return JSON.parse(fs.readFileSync(presetDir)); else return {}; } /** * 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) * @param {string} whitelistFile The file that contains the whitelisted talkgroups [optional] */ function addNewPreset(systemName, frequencies, mode, callback, trunkFile = undefined, whitelistFile = undefined) { const presets = this.getPresets(); // Create the preset for the new system presets[systemName] = { "frequencies": sanitizeFrequencies(frequencies), "mode": mode, "trunkFile": trunkFile ?? "none", "whitelistFile": whitelistFile ?? "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) * @param {string} whitelistFile The file that contains the whitelisted talkgroups [optional] */ function updatePreset(systemName, callback, { frequencies = undefined, mode = undefined, trunkFile = undefined, whitelistFile = 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 && sanitizeFrequencies(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"; if(whitelistFile && whitelistFile !== presets[systemName].whitelistFile || whitelistFile === "") presets[systemName].whitelistFile = whitelistFile ?? "none"; // Write the changes writePresets(presets, callback); } } /** * Deletes 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 */ function removePreset(systemName, callback) { const presets = this.getPresets(); // Check if a system name was passed if (systemName in presets) { delete presets[systemName]; writePresets(presets, callback); } }