forced manual notification

master^2
Bhaskar 9 months ago
parent f29eafb1bd
commit 1806a3f39a

@ -1707,23 +1707,80 @@ eventEmitter.on('sendThresholdTimeNotification', async (fcmTokens, message) => {
}
});
eventEmitter.on('sendMotorStartNotification', async (fcmTokens, message) => {
try {
await sendNotification(fcmTokens, "Motor Started", message);
console.log("Manual method time notification sent successfully.");
} catch (error) {
console.error("Error sending thresmanual method time notification:", error);
eventEmitter.on(
'sendMotorStartNotification',
async (fcmTokens, motorId, waterLevel, blockName, tankName, motorOnType, stopCriteria, manual_threshold_time) => {
try {
// Get the latest timestamp
const startTime = new Date().toLocaleString('en-IN', { timeZone: 'Asia/Kolkata' });
// Retrieve the user information
const users = await User.find({ fcmIds: { $in: fcmTokens } });
const userNames = users.map(user => user.username).join(', ');
const startMethod = motorOnType.toUpperCase() === "Forced Manual";
// Prepare the message
const message =
`🚰 Tank Name: '${tankName}'\n` +
`🕒 Pump started at: '${startTime}'\n` +
`👤 Initiated by: ${userNames}\n` +
`🔄 Pump started by: '${startMethod}'`;
// Send the notification
await sendNotification(fcmTokens, 'Motor Started 🚀', message);
console.log('Motor start notification sent successfully!');
} catch (error) {
console.error('Error in sendMotorStartNotification event:', error);
}
}
});
);
eventEmitter.on('sendMotorStopNotification', async (fcmTokens, message) => {
try {
await sendNotification(fcmTokens, "Motor Stopped", message);
console.log("Manual method time notification sent successfully.");
} catch (error) {
console.error("Error sending thresmanual method time notification:", error);
eventEmitter.on(
'sendMotorStopNotification',
async (fcmTokens, motorId, waterLevel, blockName, tankName, motorOnType) => {
try {
// Get the latest timestamp
const stopTime = new Date().toLocaleString('en-IN', { timeZone: 'Asia/Kolkata' });
// Retrieve the user information
const users = await User.find({ fcmIds: { $in: fcmTokens } });
const userNames = users.map(user => user.username).join(', ');
const stopMethod = motorOnType.toUpperCase() === "Forced Manual";
// Prepare the message
const message =
`🚰 Tank Name: '${tankName}'\n` +
`🕒 Pump stopped at: '${stopTime}'\n` +
`👤 Initiated by: ${userNames}\n` +
`🔄 Pump stopped by: '${stopMethod}'\n`;
// Send the notification
await sendNotification(fcmTokens, 'Motor Stopped 🛑', message);
console.log('Motor stop notification sent successfully!');
} catch (error) {
console.error('Error in sendMotorStopNotification event:', error);
}
}
});
);
// eventEmitter.on('sendMotorStartNotification', async (fcmTokens, message) => {
// try {
// await sendNotification(fcmTokens, "Motor Started", message);
// console.log("Manual method time notification sent successfully.");
// } catch (error) {
// console.error("Error sending thresmanual method time notification:", error);
// }
// });
// eventEmitter.on('sendMotorStopNotification', async (fcmTokens, message) => {
// try {
// await sendNotification(fcmTokens, "Motor Stopped", message);
// console.log("Manual method time notification sent successfully.");
// } catch (error) {
// console.error("Error sending thresmanual method time notification:", error);
// }
// });
@ -2270,7 +2327,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
const motorOnType = req.body.motor_on_type || "APP";
const motorOnType = req.body.motor_on_type;
const manual_threshold_time = req.body.manual_threshold_time;
let hasNotifiedStart = false;
let hasNotifiedStop = false;
@ -5167,15 +5224,40 @@ client.on('message', async (topic, message) => {
const inputConnection = motorTank.connections.inputConnections.find(conn => conn.motor_id === hw_Id); // Updated variable name
if (inputConnection) {
inputConnection.motor_status = status; // Update motor status
const tankName = motorTank.tankName;
if (inputConnection.motor_stop_status === "1" && status === 2 && inputConnection.motor_on_type !== "forced_manual") {
const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm');
inputConnection.motor_stop_status = "2";
inputConnection.motor_on_type = "forced_manual";
inputConnection.startTime = currentTime;
// Emit motor start notification with tankName
eventEmitter.emit(
"sendMotorStartNotification",
fcmToken, // FCM tokens
hw_Id, // Motor ID
inputConnection.water_level || 0, // Water level
motorTank.blockName || "N/A", // Block name
tankName, // Tank name
inputConnection.motor_on_type, // Motor on type
"threshold", // Stop criteria
manual_threshold_time // Threshold time in mins
);
}
if (inputConnection.motor_stop_status === "2" && status === 1) {
inputConnection.motor_stop_status = "1";
// Emit motor stop notification with tankName
eventEmitter.emit(
"sendMotorStopNotification",
fcmToken, // FCM tokens
hw_Id, // Motor ID
inputConnection.water_level || 0, // Water level
motorTank.blockName || "N/A", // Block name
tankName, // Tank name
inputConnection.motor_on_type // Motor on type
);
}
await motorTank.save(); // Save the updated tank

Loading…
Cancel
Save