From d8f8f0ed2f6f60e9bde8081fa44c9c3cb94b8cad Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Mon, 20 Jan 2025 15:59:23 +0530 Subject: [PATCH] notification manualmethods --- src/controllers/tanksController.js | 56 ++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 8bf74b93..7da84e2c 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -2238,6 +2238,43 @@ console.log(fcmToken) "connections.inputConnections.$.threshold_type": "time" } } ); + await Tank.updateOne( + { customerId, "connections.inputConnections.motor_id": motorId }, + { + $set: { + "connections.inputConnections.$.motor_stop_status": "2", + "connections.inputConnections.$.motor_on_type": "manual", + } + } + ); + + const startMessage = `Motor supplying water to '${tankName}' in block '${blockName}' started manually at ${startTime}.`; + eventEmitter.emit('sendMotorStartNotification', fcmToken, startMessage); + + // Schedule threshold check + // const thresholdTimeMs = manual_threshold_time * 60 * 1000; + motorIntervals[motorId] = setTimeout(async () => { + try { + const stopMessage = `Threshold time of ${manual_threshold_time} minutes reached for motor supplying water to '${tankName}' in block '${blockName}'.`; + eventEmitter.emit('sendThresholdTimeNotification', fcmToken, stopMessage); + + await Tank.updateOne( + { customerId, "connections.inputConnections.motor_id": motorId }, + { + $set: { + "connections.inputConnections.$.motor_stop_status": "1", + "connections.inputConnections.$.manual_threshold_time": null, + "connections.inputConnections.$.threshold_type": null, + } + } + ); + + delete motorIntervals[motorId]; + } catch (error) { + console.error("Error during threshold handling:", error); + } + }, thresholdTimeMs); + reply.code(200).send({ message: "Motor started successfully." }); // Schedule water level checks after motor start motorIntervals[motorId] = setInterval(async () => { @@ -2288,6 +2325,25 @@ console.log(fcmToken) stopTime, motorOnType ); + + await Tank.updateOne( + { customerId, "connections.inputConnections.motor_id": motorId }, + { + $set: { + "connections.inputConnections.$.motor_stop_status": "1", + "connections.inputConnections.$.motor_on_type": "manual", + } + } + ); + + const stopMessage = `Motor supplying water to '${tankName}' in block '${blockName}' stopped manually at ${stopTime}.`; + eventEmitter.emit('sendMotorStopNotification', fcmToken, stopMessage); + + if (motorIntervals[motorId]) { + clearTimeout(motorIntervals[motorId]); + delete motorIntervals[motorId]; + } + } else { throw new Error("Invalid action provided."); }