forced manually notifications

master^2
Bhaskar 8 months ago
parent 7b53eae263
commit 6e5fcba6b8

@ -1713,71 +1713,75 @@ eventEmitter.on('sendThresholdTimeNotification', async (customerId, fcmTokens, t
// );
// 🚀 Motor Start Notification
eventEmitter.on("sendMotorStartNotification", async (hw_Id, customerId, fcmTokens, waterLevel, blockName, tankName, motorOnType, stopCriteria, typeOfWater, manualThresholdTime) => {
eventEmitter.on("sendMotorStartNotification", async (hw_Id, customerId, fcmTokens, waterLevel, blockName, tankName, motorOnType, manualThresholdTime) => {
try {
const formattedTime = new Date().toLocaleTimeString("en-IN", { timeZone: "Asia/Kolkata" });
console.log(`✅ Received sendMotorStartNotification event for ${customerId}`);
const formattedTime = new Date().toLocaleTimeString("en-IN", { timeZone: "Asia/Kolkata" });
const normalizedMotorOnType = motorOnType.toLowerCase();
if (normalizedMotorOnType !== "forced_manual") {
console.log(`⚠️ Skipping notification: Motor started in **${motorOnType}** mode.`);
return;
}
const normalizedMotorOnType = motorOnType.toLowerCase();
if (normalizedMotorOnType !== "forced_manual") {
console.log(`⚠️ Skipping notification: Motor started in **${motorOnType}** mode.`);
return;
}
if (!Array.isArray(fcmTokens) || fcmTokens.length === 0) {
console.warn(`⚠️ No valid FCM tokens found for Customer ID: ${customerId}`);
return;
}
if (!Array.isArray(fcmTokens) || fcmTokens.length === 0) {
console.warn(`⚠️ No valid FCM tokens found for Customer ID: ${customerId}`);
return;
}
const stopConditionMessage = stopCriteria === "level"
? `🚨 Pump will stop when the water level reaches **${manualThresholdTime}%**.`
: `⚠️ Pump will stop **manually**.`;
// const stopConditionMessage = stopCriteria === "level"
// ? `🚨 Pump will stop when the water level reaches **${manualThresholdTime}%**.`
// : `⚠️ Pump will stop **manually**.`;
const message = `🚰 Motor Started 🚀\n` +
`👤 Customer ID: ${customerId}\n` +
`🔹 Motor Name: ${tankName} - ${blockName}\n` +
`💧 Water Level: ${waterLevel}\n` +
`📱 Mode: Manually Started\n` +
`🕒 Pump started at: ${formattedTime}\n`
const message = `🚰 Motor Started Manually 🚀\n` +
`👤 Customer ID: ${customerId}\n` +
`🔹 Motor Name: ${tankName} - ${blockName}\n` +
`💧 Water Level: ${waterLevel}\n` +
`📱 Mode: Manually Started\n` +
`🕒 Pump started at: ${formattedTime}\n`;
await sendNotification(hw_Id, customerId, fcmTokens, "Motor Started 🚀", message);
console.log(`✅ Motor start notification sent for Customer ID: ${customerId}`);
await sendNotification(hw_Id, customerId, fcmTokens, "Motor Started 🚀", message);
console.log(`✅ Motor start notification sent for Customer ID: ${customerId}`);
} catch (error) {
console.error(`❌ Error in sendMotorStartNotification for Customer ID: ${customerId}`, error);
console.error(`❌ Error in sendMotorStartNotification for Customer ID: ${customerId}`, error);
}
});
// 🛑 Motor Stop Notification
eventEmitter.on("sendMotorStopNotification", async (hw_Id, customerId, fcmTokens, waterLevel, blockName, tankName, motorOnType, typeOfWater) => {
eventEmitter.on("sendMotorStopNotification", async (hw_Id, customerId, fcmTokens, waterLevel, blockName, tankName, motorOnType) => {
try {
const formattedTime = new Date().toLocaleTimeString("en-IN", { timeZone: "Asia/Kolkata" });
console.log(`✅ Received sendMotorStopNotification event for ${customerId}`);
const normalizedMotorOnType = motorOnType.toLowerCase();
if (normalizedMotorOnType !== "forced_manual") {
console.log(`⚠️ Skipping notification: Motor stopped in **${motorOnType}** mode.`);
return;
}
const formattedTime = new Date().toLocaleTimeString("en-IN", { timeZone: "Asia/Kolkata" });
if (!Array.isArray(fcmTokens) || fcmTokens.length === 0) {
console.warn(`⚠️ No valid FCM tokens found for Customer ID: ${customerId}`);
return;
}
const normalizedMotorOnType = motorOnType.toLowerCase();
if (normalizedMotorOnType !== "forced_manual") {
console.log(`⚠️ Skipping notification: Motor stopped in **${motorOnType}** mode.`);
return;
}
const message = `🛑 Motor Stopped ❌\n` +
`👤 Customer ID: ${customerId}\n` +
`🔹 Motor Name: ${tankName} - ${blockName}\n` +
`💧 Water Level: ${waterLevel}\n` +
`📱 Mode: Manually Stopped\n` +
`🕒 Pump stopped at: ${formattedTime}`;
if (!Array.isArray(fcmTokens) || fcmTokens.length === 0) {
console.warn(`⚠️ No valid FCM tokens found for Customer ID: ${customerId}`);
return;
}
const message = `🛑 Motor Stopped Manually ❌\n` +
`👤 Customer ID: ${customerId}\n` +
`🔹 Motor Name: ${tankName} - ${blockName}\n` +
`💧 Water Level: ${waterLevel}\n` +
`📱 Mode: Manually Stopped\n` +
`🕒 Pump stopped at: ${formattedTime}`;
await sendNotification(hw_Id, customerId, fcmTokens, "Motor Stopped ❌", message);
console.log(`✅ Motor stop notification sent for Customer ID: ${customerId}`);
await sendNotification(hw_Id, customerId, fcmTokens, "Motor Stopped ❌", message);
console.log(`✅ Motor stop notification sent for Customer ID: ${customerId}`);
} catch (error) {
console.error(`❌ Error in sendMotorStopNotification for Customer ID: ${customerId}`, error);
console.error(`❌ Error in sendMotorStopNotification for Customer ID: ${customerId}`, error);
}
});
// eventEmitter.on('sendLowWaterNotification', (fcmTokens, message) => {
// const notificationMessage = `Warning: Water level is low in the tank.
// Tank Name: ${tankName},
@ -5987,6 +5991,87 @@ client.on('message', async (topic, message) => {
const sendMotorNotifications = async () => {
// console.log("🔄 Checking for motor notifications...");
// Find motors that need a start or stop notification
const motors = await Tank.find({
"connections.inputConnections.motor_id": { $exists: true },
});
for (const motorTank of motors) {
const inputConnection = motorTank.connections.inputConnections.find(
(conn) => conn.motor_id
);
if (!inputConnection) continue;
const { customerId, blockName, tankName } = motorTank;
const fcmTokens = await getFcmTokens(customerId); // Get FCM tokens for this customer
if (!fcmTokens.length) continue;
// 🔹 Motor Start Condition
if (
inputConnection.motor_stop_status === "2" &&
!motorTank.motor_start_notified
) {
console.log("✅ Sending Motor Start Notification...");
eventEmitter.emit(
"sendMotorStartNotification",
inputConnection.motor_id,
customerId,
fcmTokens,
inputConnection.water_level || 0,
blockName,
tankName,
"forced_manual",
inputConnection.manual_threshold_time
);
// Mark notification as sent
motorTank.motor_start_notified = true;
motorTank.motor_stop_notified = false; // Reset stop notification flag
await motorTank.save();
}
// 🔹 Motor Stop Condition
if (
inputConnection.motor_stop_status === "1" &&
!motorTank.motor_stop_notified
) {
console.log("✅ Sending Motor Stop Notification...");
eventEmitter.emit(
"sendMotorStopNotification",
inputConnection.motor_id,
customerId,
fcmTokens,
inputConnection.water_level || 0,
blockName,
tankName,
"forced_manual"
);
// Mark notification as sent
motorTank.motor_stop_notified = true;
motorTank.motor_start_notified = false; // Reset start notification flag
await motorTank.save();
}
}
};
const getFcmTokens = async (customerId) => {
const user = await User.findOne({ customerId }).select("fcmIds");
return user?.fcmIds?.filter((token) => token) || [];
};
// Run the notification check every second
cron.schedule("* * * * * *", async () => {
await sendMotorNotifications();
});
//
// API function to get survey data for a particular installer

Loading…
Cancel
Save