From dea38e44e8b79e1ccf5f3d7359e27906adad93f7 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Fri, 24 Jan 2025 11:41:30 +0530 Subject: [PATCH 1/5] chnages --- src/controllers/tanksController.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index f8b2f609..f3d6102f 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1766,7 +1766,7 @@ const sendNotification = async (fcmIds, title, body) => { notification: { title, body }, token, data: { - exampleKey: 'exampleValue', + 'target': '/tank_levels', }, }); console.log(`Notification sent successfully to token: ${token}`, response); @@ -2150,16 +2150,17 @@ const checkWaterLevelsAndNotify = async (customerId, tankName, tankLocation, fcm } else if (waterLevelPercentage <= thresholds.low) { eventEmitter.emit('sendLowWaterNotification', fcmTokens, tank); await Tank.updateOne({ customerId, tankName: tank.tankName }, { $set: { notificationSentLow: true } }); - } else if (waterLevelPercentage >= thresholds.criticallyHigh) { - eventEmitter.emit('sendCriticalHighWaterNotification', fcmTokens, tank); - await Tank.updateOne({ customerId, tankName: tank.tankName }, { $set: { notificationSentCriticalHigh: true } }); - } else if (waterLevelPercentage >= thresholds.veryHigh) { - eventEmitter.emit('sendVeryHighWaterNotification', fcmTokens, tank); - await Tank.updateOne({ customerId, tankName: tank.tankName }, { $set: { notificationSentVeryHigh: true } }); - } else if (waterLevelPercentage >= thresholds.high) { - eventEmitter.emit('sendHighWaterNotification', fcmTokens, tank); - await Tank.updateOne({ customerId, tankName: tank.tankName }, { $set: { notificationSentHigh: true } }); } + // else if (waterLevelPercentage >= thresholds.criticallyHigh) { + // eventEmitter.emit('sendCriticalHighWaterNotification', fcmTokens, tank); + // await Tank.updateOne({ customerId, tankName: tank.tankName }, { $set: { notificationSentCriticalHigh: true } }); + // } else if (waterLevelPercentage >= thresholds.veryHigh) { + // eventEmitter.emit('sendVeryHighWaterNotification', fcmTokens, tank); + // await Tank.updateOne({ customerId, tankName: tank.tankName }, { $set: { notificationSentVeryHigh: true } }); + // } else if (waterLevelPercentage >= thresholds.high) { + // eventEmitter.emit('sendHighWaterNotification', fcmTokens, tank); + // await Tank.updateOne({ customerId, tankName: tank.tankName }, { $set: { notificationSentHigh: true } }); + // } } catch (error) { console.error(`Error checking water levels for tank ${tankName}:`, error); } From 2bec7f3e0beeaea6d0da9b98e3fd639927af6b65 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Fri, 24 Jan 2025 11:55:55 +0530 Subject: [PATCH 2/5] notification changes fix mulitiple --- src/controllers/tanksController.js | 38 +++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index f3d6102f..fb050736 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -2112,6 +2112,16 @@ const stat_stop_intervals = {}; // } // }; +const notificationSentStatus = { + motorStart: false, + motorStop: false, + lowWater: false, + veryLowWater: false, + criticallyLowWater: false, + highWater: false, + veryHighWater: false, + criticallyHighWater: false, +}; const checkWaterLevelsAndNotify = async (customerId, tankName, tankLocation, fcmTokens) => { try { @@ -2141,16 +2151,29 @@ const checkWaterLevelsAndNotify = async (customerId, tankName, tankLocation, fcm }; // Check water levels and send notifications - if (waterLevelPercentage <= thresholds.criticallyLow) { + if (waterLevelPercentage <= thresholds.criticallyLow && !notificationSentStatus.criticallyLowWater) { eventEmitter.emit('sendCriticalLowWaterNotification', fcmTokens, tank); + notificationSentStatus.criticallyLowWater = true; await Tank.updateOne({ customerId, tankName: tank.tankName }, { $set: { notificationSentCritical: true } }); - } else if (waterLevelPercentage <= thresholds.veryLow) { + } else if (waterLevelPercentage <= thresholds.veryLow && !notificationSentStatus.veryLowWater) { eventEmitter.emit('sendVeryLowWaterNotification', fcmTokens, tank); + notificationSentStatus.veryLowWater = true; await Tank.updateOne({ customerId, tankName: tank.tankName }, { $set: { notificationSentVeryLow: true } }); - } else if (waterLevelPercentage <= thresholds.low) { + } else if (waterLevelPercentage <= thresholds.low && !notificationSentStatus.lowWater) { eventEmitter.emit('sendLowWaterNotification', fcmTokens, tank); + notificationSentStatus.lowWater = true; await Tank.updateOne({ customerId, tankName: tank.tankName }, { $set: { notificationSentLow: true } }); } + // if (waterLevelPercentage <= thresholds.criticallyLow) { + // eventEmitter.emit('sendCriticalLowWaterNotification', fcmTokens, tank); + // await Tank.updateOne({ customerId, tankName: tank.tankName }, { $set: { notificationSentCritical: true } }); + // } else if (waterLevelPercentage <= thresholds.veryLow) { + // eventEmitter.emit('sendVeryLowWaterNotification', fcmTokens, tank); + // await Tank.updateOne({ customerId, tankName: tank.tankName }, { $set: { notificationSentVeryLow: true } }); + // } else if (waterLevelPercentage <= thresholds.low) { + // eventEmitter.emit('sendLowWaterNotification', fcmTokens, tank); + // await Tank.updateOne({ customerId, tankName: tank.tankName }, { $set: { notificationSentLow: true } }); + // } // else if (waterLevelPercentage >= thresholds.criticallyHigh) { // eventEmitter.emit('sendCriticalHighWaterNotification', fcmTokens, tank); // await Tank.updateOne({ customerId, tankName: tank.tankName }, { $set: { notificationSentCriticalHigh: true } }); @@ -2269,7 +2292,7 @@ exports.motorAction = async (req, reply) => { // stopCriteria, // manual_threshold_time // ); - if (!hasNotifiedStart) { + if (!notificationSentStatus.motorStart) { eventEmitter.emit( "motorStart", fcmToken, @@ -2280,10 +2303,9 @@ exports.motorAction = async (req, reply) => { tankName, startTime, motorOnType, - stopCriteria, manual_threshold_time ); - hasNotifiedStart = true; // Set flag to true to prevent duplicate notifications + notificationSentStatus.motorStart = true; // Set flag to true to prevent duplicate notifications } await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId }, @@ -2322,7 +2344,7 @@ exports.motorAction = async (req, reply) => { // stopTime, // motorOnType // ); - if (!hasNotifiedStop) { + if (!notificationSentStatus.motorStop) { eventEmitter.emit( "motorStop", fcmToken, @@ -2330,7 +2352,7 @@ exports.motorAction = async (req, reply) => { stopTime, motorOnType ); - hasNotifiedStop = true; // Set flag to true to prevent duplicate notifications + notificationSentStatus.motorStop = true; // Set flag to true to prevent duplicate notifications } await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId }, From ae412ab65d24c50aed118e2745e6c1b5fc534ece Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Fri, 24 Jan 2025 12:02:07 +0530 Subject: [PATCH 3/5] chnages --- src/controllers/tanksController.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index fb050736..b0ac104c 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1766,7 +1766,7 @@ const sendNotification = async (fcmIds, title, body) => { notification: { title, body }, token, data: { - 'target': '/tank_levels', + 'target': '/route_navigation', }, }); console.log(`Notification sent successfully to token: ${token}`, response); From a831fb1093ede1dccca45e10a73d6b3e30dacd2d Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Fri, 24 Jan 2025 12:15:13 +0530 Subject: [PATCH 4/5] high water level notifications --- src/controllers/tanksController.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index b0ac104c..5a98008a 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -2502,7 +2502,22 @@ exports.motorAction = async (req, reply) => { // console.log(motorIntervals[motorId],"deleted automatically") // Emit low water level notification // clearInterval(motorIntervals[motorId]); // Clear interval // delete motorIntervals[motorId]; + await checkWaterLevelsAndNotify(customerId, tankName, supplierTank.tankLocation, fcmToken); + if (currentWaterPercentage >= highWaterThreshold && !notificationSentStatus.highWater) { + eventEmitter.emit('sendHighWaterNotification', fcmToken, `Water level has reached high levels.`); + notificationSentStatus.highWater = true; // Set flag to true to prevent duplicate notifications + } + + if (currentWaterPercentage >= veryHighWaterThreshold && !notificationSentStatus.veryHighWater) { + eventEmitter.emit('sendVeryHighWaterNotification', fcmToken, `Water level has reached very high levels.`); + notificationSentStatus.veryHighWater = true; // Set flag to true to prevent duplicate notifications + } + + if (currentWaterPercentage >= criticalHighWaterThreshold && !notificationSentStatus.criticallyHighWater) { + eventEmitter.emit('sendCriticalHighWaterNotification', fcmToken, `Water level has reached critically high levels.`); + notificationSentStatus.criticallyHighWater = true; // Set flag to true to prevent duplicate notifications + } clearInterval(motorIntervals[motorId]); // Stop the motor if condition met delete motorIntervals[motorId]; // Remove from interval object @@ -2535,11 +2550,11 @@ exports.motorAction = async (req, reply) => { } // Check for high water level and send notification - if (currentWaterPercentage >= highWaterThreshold) { - // eventEmitter.emit('sendHighWaterNotification', fcmToken, receiverTank); - await checkWaterLevelsAndNotify(customerId, tankName, supplierTank.tankLocation, fcmToken); + // if (currentWaterPercentage >= highWaterThreshold) { + // // eventEmitter.emit('sendHighWaterNotification', fcmToken, receiverTank); + // await checkWaterLevelsAndNotify(customerId, tankName, supplierTank.tankLocation, fcmToken); - } + // } }, 30000); // Check every minute } From 420beaffb619b17c9d84c8fc5e91d60e3a90cb15 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Fri, 24 Jan 2025 12:29:24 +0530 Subject: [PATCH 5/5] changes --- src/controllers/tanksController.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 5a98008a..17c0947e 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -2504,20 +2504,20 @@ exports.motorAction = async (req, reply) => { // delete motorIntervals[motorId]; await checkWaterLevelsAndNotify(customerId, tankName, supplierTank.tankLocation, fcmToken); - if (currentWaterPercentage >= highWaterThreshold && !notificationSentStatus.highWater) { - eventEmitter.emit('sendHighWaterNotification', fcmToken, `Water level has reached high levels.`); - notificationSentStatus.highWater = true; // Set flag to true to prevent duplicate notifications - } + // if (currentWaterPercentage >= highWaterThreshold && !notificationSentStatus.highWater) { + // eventEmitter.emit('sendHighWaterNotification', fcmToken, `Water level has reached high levels.`); + // notificationSentStatus.highWater = true; // Set flag to true to prevent duplicate notifications + // } - if (currentWaterPercentage >= veryHighWaterThreshold && !notificationSentStatus.veryHighWater) { - eventEmitter.emit('sendVeryHighWaterNotification', fcmToken, `Water level has reached very high levels.`); - notificationSentStatus.veryHighWater = true; // Set flag to true to prevent duplicate notifications - } + // if (currentWaterPercentage >= veryHighWaterThreshold && !notificationSentStatus.veryHighWater) { + // eventEmitter.emit('sendVeryHighWaterNotification', fcmToken, `Water level has reached very high levels.`); + // notificationSentStatus.veryHighWater = true; // Set flag to true to prevent duplicate notifications + // } - if (currentWaterPercentage >= criticalHighWaterThreshold && !notificationSentStatus.criticallyHighWater) { - eventEmitter.emit('sendCriticalHighWaterNotification', fcmToken, `Water level has reached critically high levels.`); - notificationSentStatus.criticallyHighWater = true; // Set flag to true to prevent duplicate notifications - } + // if (currentWaterPercentage >= criticalHighWaterThreshold && !notificationSentStatus.criticallyHighWater) { + // eventEmitter.emit('sendCriticalHighWaterNotification', fcmToken, `Water level has reached critically high levels.`); + // notificationSentStatus.criticallyHighWater = true; // Set flag to true to prevent duplicate notifications + // } clearInterval(motorIntervals[motorId]); // Stop the motor if condition met delete motorIntervals[motorId]; // Remove from interval object