From 8f21f33985fb3737791165af5eb29116576deaa6 Mon Sep 17 00:00:00 2001 From: varun Date: Wed, 15 May 2024 08:13:50 -0400 Subject: [PATCH] change in motoraction --- src/controllers/tanksController.js | 40 ++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index c106da54..0c94a531 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1955,6 +1955,46 @@ exports.writeMotorStatus = async (req, reply) => { } }; +exports.writeMotorStatus = async (req, reply) => { + try { + const motor_id = req.body.motor_id; + const status = req.body.status; + + // Find the tank that contains the specified motor_id in its inputConnections + const tank = await Tank.findOne({ "connections.inputConnections.motor_id": motor_id }); + + if (!tank) { + return reply.status(404).send({ + status_code: 404, + message: 'Motor not found for the specified motor_id' + }); + } + + // Find the inputConnection with the specified motor_id + const inputConnection = tank.connections.inputConnections.find(conn => conn.motor_id === motor_id); + + // Update the motor_status of the inputConnection + inputConnection.motor_status = status; + + // Update real_motor_status based on the conditions + if (status === "1" && inputConnection.real_motor_status === "4") { + inputConnection.real_motor_status = 1; + } else if (status === "2" && inputConnection.real_motor_status === "3") { + inputConnection.real_motor_status = 2; + } + + // Save the updated tank + await tank.save(); + + // Send the response with the updated motor_status + reply.send({ + status_code: 200, + motor_status: status + }); + } catch (err) { + throw boom.boomify(err); + } +};