chnages notifications

master^2
Bhaskar 7 months ago
parent 36ed1d305d
commit c78c716a0d

@ -2864,7 +2864,7 @@ exports.motorAction = async (req, reply) => {
let motorStopStatus = action === "start" ? "2" : "1"; let motorStopStatus = action === "start" ? "2" : "1";
const blockName = req.body.from || "Unknown Block"; const blockName = req.body.from || "Unknown Block";
const tankName = req.body.to || "Unknown Tank"; const tankName = req.body.to || "Unknown Tank";
const stopTime = req.body.stopTime
if (action === "start") { if (action === "start") {
if (motorIntervals[motorId]) { if (motorIntervals[motorId]) {
@ -2942,24 +2942,54 @@ 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(); // 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( try {
// "sendThresholdTimeNotification", console.log("motor stopping because it entered this condition")
// customerId,
// fcmToken, const tank = await Tank.findOne(
// manual_threshold_time, { customerId, "connections.inputConnections.motor_id": motorId },
// motorId, { "connections.inputConnections.$": 1 } // Fetch only relevant motor connection
// tankName, );
// blockName
// ); if (tank && tank.connections.inputConnections[0].motor_stop_status === "1") {
console.log("⚠️ Motor already stopped. Skipping notification.");
const notificationKey = `${customerId}_${motorId}_threshold`; } else {
console.log("🚀 Sending threshold time notification...");
eventEmitter.emit(
"sendThresholdTimeNotification",
customerId,
fcmToken,
manual_threshold_time,
motorId,
tankName,
blockName
);
}
// eventEmitter.emit(
// "sendThresholdTimeNotification",
// customerId,
// fcmToken,
// manual_threshold_time,
// motorId,
// tankName,
// blockName
// );
reply.code(200).send({ message: "Motor stopped successfully." });
} catch (error) {
console.error("Error in handleMotorStop:", error);
reply.code(500).send({ error: "Internal Server Error" });
}
//const notificationKey = `${customerId}_${motorId}_threshold`;
// Check if the notification has already been sent // Check if the notification has already been sent
// if (!notificationTracker.get(notificationKey)) { // if (!notificationTracker.get(notificationKey)) {
@ -3165,6 +3195,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." });
} }
@ -3192,7 +3235,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