diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 2a3fe73e..54a0b084 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1722,6 +1722,43 @@ eventEmitter.on( } ); +eventEmitter.on( + 'motorStartAutomatic', + async (fcmTokens, tankName, blockName, startTime, motorOnType, manual_threshold_time, typeOfWater,threshold) => { + try { + // Retrieve the user information + const users = await User.find({ fcmIds: { $in: fcmTokens } }); + console.log("users", users); + const userNames = users.map(user => user.username).join(', '); + console.log("userNames", userNames); + + const startMethod = motorOnType === "Automatic" ? "Automatic" : "Manual"; + + // Generate motor name dynamically based on tank name, block name, and type of water + const motorName = `${tankName}-${blockName}-${typeOfWater}`; + + // Get current date and time for the motor start time + const currentDateTime = new Date(); + const formattedDate = currentDateTime.toLocaleDateString(); // Customize this format as needed + const formattedTime = currentDateTime.toLocaleTimeString(); // Customize this format as needed + + // Prepare the message + const message = + `🚰 Motor Name: ${motorName}\n` + + `🚰 Tank Name: '${tankName}'\n` + + `🚰 Block Name: '${blockName}'\n` + + `👤 Started by: '${startMethod}' \n` + + `📱 Mode: "Atomatic System" \n` + + `🕒 Pump started at: ${startTime} (Time: ${formattedTime} on ${formattedDate})\n` + + `Will stop after: '${threshold}' `; + + // Send the notification + await sendNotification(fcmTokens, 'Arminta Water Management', message); + } catch (error) { + console.error('Error in motorStart event:', error); + } + } +); // eventEmitter.on('motorStop', async (fcmTokens, tankName,stopTime, motorOnType) => { @@ -3656,12 +3693,23 @@ const motorActionAuto = async (req, reply) => { const action = req.body.action; const motorId = req.body.motor_id; const motor_on_type = req.body.motor_on_type + const startTime = req.body.startTime; + const stopTime = req.body.stopTime; + const threshold = req.body.threshold || "unknown"; if (!motorId) { throw new Error("Motor ID is required."); } let motorStopStatus; + const tank = await Tank.findOne({ customerId, "connections.inputConnections.motor_id": motorId }); + + if (!tank) { + throw new Error("Tank not found for the provided motor ID."); + } + + const { tankName, blockName, typeOfWater, fcmTokens } = tank; // Extracting necessary details + if (action === "start") { await Tank.updateOne( @@ -3676,7 +3724,17 @@ const motorActionAuto = async (req, reply) => { } ); - ; + eventEmitter.emit( + "motorStartAutomatic", + fcmTokens, + tankName, + blockName, + startTime, + "Automatic", + typeOfWater, + threshold + ); + } if (action === "stop") { @@ -3690,6 +3748,21 @@ const motorActionAuto = async (req, reply) => { } } ); + + const currentDateTime = new Date(); + const formattedDate = currentDateTime.toLocaleDateString(); + const formattedTime = currentDateTime.toLocaleTimeString(); + + const stopMessage = + `🚰 Motor Name: ${tankName}-${blockName}-${typeOfWater}\n` + + `🚰 Tank Name: '${tankName}'\n` + + `🚰 Block Name: '${blockName}'\n` + + `🕒 Pump stopped at: ${stopTime} (Time: ${formattedTime} on ${formattedDate})\n` + + `⏳ Operation Duration: ${threshold} `; + + // Send stop notification + await sendNotification(fcmTokens, "Arminta Water Management", stopMessage); + }