From 276725ca119bbefbb203a596a6f63a228753441b Mon Sep 17 00:00:00 2001 From: varun Date: Mon, 20 May 2024 06:15:41 -0400 Subject: [PATCH] automation of motor switch test2 --- src/controllers/tanksController.js | 73 +++++++++++++++--------------- 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 2c4d65c8..3dc811da 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1075,47 +1075,48 @@ const motorActionAuto = async (req, reply) => { }; - const checkAutoMode = async () => { try { const tanks = await Tank.find(); for (const tank of tanks) { - if (tank.auto_mode === "active") { - console.log("this is automation " + tank.tankName) - const waterLevel = parseInt(tank.waterlevel, 10); - const capacity = parseInt(tank.capacity, 10); - const autoMinPercentage = parseInt(tank.auto_min_percentage, 10); - const autoMaxPercentage = parseInt(tank.auto_max_percentage, 10); - const currentPercentage = (waterLevel / capacity) * 100; - console.log("this is automation percentage " + currentPercentage) - const now = moment().format('DD-MMM-YYYY-HH-mm'); - - if (capacity > 0) { - if (currentPercentage <= autoMinPercentage) { - await motorActionAuto({ - params: { customerId: tank.customerId }, - body: { - action: "start", - motor_id: tank.connections.inputConnections[0].motor_id, - motor_on_type: "auto", - startTime: now - } - }, { - code: (statusCode) => ({ send: (response) => console.log(response) }) - }); - } else if (currentPercentage >= autoMaxPercentage && tank.connections.inputConnections[0].motor_on_type === "auto") { - await motorActionAuto({ - params: { customerId: tank.customerId }, - body: { - action: "stop", - motor_id: tank.connections.inputConnections[0].motor_id, - motor_on_type: "auto", - stopTime: now - } - }, { - code: (statusCode) => ({ send: (response) => console.log(response) }) - }); + for (const inputConnection of tank.connections.inputConnections) { + if (inputConnection.auto_mode === "active") { + console.log("This is automation for tank: " + tank.tankName); + const waterLevel = parseInt(tank.waterlevel, 10); + const capacity = parseInt(tank.capacity, 10); + const autoMinPercentage = parseInt(inputConnection.auto_min_percentage, 10); + const autoMaxPercentage = parseInt(inputConnection.auto_max_percentage, 10); + const currentPercentage = (waterLevel / capacity) * 100; + console.log("This is automation percentage: " + currentPercentage); + const now = moment().format('DD-MMM-YYYY-HH-mm'); + + if (capacity > 0) { + if (currentPercentage <= autoMinPercentage) { + await motorActionAuto({ + params: { customerId: tank.customerId }, + body: { + action: "start", + motor_id: inputConnection.motor_id, + motor_on_type: "auto", + startTime: now + } + }, { + code: (statusCode) => ({ send: (response) => console.log(response) }) + }); + } else if (currentPercentage >= autoMaxPercentage && inputConnection.motor_on_type === "auto") { + await motorActionAuto({ + params: { customerId: tank.customerId }, + body: { + action: "stop", + motor_id: inputConnection.motor_id, + motor_on_type: "auto", + stopTime: now + } + }, { + code: (statusCode) => ({ send: (response) => console.log(response) }) + }); + } } } }