diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index d202b6c2..8533bd0a 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -3080,22 +3080,50 @@ exports.motorAction = async (req, reply) => { const supplierTank = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() }); const currentWaterLevel = parseInt(supplierTank.waterlevel, 10); const currentWaterPercentage = (currentWaterLevel / parseInt(supplierTank.capacity.replace(/,/g, ''), 10)) * 100; - + const notificationTracker = new Map(); if (new Date() >= thresholdTime || currentWaterPercentage <= lowWaterThreshold) { console.log(new Date(),"new date") console.log(thresholdTime,"thresholdTime") console.log("motor stopping because it entered this condition") // Emit the threshold time notification - eventEmitter.emit( + // eventEmitter.emit( + // "sendThresholdTimeNotification", + // customerId, + // fcmToken, + // manual_threshold_time, + // motorId, + // tankName, + // blockName + // ); + + const notificationKey = `${customerId}_${motorId}_threshold`; + + // Check if the notification has already been sent + if (!notificationTracker.get(notificationKey)) { + console.log("Sending threshold time notification..."); + + eventEmitter.emit( "sendThresholdTimeNotification", - customerId, - fcmToken, - manual_threshold_time, + customerId, + fcmToken, + manual_threshold_time, motorId, tankName, - blockName - ); + blockName + ); + + // Mark notification as sent + notificationTracker.set(notificationKey, true); + + // Optionally, reset the flag after some time (e.g., 24 hours) + setTimeout(() => { + notificationTracker.delete(notificationKey); + }, 24 * 60 * 60 * 1000); // Reset after 24 hours + } else { + console.log("Notification already sent, skipping..."); + } + const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm'); await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId }, @@ -6004,7 +6032,8 @@ const sendMotorNotifications = async () => { (conn) => conn.motor_id ); const status = inputConnection.motor_status - console.log("motorTank",inputConnection) + //console.log("motorTank",inputConnection) +// console.log("inputConnection.motor_on_type ",inputConnection.motor_on_type ) if (!inputConnection) continue; const { customerId, blockName, tankName } = motorTank; @@ -6013,7 +6042,7 @@ const sendMotorNotifications = async () => { // 🔹 Motor Start Condition if ( - inputConnection.motor_stop_status === "2" && status ===1 && + inputConnection.motor_stop_status === "2" && status ===1 && inputConnection.motor_on_type === "forced_manual" && !motorTank.motor_start_notified ) { console.log("✅ Sending Motor Start Notification..."); @@ -6038,7 +6067,7 @@ const sendMotorNotifications = async () => { // 🔹 Motor Stop Condition if ( - inputConnection.motor_stop_status === "1" && + inputConnection.motor_stop_status === "1" && inputConnection.motor_on_type === "forced_manual" && status ===2 && !motorTank.motor_stop_notified ) { console.log("✅ Sending Motor Stop Notification..."); @@ -6067,10 +6096,10 @@ const sendMotorNotifications = async () => { return user?.fcmIds?.filter((token) => token) || []; }; - // Run the notification check every second -// cron.schedule("* * * * * *", async () => { -// // await sendMotorNotifications(); -// }); + //Run the notification check every second + cron.schedule("* * * * * *", async () => { + await sendMotorNotifications(); + });