Implement basic initial setup and dashboard with web portal

This commit is contained in:
Logan Cusano
2024-05-12 22:49:39 -04:00
parent 580513997d
commit 15b0650550
11 changed files with 418 additions and 36 deletions

View File

@@ -0,0 +1,11 @@
// dashboardRoutes.js
import express from 'express';
const router = express.Router();
// Define routes
router.get('/', (req, res) => {
res.render('dashboard');
});
export default router;

View File

@@ -0,0 +1,136 @@
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'
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('/', (req, res) => {
res.render('setup/setup');
});
// 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 } = req.body;
console.log(clientName, clientLocation, clientCapabilities);
nodeData.clientName = clientName;
nodeData.clientLocation = clientLocation;
nodeData.clientCapabilities = clientCapabilities;
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: `${__dirname}/op25/op25/gr-op25_repeater/apps`,
CONFIG_PATH: "./config/radioPresets.json",
};
// 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.writeFile('.env', envContent, (err) => {
if (err) {
console.error('Error writing to .env file:', err);
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.writeFile(radioPresetsPath, JSON.stringify(radioPresetsData, null, 4), (err) => {
if (err) {
console.error('Error writing to radioPresets.json:', err);
res(false);
} else {
console.log('radioPresets.json updated successfully');
res(true);
}
});
})
}

60
express/server.mjs Normal file
View File

@@ -0,0 +1,60 @@
import express from 'express';
import http from 'http';
import path from 'path';
import { fileURLToPath } from 'url';
import bodyParser from 'body-parser';
import setupRoutes from './routes/setupRoutes.js';
import dashboardRoutes from './routes/dashboardRoutes.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url)); // Define __dirname for ESM
export var isSetupComplete = false;
const app = express();
const server = http.createServer(app);
// Start the server
const startServer = (port, openSocket) => {
if (openSocket) isSetupComplete = true;
server.listen(port, () => {
console.log(`Server running on port ${port}`);
});
};
export default startServer;
// Set EJS as the default template engine
app.set('view engine', 'ejs');
// Set the views directory to express/views
const viewsPath = path.join(__dirname, 'views');
app.set('views', viewsPath);
// Use body-parser middleware to parse JSON requests
app.use(bodyParser.json());
// Use body-parser middleware to parse URL-encoded form data
app.use(bodyParser.urlencoded({ extended: true }));
// Define static folder for serving HTML, CSS, and client-side JavaScript files
const staticPath = path.join(__dirname, 'public');
app.use(express.static(staticPath));
// ---------------- Defualt Route
app.get('/', (req, res) => {
if (!isSetupComplete) {
return res.redirect('/setup');
}
else {
return res.redirect('/dashboard');
}
});
// ---------------- Routers
// Use the setup router for '/setup' routes
app.use('/setup', setupRoutes);
// Use the dashboard router for '/dashboard' routes
app.use('/dashboard', dashboardRoutes);

View File

@@ -0,0 +1,15 @@
<!-- dashboard.ejs -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dashboard</title>
</head>
<body>
<h1>Welcome to the Dashboard</h1>
<p>This is a very basic dashboard.</p>
<!-- Add more content as needed -->
</body>
</html>

View File

@@ -0,0 +1,34 @@
<!-- nearby_systems.ejs -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add Nearby Systems</title>
</head>
<body>
<h1>Add Nearby Systems</h1>
<form action="/setup/add-system" method="post">
<label for="systemName">System Name:</label>
<input type="text" id="systemName" name="systemName" required>
<br>
<label for="frequencies">Frequencies (comma-separated):</label>
<input type="text" id="frequencies" name="frequencies" required>
<br>
<label for="mode">Mode:</label>
<select id="mode" name="mode" required>
<option value="p25">P25</option>
<option value="nbfm">NBFM</option>
</select>
<br>
<label for="trunkFile">Trunk File:</label>
<input type="text" id="trunkFile" name="trunkFile">
<br>
<label for="whitelistFile">Whitelist File:</label>
<input type="text" id="whitelistFile" name="whitelistFile">
<br>
<button type="submit">Add System</button>
</form>
</body>
</html>

View File

@@ -0,0 +1,19 @@
<!-- prompt_add_another_system.ejs -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add Another System?</title>
</head>
<body>
<h1>Add Another System?</h1>
<form action="/setup/add-system" method="get">
<button type="submit">Add Another System</button>
</form>
<form action="/setup/finish-setup" method="post">
<button type="submit">Finish Setup</button>
</form>
</body>
</html>

View File

@@ -0,0 +1,25 @@
<!-- setup.ejs -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Setup</title>
</head>
<body>
<h1>Setup</h1>
<form action="/setup" method="post">
<label for="clientName">Client Name:</label>
<input type="text" id="clientName" name="clientName" required>
<br>
<label for="clientLocation">Client Location:</label>
<input type="text" id="clientLocation" name="clientLocation" required>
<br>
<label for="clientCapabilities">Client Capabilities (comma-separated):</label>
<input type="text" id="clientCapabilities" name="clientCapabilities" required>
<br>
<button type="submit">Next</button>
</form>
</body>
</html>