Skip to content

Commit

Permalink
update app.js
Browse files Browse the repository at this point in the history
  • Loading branch information
chourmovs committed Sep 5, 2024
1 parent 2bf0880 commit cde65ba
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 205 deletions.
224 changes: 72 additions & 152 deletions webapp/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,178 +8,98 @@ const { spawn } = require('child_process');
const app = express();
const port = 3000;

// Servir les fichiers statiques depuis le dossier public
// Serve static files from the public directory
app.use(express.static(path.join(__dirname, 'public')));

// Route pour la page d'accueil
// Route for the homepage
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});

app.get('/mount-nas', (req, res) => {
const networkPath = req.query.path;
const networkPath = req.query.path;

// Commande pour monter le partage NAS en lecture seule sans sudo
const mountCommand = `mount -t cifs -o username=chourmovs,password='3$*ES3KSu4tYtX',file_mode=0777,dir_mode=0777,rw ${networkPath} /mnt/Musique`;
// Command to mount the NAS share as read-only with no sudo
const mountCommand = `mount -t cifs -o username=chourmovs,password='3$*ES3KSu4tYtX',file_mode=0777,dir_mode=0777,rw ${networkPath} /mnt/Music`;

const mountProcess = spawn('sh', ['-c', mountCommand]);
const mountProcess = spawn('sh', ['-c', mountCommand]);

let output = '';
let output = '';

mountProcess.stdout.on('data', (data) => {
output += data.toString();
});
mountProcess.stdout.on('data', (data) => {
output += data.toString();
});

mountProcess.stderr.on('data', (data) => {
output += data.toString();
});
mountProcess.stderr.on('data', (data) => {
output += data.toString();
});

mountProcess.on('close', (code) => {
if (code !== 0) {
return res.status(500).send(`Erreur lors du montage : ${output}`);
}
res.send(`Partage NAS monté avec succès : ${output}`);
});
});


app.get('/start-analysis', (req, res) => {
const command = 'blissify init /mnt/Musique';
const analysisProcess = spawn('sh', ['-c', command]);

res.setHeader('Content-Type', 'text/plain');

analysisProcess.stdout.on('data', (data) => {
res.write(data.toString());
});

analysisProcess.stderr.on('data', (data) => {
res.write(data.toString());
});

analysisProcess.on('close', (code) => {
if (code !== 0) {
res.write(`\nErreur lors de l'exécution de blissify init (code ${code})`);
} else {
res.write(`\nBlissify init exécuté avec succès`);
}
res.end();
});
mountProcess.on('close', (code) => {
if (code !== 0) {
return res.status(500).send(`Error mounting share: ${output}`);
}
res.send(`NAS share mounted successfully: ${output}`);
});
});

app.get('/mpc-update', (req, res) => {
const command = 'mpc update';
const mpcProcess = spawn('sh', ['-c', command]);

res.setHeader('Content-Type', 'text/plain');

mpcProcess.stdout.on('data', (data) => {
res.write(data.toString());
});

mpcProcess.stderr.on('data', (data) => {
res.write(data.toString());
});

mpcProcess.on('close', (code) => {
if (code !== 0) {
res.write(`\nErreur lors de la mise à jour MPC (code ${code})`);
} else {
res.write(`\nMPC mis à jour avec succès`);
}
res.end();
});
const command = 'mpc update';
const mpcProcess = spawn('sh', ['-c', command]);

res.setHeader('Content-Type', 'text/plain');

mpcProcess.stdout.on('data', (data) => {
res.write(data.toString());
});

mpcProcess.stderr.on('data', (data) => {
res.write(data.toString());
});

mpcProcess.on('close', (code) => {
if (code !== 0) {
res.write(`\nError updating MPC (code ${code})`);
} else {
res.write(`\nMPC updated successfully`);
}
res.end();
});
});

app.get('/blissify-update', (req, res) => {
const command = 'blissify update';
const updateProcess = spawn('sh', ['-c', command]);

res.setHeader('Content-Type', 'text/plain');

updateProcess.stdout.on('data', (data) => {
res.write(data.toString());
});

updateProcess.stderr.on('data', (data) => {
res.write(data.toString());
});

updateProcess.on('close', (code) => {
if (code !== 0) {
res.write(`\nErreur lors de l'exécution de blissify update (code ${code})`);
} else {
res.write(`\nBlissify update exécuté avec succès`);
}
res.end();
});
const command = 'blissify update';
const updateProcess = spawn('sh', ['-c', command]);

res.setHeader('Content-Type', 'text/plain');

updateProcess.stdout.on('data', (data) => {
res.write(data.toString());
});

updateProcess.stderr.on('data', (data) => {
res.write(data.toString());
});

updateProcess.on('close', (code) => {
if (code !== 0) {
res.write(`\nError executing blissify update (code ${code})`);
} else {
res.write(`\nBlissify update executed successfully`);
}
res.end();
});
});

// Route pour chercher l'instance Volumio
// Route to find the Volumio instance
app.get('/search-volumio', (req, res) => {
// Exécuter une commande pour scanner le réseau (exemple avec nmap)
exec('nmap -p 22 --open -sV 192.168.1.0/24', (error, stdout, stderr) => {
if (error) {
return res.status(500).send(`Erreur: ${stderr}`);
}
// Analyser stdout pour trouver les instances Volumio
const volumioInstance = stdout.match(/192\.168\.1\.\d+/); // exemple simple de regex
res.send(volumioInstance ? volumioInstance[0] : 'Aucune instance Volumio trouvée');
});
// Execute a command to scan the network (example with nmap)
exec('nmap -p 22 --open -sV 192.168.1.0/24', (error, stdout, stderr) => {
if (error) {
return res.status(500).send(`Error: ${stderr}`);
}
// Parse stdout to find Volumio instances
const volumioInstance = stdout.match(/192\.168\.1\.\d+/); // simple example of regex
res.send(volumioInstance ? volumioInstance[0] : 'No Volumio instances found');
});
});

// Route pour uploader le fichier song.db vers Volumio via SFTP
app.post('/upload-songdb', (req, res) => {
const conn = new Client();
const songDbPath = '~/.local/share/bliss-rs/song.db'; // Remplacez par le chemin de votre song.db
const remotePath = '/home/volumio/.local/share/bliss-rs/song.db';

conn.on('ready', () => {
conn.sftp((err, sftp) => {
if (err) throw err;
sftp.fastPut(songDbPath, remotePath, (err) => {
if (err) return res.status(500).send(`Erreur: ${err.message}`);
res.send('Upload réussi!');
conn.end();
});
});
}).connect({
host: '192.168.1.20', // Adresse IP de l'instance Volumio trouvée
port: 22,
username: 'volumio',
password: 'volumio' // Remplacez par le mot de passe correct
});
});

app.get('/test-command', (req, res) => {
const command = 'ls /mnt/Musique'; // Simple commande de test
const testProcess = spawn('sh', ['-c', command]);

res.setHeader('Content-Type', 'text/plain');

testProcess.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
res.write(data.toString());
});

testProcess.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
res.write(data.toString());
});

testProcess.on('close', (code) => {
console.log(`Processus terminé avec code : ${code}`);
if (code !== 0) {
res.write(`\nErreur (code ${code})`);
} else {
res.write(`\nCommande exécutée avec succès`);
}
res.end();
});
});


const PORT = 3000;
app.listen(PORT, () => {
console.log(`Serveur en écoute sur le port ${PORT}`);
});
110 changes: 57 additions & 53 deletions webapp/public/index.html
Original file line number Diff line number Diff line change
@@ -1,73 +1,77 @@
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gestion NAS et Commandes</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gestion NAS et Commandes</title>
</head>
<body>
<h1>Gestion du NAS et des analyses</h1>
<h1>Gestion du NAS et des analyses</h1>

<form id="network-path-form">
<label for="network-path">Chemin du partage NAS :</label>
<input type="text" id="network-path" name="path">
<button type="submit">Monter le partage NAS</button>
</form>
<!-- Network path form section -->
<form id="network-path-form">
<label for="network-path">Chemin du partage NAS :</label>
<input type="text" id="network-path" name="path">
<button type="submit">Monter le partage NAS</button>
</form>

<button id="mpc-update-button">Mise à jour MPC</button>
<button id="blissify-init-button">Blissify Init</button>
<button id="blissify-update-button">Blissify Update</button>
<!-- Buttons for different operations -->
<button id="mpc-update-button">Mise à jour MPC</button>
<button id="blissify-init-button">Blissify Init</button>
<button id="blissify-update-button">Blissify Update</button>

<pre id="console-output"></pre>
<!-- Container for output messages -->
<pre id="console-output"></pre>

<script>
function displayOutput(data) {
// Affiche l'output dans le conteneur dédié
document.getElementById('console-output').innerText += data;
}
<script>
// Function to display output messages in the dedicated container
function displayOutput(data) {
document.getElementById('console-output').innerText += data;
}

document.getElementById('network-path-form').addEventListener('submit', function(event) {
event.preventDefault();
const networkPath = document.getElementById('network-path').value;
// Event listeners for operations
document.getElementById('network-path-form').addEventListener('submit', function(event) {
event.preventDefault();
const networkPath = document.getElementById('network-path').value;

// Monter le partage NAS
fetch(`/mount-nas?path=${encodeURIComponent(networkPath)}`)
.then(response => response.text())
.then(displayOutput)
.catch(error => {
displayOutput(`Erreur lors du montage : ${error}`);
});
// Mount the NAS share
fetch(`/mount-nas?path=${encodeURIComponent(networkPath)}`)
.then(response => response.text())
.then(displayOutput)
.catch(error => {
displayOutput(`Erreur lors du montage : ${error}`);
});
});

document.getElementById('mpc-update-button').addEventListener('click', function() {
// Mise à jour MPC après montage
fetch('/mpc-update')
.then(response => response.text())
.then(displayOutput)
.catch(error => {
displayOutput(`Erreur lors de la mise à jour MPC : ${error}`);
});
// MPC update after mounting
document.getElementById('mpc-update-button').addEventListener('click', function() {
fetch('/mpc-update')
.then(response => response.text())
.then(displayOutput)
.catch(error => {
displayOutput(`Erreur lors de la mise à jour MPC : ${error}`);
});
});

document.getElementById('blissify-init-button').addEventListener('click', function() {
// Lancer blissify init
fetch('/start-analysis')
.then(response => response.text())
.then(displayOutput)
.catch(error => {
displayOutput(`Erreur lors de l'exécution de blissify init : ${error}`);
});
// Trigger blissify init
document.getElementById('blissify-init-button').addEventListener('click', function() {
fetch('/start-analysis')
.then(response => response.text())
.then(displayOutput)
.catch(error => {
displayOutput(`Erreur lors de l'exécution de blissify init : ${error}`);
});
});

document.getElementById('blissify-update-button').addEventListener('click', function() {
// Lancer blissify update
fetch('/blissify-update')
.then(response => response.text())
.then(displayOutput)
.catch(error => {
displayOutput(`Erreur lors de l'exécution de blissify update : ${error}`);
});
// Trigger blissify update
document.getElementById('blissify-update-button').addEventListener('click', function() {
fetch('/blissify-update')
.then(response => response.text())
.then(displayOutput)
.catch(error => {
displayOutput(`Erreur lors de l'exécution de blissify update : ${error}`);
});
</script>
});
</script>
</body>
</html>

0 comments on commit cde65ba

Please sign in to comment.