diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 49df2574..8bf74b93 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1580,7 +1580,7 @@ eventEmitter.on( `Pump started at: '${startTime}'\n` + `Initiated by user(s): ${userNames}\n` + `Pump started by: '${motorOnType.toUpperCase()}'\n` + - `Will stop at: '${manual_threshold_time}'`; + `Will stop at after: '${manual_threshold_time}' mins`; // Send the notification await sendNotification(fcmTokens, 'Arminta Water Management', message); @@ -2184,8 +2184,8 @@ console.log(fcmToken) motorStopStatus = "2"; const startTime = req.body.startTime; - const startMessage = `The motor supplying water to '${tankName}' in block '${blockName}' has started manually at ${new Date().toISOString()}.`; - eventEmitter.emit("sendMotorStartNotification", fcmToken, startMessage); + // const startMessage = `The motor supplying water to '${tankName}' in block '${blockName}' has started manually at ${new Date().toISOString()}.`; + // eventEmitter.emit("sendMotorStartNotification", fcmToken, startMessage); await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId }, @@ -2197,39 +2197,21 @@ console.log(fcmToken) //const startTime = req.body.startTime; // Schedule a task to send a notification when the threshold time is reached + motorIntervals[motorId] = setTimeout(async () => { try { - // Fetch the latest tank and motor data - const receiverTank = await Tank.findOne({ - customerId, - tankName: req.body.to, - tankLocation: req.body.to_type.toLowerCase(), - }); - - // Send a notification for threshold time reached - const message = `Threshold time of ${req.body.manual_threshold_time} minutes has been reached for the motor supplying '${receiverTank.tankName}' located at '${receiverTank.tankLocation}'. Please review the motor operation.`; - - eventEmitter.emit('sendThresholdTimeNotification', fcmToken, message); - - // Optionally update the tank or motor state in the database - await Tank.updateOne( - { customerId, tankName: receiverTank.tankName }, - { $set: { notificationSentThresholdTime: true } } - ); - - // Optionally stop the motor if needed + // Perform threshold time logic (e.g., notification, motor stop) + console.log(`Threshold time of ${manualThresholdTime} minutes reached for motor ${motorId}`); await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId }, { $set: { "connections.inputConnections.$.motor_stop_status": "1" } } ); - - clearTimeout(motorIntervals[motorId]); + clearTimeout(motorIntervals[motorId]); // Clear timeout delete motorIntervals[motorId]; } catch (error) { - console.error("Error handling threshold time notification:", error); + console.error("Error in threshold time handling:", error); } - }, thresholdTimeMs) - + }, thresholdTimeMs); const stopCriteria = motorOnType === "time" ? `${req.body.manual_threshold_time} minutes` @@ -2246,12 +2228,14 @@ console.log(fcmToken) startTime, motorOnType, stopCriteria, - stopTime ,manual_threshold_time + manual_threshold_time ); await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId }, - { $set: { "connections.inputConnections.$.motor_stop_status": "2" } } + { $set: { "connections.inputConnections.$.motor_stop_status": "2", + "connections.inputConnections.$.manual_threshold_time": manual_threshold_time, + "connections.inputConnections.$.threshold_type": "time" } } ); reply.code(200).send({ message: "Motor started successfully." }); @@ -2275,8 +2259,8 @@ console.log(fcmToken) } else if (action === "stop") { motorStopStatus = "1"; // If action is stop, set stop status to "1" // Emit stop notification - const stopMessage = `The motor supplying water to '${tankName}' in block '${blockName}' was stopped manually at ${stopTime}.`; - eventEmitter.emit("sendMotorStopNotification", fcmToken, stopMessage); + // const stopMessage = `The motor supplying water to '${tankName}' in block '${blockName}' was stopped manually at ${stopTime}.`; + // eventEmitter.emit("sendMotorStopNotification", fcmToken, stopMessage); await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId },