diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index b7aaf48d..ffc8c515 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -2105,6 +2105,7 @@ 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 }, @@ -2113,20 +2114,43 @@ 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}.start_instance_id`]: start_instance_id, + [`connections.inputConnections.${index}.manual_stop_threshold_time`]: req.body.manual_stop_threshold_time, } } ); } } - const thresholdTime = moment().add(req.body.manual_threshold_time, 'minutes').toDate(); + const intervalId = 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; - - if (new Date() >= thresholdTime || currentWaterPercentage <= lowWaterThreshold) { + 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) { console.log("motor stopping because it entered this condition") await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId }, diff --git a/src/models/tanks.js b/src/models/tanks.js index b2da1e2d..fd6232f7 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -70,6 +70,7 @@ const tanksSchema = new mongoose.Schema({ water_level: { type: String, default: null }, manual_threshold_percentage: { type: String, default: "90" }, manual_threshold_time: { type: String, default: null }, + manual_stop_threshold_time: { type: String, default: null }, stop_threshold_time: { type: String, default: null }, threshold_type: { type: String, default: "percentage" }, startTime: { type: String, default: null },