gestuurdd

This commit is contained in:
mohammedcifci78
2024-03-12 11:03:45 +01:00
parent a73fa98a5c
commit 44750a12a2
11 changed files with 2263 additions and 397 deletions

126
app.js
View File

@@ -1,93 +1,113 @@
const { app, BrowserWindow,ipcMain } = require('electron'); /* TYPE IN TERMINAL: "npm install electron" */
const express = require('express'); /* TYPE IN TERMINAL: "npm install express" */
const bodyParser = require('body-parser'); /* TYPE IN TERMINAL: "npm install body-parser" */
const { PythonShell } = require('python-shell'); /* TYPE IN TERMINAL: "npm install python-shell" */
const { app, BrowserWindow, ipcMain } = require('electron');
const express = require('express');
const bodyParser = require('body-parser');
const { PythonShell } = require('python-shell');
const path = require('path');
const urlElectron = path.join(__dirname, "src/index.html");
// Maak een Express-app
// Create an Express app
const server = express();
server.use(bodyParser.urlencoded({ extended: true }));
// Definieer een route voor form POST verzoeken
// Define a route for form POST requests
server.post('/submit-form', (req, res) => {
const { plant_naam, plantensoort } = req.body; // Verkrijg de plant_naam uit het formulier
const plant_geteelt = req.body.plant_geteelt == 'true' ? 'true' : 'false'; // Zorgt dat de string "true" herkent wordt
const { plant_naam, plantensoort } = req.body;
const plant_geteelt = req.body.plant_geteelt == 'true' ? 'true' : 'false';
let options = {
mode: 'text',
args: [plant_naam, plantensoort, plant_geteelt], // Zet hier een variable bij om de data toe te voegen aan de databas
args: [plant_naam, plantensoort, plant_geteelt],
};
/*Om python te gebruiken*/
// ipcMain.on('request-update-temp', (event, args) => {
// let options = {
// mode: 'text',
// scriptPath: 'path/to/your/python/script',
// args: args
// };
// PythonShell.run('calculate.py', options, (err, results) => {
// if (err) {
// console.error('Error running python script', err);
// event.reply('update-temp-result', 'error');
// } else {
// console.log('Python script results:', results);
// event.reply('update-temp-result', results[0]); // Verstuur het resultaat terug naar de renderer proces
// }
// });
// });
// En dan in je renderer proces, stuur je een bericht om de update te verzoeken
ipcRenderer.send('request-update-temp', [/* hier kunnen argumenten komen die je Python script nodig heeft */]);
// The following line was causing issues and has been commented out
// ipcRenderer.send('request-update-temp', [/* arguments for Python script */]);
});
// Start de server voor verbinding met de database
// Start the server for connecting to the database
const PORT = 3000;
server.listen(PORT, () => {
console.log(`Server luistert op port ${PORT}`);
console.log(`Server is listening on port ${PORT}`);
});
// Maak de Electron applicatie aan met bijbehorende waardes
let mainWindow; // Variable to store the reference to the main window
// Create the Electron application with associated values
function createWindow() {
const mainWindow = new BrowserWindow({
mainWindow = new BrowserWindow({
width: 1280,
height: 800,
frame: false,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
contextIsolation: false,
enableRemoteModule: true,
webSecurity: true,
}
});
mainWindow.loadFile(path.join(__dirname, 'src', 'py', 'templates', 'index.html'));
/*Is om het Python script te kunnen gebruiken*/
// Enable Python script execution
ipcMain.on('run-python-script', (event, args) => {
let options = {
mode: 'text',
args: args
mode: 'text',
args: args,
};
PythonShell.run('../src/py/calculate.py', options, (err, results) => {
if (err)
{
console.error('Error running python script', err);
event.reply('python-script-response', 'error');
}
else
{
console.log('Python script results:', results);
event.reply('python-script-response', results);
}
if (err) {
console.error('Error running python script', err);
event.reply('python-script-response', 'error');
} else {
console.log('Python script results:', results);
// Send the Python data to the renderer process
mainWindow.webContents.send('python-script-response', JSON.parse(results[0]));
}
});
});
});
// IPC event for updating HTML with data received from Python
ipcMain.on('update-html-data', (event, data) => {
mainWindow.webContents.send('update-html-data', data);
});
}
app.whenReady().then(createWindow);
// Start the Electron app
app.whenReady().then(() => {
createWindow();
// Functionaliteit voor het openen en sluiten van de app
// Execute Python script when the app is ready
const options = {
mode: 'text',
scriptPath: path.join(__dirname, 'src', 'py'),
args: [/* arguments for the Python script */]
};
PythonShell.run('calculate.py', options, (err, results) => {
if (err) {
console.error('Error running python script', err);
mainWindow.webContents.send('python-script-response', 'error');
} else {
console.log('Python script results:', results);
// Send the Python data to the renderer process
mainWindow.webContents.send('python-script-response', JSON.parse(results[0]));
}
});
});
// IPC event for requesting data update
ipcMain.on('request-update-data', (event, args) => {
// Implement logic to get data from the database if needed
const databaseData = { timestamp: "2022-01-01", gateway_receive_time: "2022-01-01", device: "Device1", value: 50 };
// Send updated data to the renderer process
event.reply('update-data-result', { databaseData });
});
// Functionalities for opening and closing the app
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
@@ -98,4 +118,4 @@ app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});