From 98128253d9ba6523fe6937e3e6eb7783868a8588 Mon Sep 17 00:00:00 2001 From: Kerboul Date: Sun, 27 Apr 2025 11:54:29 +0200 Subject: [PATCH] =?UTF-8?q?feat(camera):=20Am=C3=A9liorer=20la=20gestion?= =?UTF-8?q?=20de=20l'arr=C3=AAt=20de=20la=20cam=C3=A9ra=20en=20ajoutant=20?= =?UTF-8?q?la=20recherche=20de=20projets=20en=20cours=20d'arr=C3=AAt=20et?= =?UTF-8?q?=20en=20mettant=20=C3=A0=20jour=20les=20statuts=20appropri?= =?UTF-8?q?=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/cameraController.js | 56 +++++++++++++++++++++-------- src/models/Project.js | 10 ++++++ 2 files changed, 52 insertions(+), 14 deletions(-) diff --git a/src/controllers/cameraController.js b/src/controllers/cameraController.js index 1462b8a..8f392a9 100644 --- a/src/controllers/cameraController.js +++ b/src/controllers/cameraController.js @@ -73,18 +73,26 @@ class CameraController { * Initie l'arrêt de la procédure de capture */ static stopProcedure = asyncHandler(async (req, res) => { - const { project_id } = req.body; - try { + // Trouve le projet actuellement en cours de capture + const currentProject = await Project.findCurrentRenderingProject(); + + if (!currentProject) { + return sendError('Aucun projet en cours de capture trouvé', res, null, 404); + } + // Met à jour le statut du projet en cours d'arrêt - await Project.updateProject(project_id, { status: config.projectStatus.stopping }); - + await Project.updateProjectStatus(currentProject.id, config.projectStatus.stopping); + // Marque le drapeau d'arrêt await Camera.updateCamera(1, { stop_flag: true }); - console.log('[CAMERA] Arrêt de la caméra demandé, en attente de confirmation...'); + console.log(`[CAMERA] Arrêt de la caméra demandé pour le projet ${currentProject.id}, en attente de confirmation...`); - res.json({ message: 'Procédure d\'arrêt de la caméra initiée avec succès' }); + res.json({ + message: 'Procédure d\'arrêt de la caméra initiée avec succès', + project_id: currentProject.id + }); } catch (error) { sendError('Erreur lors de l\'arrêt de la procédure de capture', res, error, 500); } @@ -105,19 +113,39 @@ class CameraController { await Camera.updateCamera(1, newSettings); - // Réinitialise le statut du projet en cours - const currentProject = await Project.findCurrentRenderingProject(); + // Recherche le projet en cours d'arrêt + const stoppingProject = await Project.findStoppingProject(); - if (currentProject) { - await Project.updateProject(currentProject.id, { status: config.projectStatus.idle}); - console.log(`[CAMERA] Projet : ${currentProject.id} arrêté.`); + if (stoppingProject) { + // Mettre à jour le statut du projet en cours d'arrêt vers idle + await Project.updateProjectStatus(stoppingProject.id, config.projectStatus.idle); + console.log(`[CAMERA] Projet : ${stoppingProject.id} arrêté avec succès.`); + + res.json({ + message: 'Caméra arrêtée avec succès', + project_id: stoppingProject.id, + status: config.projectStatus.idle + }); } else { - console.log('[CAMERA] Aucun projet à arrêter.'); + // Vérifier s'il y a un projet en cours de capture qui n'aurait pas été marqué comme stopping + const currentProject = await Project.findCurrentRenderingProject(); + + if (currentProject) { + await Project.updateProjectStatus(currentProject.id, config.projectStatus.idle); + console.log(`[CAMERA] Projet : ${currentProject.id} arrêté (était en capture).`); + + res.json({ + message: 'Caméra arrêtée avec succès (projet était en capture)', + project_id: currentProject.id, + status: config.projectStatus.idle + }); + } else { + console.log('[CAMERA] Aucun projet en cours d\'arrêt ou de capture trouvé.'); + res.json({ message: 'Caméra arrêtée avec succès mais aucun projet à mettre à jour' }); + } } console.log('[CAMERA] Caméra arrêtée.'); - - res.json({ message: 'Caméra arrêtée avec succès' }); } catch (error) { sendError('Erreur lors de la confirmation de l\'arrêt de la caméra', res, error, 500); } diff --git a/src/models/Project.js b/src/models/Project.js index cfa7a7d..cb70415 100644 --- a/src/models/Project.js +++ b/src/models/Project.js @@ -94,6 +94,16 @@ class Project { const result = await db.query(query); return result.rows[0] || null; }); + + /** + * Trouve le projet en cours d'arrêt (status = stopping) + * @returns {Promise} Projet en cours d'arrêt ou null + */ + static findStoppingProject = wrapDatabaseOperation(async () => { + const query = `SELECT * FROM projects WHERE status = ${config.projectStatus.stopping};`; + const result = await db.query(query); + return result.rows[0] || null; + }); } module.exports = Project; \ No newline at end of file