From 33edd91c61b1ea410ad154b5c4d24b129c29d922 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Wed, 28 Aug 2024 14:59:06 +0530 Subject: [PATCH] changes notification --- src/controllers/tanksController.js | 72 +++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 2 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 606db97b..a7a7252e 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1453,7 +1453,30 @@ exports.motorAction = async (req, reply) => { if (action === "start") { if (req.body.threshold_type === "time") { - // (Existing code for time-based thresholds) + const receiver_tank_info7 = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() }); + + const newMotorData = new MotorData({ + customerId: customerId, + motor_id: motorId, + start_instance_id: start_instance_id, + supplierTank: req.body.from, + receiverTank: req.body.to, + supplier_type: req.body.from_type, + receiver_type: req.body.to_type, + startTime: req.body.startTime, + receiverInitialwaterlevel:parseInt(receiver_tank_info7.waterlevel, 10) + }); + await newMotorData.save(); + + 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,[`connections.inputConnections.${index}.start_instance_id`]: start_instance_id } } + ); + } + } // Start monitoring water level based on threshold time const thresholdTime = moment().add(req.body.manual_threshold_time, 'minutes').toDate(); @@ -1514,7 +1537,52 @@ exports.motorAction = async (req, reply) => { } }, 60000); } else if (req.body.threshold_type === "litres") { - // (Existing code for litres-based thresholds) + console.log("entered litres") + const receiver_tank_info7 = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() }); + + const newMotorData = new MotorData({ + customerId: customerId, + motor_id: motorId, + start_instance_id: start_instance_id, + supplierTank: req.body.from, + receiverTank: req.body.to, + supplier_type: req.body.from_type, + receiver_type: req.body.to_type, + startTime: req.body.startTime, + receiverInitialwaterlevel:parseInt(receiver_tank_info7.waterlevel, 10) + }); + await newMotorData.save(); + // 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() }); + const supplier_tank_info = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() }); + if (!receiver_tank_info) { + throw new Error("Receiver tank not found."); + } + if (!supplier_tank_info) { + throw new Error("Supplierr tank not found."); + } + const supplier_capacity = parseInt(supplier_tank_info.capacity, 10); + const supplier_waterLevel = parseInt(supplier_tank_info.waterlevel, 10); + + const capacity = parseInt(receiver_tank_info.capacity, 10); + const waterLevel = parseInt(receiver_tank_info.waterlevel, 10); + const desired_percentage = parseInt(req.body.manual_threshold_litres.replace(/,/g, ''), 10); + + console.log(desired_percentage) + const threshold_water_level = waterLevel+desired_percentage; + + const supplier_threshold = supplier_waterLevel-desired_percentage + console.log(supplier_threshold,"supplier_threshold") + + 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`]: supplier_threshold.toString(), [`connections.inputConnections.${index}.startTime`]: req.body.startTime } } + ); + } + } // Start monitoring water level based on litres const intervalId = setInterval(async () => {