diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 8aa0519a..ddaa05c5 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1564,16 +1564,57 @@ admin.initializeApp({ // await sendNotification(fcmTokens, 'Motor Started', `Motor ID: ${motorId} started successfully at ${timestamp}. Current Water Level: ${waterLevel} Ltrs`); // }); +// eventEmitter.on('motorStart', async (fcmTokens, timestamp, motorId, waterLevel, blockName, tankName, startTime, motorOnType, stopCriteria) => { +// const message = `MotorId '${motorId}' Water supply from '${blockName}' to '${tankName}' started at ${startTime} by '${motorOnType}' mode and will stop after ${stopCriteria}. Current Water Level: ${waterLevel} Ltrs.`; +// await sendNotification(fcmTokens, 'Motor Started', message); +// }); + +// eventEmitter.on('motorStart', async (fcmTokens, timestamp, motorId, waterLevel, blockName, tankName, startTime, motorOnType, stopCriteria) => { +// const message = `Tank Name : '${tankName}' Pump Starts at '${startTime}' by '${motorOnType}' will stop at '${stopCriteria} mins'`; +// await sendNotification(fcmTokens, 'Arminta Water Management', message); +// }); + eventEmitter.on('motorStart', async (fcmTokens, timestamp, motorId, waterLevel, blockName, tankName, startTime, motorOnType, stopCriteria) => { - const message = `MotorId '${motorId}' Water supply from '${blockName}' to '${tankName}' started at ${startTime} by '${motorOnType}' mode and will stop after ${stopCriteria}. Current Water Level: ${waterLevel} Ltrs.`; - await sendNotification(fcmTokens, 'Motor Started', message); + try { + // Retrieve the user information + const users = await User.find({ fcmId: { $in: fcmTokens } }); + console.log("users",users) + const userNames = users.map(user => user.username).join(', '); + console.log("userNames",userNames) + + // Prepare the message + const message = `Tank Name: '${tankName}', Pump started at '${startTime}' by Initiated by user(s): ${userNames} '${motorOnType}' and will stop after '${stopCriteria}'`; + + // Send the notification + await sendNotification(fcmTokens, 'Arminta Water Management', message); + } catch (error) { + console.error('Error in motorStart event:', error); + } }); // Emit motor stop event with motorId -eventEmitter.on('motorStop', async (fcmTokens, motorId, waterLevel, blockName, tankName, stopTime, motorOnType) => { - const message = `MotorId '${motorId}' Water supply from '${blockName}' to '${tankName}' stopped at ${stopTime} by '${motorOnType}' mode. Current Water Level: ${waterLevel} Ltrs.`; - await sendNotification(fcmTokens, 'Motor Stopped', message); +// eventEmitter.on('motorStop', async (fcmTokens, motorId, waterLevel, blockName, tankName, stopTime, motorOnType) => { +// const message = `MotorId '${motorId}' Water supply from '${blockName}' to '${tankName}' stopped at ${stopTime} by '${motorOnType}' mode. Current Water Level: ${waterLevel} Ltrs.`; +// await sendNotification(fcmTokens, 'Motor Stopped', message); +// }); + +eventEmitter.on('motorStop', async (fcmTokens, tankName,stopTime, motorOnType) => { + try { + // Retrieve the user information + const users = await User.find({ fcmId: { $in: fcmTokens } }); + console.log("users",users) + const userNames = users.map(user => user.username).join(', '); + console.log("userNames",userNames) + + // Prepare the message + const message = `Tank Name: '${tankName}', Pump stopped at '${stopTime}' by Initiated by user(s): ${userNames} '${motorOnType}'`; + + // Send the notification + await sendNotification(fcmTokens, 'Arminta Water Management', message); + } catch (error) { + console.error('Error in motorStart event:', error); + } }); @@ -2071,7 +2112,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 || new Date().toISOString(); - const motorOnType = req.body.motor_on_type || "application"; + const motorOnType = req.body.motor_on_type || "app"; if (action === "start") { motorStopStatus = "2"; @@ -2105,9 +2146,6 @@ exports.motorAction = async (req, reply) => { eventEmitter.emit( "motorStop", fcmToken, - motorId, - currentWaterLevel, - blockName, tankName, stopTime, motorOnType