From d5361cf26cbcb897ae641a10f905be8346edcc32 Mon Sep 17 00:00:00 2001 From: varun Date: Thu, 16 May 2024 05:15:24 -0400 Subject: [PATCH] made changes in start and stop --- src/controllers/tanksController.js | 76 +++++++++++++++++++++++++----- 1 file changed, 64 insertions(+), 12 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index c86a3d2f..8e7d127c 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -883,8 +883,22 @@ exports.motorAction = async (req, reply) => { if (action === "stop") { await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId }, - { $set: { "connections.inputConnections.$.motor_stop_status": "1",stopTime:req.body.stopTime } } + { + $set: { + "connections.inputConnections.$.motor_stop_status": "1", + "connections.inputConnections.$.stopTime": req.body.stopTime, + "connections.inputConnections.$.threshold_type": null, + "connections.inputConnections.$.manual_threshold_time": null, + "connections.inputConnections.$.manual_threshold_percentage": null + } + } ); + + + + + + } else { // Update the motor stop status to "2" for start action await Tank.updateOne( @@ -897,10 +911,22 @@ exports.motorAction = async (req, reply) => { if (action === "start") { if (req.body.threshold_type === "time") { // If threshold type is time, update threshold time - await Tank.updateOne( - { customerId, "connections.inputConnections.motor_id": motorId }, - { $set: { "connections.inputConnections.$.manual_threshold_time": req.body.manual_threshold_time,startTime:req.body.startTime } } - ); + // await Tank.updateOne( + // { customerId, "connections.inputConnections.motor_id": motorId }, + // { $set: { "connections.inputConnections.$.manual_threshold_time": req.body.manual_threshold_time,startTime:req.body.startTime } } + // ); + + + for await (const tank of Tank.find({ "connections.inputConnections.motor_id": motorId })) { + const index = tank.connections.inputConnections.findIndex(connection => connection.motor_id === motorId); + if (index !== -1) { + await Tank.updateOne( + { customerId, "connections.inputConnections.motor_id": motorId }, + { $set: { [`connections.inputConnections.${index}.manual_threshold_time`]: req.body.manual_threshold_time, [`connections.inputConnections.${index}.startTime`]: req.body.startTime } } + ); + } + } + // Start monitoring water level based on threshold time const thresholdTime = moment().add(req.body.manual_threshold_time, 'minutes').toDate(); @@ -910,14 +936,22 @@ exports.motorAction = async (req, reply) => { // Stop the motor pump await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId }, - { $set: { "connections.inputConnections.$.motor_stop_status": "1",manual_threshold_time:null } } + { + $set: { + "connections.inputConnections.$.motor_stop_status": "1", + + "connections.inputConnections.$.threshold_type": null, + "connections.inputConnections.$.manual_threshold_time": null, + "connections.inputConnections.$.manual_threshold_percentage": null + } + } ); clearInterval(intervalId); // Stop monitoring water level } }, 60000); // Check water level every minute } else if (req.body.threshold_type === "litres") { // If threshold type is percentage, calculate percentage threshold - const receiver_tank_info = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase(),startTime:req.body.startTime }); + const receiver_tank_info = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() }); if (!receiver_tank_info) { throw new Error("Receiver tank not found."); } @@ -926,11 +960,21 @@ exports.motorAction = async (req, reply) => { const desired_percentage = parseInt(req.body.manual_threshold_litres, 10); const threshold_water_level = waterLevel+desired_percentage; + + for await (const tank of Tank.find({ "connections.inputConnections.motor_id": motorId })) { + const index = tank.connections.inputConnections.findIndex(connection => connection.motor_id === motorId); + if (index !== -1) { + await Tank.updateOne( + { customerId, "connections.inputConnections.motor_id": motorId }, + { $set: { [`connections.inputConnections.${index}.manual_threshold_percentage`]: threshold_water_level.toString(), [`connections.inputConnections.${index}.startTime`]: req.body.startTime } } + ); + } + } + + + // Update water level threshold - await Tank.updateOne( - { customerId, "connections.inputConnections.motor_id": motorId }, - { $set: { "connections.inputConnections.$.manual_threshold_percentage": threshold_water_level.toString() } } - ); + // Start monitoring water level based on threshold percentage const intervalId = setInterval(async () => { @@ -940,7 +984,15 @@ exports.motorAction = async (req, reply) => { // Stop the motor pump await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId }, - { $set: { "connections.inputConnections.$.motor_stop_status": "1",manual_threshold_percentage:null} } + { + $set: { + "connections.inputConnections.$.motor_stop_status": "1", + + "connections.inputConnections.$.threshold_type": null, + "connections.inputConnections.$.manual_threshold_time": null, + "connections.inputConnections.$.manual_threshold_percentage": null + } + } ); clearInterval(intervalId); // Stop monitoring water level }