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,6 +1027,11 @@ exports.motorAction = async (req, reply) => {
} }
); );
// Send immediate response to the client
reply.code(200).send({ message: "Motor stopped successfully." });
// Perform stop operations in the background
(async () => {
await delay(300000); await delay(300000);
// Update the existing motor data entry with stop details // Update the existing motor data entry with stop details
@ -1035,8 +1041,11 @@ exports.motorAction = async (req, reply) => {
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);
const water_pumped_till_now = parseInt(receiverTank.total_water_added_from_midnight, 10); const water_pumped_till_now = parseInt(receiverTank.total_water_added_from_midnight, 10);
const totalwaterpumped = quantityDelivered + water_pumped_till_now 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 Tank.findOneAndUpdate(
{ customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() },
{ $set: { total_water_added_from_midnight: totalwaterpumped } }
);
await MotorData.updateOne( await MotorData.updateOne(
{ customerId, motor_id: motorId, start_instance_id: start_instance_id }, { customerId, motor_id: motorId, start_instance_id: start_instance_id },
@ -1049,9 +1058,10 @@ exports.motorAction = async (req, reply) => {
} }
); );
} }
})();
// 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