notifications changes

master^2
Bhaskar 9 months ago
parent 4f51941292
commit 45a0b540a1

@ -1564,30 +1564,32 @@ 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,stopTime) => { eventEmitter.on(
// const message = `Water supply from '${blockName}' to '${tankName}' started at ${startTime} by '${motorOnType}' mode and will stop after ${stopCriteria}. Current Water Level: ${waterLevel} Ltrs.`; 'motorStart',
// await sendNotification(fcmTokens, 'Motor Started', message); async (fcmTokens, timestamp, motorId, waterLevel, blockName, tankName, startTime, motorOnType, stopCriteria, manual_threshold_time) => {
try { try {
// Retrieve the user information // Retrieve the user information
const users = await User.find({ fcmIds: { $in: fcmTokens } }); const users = await User.find({ fcmIds: { $in: fcmTokens } });
console.log("users",users) console.log("users", users);
const userNames = users.map(user => user.username).join(', '); const userNames = users.map(user => user.username).join(', ');
console.log("userNames",userNames) console.log("userNames", userNames);
// Prepare the message // Prepare the message
// const message = `Tank Name: '${tankName}', Pump started at '${startTime}' by Initiated by user(s): ${userNames} '${motorOnType}' and will stop after '${stopTime}'`; const message =
const message = `Tank Name: '${tankName}'\n` +
`Tank Name: '${tankName}'\n` + `Pump started at: '${startTime}'\n` +
`Pump started at: '${startTime}'\n` + `Initiated by user(s): ${userNames}\n` +
`Initiated by user(s): ${userNames}\n` + `Pump started by: '${motorOnType.toUpperCase()}'\n` +
`Pump started by: '${motorOnType.toUpperCase()}'\n` + `Will stop at: '${manual_threshold_time}'`;
`Will stop at: '${stopTime}'`;
// Send the notification // Send the notification
await sendNotification(fcmTokens, 'Arminta Water Management', message); await sendNotification(fcmTokens, 'Arminta Water Management', message);
} catch (error) { } catch (error) {
console.error('Error in motorStart event:', error); console.error('Error in motorStart event:', error);
}
} }
}); );
eventEmitter.on('motorStop', async (fcmTokens, tankName,stopTime, motorOnType) => { eventEmitter.on('motorStop', async (fcmTokens, tankName,stopTime, motorOnType) => {
try { try {
@ -2147,7 +2149,7 @@ console.log(fcmToken)
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
const motorOnType = req.body.motor_on_type || "app"; const motorOnType = req.body.motor_on_type || "app";
if (action === "start") { if (action === "start") {

Loading…
Cancel
Save