diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index baac6874..e556f4be 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -963,7 +963,7 @@ exports.consumption = async (request, reply) => { const consumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel + total_consumption_from_records; - tankData.push({ tankname, totalConsumption: consumption }); + tankData.push({ tankname, totalConsumption: consumption,block:tank.blockName,TypeofWater:tank.typeOfWater,location:tank.tankLocation,capacity:tank.capacity,waterlevel:tank.waterlevel }); } reply.send({ status_code: 200, tankData }); @@ -1013,6 +1013,7 @@ exports.motorAction = async (req, reply) => { // Update the motor stop status immediately if action is stop if (action === "stop") { + // Update the motor stop status and other fields await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId }, { @@ -1026,32 +1027,41 @@ exports.motorAction = async (req, reply) => { } ); - await delay(300000); - - // Update the existing motor data entry with stop details - const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id: start_instance_id }); - if (motorData) { - const receiverTank = await Tank.findOne({ 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 - await Tank.findOneAndUpdate({customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase()}, { $set: { total_water_added_from_midnight: totalwaterpumped } }) - - await MotorData.updateOne( - { customerId, motor_id: motorId, start_instance_id: start_instance_id }, - { - $set: { - stopTime: req.body.stopTime, - receiverfinalwaterlevel: receiverFinalWaterLevel.toString(), - quantity_delivered: quantityDelivered.toString() + // Send immediate response to the client + reply.code(200).send({ message: "Motor stopped successfully." }); + + // Perform stop operations in the background + (async () => { + await delay(300000); + + // Update the existing motor data entry with stop details + const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id: start_instance_id }); + if (motorData) { + const receiverTank = await Tank.findOne({ 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; + await Tank.findOneAndUpdate( + { customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() }, + { $set: { total_water_added_from_midnight: totalwaterpumped } } + ); + + await MotorData.updateOne( + { customerId, motor_id: motorId, start_instance_id: start_instance_id }, + { + $set: { + stopTime: req.body.stopTime, + receiverfinalwaterlevel: receiverFinalWaterLevel.toString(), + quantity_delivered: quantityDelivered.toString() + } } - } - ); - } - + ); + } + })(); - + // Return here to ensure the rest of the code is not executed for the stop action + return; } else { // Update the motor stop status to "2" for start action await Tank.updateOne( @@ -1099,11 +1109,15 @@ exports.motorAction = async (req, reply) => { const intervalId = setInterval(async () => { const splr_tank_info3 = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() }); const splr_tank_info3_waterlevel = parseInt(splr_tank_info3.waterlevel, 10); - const splr_tank_info3_capacity = parseInt(splr_tank_info3.capacity, 10); + //console.log(splr_tank_info3_waterlevel,"splr_tank_info3_waterlevel") + const splr_tank_info3_capacity = parseInt(splr_tank_info3.capacity.replace(/,/g, ''), 10); + // const splr_tank_info3_capacity = parseInt(splr_tank_info3.capacity, 10); + // console.log(splr_tank_info3.capacity,splr_tank_info3_capacity,"splr_tank_info3_capacity") const splr_tank_info3_percentage = (splr_tank_info3_waterlevel / splr_tank_info3_capacity) * 100; - console.log(splr_tank_info3_percentage, "percentage for less than 20"); + // console.log(splr_tank_info3_percentage, "percentage for less than 20"); if (new Date() >= thresholdTime || splr_tank_info3_percentage <= 20) { + console.log(splr_tank_info3_percentage,) await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId }, { @@ -1144,6 +1158,7 @@ exports.motorAction = async (req, reply) => { } }, 60000); } else if (req.body.threshold_type === "litres") { + 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({