diff --git a/ReadPortSerieArduino/ReadPortSerieArduino.py b/ReadPortSerieArduino/ReadPortSerieArduino.py index c8e76b59427da43479de12e17be5399135a7ce40..3dbf8254a5c9bf27f365b28b8db1b1f9631e66eb 100644 --- a/ReadPortSerieArduino/ReadPortSerieArduino.py +++ b/ReadPortSerieArduino/ReadPortSerieArduino.py @@ -1,10 +1,11 @@ import serial import requests +import time arduino_port = '/dev/ttyACM0' ser = serial.Serial(arduino_port, 9600, timeout=1) -api_url = '' +api_url = 'http://localhost:3000/calculer-volume' try: while True: @@ -16,7 +17,15 @@ try: print(f"Distance : {distance} cm") payload = {'distance': distance} - #response = requests.post(api_url, data=payload) + try: + response = requests.post(api_url, json=payload) + response.raise_for_status() + data = response.json() + print(f"API Response: {data}") + except requests.exceptions.RequestException as e: + print(f"Erreur lors de l'appel API : {e}") + + time.sleep(300) except KeyboardInterrupt: ser.close() diff --git a/app/routes/index.js b/app/routes/index.js index dfaf956f045a8e889a3a9d48cdc055bdbf370cf3..78d56b4e9142c3a7193a7553f740ea616399d5f1 100644 --- a/app/routes/index.js +++ b/app/routes/index.js @@ -7,6 +7,13 @@ const mongoURI = process.env.MONGO_URI || 'mongodb://localhost/abreuvoir'; const port = 3000; + +app.use(express.json()); + +const path = require('path'); +const viewsPath = path.join(__dirname, '../views'); +app.use(express.static(viewsPath)); + // Connexion à MongoDB mongoose.connect(mongoURI, { useNewUrlParser: true, useUnifiedTopology: true }) .then(() => console.log('MongoDB Connected')) @@ -21,12 +28,17 @@ const hauteurInitialeEau = 50; // la hauteur initiale de l'eau en cm const distanceSupplementaire = 5; // la distance supplémentaire en cm entre le capteur et le bord supérieur du seau let volumeEauInitial = Math.PI * rayonSeau**2 * hauteurInitialeEau; // Calcul du volume d'eau initial en cm^3 (mL) -app.use(express.json()); - -const path = require('path'); app.get('/', (req, res) => { - res.sendFile(path.join(__dirname, 'index.html')); + res.sendFile(path.join(viewsPath, 'index.html')); +}); + +app.get('/information-generale', (req, res) => { + res.sendFile(path.join(viewsPath, 'dashboard.html')); +}); + +app.get('/graphique-consommations', (req, res) => { + res.sendFile(path.join(viewsPath, 'chart.html')); }); app.post('/distance', (req, res) => { @@ -42,10 +54,11 @@ app.post('/definir-volume-initial', (req, res) => { res.json({ message: 'Volume d\'eau initial mis à jour', volumeEauInitial }); }); - // Route pour calculer le volume d'eau et la consommation app.post('/calculer-volume', async (req, res) => { - const { distanceCapteur } = req.body; + let { distanceCapteur } = req.body; + + distanceCapteur = parseInt(distanceCapteur); if (typeof distanceCapteur !== 'number' || distanceCapteur < 0) { return res.status(400).json({ message: 'Distance invalide' }); @@ -82,7 +95,14 @@ app.post('/calculer-volume', async (req, res) => { } }); - +app.get('/consommations', async (req, res) => { + try { + const consommations = await Consommation.find(); + res.json(consommations); + } catch (error) { + res.status(500).json({ message: error.message }); + } +}); app.listen(port, () => { console.log(`Server listening at http://localhost:${port}`); diff --git a/app/views/chart.html b/app/views/chart.html new file mode 100644 index 0000000000000000000000000000000000000000..abd3ea2e0112f5d86532081c99aa8a6edbf5956b --- /dev/null +++ b/app/views/chart.html @@ -0,0 +1,71 @@ + + +
+ + +