diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 9b7472a8..a6dd275a 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1217,9 +1217,11 @@ if (isSameTime && !isToday) { // Add to the total consumption and capacities based on water type if (tank.typeOfWater === "bore" || tank.typeOfWater === "Bore Water") { totalBoreConsumptionForSelectedBlockAndTypeOfWater += consumption; + totalConsumptionForSelectedBlockAndTypeOfWater += consumption totalBoreCapacityForSelectedBlockAndTypeOfWater += capacity; } else if (tank.typeOfWater === "drinking" || tank.typeOfWater === "Drinking Water") { totalDrinkingConsumptionForSelectedBlockAndTypeOfWater += consumption; + totalConsumptionForSelectedBlockAndTypeOfWater += consumption totalDrinkingCapacityForSelectedBlockAndTypeOfWater += capacity; } @@ -3272,7 +3274,24 @@ exports.motorAction = async (req, reply) => { } } else if (action === "stop") { - await stopMotor(motorId, customerId, start_instance_id); + // Dynamically find start_instance_id from tank + const tankWithMotor = await Tank.findOne({ + customerId, + "connections.inputConnections.motor_id": motorId + }); + + let dynamicInstanceId = null; + + if (tankWithMotor) { + const connection = tankWithMotor.connections.inputConnections.find(conn => conn.motor_id === motorId); + if (connection && connection.start_instance_id) { + dynamicInstanceId = connection.start_instance_id; + } + } + + await stopMotor(motorId, customerId, dynamicInstanceId); + + try { @@ -6231,6 +6250,7 @@ async function processIotData(hw_Id, data) { 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 startinstance = 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"; @@ -6247,7 +6267,7 @@ async function processIotData(hw_Id, data) { 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); @@ -6260,12 +6280,12 @@ async function processIotData(hw_Id, data) { await Tank.findOneAndUpdate( - { customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() }, + { customerId:motorTank.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 }, + { customerId:motorTank.customerId, motor_id: motorId, start_instance_id: startinstance }, { $set: { stopTime: currentTime, @@ -6277,7 +6297,8 @@ async function processIotData(hw_Id, data) { ); } } - + await motorTank.save(); + } console.log(`✅ Data processed successfully for hw_Id: ${hw_Id}`); @@ -6462,6 +6483,7 @@ exports.consumptionofparticulartank = async (request, reply) => { // Convert input dates into proper JavaScript Date objects for comparison const start = moment(startDate, "DD-MMM-YYYY - HH:mm").toDate(); const end = moment(stopDate, "DD-MMM-YYYY - HH:mm").toDate(); + end.setHours(23, 59, 59, 999); // Ensure full day is included // Find the tank by customerId, tankLocation, and tankName const tank = await Tank.findOne({ @@ -6501,6 +6523,7 @@ exports.consumptionofparticulartank = async (request, reply) => { return dateA - dateB; // Sort in ascending order }); + // Calculate total consumption from filtered records const total_consumption_from_records = filteredConsumptions.reduce((acc, record) => { return acc + parseInt(record.consumption, 10); @@ -6508,6 +6531,7 @@ exports.consumptionofparticulartank = async (request, reply) => { // Calculate final consumption const consumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel + total_consumption_from_records; + // Prepare response data const tankData = { @@ -6519,6 +6543,19 @@ exports.consumptionofparticulartank = async (request, reply) => { capacity: tank.capacity, waterlevel: tank.waterlevel, }; + const stopDateMoment = moment(stopDate, "DD-MMM-YYYY - HH:mm"); +const today = moment().startOf('day'); + +if (stopDateMoment.isSame(today, 'day')) { + const latestConsumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel; + const nowFormatted = moment().format("DD-MMM-YYYY - HH:mm"); + + filteredConsumptions.push({ + tankName: tank.tankName, + consumption: latestConsumption.toString(), + time: nowFormatted + }); +} // Send the response, including both total consumption and filtered consumption records reply.send({