diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 88292bf0..f40ef152 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -2861,7 +2861,7 @@ exports.motorAction = async (req, reply) => { let motorStopStatus = action === "start" ? "2" : "1"; const blockName = req.body.from || "Unknown Block"; const tankName = req.body.to || "Unknown Tank"; - + const stopTime = req.body.stopTime if (action === "start") { motorStopStatus = "2"; const startTime = req.body.startTime; @@ -2870,10 +2870,10 @@ exports.motorAction = async (req, reply) => { { $set: { "connections.inputConnections.$.motor_stop_status": motorStopStatus } } ); const thresholdTimeMs = req.body.manual_threshold_time * 60 * 1000; // Convert minutes to milliseconds - const stopCriteria = - motorOnType === "time" - ? `${req.body.manual_threshold_time} minutes` - : `${req.body.manual_threshold_litres} litres`; + // const stopCriteria = + // motorOnType === "time" + // ? `${req.body.manual_threshold_time} minutes` + // : `${req.body.manual_threshold_litres} litres`; try { console.log("enter the start") eventEmitter.emit( @@ -3254,6 +3254,33 @@ exports.motorAction = async (req, reply) => { } else if (action === "stop") { await stopMotor(motorId, customerId, start_instance_id); this.publishMotorStopStatus(motorId, motorStopStatus); + // Fetch motor data to check its threshold time + const motorData = await Tank.findOne( + { customerId, "connections.inputConnections.motor_id": motorId }, + { "connections.inputConnections.$": 1 } + ); + + if (motorData) { + const motor = motorData.connections.inputConnections[0]; + const thresholdTime = motor.manual_threshold_time; // Get threshold time + const hw_Id = motor.hw_Id; // Get hardware ID + + // Check if motor was running for at least the threshold time + if (motor.runTime >= thresholdTime) { + console.log(`Sending threshold time notification for motor ${motorId}`); + + eventEmitter.emit( + "sendThresholdTimeNotification", + customerId, + fcmToken, // Use correct FCM token list + thresholdTime, + hw_Id, + tankName, + blockName + ); + } + } + reply.code(200).send({ message: "Motor stopped successfully." }); } } catch (err) {