diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index f5f201b9..aec3e273 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -2070,23 +2070,23 @@ exports.motorAction = async (req, reply) => { const waterLevelThresholds = { low: 30, veryLow: 20, criticallyLow: 10 }; // Check if the water level is below any of the thresholds -// if (currentWaterLevel < waterLevelThresholds.criticallyLow) { -// if (!receiverTank.notificationSentCritical) { -// eventEmitter.emit('sendCriticalLowWaterNotification', fcmToken, receiverTank); -// await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentCritical: true } }); -// } -// } else if (currentWaterLevel < waterLevelThresholds.veryLow) { -// if (!receiverTank.notificationSentVeryLow) { -// eventEmitter.emit('sendVeryLowWaterNotification', fcmToken, receiverTank); -// await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentVeryLow: true } }); -// } -// } else if (currentWaterLevel < waterLevelThresholds.low) { -// if (!receiverTank.notificationSentLow) { -// eventEmitter.emit('sendLowWaterNotification', fcmToken, receiverTank); -// await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentLow: true } }); -// } -// } -// // Check for critical high water level + if (currentWaterLevel < waterLevelThresholds.criticallyLow) { + if (!receiverTank.notificationSentCritical) { + eventEmitter.emit('sendCriticalLowWaterNotification', fcmToken, receiverTank); + await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentCritical: true } }); + } + } else if (currentWaterLevel < waterLevelThresholds.veryLow) { + if (!receiverTank.notificationSentVeryLow) { + eventEmitter.emit('sendVeryLowWaterNotification', fcmToken, receiverTank); + await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentVeryLow: true } }); + } + } else if (currentWaterLevel < waterLevelThresholds.low) { + if (!receiverTank.notificationSentLow) { + eventEmitter.emit('sendLowWaterNotification', fcmToken, receiverTank); + await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentLow: true } }); + } + } +// Check for critical high water level // if (currentWaterLevel >= criticalHighWaterThreshold) { // if (!receiverTank.notificationSentCriticalHigh) { // eventEmitter.emit('sendCriticalHighWaterNotification', fcmToken, receiverTank); @@ -2142,6 +2142,23 @@ exports.motorAction = async (req, reply) => { ); reply.code(200).send({ message: "Motor started successfully." }); + // Schedule water level checks after motor start + motorIntervals[motorId] = setInterval(async () => { + const receiverTank = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() }); + const currentWaterLevel = parseInt(receiverTank.waterlevel, 10); + + // Check water levels and send notifications + if (currentWaterLevel >= criticalHighWaterThreshold && !receiverTank.notificationSentCriticalHigh) { + eventEmitter.emit('sendCriticalHighWaterNotification', fcmToken, receiverTank); + await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentCriticalHigh: true } }); + } else if (currentWaterLevel >= veryHighWaterThreshold && !receiverTank.notificationSentVeryHigh) { + eventEmitter.emit('sendVeryHighWaterNotification', fcmToken, receiverTank); + await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentVeryHigh: true } }); + } else if (currentWaterLevel >= highWaterThreshold && !receiverTank.notificationSentHigh) { + eventEmitter.emit('sendHighWaterNotification', fcmToken, receiverTank); + await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentHigh: true } }); + } + }, 30000); // Check every 30 seconds } else if (action === "stop") { motorStopStatus = "1"; // If action is stop, set stop status to "1" eventEmitter.emit( @@ -2282,7 +2299,7 @@ exports.motorAction = async (req, reply) => { } } ); - emitWithTimestamp('lowWaterLevel', fcmToken); + eventEmitter.emit('sendLowWaterNotification', fcmToken, receiverTank); console.log(motorIntervals[motorId],"deleted automatically") // Emit low water level notification clearInterval(motorIntervals[motorId]); // Clear interval delete motorIntervals[motorId]; @@ -2317,7 +2334,7 @@ exports.motorAction = async (req, reply) => { // Check for high water level and send notification if (currentWaterPercentage >= highWaterThreshold) { - emitWithTimestamp('highWaterLevel', fcmToken); // Emit high water level notification + eventEmitter.emit('sendHighWaterNotification', fcmToken, receiverTank); } }, 30000); // Check every minute diff --git a/src/models/tanks.js b/src/models/tanks.js index 04d4813e..3e535a42 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -55,6 +55,13 @@ const tanksSchema = new mongoose.Schema({ auto_min_percentage: { type: String, default: "20" }, reserved_percentage: { type: String, default: "20" }, auto_max_percentage: { type: String, default: "80" }, + notificationSentCritical: { type: Boolean }, + notificationSentVeryLow: { type: Boolean }, + notificationSentLow: { type: Boolean }, + notificationSentCriticalHigh: { type: Boolean }, + notificationSentVeryHigh: { type: Boolean }, + notificationSentHigh: { type: Boolean }, + connections: { source: { type: String }, inputConnections: [