Bhaskar 9 months ago
commit 28999c5d98

@ -1217,9 +1217,11 @@ if (isSameTime && !isToday) {
// Add to the total consumption and capacities based on water type // Add to the total consumption and capacities based on water type
if (tank.typeOfWater === "bore" || tank.typeOfWater === "Bore Water") { if (tank.typeOfWater === "bore" || tank.typeOfWater === "Bore Water") {
totalBoreConsumptionForSelectedBlockAndTypeOfWater += consumption; totalBoreConsumptionForSelectedBlockAndTypeOfWater += consumption;
totalConsumptionForSelectedBlockAndTypeOfWater += consumption
totalBoreCapacityForSelectedBlockAndTypeOfWater += capacity; totalBoreCapacityForSelectedBlockAndTypeOfWater += capacity;
} else if (tank.typeOfWater === "drinking" || tank.typeOfWater === "Drinking Water") { } else if (tank.typeOfWater === "drinking" || tank.typeOfWater === "Drinking Water") {
totalDrinkingConsumptionForSelectedBlockAndTypeOfWater += consumption; totalDrinkingConsumptionForSelectedBlockAndTypeOfWater += consumption;
totalConsumptionForSelectedBlockAndTypeOfWater += consumption
totalDrinkingCapacityForSelectedBlockAndTypeOfWater += capacity; totalDrinkingCapacityForSelectedBlockAndTypeOfWater += capacity;
} }
@ -3272,7 +3274,24 @@ exports.motorAction = async (req, reply) => {
} }
} else if (action === "stop") { } 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 { try {
@ -6231,6 +6250,7 @@ async function processIotData(hw_Id, data) {
if (inputConnection.motor_stop_status === "2" && status === 1) { 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 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'); const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm');
inputConnection.motor_stop_status = "1"; inputConnection.motor_stop_status = "1";
inputConnection.motor_on_type = "manual"; inputConnection.motor_on_type = "manual";
@ -6247,7 +6267,7 @@ async function processIotData(hw_Id, data) {
console.log(motorData,"motorData") console.log(motorData,"motorData")
if (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 receiverTank = await Tank.findOne({ customerId:motorTank.customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() });
const receiverFinalWaterLevel = parseInt(receiverTank.waterlevel, 10); const receiverFinalWaterLevel = parseInt(receiverTank.waterlevel, 10);
const quantityDelivered = receiverFinalWaterLevel - parseInt(motorData.receiverInitialwaterlevel, 10); const quantityDelivered = receiverFinalWaterLevel - parseInt(motorData.receiverInitialwaterlevel, 10);
@ -6260,12 +6280,12 @@ async function processIotData(hw_Id, data) {
await Tank.findOneAndUpdate( 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 } } { $set: { total_water_added_from_midnight: totalwaterpumped } }
); );
await MotorData.updateOne( await MotorData.updateOne(
{ customerId, motor_id: motorId, start_instance_id: start_instance_id }, { customerId:motorTank.customerId, motor_id: motorId, start_instance_id: startinstance },
{ {
$set: { $set: {
stopTime: currentTime, stopTime: currentTime,
@ -6277,6 +6297,7 @@ async function processIotData(hw_Id, data) {
); );
} }
} }
await motorTank.save();
} }
@ -6462,6 +6483,7 @@ exports.consumptionofparticulartank = async (request, reply) => {
// Convert input dates into proper JavaScript Date objects for comparison // Convert input dates into proper JavaScript Date objects for comparison
const start = moment(startDate, "DD-MMM-YYYY - HH:mm").toDate(); const start = moment(startDate, "DD-MMM-YYYY - HH:mm").toDate();
const end = moment(stopDate, "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 // Find the tank by customerId, tankLocation, and tankName
const tank = await Tank.findOne({ const tank = await Tank.findOne({
@ -6501,6 +6523,7 @@ exports.consumptionofparticulartank = async (request, reply) => {
return dateA - dateB; // Sort in ascending order return dateA - dateB; // Sort in ascending order
}); });
// Calculate total consumption from filtered records // Calculate total consumption from filtered records
const total_consumption_from_records = filteredConsumptions.reduce((acc, record) => { const total_consumption_from_records = filteredConsumptions.reduce((acc, record) => {
return acc + parseInt(record.consumption, 10); return acc + parseInt(record.consumption, 10);
@ -6509,6 +6532,7 @@ exports.consumptionofparticulartank = async (request, reply) => {
// Calculate final consumption // Calculate final consumption
const consumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel + total_consumption_from_records; const consumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel + total_consumption_from_records;
// Prepare response data // Prepare response data
const tankData = { const tankData = {
tankname: tank.tankName, tankname: tank.tankName,
@ -6519,6 +6543,19 @@ exports.consumptionofparticulartank = async (request, reply) => {
capacity: tank.capacity, capacity: tank.capacity,
waterlevel: tank.waterlevel, 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 // Send the response, including both total consumption and filtered consumption records
reply.send({ reply.send({

Loading…
Cancel
Save