From aa7e8cf8d456c6c16b84db2f0996cd82bd1ad1bf Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Thu, 5 Sep 2024 11:26:20 +0530 Subject: [PATCH] chnages on notification --- src/controllers/tanksController.js | 33 ++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 5cc3d2ce..01ba1270 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1373,6 +1373,10 @@ exports.motorAction = async (req, reply) => { throw new Error("Motor ID is required."); } + const users = await User.find({ customerId: customerId }); + const fcmToken = users.map(user => user.fcmId).filter(fcmId => fcmId); + console.log(fcmToken) + // Determine the motor stop status based on the action let motorStopStatus; if (action === "start") { @@ -1402,6 +1406,10 @@ exports.motorAction = async (req, reply) => { // Send immediate response to the client reply.code(200).send({ message: "Motor stopped successfully." }); + if (fcmToken) { + await sendNotification(fcmToken, 'Motor Stopped', `Motor ${motorId} has been stopped.`); + } + // Perform stop operations in the background (async () => { await delay(300000); @@ -1429,6 +1437,12 @@ exports.motorAction = async (req, reply) => { } } ); + + if (receiverFinalWaterLevel / parseInt(receiverTank.capacity, 10) * 100 >= 90) { + if (fcmToken) { + await sendNotification(fcmToken, 'High Water Level Alert', `Water level in tank ${motorData.receiverTank} has reached 90%.`); + } + } } })(); @@ -1440,6 +1454,10 @@ exports.motorAction = async (req, reply) => { { customerId, "connections.inputConnections.motor_id": motorId }, { $set: { "connections.inputConnections.$.motor_stop_status": "2" } } ); + // Send start notification + if (fcmToken) { + await sendNotification(fcmToken, 'Motor Started', `Motor ${motorId} has been started.`); + } } // Check threshold settings if action is start @@ -1501,6 +1519,10 @@ exports.motorAction = async (req, reply) => { } } ); + // Send notification after motor stops due to time threshold + if (fcmToken) { + await sendNotification(fcmToken, 'Motor Stopped After Time Threshold', `Motor ${motorId} has been stopped after reaching the set time threshold.`); + } clearInterval(intervalId); await delay(300000); @@ -1526,6 +1548,11 @@ exports.motorAction = async (req, reply) => { } } ); + if (receiverFinalWaterLevel / parseInt(receiverTank.capacity, 10) * 100 <= 20) { + if (fcmToken) { + await sendNotification(fcmToken, 'Low Water Level Alert', `Water level in tank ${motorData.receiverTank} has dropped below 20%.`); + } + } } } }, 60000); @@ -1623,6 +1650,12 @@ exports.motorAction = async (req, reply) => { } } ); + // Send low water level notification + if (receiverFinalWaterLevel / parseInt(receiverTank.capacity, 10) * 100 <= 20) { + if (fcmToken) { + await sendNotification(fcmToken, 'Low Water Level Alert', `Water level in tank ${motorData.receiverTank} has dropped below 20%.`); + } + } } } }, 20000); // Check water level every minute