175 lines
6.2 KiB
JavaScript
175 lines
6.2 KiB
JavaScript
import fs from 'fs';
|
|
import path from 'path';
|
|
import express from 'express';
|
|
import { fileURLToPath } from 'url';
|
|
import { generateUniqueID } 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)
|
|
console.log("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;
|
|
|
|
console.log(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
|
|
console.log('System Name:', systemName);
|
|
console.log('Frequencies:', frequencies);
|
|
console.log('Mode:', mode);
|
|
console.log('Trunk File:', trunkFile);
|
|
console.log('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
|
|
console.log('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 <a href="/dashboard">here</a> for the dashboard when the service restarts.');
|
|
restartApplication();
|
|
});
|
|
|
|
export default router;
|
|
|
|
|
|
const exportCsv = (nodeData) => {
|
|
const nuid = generateUniqueID();
|
|
console.log(`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) {
|
|
console.error('Error writing to .env file:', writeErr);
|
|
res(false);
|
|
} else {
|
|
console.log('.env file created successfully');
|
|
res(true);
|
|
}
|
|
});
|
|
} else {
|
|
// File exists, update it
|
|
fs.writeFile('.env', envContent, (writeErr) => {
|
|
if (writeErr) {
|
|
console.error('Error writing to .env file:', writeErr);
|
|
res(false);
|
|
} else {
|
|
console.log('.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 || ''
|
|
}
|
|
));
|
|
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) {
|
|
console.error('Error writing to radioPresets.json:', writeErr);
|
|
res(false);
|
|
} else {
|
|
console.log('radioPresets.json created successfully');
|
|
res(true);
|
|
}
|
|
});
|
|
} else {
|
|
// File exists, update it
|
|
fs.writeFile(radioPresetsPath, JSON.stringify(radioPresetsData, null, 4), (writeErr) => {
|
|
if (writeErr) {
|
|
console.error('Error writing to radioPresets.json:', writeErr);
|
|
res(false);
|
|
} else {
|
|
console.log('radioPresets.json updated successfully');
|
|
res(true);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
})
|
|
} |