From db73fe578661e135c59412a6050ea18d563e362e Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Mon, 20 Jan 2025 11:49:23 +0530 Subject: [PATCH] changes --- src/controllers/tanksController.js | 98 +++++++++++++++++++++++++++++- 1 file changed, 96 insertions(+), 2 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 5b9bdbfb..047a1ed1 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1681,6 +1681,34 @@ eventEmitter.on('sendCriticalHighWaterNotification', async (fcmTokens, tankInfo, // await sendNotification(fcmTokens, 'High Water Level', `Motor ID: ${motorId}, water level reached above 90% at ${timestamp}. Current Water Level: ${waterLevel} Ltrs`); // }); +eventEmitter.on('sendThresholdTimeNotification', async (fcmTokens, message) => { + try { + await sendNotification(fcmTokens, 'Threshold Time Reached', message, 'Motor Alert'); + console.log("Threshold time notification sent successfully."); + } catch (error) { + console.error("Error sending threshold time notification:", error); + } +}); + +eventEmitter.on('sendMotorStartNotification', async (fcmTokens, message) => { + try { + await sendNotification(fcmTokens, "Motor Started", message); + console.log("Manual method time notification sent successfully."); + } catch (error) { + console.error("Error sending thresmanual method time notification:", error); + } +}); + +eventEmitter.on('sendMotorStopNotification', async (fcmTokens, message) => { + try { + await sendNotification(fcmTokens, "Motor Stopped", message); + console.log("Manual method time notification sent successfully."); + } catch (error) { + console.error("Error sending thresmanual method time notification:", error); + } +}); + + // Function to emit events with timestamps const emitWithTimestamp = (eventName, fcmTokens, motorId, waterLevel) => { @@ -2150,11 +2178,57 @@ console.log(fcmToken) const blockName = req.body.from || "Unknown Block"; // Provide a fallback if `from` is missing const tankName = req.body.to || "Unknown Tank"; // Provide a fallback if `to` is missing const stopTime = req.body.stopTime - const motorOnType = req.body.motor_on_type || "app"; + const motorOnType = req.body.motor_on_type || "App"; if (action === "start") { 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); + + await Tank.updateOne( + { customerId, "connections.inputConnections.motor_id": motorId }, + { $set: { "connections.inputConnections.$.motor_stop_status": motorStopStatus } } + ); + + const thresholdTimeMs = req.body.manual_threshold_time * 60 * 1000; // Convert minutes to milliseconds + //const startTime = new Date(); // Record the start time + //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 + await Tank.updateOne( + { customerId, "connections.inputConnections.motor_id": motorId }, + { $set: { "connections.inputConnections.$.motor_stop_status": "1" } } + ); + + clearTimeout(motorIntervals[motorId]); + delete motorIntervals[motorId]; + } catch (error) { + console.error("Error handling threshold time notification:", error); + } + }, thresholdTimeMs) + const stopCriteria = motorOnType === "time" ? `${req.body.manual_threshold_time} minutes` @@ -2171,7 +2245,7 @@ console.log(fcmToken) startTime, motorOnType, stopCriteria, - stopTime + stopTime ,manual_threshold_time ); await Tank.updateOne( @@ -2199,6 +2273,26 @@ console.log(fcmToken) }, 30000); // Check every 30 seconds } 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); + + await Tank.updateOne( + { customerId, "connections.inputConnections.motor_id": motorId }, + { + $set: { + "connections.inputConnections.$.motor_stop_status": motorStopStatus, + "connections.inputConnections.$.motor_on_type": "manual", + "connections.inputConnections.$.stopTime": stopTime + } + } + ); + + // Clear intervals if any + if (motorIntervals[motorId]) { + clearInterval(motorIntervals[motorId]); + delete motorIntervals[motorId]; + } eventEmitter.emit( "motorStop", fcmToken,