|
|
|
@ -1908,7 +1908,7 @@ const stat_stop_intervals = {};
|
|
|
|
|
// throw boom.boomify(err);
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
const motorIntervals = {};
|
|
|
|
|
exports.motorAction = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const customerId = req.params.customerId;
|
|
|
|
@ -2040,7 +2040,12 @@ else if (currentWaterLevel >= highWaterThreshold) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
if (motorIntervals[motorId]) {
|
|
|
|
|
clearInterval(motorIntervals[motorId]); // Clear the interval
|
|
|
|
|
delete motorIntervals[motorId]; // Remove the interval from the object
|
|
|
|
|
}
|
|
|
|
|
this.publishMotorStopStatus(motorId, motorStopStatus);
|
|
|
|
|
|
|
|
|
|
// Send immediate response to the client
|
|
|
|
|
reply.code(200).send({ message: "Motor stopped successfully." });
|
|
|
|
|
|
|
|
|
@ -2105,7 +2110,6 @@ else if (currentWaterLevel >= highWaterThreshold) {
|
|
|
|
|
this.publishMotorStopStatus(motorId, motorStopStatus);
|
|
|
|
|
for await (const tank of Tank.find({ "connections.inputConnections.motor_id": motorId })) {
|
|
|
|
|
const index = tank.connections.inputConnections.findIndex(connection => connection.motor_id === motorId);
|
|
|
|
|
const thresholdTime = moment().add(req.body.manual_threshold_time, 'minutes').toDate();
|
|
|
|
|
if (index !== -1) {
|
|
|
|
|
await Tank.updateOne(
|
|
|
|
|
{ customerId, "connections.inputConnections.motor_id": motorId },
|
|
|
|
@ -2114,43 +2118,20 @@ else if (currentWaterLevel >= highWaterThreshold) {
|
|
|
|
|
[`connections.inputConnections.${index}.manual_threshold_time`]: req.body.manual_threshold_time,
|
|
|
|
|
[`connections.inputConnections.${index}.threshold_type`]: "time",
|
|
|
|
|
[`connections.inputConnections.${index}.startTime`]: req.body.startTime,
|
|
|
|
|
[`connections.inputConnections.${index}.start_instance_id`]: start_instance_id,
|
|
|
|
|
[`connections.inputConnections.${index}.manual_stop_threshold_time`]: req.body.manual_stop_threshold_time,
|
|
|
|
|
[`connections.inputConnections.${index}.start_instance_id`]: start_instance_id
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const intervalId = setInterval(async () => {
|
|
|
|
|
const thresholdTime = new Date(new Date().getTime() + req.body.manual_threshold_time * 60000);
|
|
|
|
|
motorIntervals[motorId] = setInterval(async () => {
|
|
|
|
|
const supplierTank = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() });
|
|
|
|
|
const currentWaterLevel = parseInt(supplierTank.waterlevel, 10);
|
|
|
|
|
const currentWaterPercentage = (currentWaterLevel / parseInt(supplierTank.capacity.replace(/,/g, ''), 10)) * 100;
|
|
|
|
|
const receiverTank1 = await Tank.findOne({
|
|
|
|
|
customerId,
|
|
|
|
|
tankName: req.body.to,
|
|
|
|
|
tankLocation: req.body.to_type.toLowerCase()
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Ensure the tank exists
|
|
|
|
|
if (!receiverTank1) {
|
|
|
|
|
throw new Error('Receiver tank not found');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Extract the manual_stop_threshold_time from the appropriate input connection
|
|
|
|
|
let stop_time = null;
|
|
|
|
|
if (receiverTank1.connections && receiverTank1.connections.inputConnections) {
|
|
|
|
|
const inputConnection = receiverTank1.connections.inputConnections.find(connection => connection.motor_id === req.body.motorId);
|
|
|
|
|
|
|
|
|
|
if (inputConnection && inputConnection.manual_stop_threshold_time) {
|
|
|
|
|
stop_time = inputConnection.manual_stop_threshold_time;
|
|
|
|
|
} else {
|
|
|
|
|
console.log('No matching input connection or manual_stop_threshold_time found');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (new Date() >= stop_time || currentWaterPercentage <= lowWaterThreshold) {
|
|
|
|
|
|
|
|
|
|
if (new Date() >= thresholdTime || currentWaterPercentage <= lowWaterThreshold) {
|
|
|
|
|
console.log("motor stopping because it entered this condition")
|
|
|
|
|
await Tank.updateOne(
|
|
|
|
|
{ customerId, "connections.inputConnections.motor_id": motorId },
|
|
|
|
@ -2165,8 +2146,9 @@ else if (currentWaterLevel >= highWaterThreshold) {
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
emitWithTimestamp('lowWaterLevel', fcmToken); // Emit low water level notification
|
|
|
|
|
|
|
|
|
|
clearInterval(intervalId);
|
|
|
|
|
clearInterval(motorIntervals[motorId]); // Clear the interval when stopping
|
|
|
|
|
delete motorIntervals[motorId];
|
|
|
|
|
|
|
|
|
|
this.publishMotorStopStatus(motorId, "1");
|
|
|
|
|
await delay(300000);
|
|
|
|
|
const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id: start_instance_id });
|
|
|
|
@ -2256,7 +2238,7 @@ else if (currentWaterLevel >= highWaterThreshold) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Start monitoring water level based on threshold percentage
|
|
|
|
|
const intervalId = setInterval(async () => {
|
|
|
|
|
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);
|
|
|
|
@ -2274,7 +2256,8 @@ else if (currentWaterLevel >= highWaterThreshold) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
clearInterval(intervalId); // Stop monitoring water level
|
|
|
|
|
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 });
|
|
|
|
|