diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index f3d6102f..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); @@ -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 },