diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 3afa3376..67ab1693 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -2767,38 +2767,40 @@ exports.getPumpsAndUsers = async (req, reply) => { } }; - exports.motoractiontest = async (req, reply) => { try { const { customerId } = req.params; const { motor_id, action } = req.body; - // Assuming you have a MongoDB model or connection + // Fetch Tank data using customerId const customer = await Tank.findOne({ customerId }); + + // console.log("Customer Data:", customer); + if (!customer) { return reply.status(404).send({ success: false, message: "Customer not found" }); } let motorFound = false; - // Traverse through tanks and their input connections - - - - + // Traverse through inputConnections instead of tanks.inputconnections + for (const inputConnection of customer.connections.inputConnections || []) { + console.log("Checking Motor ID:", inputConnection.motor_id); - if (action === 'start') { - await this.publishMotorStopStatus(motor_id, "2"); - } else if (action === 'stop') { - await this.publishMotorStopStatus(motor_id, "1"); - } else { - return reply.status(400).send({ success: false, message: "Invalid action" }); - } + if (String(inputConnection.motor_id) === String(motor_id)) { // Convert both to string + motorFound = true; - - - - + if (action === "start") { + await this.publishMotorStopStatus(motor_id, "2"); + } else if (action === "stop") { + await this.publishMotorStopStatus(motor_id, "1"); + } else { + return reply.status(400).send({ success: false, message: "Invalid action" }); + } + + return reply.send({ success: true, message: `Motor ${action} command sent.` }); + } + } if (!motorFound) { return reply.status(404).send({ success: false, message: "Motor ID not found" }); @@ -2812,6 +2814,7 @@ exports.motoractiontest = async (req, reply) => { + const motorIntervals = {}; async function calculateTotalPumpedWater(customerId, motorId, start_instance_id) { const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id: start_instance_id });