From e621b2171f67bf12b73586897959c46340960c0a Mon Sep 17 00:00:00 2001 From: varun Date: Thu, 25 Jan 2024 04:44:03 -0500 Subject: [PATCH] changed motor status read and write --- src/controllers/tanksController.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 1491b8de..0656f7c6 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1502,10 +1502,11 @@ exports.readMotorStatusFromIot = async (req, reply) => { } const motor_status = motorInfo.motor_status; + const motor_stop_status = motorInfo.motor_stop_status; - reply.send({ status_code: 200, motor_status:motor_status }); + reply.send({ status_code: 200, motor_status:motor_status ,motor_stop_status:motor_stop_status}); } catch (err) { throw boom.boomify(err); } @@ -1573,6 +1574,7 @@ exports.writeMotorStatus = async (req, reply) => { + exports.changeMotorStatus = async (req, reply) => { try { const motor_id = req.body.motor_id; @@ -1595,12 +1597,33 @@ exports.changeMotorStatus = async (req, reply) => { motor_stop_status: action, motor_status: updatedMotor.motor_status // Assuming motor_status is a field in your Tank model }); + + // Schedule a check for motor_status after 2 minutes + setTimeout(async () => { + try { + // Fetch the motor_status again + const finalMotor = await Tank.findOne({ motor_id: motor_id }); + + // Check if motor_status is different from motor_stop_status + if (finalMotor && finalMotor.motor_status !== finalMotor.motor_stop_status) { + // If they are different, update motor_stop_status to motor_status + await Tank.findOneAndUpdate( + { motor_id: motor_id }, + { $set: { motor_stop_status: finalMotor.motor_status } } + ); + } + } catch (error) { + console.error("Error in setTimeout:", error); + } + }, 2 * 60 * 1000); // 2 minutes in milliseconds } catch (err) { throw boom.boomify(err); } }; + + exports.motortemperature = async (req, reply) => { try {