manual method changes

master^2
Bhaskar 9 months ago
parent 63e842b9d6
commit 59bb6b1019

@ -1674,6 +1674,39 @@ eventEmitter.on('sendCriticalHighWaterNotification', async (fcmTokens, tankInfo,
const message = `Attention: Water level in '${tankInfo.tankName}' located at '${tankInfo.tankLocation}' is critically high at ${tankInfo.waterLevel}% (${tankInfo.volumeInLitres} L). Water may overflow. Please stop the motor. Motor running from ${startTime} to ${stopTime}.`;
await sendNotification(fcmTokens, 'Critical High Water Level', message, 'Stop Motor');
});
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);
}
});
// Emit high water level event with motorId
// eventEmitter.on('highWaterLevel', async (fcmTokens, timestamp, motorId, waterLevel) => {
// await sendNotification(fcmTokens, 'High Water Level', `Motor ID: ${motorId}, water level reached above 90% at ${timestamp}. Current Water Level: ${waterLevel} Ltrs`);
@ -2152,7 +2185,51 @@ console.log(fcmToken)
if (action === "start") {
motorStopStatus = "2";
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 =
motorOnType === "time"
? `${req.body.manual_threshold_time} minutes`
@ -2197,6 +2274,26 @@ console.log(fcmToken)
}, 30000); // Check every 30 seconds
} else if (action === "stop") {
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(
"motorStop",
fcmToken,

Loading…
Cancel
Save