notifications added

master^2
Bhaskar 7 months ago
parent 776586ece4
commit 020421b516

@ -2946,7 +2946,29 @@ exports.motorAction = async (req, reply) => {
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
try {
console.log("motor stopping because it entered this condition")
const tank = await Tank.findOne(
{ customerId, "connections.inputConnections.motor_id": motorId },
{ "connections.inputConnections.$": 1 } // Fetch only relevant motor connection
);
if (tank && tank.connections.inputConnections[0].motor_stop_status === "1") {
console.log("⚠️ Motor already stopped. Skipping notification.");
} else {
console.log("🚀 Sending threshold time notification...");
eventEmitter.emit(
"sendThresholdTimeNotification",
customerId,
fcmToken,
manual_threshold_time,
motorId,
tankName,
blockName
);
}
// eventEmitter.emit( // eventEmitter.emit(
// "sendThresholdTimeNotification", // "sendThresholdTimeNotification",
// customerId, // customerId,
@ -2957,32 +2979,48 @@ exports.motorAction = async (req, reply) => {
// blockName // blockName
// ); // );
const notificationKey = `${customerId}_${motorId}_threshold`; reply.code(200).send({ message: "Motor stopped successfully." });
} catch (error) {
console.error("Error in handleMotorStop:", error);
reply.code(500).send({ error: "Internal Server Error" });
}
// Check if the notification has already been sent // eventEmitter.emit(
if (!notificationTracker.get(notificationKey)) { // "sendThresholdTimeNotification",
console.log("Sending threshold time notification..."); // customerId,
// fcmToken,
// manual_threshold_time,
// motorId,
// tankName,
// blockName
// );
eventEmitter.emit( // const notificationKey = `${customerId}_${motorId}_threshold`;
"sendThresholdTimeNotification",
customerId,
fcmToken,
manual_threshold_time,
motorId,
tankName,
blockName
);
// Mark notification as sent // // Check if the notification has already been sent
notificationTracker.set(notificationKey, true); // if (!notificationTracker.get(notificationKey)) {
// console.log("Sending threshold time notification...");
// Optionally, reset the flag after some time (e.g., 24 hours) // eventEmitter.emit(
setTimeout(() => { // "sendThresholdTimeNotification",
notificationTracker.delete(notificationKey); // customerId,
}, 24 * 60 * 60 * 1000); // Reset after 24 hours // fcmToken,
} else { // manual_threshold_time,
console.log("Notification already sent, skipping..."); // motorId,
} // tankName,
// 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(
@ -3163,6 +3201,19 @@ exports.motorAction = async (req, reply) => {
} else if (action === "stop") { } else if (action === "stop") {
await stopMotor(motorId, customerId, start_instance_id); await stopMotor(motorId, customerId, start_instance_id);
try {
const totalWaterPumped = await calculateTotalPumpedWater(customerId, motorId, start_instance_id);
eventEmitter.emit("motorStop", customerId, fcmToken, tankName, blockName, stopTime, "Mobile APP", totalWaterPumped, typeOfWater, motorId,
loggedInUser.phone,);
reply.code(200).send({ message: "Motor stopped successfully." });
} catch (error) {
console.error("Error in handleMotorStop:", error);
reply.code(500).send({ error: "Internal Server Error" });
}
this.publishMotorStopStatus(motorId, motorStopStatus); this.publishMotorStopStatus(motorId, motorStopStatus);
reply.code(200).send({ message: "Motor stopped successfully." }); reply.code(200).send({ message: "Motor stopped successfully." });
} }
@ -3190,7 +3241,7 @@ async function stopMotor(motorId, customerId, start_instance_id) {
delete motorIntervals[motorId]; delete motorIntervals[motorId];
} }
eventEmitter.emit("motorStop", customerId, [], "", "", currentTime, "Mobile APP", 0, "", motorId, ""); // eventEmitter.emit("motorStop", customerId, [], "", "", currentTime, "Mobile APP", 0, "", motorId, "");
const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id }); const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id });
if (motorData) { if (motorData) {

Loading…
Cancel
Save