diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index ec736822..b9297aa3 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1727,23 +1727,80 @@ eventEmitter.on('sendThresholdTimeNotification', async (fcmTokens, message) => { } }); -eventEmitter.on('sendMotorStartNotification', async (fcmTokens, message) => { - try { - await sendNotification(fcmTokens, "Motor Started", message); - console.log("Manual method time notification sent successfully."); - } catch (error) { - console.error("Error sending thresmanual method time notification:", error); +eventEmitter.on( + 'sendMotorStartNotification', + async (fcmTokens, motorId, waterLevel, blockName, tankName, motorOnType, stopCriteria, manual_threshold_time) => { + try { + // Get the latest timestamp + const startTime = new Date().toLocaleString('en-IN', { timeZone: 'Asia/Kolkata' }); + + // Retrieve the user information + const users = await User.find({ fcmIds: { $in: fcmTokens } }); + const userNames = users.map(user => user.username).join(', '); + const startMethod = motorOnType.toUpperCase() === "Forced Manual"; + + // Prepare the message + const message = + `🚰 Tank Name: '${tankName}'\n` + + `🕒 Pump started at: '${startTime}'\n` + + `👤 Initiated by: ${userNames}\n` + + `🔄 Pump started by: '${startMethod}'`; + + // Send the notification + await sendNotification(fcmTokens, 'Motor Started 🚀', message); + console.log('Motor start notification sent successfully!'); + } catch (error) { + console.error('Error in sendMotorStartNotification event:', error); + } } -}); +); -eventEmitter.on('sendMotorStopNotification', async (fcmTokens, message) => { - try { - await sendNotification(fcmTokens, "Motor Stopped", message); - console.log("Manual method time notification sent successfully."); - } catch (error) { - console.error("Error sending thresmanual method time notification:", error); +eventEmitter.on( + 'sendMotorStopNotification', + async (fcmTokens, motorId, waterLevel, blockName, tankName, motorOnType) => { + try { + // Get the latest timestamp + const stopTime = new Date().toLocaleString('en-IN', { timeZone: 'Asia/Kolkata' }); + + // Retrieve the user information + const users = await User.find({ fcmIds: { $in: fcmTokens } }); + const userNames = users.map(user => user.username).join(', '); + const stopMethod = motorOnType.toUpperCase() === "Forced Manual"; + + // Prepare the message + const message = + `🚰 Tank Name: '${tankName}'\n` + + `🕒 Pump stopped at: '${stopTime}'\n` + + `👤 Initiated by: ${userNames}\n` + + `🔄 Pump stopped by: '${stopMethod}'\n`; + + // Send the notification + await sendNotification(fcmTokens, 'Motor Stopped 🛑', message); + console.log('Motor stop notification sent successfully!'); + } catch (error) { + console.error('Error in sendMotorStopNotification event:', error); + } } -}); +); + + +// eventEmitter.on('sendMotorStartNotification', async (fcmTokens, message) => { +// try { +// await sendNotification(fcmTokens, "Motor Started", message); +// console.log("Manual method time notification sent successfully."); +// } catch (error) { +// console.error("Error sending thresmanual method time notification:", error); +// } +// }); + +// eventEmitter.on('sendMotorStopNotification', async (fcmTokens, message) => { +// try { +// await sendNotification(fcmTokens, "Motor Stopped", message); +// console.log("Manual method time notification sent successfully."); +// } catch (error) { +// console.error("Error sending thresmanual method time notification:", error); +// } +// }); @@ -2290,7 +2347,7 @@ exports.motorAction = async (req, reply) => { const blockName = req.body.from || "Unknown Block"; // Provide a fallback if `from` is missing const tankName = req.body.to || "Unknown Tank"; // Provide a fallback if `to` is missing const stopTime = req.body.stopTime - const motorOnType = req.body.motor_on_type || "APP"; + const motorOnType = req.body.motor_on_type; const manual_threshold_time = req.body.manual_threshold_time; let hasNotifiedStart = false; let hasNotifiedStop = false; @@ -5187,15 +5244,40 @@ client.on('message', async (topic, message) => { const inputConnection = motorTank.connections.inputConnections.find(conn => conn.motor_id === hw_Id); // Updated variable name if (inputConnection) { inputConnection.motor_status = status; // Update motor status + const tankName = motorTank.tankName; if (inputConnection.motor_stop_status === "1" && status === 2 && inputConnection.motor_on_type !== "forced_manual") { const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm'); inputConnection.motor_stop_status = "2"; inputConnection.motor_on_type = "forced_manual"; inputConnection.startTime = currentTime; + // Emit motor start notification with tankName + eventEmitter.emit( + "sendMotorStartNotification", + fcmToken, // FCM tokens + hw_Id, // Motor ID + inputConnection.water_level || 0, // Water level + motorTank.blockName || "N/A", // Block name + tankName, // Tank name + inputConnection.motor_on_type, // Motor on type + "threshold", // Stop criteria + manual_threshold_time // Threshold time in mins + ); + } if (inputConnection.motor_stop_status === "2" && status === 1) { inputConnection.motor_stop_status = "1"; + + // Emit motor stop notification with tankName + eventEmitter.emit( + "sendMotorStopNotification", + fcmToken, // FCM tokens + hw_Id, // Motor ID + inputConnection.water_level || 0, // Water level + motorTank.blockName || "N/A", // Block name + tankName, // Tank name + inputConnection.motor_on_type // Motor on type + ); } await motorTank.save(); // Save the updated tank