import { DebugBuilder } from "../../modules/debugger.mjs"; const log = new DebugBuilder("client", "client.express.setupRoutes"); import fs from 'fs'; import path from 'path'; import express from 'express'; import { fileURLToPath } from 'url'; import { generateUniqueID, ensureDirectoryExists } from '../../modules/baseUtils.mjs'; import { restartApplication } from '../../modules/selfUpdater.mjs' import { launchProcess } from '../../modules/subprocessHandler.mjs' const router = express.Router(); const __dirname = path.dirname(fileURLToPath(import.meta.url)); // Define __dirname for ESM // Array to store information about added systems let systemsData = []; let nodeData = {}; router.get('/', async (req, res) => { const output = await launchProcess('python', ['./discordAudioBot/pdab/getDevices.py'], true, true) log.INFO("Device List", output); res.render('setup/setup', { deviceList: output }); }); // Route to serve the page for adding nearby systems router.get('/add-system', (req, res) => { res.render('setup/add_system'); }); router.post('/', (req, res) => { // Handle form submission here const { clientName, clientLocation, clientCapabilities, audioDeviceId } = req.body; log.INFO(clientName, clientLocation, clientCapabilities, audioDeviceId); nodeData.clientName = clientName; nodeData.clientLocation = clientLocation; nodeData.clientCapabilities = clientCapabilities; nodeData.audioDeviceId = audioDeviceId; res.redirect('/setup/add-system'); }); // Route to handle form submission for adding a system router.post('/add-system', (req, res) => { const { systemName, frequencies, mode, trunkFile, whitelistFile } = req.body; // Store system information for later use // For now, let's just log the information log.INFO('System Name:', systemName); log.INFO('Frequencies:', frequencies); log.INFO('Mode:', mode); log.INFO('Trunk File:', trunkFile); log.INFO('Whitelist File:', whitelistFile); // Store system information in the array systemsData.push({ systemName, frequencies, mode, trunkFile, whitelistFile }); // Prompt user to add another system or proceed res.render('setup/prompt_add_another_system'); }); // Route to write collected information to .env file router.post('/finish-setup', async (req, res) => { // Write collected information to .env file // For now, let's just log the collected information log.INFO('Collected System Information:', nodeData, systemsData); if (!await exportCsv(nodeData)) return res.status(500).send('Error writing to .env file'); if (!await exportSystems(systemsData)) return res.status(500).send('Error writing the systems config file'); res.send('Setup process completed successfully! Click here for the dashboard when the service restarts.'); restartApplication(); }); export default router; const exportCsv = (nodeData) => { const nuid = generateUniqueID(); log.INFO(`Generated a new unique ID for this node: '${nuid}'`); const envData = { CLIENT_NUID: nuid, CLIENT_NAME: nodeData.clientName, CLIENT_LOCATION: nodeData.clientCapabilities, CLIENT_CAPABILITIES: nodeData.clientCapabilities, SERVER_IP: "", SERVER_PORT: 0, OP25_FULL_PATH: path.resolve(__dirname, '../../op25/op25/gr-op25_repeater/apps'), CONFIG_PATH: "./config/radioPresets.json", AUDIO_DEVICE_ID: nodeData.audioDeviceId, PDAB_PORT: 3110, NODE_ENV: "production", }; // Generate .env file content const envContent = Object.entries(envData) .map(([key, value]) => `${key}=${value}`) .join('\n'); // Write to .env file return new Promise(res => { fs.access('.env', fs.constants.F_OK, (err) => { if (err) { // File doesn't exist, create it fs.writeFile('.env', envContent, (writeErr) => { if (writeErr) { log.ERROR('Error writing to .env file:', writeErr); res(false); } else { log.INFO('.env file created successfully'); res(true); } }); } else { // File exists, update it fs.writeFile('.env', envContent, (writeErr) => { if (writeErr) { log.ERROR('Error writing to .env file:', writeErr); res(false); } else { log.INFO('.env file updated successfully'); res(true); } }); } }); }); } const exportSystems = (systemsData) => { // Write systems data to radioPresets.json const radioPresetsPath = './config/radioPresets.json'; const radioPresetsData = {}; systemsData.forEach((system, index) => ( radioPresetsData[system.systemName] = { frequencies: system.frequencies.split(','), mode: system.mode, trunkFile: system.trunkFile || '', whitelistFile: system.whitelistFile || '' } )); // Ensure directory exists ensureDirectoryExists(radioPresetsPath); return new Promise(res => { fs.access(radioPresetsPath, fs.constants.F_OK, (err) => { if (err) { // File doesn't exist, create it fs.writeFile(radioPresetsPath, JSON.stringify(radioPresetsData, null, 4), (writeErr) => { if (writeErr) { log.ERROR('Error writing to radioPresets.json:', writeErr); res(false); } else { log.INFO('radioPresets.json created successfully'); res(true); } }); } else { // File exists, update it fs.writeFile(radioPresetsPath, JSON.stringify(radioPresetsData, null, 4), (writeErr) => { if (writeErr) { log.ERROR('Error writing to radioPresets.json:', writeErr); res(false); } else { log.INFO('radioPresets.json updated successfully'); res(true); } }); } }); }); }