From b33a945c4522e2be0838641fc4e3e895d8e7d28c Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 26 Mar 2025 13:15:51 +0530 Subject: [PATCH] change in water/iotdata motor data saving included --- src/controllers/tanksController.js | 59 +++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 5 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 83d4ce20..574b5569 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -3002,7 +3002,7 @@ exports.motorAction = async (req, reply) => { console.log(motorId,"interval created") console.log(customerId,req.body.from,req.body.from_type.toLowerCase()) const supplierTank = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() }); -console.log("up",supplierTank) + console.log("up",supplierTank) console.log(supplierTank.waterlevel,"parseInt(supplierTank.waterlevel, 10)") const currentWaterLevel = parseInt(supplierTank.waterlevel, 10); const currentWaterPercentage = (currentWaterLevel / parseInt(supplierTank.capacity.replace(/,/g, ''), 10)) * 100; @@ -3094,7 +3094,7 @@ console.log("up",supplierTank) { $set: { "connections.inputConnections.$.motor_stop_status": "1", - + "connections.inputConnections.$.start_instance_id": null, "connections.inputConnections.$.threshold_type": null, "connections.inputConnections.$.manual_threshold_time": null, "connections.inputConnections.$.manual_threshold_percentage": null, @@ -3232,7 +3232,7 @@ console.log("up",supplierTank) { $set: { "connections.inputConnections.$.motor_stop_status": "1", - + "connections.inputConnections.$.start_instance_id": null, "connections.inputConnections.$.threshold_type": null, "connections.inputConnections.$.manual_threshold_time": null, "connections.inputConnections.$.manual_threshold_percentage": null @@ -3302,6 +3302,7 @@ async function stopMotor(motorId, customerId, start_instance_id) { { $set: { "connections.inputConnections.$.motor_stop_status": "1", "connections.inputConnections.$.stopTime": currentTime, + "connections.inputConnections.$.start_instance_id": null, "connections.inputConnections.$.threshold_type": null, "connections.inputConnections.$.manual_threshold_time": null, "connections.inputConnections.$.manual_threshold_percentage": null @@ -6213,22 +6214,70 @@ async function processIotData(hw_Id, data) { inputConnection.motor_on_type = "forced_manual"; inputConnection.startTime = formattedTime; inputConnection.start_instance_id = startInstanceId; + const newMotorData = new MotorData({ + customerId:motorTank.customerId, + motor_id: hw_Id, + start_instance_id: startInstanceId, + supplierTank: inputConnection.inputConnections, + receiverTank: motorTank.tankName, + supplier_type: inputConnection.input_type, + receiver_type: motorTank.tankLocation, + startTime: formattedTime, + receiverInitialwaterlevel: parseInt(inputConnection.water_level, 10) + }); + await newMotorData.save(); + } if (inputConnection.motor_stop_status === "2" && status === 1) { + const motorData = await MotorData.findOne({ customerId:motorTank.customerId, motor_id: hw_Id, start_instance_id: inputConnection.start_instance_id }); const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm'); inputConnection.motor_stop_status = "1"; inputConnection.motor_on_type = "manual"; inputConnection.stopTime = currentTime; + inputConnection.start_instance_id = null; + const motorId = hw_Id; if (motorIntervals[motorId]) { clearInterval(motorIntervals[motorId]); delete motorIntervals[motorId]; console.log("motor interval deleted") } - } + + + console.log(motorData,"motorData") + if (motorData) { + console.log("got into if") + const receiverTank = await Tank.findOne({ customerId:motorTank.customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() }); + const receiverFinalWaterLevel = parseInt(receiverTank.waterlevel, 10); + const quantityDelivered = receiverFinalWaterLevel - parseInt(motorData.receiverInitialwaterlevel, 10); + const water_pumped_till_now = parseInt(receiverTank.total_water_added_from_midnight, 10); + const totalwaterpumped = quantityDelivered + water_pumped_till_now; + const start = moment(motorData.startTime, 'DD-MMM-YYYY - HH:mm'); + const stop = moment(currentTime, 'DD-MMM-YYYY - HH:mm'); + const duration = moment.duration(stop.diff(start)); + const runtime = Math.floor(duration.asMinutes()); // runtime in minutes + + + await Tank.findOneAndUpdate( + { customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() }, + { $set: { total_water_added_from_midnight: totalwaterpumped } } + ); - await motorTank.save(); + await MotorData.updateOne( + { customerId, motor_id: motorId, start_instance_id: start_instance_id }, + { + $set: { + stopTime: currentTime, + receiverfinalwaterlevel: receiverFinalWaterLevel.toString(), + quantity_delivered: quantityDelivered.toString(), + runtime: runtime.toString() + } + } + ); + } + } + } console.log(`✅ Data processed successfully for hw_Id: ${hw_Id}`);