notification changes

master^2
Bhaskar 9 months ago
parent 04b49063b0
commit 6fc50d716d

@ -1564,18 +1564,50 @@ admin.initializeApp({
// await sendNotification(fcmTokens, 'Motor Started', `Motor ID: ${motorId} started successfully at ${timestamp}. Current Water Level: ${waterLevel} Ltrs`); // 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) => { eventEmitter.on('motorStart', async (fcmTokens, timestamp, motorId, waterLevel, blockName, tankName, startTime, motorOnType, stopCriteria,stopTime) => {
const message = `Water supply from '${blockName}' to '${tankName}' started at ${startTime} by '${motorOnType}' mode and will stop after ${stopCriteria}. Current Water Level: ${waterLevel} Ltrs.`; // const message = `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); // 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 '${stopTime}'`;
// 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) => {
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)
// Emit motor stop event with motorId // Prepare the message
eventEmitter.on('motorStop', async (fcmTokens, timestamp, motorId, waterLevel, blockName, tankName,stopTime,motorOnType) => { const message = `Tank Name: '${tankName}', Pump stopped at '${stopTime}' by Initiated by user(s): ${userNames} '${motorOnType}'`;
const message = `Water supply from '${blockName}' to '${tankName}' stopped at ${stopTime} by '${motorOnType}' mode. Current Water Level: ${waterLevel} Ltrs.`;
await sendNotification(fcmTokens, 'Motor Stopped', message); // 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, timestamp, motorId, waterLevel, blockName, tankName,stopTime,motorOnType) => {
// const message = `Water supply from '${blockName}' to '${tankName}' stopped at ${stopTime} by '${motorOnType}' mode. Current Water Level: ${waterLevel} Ltrs.`;
// await sendNotification(fcmTokens, 'Motor Stopped', message);
// });
// Event listener to handle notification // Event listener to handle notification
eventEmitter.on('sendLowWaterNotification', async (fcmToken, tankInfo) => { eventEmitter.on('sendLowWaterNotification', async (fcmToken, tankInfo) => {
const message = formatWaterLevelMessage(tankInfo, 'low'); const message = formatWaterLevelMessage(tankInfo, 'low');
@ -2028,49 +2060,49 @@ exports.motorAction = async (req, reply) => {
const waterLevelThresholds = { low: 30, veryLow: 20, criticallyLow: 10 }; const waterLevelThresholds = { low: 30, veryLow: 20, criticallyLow: 10 };
// Check if the water level is below any of the thresholds // Check if the water level is below any of the thresholds
if (currentWaterLevel < waterLevelThresholds.criticallyLow) { // if (currentWaterLevel < waterLevelThresholds.criticallyLow) {
if (!receiverTank.notificationSentCritical) { // if (!receiverTank.notificationSentCritical) {
eventEmitter.emit('sendCriticalLowWaterNotification', fcmToken, receiverTank); // eventEmitter.emit('sendCriticalLowWaterNotification', fcmToken, receiverTank);
await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentCritical: true } }); // await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentCritical: true } });
} // }
} else if (currentWaterLevel < waterLevelThresholds.veryLow) { // } else if (currentWaterLevel < waterLevelThresholds.veryLow) {
if (!receiverTank.notificationSentVeryLow) { // if (!receiverTank.notificationSentVeryLow) {
eventEmitter.emit('sendVeryLowWaterNotification', fcmToken, receiverTank); // eventEmitter.emit('sendVeryLowWaterNotification', fcmToken, receiverTank);
await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentVeryLow: true } }); // await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentVeryLow: true } });
} // }
} else if (currentWaterLevel < waterLevelThresholds.low) { // } else if (currentWaterLevel < waterLevelThresholds.low) {
if (!receiverTank.notificationSentLow) { // if (!receiverTank.notificationSentLow) {
eventEmitter.emit('sendLowWaterNotification', fcmToken, receiverTank); // eventEmitter.emit('sendLowWaterNotification', fcmToken, receiverTank);
await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentLow: true } }); // await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentLow: true } });
} // }
} // }
// Check for critical high water level // // Check for critical high water level
if (currentWaterLevel >= criticalHighWaterThreshold) { // if (currentWaterLevel >= criticalHighWaterThreshold) {
if (!receiverTank.notificationSentCriticalHigh) { // if (!receiverTank.notificationSentCriticalHigh) {
eventEmitter.emit('sendCriticalHighWaterNotification', fcmToken, receiverTank); // eventEmitter.emit('sendCriticalHighWaterNotification', fcmToken, receiverTank);
await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentCriticalHigh: true } }); // await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentCriticalHigh: true } });
} // }
} // }
// Check for very high water level // // Check for very high water level
else if (currentWaterLevel >= veryHighWaterThreshold) { // else if (currentWaterLevel >= veryHighWaterThreshold) {
if (!receiverTank.notificationSentVeryHigh) { // if (!receiverTank.notificationSentVeryHigh) {
eventEmitter.emit('sendVeryHighWaterNotification', fcmToken, receiverTank); // eventEmitter.emit('sendVeryHighWaterNotification', fcmToken, receiverTank);
await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentVeryHigh: true } }); // await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentVeryHigh: true } });
} // }
} // }
// Check for high water level // // Check for high water level
else if (currentWaterLevel >= highWaterThreshold) { // else if (currentWaterLevel >= highWaterThreshold) {
if (!receiverTank.notificationSentHigh) { // if (!receiverTank.notificationSentHigh) {
eventEmitter.emit('sendHighWaterNotification', fcmToken, receiverTank); // eventEmitter.emit('sendHighWaterNotification', fcmToken, receiverTank);
await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentHigh: true } }); // await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentHigh: true } });
} // }
} // }
// Determine the motor stop status based on the action // Determine the motor stop status based on the action
let motorStopStatus; let motorStopStatus;
const blockName = req.body.from || "Unknown Block"; // Provide a fallback if `from` is missing 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 tankName = req.body.to || "Unknown Tank"; // Provide a fallback if `to` is missing
const stopTime = req.body.stopTime || new Date().toISOString(); const stopTime = req.body.stopTime || new Date().toISOString();
const motorOnType = req.body.motor_on_type || "manual"; const motorOnType = req.body.motor_on_type || "app";
if (action === "start") { if (action === "start") {
motorStopStatus = "2"; motorStopStatus = "2";
@ -2090,7 +2122,8 @@ else if (currentWaterLevel >= highWaterThreshold) {
tankName, // Tank Name tankName, // Tank Name
startTime, startTime,
motorOnType, motorOnType,
stopCriteria stopCriteria,
stopTime
); );
await Tank.updateOne( await Tank.updateOne(
@ -2104,9 +2137,9 @@ else if (currentWaterLevel >= highWaterThreshold) {
eventEmitter.emit( eventEmitter.emit(
"motorStop", "motorStop",
fcmToken, fcmToken,
motorId, // motorId,
currentWaterLevel, // currentWaterLevel,
blockName, // blockName,
tankName, tankName,
stopTime, stopTime,
motorOnType motorOnType

Loading…
Cancel
Save