|
|
|
@ -1713,8 +1713,11 @@ 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 {
|
|
|
|
|
console.log(`✅ Received sendMotorStartNotification event for ${customerId}`);
|
|
|
|
|
|
|
|
|
|
const formattedTime = new Date().toLocaleTimeString("en-IN", { timeZone: "Asia/Kolkata" });
|
|
|
|
|
|
|
|
|
|
const normalizedMotorOnType = motorOnType.toLowerCase();
|
|
|
|
@ -1728,17 +1731,16 @@ eventEmitter.on("sendMotorStartNotification", async (hw_Id, customerId, fcmToken
|
|
|
|
|
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` +
|
|
|
|
|
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`
|
|
|
|
|
|
|
|
|
|
`🕒 Pump started at: ${formattedTime}\n`;
|
|
|
|
|
|
|
|
|
|
await sendNotification(hw_Id, customerId, fcmTokens, "Motor Started 🚀", message);
|
|
|
|
|
console.log(`✅ Motor start notification sent for Customer ID: ${customerId}`);
|
|
|
|
@ -1747,9 +1749,10 @@ eventEmitter.on("sendMotorStartNotification", async (hw_Id, customerId, fcmToken
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 🛑 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 {
|
|
|
|
|
console.log(`✅ Received sendMotorStopNotification event for ${customerId}`);
|
|
|
|
|
|
|
|
|
|
const formattedTime = new Date().toLocaleTimeString("en-IN", { timeZone: "Asia/Kolkata" });
|
|
|
|
|
|
|
|
|
|
const normalizedMotorOnType = motorOnType.toLowerCase();
|
|
|
|
@ -1763,7 +1766,7 @@ eventEmitter.on("sendMotorStopNotification", async (hw_Id, customerId, fcmTokens
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const message = `🛑 Motor Stopped ❌\n` +
|
|
|
|
|
const message = `🛑 Motor Stopped Manually ❌\n` +
|
|
|
|
|
`👤 Customer ID: ${customerId}\n` +
|
|
|
|
|
`🔹 Motor Name: ${tankName} - ${blockName}\n` +
|
|
|
|
|
`💧 Water Level: ${waterLevel}\n` +
|
|
|
|
@ -1778,6 +1781,7 @@ eventEmitter.on("sendMotorStopNotification", async (hw_Id, customerId, fcmTokens
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|