|
|
|
@ -2041,6 +2041,7 @@ else if (currentWaterLevel >= highWaterThreshold) {
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
if (motorIntervals[motorId]) {
|
|
|
|
|
console.log(motorIntervals[motorId],"deleted")
|
|
|
|
|
clearInterval(motorIntervals[motorId]); // Clear the interval
|
|
|
|
|
delete motorIntervals[motorId]; // Remove the interval from the object
|
|
|
|
|
}
|
|
|
|
@ -2147,9 +2148,10 @@ else if (currentWaterLevel >= highWaterThreshold) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
emitWithTimestamp('lowWaterLevel', fcmToken); // Emit low water level notification
|
|
|
|
|
clearInterval(motorIntervals[motorId]); // Clear the interval when stopping
|
|
|
|
|
delete motorIntervals[motorId];
|
|
|
|
|
emitWithTimestamp('lowWaterLevel', fcmToken);
|
|
|
|
|
console.log(motorIntervals[motorId],"deleted automatically") // Emit low water level notification
|
|
|
|
|
clearInterval(motorIntervals[motorId]); // Clear interval
|
|
|
|
|
delete motorIntervals[motorId];
|
|
|
|
|
|
|
|
|
|
this.publishMotorStopStatus(motorId, "1");
|
|
|
|
|
await delay(300000);
|
|
|
|
@ -2184,106 +2186,8 @@ else if (currentWaterLevel >= highWaterThreshold) {
|
|
|
|
|
emitWithTimestamp('highWaterLevel', fcmToken); // Emit high water level notification
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}, 60000); // Check every minute
|
|
|
|
|
}, 30000); // Check every minute
|
|
|
|
|
}
|
|
|
|
|
} 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 newMotorData = new MotorData({
|
|
|
|
|
customerId: customerId,
|
|
|
|
|
motor_id: motorId,
|
|
|
|
|
start_instance_id: start_instance_id,
|
|
|
|
|
supplierTank: req.body.from,
|
|
|
|
|
receiverTank: req.body.to,
|
|
|
|
|
supplier_type: req.body.from_type,
|
|
|
|
|
receiver_type: req.body.to_type,
|
|
|
|
|
startTime: req.body.startTime,
|
|
|
|
|
receiverInitialwaterlevel:parseInt(receiver_tank_info7.waterlevel, 10)
|
|
|
|
|
});
|
|
|
|
|
await newMotorData.save();
|
|
|
|
|
// If threshold type is percentage, calculate percentage threshold
|
|
|
|
|
const receiver_tank_info = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() });
|
|
|
|
|
const supplier_tank_info = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() });
|
|
|
|
|
if (!receiver_tank_info) {
|
|
|
|
|
throw new Error("Receiver tank not found.");
|
|
|
|
|
}
|
|
|
|
|
if (!supplier_tank_info) {
|
|
|
|
|
throw new Error("Supplierr tank not found.");
|
|
|
|
|
}
|
|
|
|
|
const supplier_capacity = parseInt(supplier_tank_info.capacity, 10);
|
|
|
|
|
const supplier_waterLevel = parseInt(supplier_tank_info.waterlevel, 10);
|
|
|
|
|
|
|
|
|
|
const capacity = parseInt(receiver_tank_info.capacity, 10);
|
|
|
|
|
const waterLevel = parseInt(receiver_tank_info.waterlevel, 10);
|
|
|
|
|
const desired_percentage = parseInt(req.body.manual_threshold_litres.replace(/,/g, ''), 10);
|
|
|
|
|
|
|
|
|
|
console.log(desired_percentage)
|
|
|
|
|
const threshold_water_level = waterLevel+desired_percentage;
|
|
|
|
|
|
|
|
|
|
const supplier_threshold = supplier_waterLevel-desired_percentage
|
|
|
|
|
console.log(supplier_threshold,"supplier_threshold")
|
|
|
|
|
|
|
|
|
|
for await (const tank of Tank.find({ "connections.inputConnections.motor_id": motorId })) {
|
|
|
|
|
const index = tank.connections.inputConnections.findIndex(connection => connection.motor_id === motorId);
|
|
|
|
|
if (index !== -1) {
|
|
|
|
|
await Tank.updateOne(
|
|
|
|
|
{ customerId, "connections.inputConnections.motor_id": motorId },
|
|
|
|
|
{ $set: { [`connections.inputConnections.${index}.manual_threshold_percentage`]: supplier_threshold.toString(), [`connections.inputConnections.${index}.startTime`]: req.body.startTime } }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Update water level threshold
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Start monitoring water level based on threshold percentage
|
|
|
|
|
motorIntervals[motorId] = setInterval(async () => {
|
|
|
|
|
// Check if water level has reached the threshold percentage
|
|
|
|
|
const supplier_tank_info1 = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() });
|
|
|
|
|
const current_water_level = parseInt(supplier_tank_info1.waterlevel, 10);
|
|
|
|
|
if (current_water_level <= supplier_threshold) {
|
|
|
|
|
// Stop the motor pump
|
|
|
|
|
await Tank.updateOne(
|
|
|
|
|
{ customerId, "connections.inputConnections.motor_id": motorId },
|
|
|
|
|
{
|
|
|
|
|
$set: {
|
|
|
|
|
"connections.inputConnections.$.motor_stop_status": "1",
|
|
|
|
|
|
|
|
|
|
"connections.inputConnections.$.threshold_type": null,
|
|
|
|
|
"connections.inputConnections.$.manual_threshold_time": null,
|
|
|
|
|
"connections.inputConnections.$.manual_threshold_percentage": null
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
clearInterval(motorIntervals[motorId]); // Clear the interval when stopping
|
|
|
|
|
delete motorIntervals[motorId];// Stop monitoring water level
|
|
|
|
|
await delay(300000);
|
|
|
|
|
|
|
|
|
|
const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id: start_instance_id });
|
|
|
|
|
if (motorData) {
|
|
|
|
|
const receiverTank = await Tank.findOne({ customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() });
|
|
|
|
|
const receiverFinalWaterLevel = parseInt(receiverTank.waterlevel, 10);
|
|
|
|
|
const quantityDelivered = receiverFinalWaterLevel - parseInt(motorData.receiverInitialwaterlevel, 10);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const stopTime = formatDate(new Date());
|
|
|
|
|
|
|
|
|
|
await MotorData.updateOne(
|
|
|
|
|
{ customerId, motor_id: motorId, start_instance_id: start_instance_id },
|
|
|
|
|
{
|
|
|
|
|
$set: {
|
|
|
|
|
stopTime:stopTime,
|
|
|
|
|
receiverfinalwaterlevel: receiverFinalWaterLevel.toString(),
|
|
|
|
|
quantity_delivered: quantityDelivered.toString()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, 20000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Respond with success message
|
|
|
|
|