From 30aea23ee411174ab57d7e021f693826d2eb0700 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Tue, 28 Jan 2025 15:07:04 +0530 Subject: [PATCH 1/2] automatic notification --- src/controllers/tanksController.js | 75 +++++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) 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); + } From de57e6c38b2b87c3621572957d492e4d36277d57 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Tue, 28 Jan 2025 15:16:19 +0530 Subject: [PATCH 2/2] zone and city based location --- src/routes/departmentRoute.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/departmentRoute.js b/src/routes/departmentRoute.js index 9ebc96a7..4d5d5934 100644 --- a/src/routes/departmentRoute.js +++ b/src/routes/departmentRoute.js @@ -374,8 +374,8 @@ module.exports = function (fastify, opts, next) { type: "object", required: ["city", "zone"], properties: { - city: { type: "string" }, zone: { type: "string" }, + city: { type: "string" }, }, }, },