ashok 9 months ago
commit fc7183c13f

@ -1766,7 +1766,7 @@ const sendNotification = async (fcmIds, title, body) => {
notification: { title, body }, notification: { title, body },
token, token,
data: { data: {
'target': '/tank_levels', 'target': '/route_navigation',
}, },
}); });
console.log(`Notification sent successfully to token: ${token}`, response); 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) => { const checkWaterLevelsAndNotify = async (customerId, tankName, tankLocation, fcmTokens) => {
try { try {
@ -2141,16 +2151,29 @@ const checkWaterLevelsAndNotify = async (customerId, tankName, tankLocation, fcm
}; };
// Check water levels and send notifications // Check water levels and send notifications
if (waterLevelPercentage <= thresholds.criticallyLow) { if (waterLevelPercentage <= thresholds.criticallyLow && !notificationSentStatus.criticallyLowWater) {
eventEmitter.emit('sendCriticalLowWaterNotification', fcmTokens, tank); eventEmitter.emit('sendCriticalLowWaterNotification', fcmTokens, tank);
notificationSentStatus.criticallyLowWater = true;
await Tank.updateOne({ customerId, tankName: tank.tankName }, { $set: { notificationSentCritical: 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); eventEmitter.emit('sendVeryLowWaterNotification', fcmTokens, tank);
notificationSentStatus.veryLowWater = true;
await Tank.updateOne({ customerId, tankName: tank.tankName }, { $set: { notificationSentVeryLow: 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); eventEmitter.emit('sendLowWaterNotification', fcmTokens, tank);
notificationSentStatus.lowWater = true;
await Tank.updateOne({ customerId, tankName: tank.tankName }, { $set: { notificationSentLow: 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) { // else if (waterLevelPercentage >= thresholds.criticallyHigh) {
// eventEmitter.emit('sendCriticalHighWaterNotification', fcmTokens, tank); // eventEmitter.emit('sendCriticalHighWaterNotification', fcmTokens, tank);
// await Tank.updateOne({ customerId, tankName: tank.tankName }, { $set: { notificationSentCriticalHigh: true } }); // await Tank.updateOne({ customerId, tankName: tank.tankName }, { $set: { notificationSentCriticalHigh: true } });
@ -2269,7 +2292,7 @@ exports.motorAction = async (req, reply) => {
// stopCriteria, // stopCriteria,
// manual_threshold_time // manual_threshold_time
// ); // );
if (!hasNotifiedStart) { if (!notificationSentStatus.motorStart) {
eventEmitter.emit( eventEmitter.emit(
"motorStart", "motorStart",
fcmToken, fcmToken,
@ -2280,10 +2303,9 @@ exports.motorAction = async (req, reply) => {
tankName, tankName,
startTime, startTime,
motorOnType, motorOnType,
stopCriteria,
manual_threshold_time 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( await Tank.updateOne(
{ customerId, "connections.inputConnections.motor_id": motorId }, { customerId, "connections.inputConnections.motor_id": motorId },
@ -2322,7 +2344,7 @@ exports.motorAction = async (req, reply) => {
// stopTime, // stopTime,
// motorOnType // motorOnType
// ); // );
if (!hasNotifiedStop) { if (!notificationSentStatus.motorStop) {
eventEmitter.emit( eventEmitter.emit(
"motorStop", "motorStop",
fcmToken, fcmToken,
@ -2330,7 +2352,7 @@ exports.motorAction = async (req, reply) => {
stopTime, stopTime,
motorOnType 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( await Tank.updateOne(
{ customerId, "connections.inputConnections.motor_id": motorId }, { customerId, "connections.inputConnections.motor_id": motorId },

Loading…
Cancel
Save