From 5c3658fec6ca51b991057f031de27788e4097b28 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Wed, 16 Oct 2024 12:04:34 +0530 Subject: [PATCH] chnages on notification --- src/controllers/tanksController.js | 40 +++++++++++++++++------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 0e8b3d5e..aff63e2b 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1342,31 +1342,33 @@ admin.initializeApp({ // }); // Handle motor start event with timestamp -eventEmitter.on('motorStart', async (fcmTokens, timestamp) => { - await sendNotification(fcmTokens, 'Motor Started', `The motor has been started successfully at ${timestamp}.`); +eventEmitter.on('motorStart', async (fcmTokens, timestamp, motorId, waterLevel) => { + await sendNotification(fcmTokens, 'Motor Started', `Motor ID: ${motorId} started successfully at ${timestamp}. Current Water Level: ${waterLevel} Ltrs`); }); -// Handle motor stop event with timestamp -eventEmitter.on('motorStop', async (fcmTokens, timestamp) => { - await sendNotification(fcmTokens, 'Motor Stopped', `The motor has been stopped successfully at ${timestamp}.`); +// Emit motor stop event with motorId +eventEmitter.on('motorStop', async (fcmTokens, timestamp, motorId, waterLevel) => { + await sendNotification(fcmTokens, 'Motor Stopped', `Motor ID: ${motorId} stopped successfully at ${timestamp}.Current Water Level: ${waterLevel} Ltrs`); }); -// Handle low water level event with timestamp -eventEmitter.on('lowWaterLevel', async (fcmTokens, timestamp) => { - await sendNotification(fcmTokens, 'Low Water Level', `The water level dropped below 20% at ${timestamp}.`); +// Emit low water level event with motorId +eventEmitter.on('lowWaterLevel', async (fcmTokens, timestamp, motorId, waterLevel) => { + await sendNotification(fcmTokens, 'Low Water Level', `Motor ID: ${motorId}, water level dropped below 20% at ${timestamp}. Current Water Level: ${waterLevel} Ltrs`); }); -// Handle high water level event with timestamp -eventEmitter.on('highWaterLevel', async (fcmTokens, timestamp) => { - await sendNotification(fcmTokens, 'High Water Level', `The water level reached above 90% at ${timestamp}.`); +// Emit high water level event with motorId +eventEmitter.on('highWaterLevel', async (fcmTokens, timestamp, motorId, waterLevel) => { + await sendNotification(fcmTokens, 'High Water Level', `Motor ID: ${motorId}, water level reached above 90% at ${timestamp}. Current Water Level: ${waterLevel} Ltrs`); }); + // Function to emit events with timestamps -const emitWithTimestamp = (eventName, fcmTokens) => { - const timestamp = moment().format('YYYY-MM-DD HH:mm:ss'); - eventEmitter.emit(eventName, fcmTokens, timestamp); +const emitWithTimestamp = (eventName, fcmTokens, motorId, waterLevel) => { + const timestamp = moment().format('HH:mm:ss YYYY-MM-DD '); + eventEmitter.emit(eventName, fcmTokens, timestamp, motorId, waterLevel); }; + const sendNotification = async (fcmTokens, title, body) => { if (!Array.isArray(fcmTokens) || fcmTokens.length === 0) { console.error('No FCM tokens provided.'); @@ -1742,14 +1744,18 @@ exports.motorAction = async (req, reply) => { const users = await User.find({ customerId }); const fcmToken = users.map(user => user.fcmId).filter(fcmId => fcmId); + const receiverTank = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() }); + console.log(receiverTank) + const currentWaterLevel = parseInt(receiverTank.waterlevel, 10); + // Determine the motor stop status based on the action let motorStopStatus; if (action === "start") { motorStopStatus = "2"; // If action is start, set stop status to "2" - emitWithTimestamp('motorStart', fcmToken); // Emit motor start notification with timestamp + emitWithTimestamp('motorStart', fcmToken, motorId, currentWaterLevel); } else if (action === "stop") { motorStopStatus = "1"; // If action is stop, set stop status to "1" - emitWithTimestamp('motorStop', fcmToken); // Emit motor stop notification with timestamp + emitWithTimestamp('motorStop', fcmToken, motorId, currentWaterLevel); } else { throw new Error("Invalid action provided."); } @@ -4270,7 +4276,7 @@ exports.getBlockData = async (req, reply) => { const mqtt = require('mqtt'); -const client = mqtt.connect('mqtt://35.207.198.4:1883'); // Connect to MQTT broker +const client = mqtt.connect('mqtt://35.207.198.4:1884'); // Connect to MQTT broker client.on('connect', () => { console.log('Connected to MQTT broker');