master^2
Bhaskar 9 months ago
parent 45a0b540a1
commit db73fe5786

@ -1681,6 +1681,34 @@ eventEmitter.on('sendCriticalHighWaterNotification', async (fcmTokens, tankInfo,
// await sendNotification(fcmTokens, 'High Water Level', `Motor ID: ${motorId}, water level reached above 90% at ${timestamp}. Current Water Level: ${waterLevel} Ltrs`); // await sendNotification(fcmTokens, 'High Water Level', `Motor ID: ${motorId}, water level reached above 90% at ${timestamp}. Current Water Level: ${waterLevel} Ltrs`);
// }); // });
eventEmitter.on('sendThresholdTimeNotification', async (fcmTokens, message) => {
try {
await sendNotification(fcmTokens, 'Threshold Time Reached', message, 'Motor Alert');
console.log("Threshold time notification sent successfully.");
} catch (error) {
console.error("Error sending threshold time notification:", 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);
}
});
// Function to emit events with timestamps // Function to emit events with timestamps
const emitWithTimestamp = (eventName, fcmTokens, motorId, waterLevel) => { const emitWithTimestamp = (eventName, fcmTokens, motorId, waterLevel) => {
@ -2150,11 +2178,57 @@ console.log(fcmToken)
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 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") {
motorStopStatus = "2"; motorStopStatus = "2";
const startTime = req.body.startTime; const startTime = req.body.startTime;
const startMessage = `The motor supplying water to '${tankName}' in block '${blockName}' has started manually at ${new Date().toISOString()}.`;
eventEmitter.emit("sendMotorStartNotification", fcmToken, startMessage);
await Tank.updateOne(
{ customerId, "connections.inputConnections.motor_id": motorId },
{ $set: { "connections.inputConnections.$.motor_stop_status": motorStopStatus } }
);
const thresholdTimeMs = req.body.manual_threshold_time * 60 * 1000; // Convert minutes to milliseconds
//const startTime = new Date(); // Record the start time
//const startTime = req.body.startTime;
// Schedule a task to send a notification when the threshold time is reached
motorIntervals[motorId] = setTimeout(async () => {
try {
// Fetch the latest tank and motor data
const receiverTank = await Tank.findOne({
customerId,
tankName: req.body.to,
tankLocation: req.body.to_type.toLowerCase(),
});
// Send a notification for threshold time reached
const message = `Threshold time of ${req.body.manual_threshold_time} minutes has been reached for the motor supplying '${receiverTank.tankName}' located at '${receiverTank.tankLocation}'. Please review the motor operation.`;
eventEmitter.emit('sendThresholdTimeNotification', fcmToken, message);
// Optionally update the tank or motor state in the database
await Tank.updateOne(
{ customerId, tankName: receiverTank.tankName },
{ $set: { notificationSentThresholdTime: true } }
);
// Optionally stop the motor if needed
await Tank.updateOne(
{ customerId, "connections.inputConnections.motor_id": motorId },
{ $set: { "connections.inputConnections.$.motor_stop_status": "1" } }
);
clearTimeout(motorIntervals[motorId]);
delete motorIntervals[motorId];
} catch (error) {
console.error("Error handling threshold time notification:", error);
}
}, thresholdTimeMs)
const stopCriteria = const stopCriteria =
motorOnType === "time" motorOnType === "time"
? `${req.body.manual_threshold_time} minutes` ? `${req.body.manual_threshold_time} minutes`
@ -2171,7 +2245,7 @@ console.log(fcmToken)
startTime, startTime,
motorOnType, motorOnType,
stopCriteria, stopCriteria,
stopTime stopTime ,manual_threshold_time
); );
await Tank.updateOne( await Tank.updateOne(
@ -2199,6 +2273,26 @@ console.log(fcmToken)
}, 30000); // Check every 30 seconds }, 30000); // Check every 30 seconds
} else if (action === "stop") { } else if (action === "stop") {
motorStopStatus = "1"; // If action is stop, set stop status to "1" motorStopStatus = "1"; // If action is stop, set stop status to "1"
// Emit stop notification
const stopMessage = `The motor supplying water to '${tankName}' in block '${blockName}' was stopped manually at ${stopTime}.`;
eventEmitter.emit("sendMotorStopNotification", fcmToken, stopMessage);
await Tank.updateOne(
{ customerId, "connections.inputConnections.motor_id": motorId },
{
$set: {
"connections.inputConnections.$.motor_stop_status": motorStopStatus,
"connections.inputConnections.$.motor_on_type": "manual",
"connections.inputConnections.$.stopTime": stopTime
}
}
);
// Clear intervals if any
if (motorIntervals[motorId]) {
clearInterval(motorIntervals[motorId]);
delete motorIntervals[motorId];
}
eventEmitter.emit( eventEmitter.emit(
"motorStop", "motorStop",
fcmToken, fcmToken,

Loading…
Cancel
Save