made changes in motorction start and stop

master
varun 1 year ago
parent 68a4749da7
commit 8bee134e88

@ -963,7 +963,7 @@ exports.consumption = async (request, reply) => {
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;
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 }); 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 // Update the motor stop status immediately if action is stop
if (action === "stop") { if (action === "stop") {
// Update the motor stop status and other fields
await Tank.updateOne( await Tank.updateOne(
{ customerId, "connections.inputConnections.motor_id": motorId }, { customerId, "connections.inputConnections.motor_id": motorId },
{ {
@ -1026,32 +1027,41 @@ exports.motorAction = async (req, reply) => {
} }
); );
await delay(300000); // Send immediate response to the client
reply.code(200).send({ message: "Motor stopped successfully." });
// Update the existing motor data entry with stop details
const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id: start_instance_id }); // Perform stop operations in the background
if (motorData) { (async () => {
const receiverTank = await Tank.findOne({ customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() }); await delay(300000);
const receiverFinalWaterLevel = parseInt(receiverTank.waterlevel, 10);
const quantityDelivered = receiverFinalWaterLevel - parseInt(motorData.receiverInitialwaterlevel, 10); // Update the existing motor data entry with stop details
const water_pumped_till_now = parseInt(receiverTank.total_water_added_from_midnight, 10); const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id: start_instance_id });
const totalwaterpumped = quantityDelivered + water_pumped_till_now if (motorData) {
await Tank.findOneAndUpdate({customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase()}, { $set: { total_water_added_from_midnight: totalwaterpumped } }) const receiverTank = await Tank.findOne({ customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() });
const receiverFinalWaterLevel = parseInt(receiverTank.waterlevel, 10);
await MotorData.updateOne( const quantityDelivered = receiverFinalWaterLevel - parseInt(motorData.receiverInitialwaterlevel, 10);
{ customerId, motor_id: motorId, start_instance_id: start_instance_id }, const water_pumped_till_now = parseInt(receiverTank.total_water_added_from_midnight, 10);
{ const totalwaterpumped = quantityDelivered + water_pumped_till_now;
$set: { await Tank.findOneAndUpdate(
stopTime: req.body.stopTime, { customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() },
receiverfinalwaterlevel: receiverFinalWaterLevel.toString(), { $set: { total_water_added_from_midnight: totalwaterpumped } }
quantity_delivered: quantityDelivered.toString() );
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 { } else {
// Update the motor stop status to "2" for start action // Update the motor stop status to "2" for start action
await Tank.updateOne( await Tank.updateOne(
@ -1099,11 +1109,15 @@ exports.motorAction = async (req, reply) => {
const intervalId = setInterval(async () => { 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 = 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_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; 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) { if (new Date() >= thresholdTime || splr_tank_info3_percentage <= 20) {
console.log(splr_tank_info3_percentage,)
await Tank.updateOne( await Tank.updateOne(
{ customerId, "connections.inputConnections.motor_id": motorId }, { customerId, "connections.inputConnections.motor_id": motorId },
{ {
@ -1144,6 +1158,7 @@ exports.motorAction = async (req, reply) => {
} }
}, 60000); }, 60000);
} else if (req.body.threshold_type === "litres") { } 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 receiver_tank_info7 = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() });
const newMotorData = new MotorData({ const newMotorData = new MotorData({

Loading…
Cancel
Save