master^2
Bhaskar 7 months ago
parent 7321fcd8b2
commit 1a7d82a23f

@ -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 supplierTank = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() });
const currentWaterLevel = parseInt(supplierTank.waterlevel, 10); const currentWaterLevel = parseInt(supplierTank.waterlevel, 10);
const currentWaterPercentage = (currentWaterLevel / parseInt(supplierTank.capacity.replace(/,/g, ''), 10)) * 100; const currentWaterPercentage = (currentWaterLevel / parseInt(supplierTank.capacity.replace(/,/g, ''), 10)) * 100;
const notificationTracker = new Map();
if (new Date() >= thresholdTime || currentWaterPercentage <= lowWaterThreshold) { if (new Date() >= thresholdTime || currentWaterPercentage <= lowWaterThreshold) {
console.log(new Date(),"new date") console.log(new Date(),"new date")
console.log(thresholdTime,"thresholdTime") console.log(thresholdTime,"thresholdTime")
console.log("motor stopping because it entered this condition") console.log("motor stopping because it entered this condition")
// Emit the threshold time notification // 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", "sendThresholdTimeNotification",
customerId, customerId,
fcmToken, fcmToken,
manual_threshold_time, manual_threshold_time,
motorId, motorId,
tankName, 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'); const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm');
await Tank.updateOne( await Tank.updateOne(
{ customerId, "connections.inputConnections.motor_id": motorId }, { customerId, "connections.inputConnections.motor_id": motorId },
@ -6004,7 +6032,8 @@ const sendMotorNotifications = async () => {
(conn) => conn.motor_id (conn) => conn.motor_id
); );
const status = inputConnection.motor_status 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; if (!inputConnection) continue;
const { customerId, blockName, tankName } = motorTank; const { customerId, blockName, tankName } = motorTank;
@ -6013,7 +6042,7 @@ const sendMotorNotifications = async () => {
// 🔹 Motor Start Condition // 🔹 Motor Start Condition
if ( 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 !motorTank.motor_start_notified
) { ) {
console.log("✅ Sending Motor Start Notification..."); console.log("✅ Sending Motor Start Notification...");
@ -6038,7 +6067,7 @@ const sendMotorNotifications = async () => {
// 🔹 Motor Stop Condition // 🔹 Motor Stop Condition
if ( if (
inputConnection.motor_stop_status === "1" && inputConnection.motor_stop_status === "1" && inputConnection.motor_on_type === "forced_manual" && status ===2 &&
!motorTank.motor_stop_notified !motorTank.motor_stop_notified
) { ) {
console.log("✅ Sending Motor Stop Notification..."); console.log("✅ Sending Motor Stop Notification...");
@ -6067,10 +6096,10 @@ const sendMotorNotifications = async () => {
return user?.fcmIds?.filter((token) => token) || []; return user?.fcmIds?.filter((token) => token) || [];
}; };
// Run the notification check every second //Run the notification check every second
// cron.schedule("* * * * * *", async () => { cron.schedule("* * * * * *", async () => {
// // await sendMotorNotifications(); await sendMotorNotifications();
// }); });

Loading…
Cancel
Save