Varun 8 months ago
commit 6b26b74dac

@ -1780,7 +1780,7 @@ const sendNotification = async (fcmIds, title, body) => {
notification: { title, body },
token,
data: {
exampleKey: 'exampleValue',
'target': '/route_navigation',
},
});
console.log(`Notification sent successfully to token: ${token}`, response);
@ -2126,6 +2126,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 {
@ -2155,25 +2165,39 @@ 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 } });
} 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 } });
}
// 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 } });
// } 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);
}
@ -2282,7 +2306,7 @@ exports.motorAction = async (req, reply) => {
// stopCriteria,
// manual_threshold_time
// );
if (!hasNotifiedStart) {
if (!notificationSentStatus.motorStart) {
eventEmitter.emit(
"motorStart",
fcmToken,
@ -2293,10 +2317,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 },
@ -2335,7 +2358,7 @@ exports.motorAction = async (req, reply) => {
// stopTime,
// motorOnType
// );
if (!hasNotifiedStop) {
if (!notificationSentStatus.motorStop) {
eventEmitter.emit(
"motorStop",
fcmToken,
@ -2343,7 +2366,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 },
@ -2493,7 +2516,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
@ -2526,11 +2564,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
}

Loading…
Cancel
Save