From 3c9a2e491257ccfaaf02e524cfc1963a5c8c52d2 Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 26 Mar 2025 13:31:02 +0530 Subject: [PATCH 1/9] changes --- src/controllers/tanksController.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 574b5569..c24a2842 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -6247,7 +6247,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 +6260,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: start_instance_id }, { $set: { stopTime: currentTime, From 348204eb6cdc346e57b177797731d9d96077cd91 Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 26 Mar 2025 13:35:39 +0530 Subject: [PATCH 2/9] changes --- src/controllers/tanksController.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index ff4b7d51..5179d7df 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -6231,6 +6231,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"; @@ -6265,7 +6266,7 @@ async function processIotData(hw_Id, data) { ); await MotorData.updateOne( - { customerId:motorTank.customerId, motor_id: motorId, start_instance_id: start_instance_id }, + { customerId:motorTank.customerId, motor_id: motorId, start_instance_id: startinstance }, { $set: { stopTime: currentTime, From 0fbf4958532231206d064ddc7014474af294fcaf Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 26 Mar 2025 13:43:43 +0530 Subject: [PATCH 3/9] changes --- src/controllers/tanksController.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 5179d7df..43e0a5b5 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -6278,7 +6278,8 @@ async function processIotData(hw_Id, data) { ); } } - + await motorTank.save(); + } console.log(`✅ Data processed successfully for hw_Id: ${hw_Id}`); From 0c063e321b9f07641ffa2593e55428a70adb60bb Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 26 Mar 2025 16:20:15 +0530 Subject: [PATCH 4/9] changes in consumption --- src/controllers/tanksController.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 43e0a5b5..e7970c59 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; } From 8cd1c472cf4614e3d6c5e9e9bfbeb151f001ea48 Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 26 Mar 2025 16:45:14 +0530 Subject: [PATCH 5/9] changes in consumption --- src/controllers/tanksController.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index e7970c59..c0e79d69 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -6466,6 +6466,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({ From 6cc3ed7421d60337c468d5a1e42dd3195bccad75 Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 26 Mar 2025 16:50:11 +0530 Subject: [PATCH 6/9] changes --- src/controllers/tanksController.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index c0e79d69..17365ce8 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -6506,6 +6506,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); @@ -6513,6 +6514,20 @@ exports.consumptionofparticulartank = async (request, reply) => { // Calculate final consumption const consumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel + total_consumption_from_records; + // If end date is today, attach one extra record +const today = moment().startOf('day'); +const isEndDateToday = moment(end).isSame(today, 'day'); + +if (isEndDateToday) { + const extraConsumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel; + const now = moment().format("DD-MMM-YYYY - HH:mm"); + + filteredConsumptions.push({ + tankName: tank.tankName, + consumption: extraConsumption.toString(), + time: now + }); +} // Prepare response data const tankData = { From fdc63661b33affa5b377356e497a4635a3c51d33 Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 26 Mar 2025 16:51:40 +0530 Subject: [PATCH 7/9] changes --- src/controllers/tanksController.js | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 17365ce8..e36e6950 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -6514,20 +6514,7 @@ exports.consumptionofparticulartank = async (request, reply) => { // Calculate final consumption const consumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel + total_consumption_from_records; - // If end date is today, attach one extra record -const today = moment().startOf('day'); -const isEndDateToday = moment(end).isSame(today, 'day'); - -if (isEndDateToday) { - const extraConsumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel; - const now = moment().format("DD-MMM-YYYY - HH:mm"); - - filteredConsumptions.push({ - tankName: tank.tankName, - consumption: extraConsumption.toString(), - time: now - }); -} + // Prepare response data const tankData = { From ee1892de052de1eccdde3c12680a055ef6bfe5f1 Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 26 Mar 2025 16:54:43 +0530 Subject: [PATCH 8/9] changes --- src/controllers/tanksController.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index e36e6950..e25884a9 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -6526,6 +6526,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({ From a356f2e263fd9627abeec2a143a4f1b7196a8c67 Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 26 Mar 2025 17:04:11 +0530 Subject: [PATCH 9/9] changes in stop motor --- src/controllers/tanksController.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index e25884a9..aafe0a51 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -3274,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 {