notification chnages

master^2
Bhaskar 9 months ago
parent a25023fadc
commit 7922c364a1

@ -1580,7 +1580,7 @@ eventEmitter.on(
`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 after: '${manual_threshold_time}' mins`;
// Send the notification // Send the notification
await sendNotification(fcmTokens, 'Arminta Water Management', message); await sendNotification(fcmTokens, 'Arminta Water Management', message);
@ -2184,8 +2184,8 @@ console.log(fcmToken)
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()}.`; // const startMessage = `The motor supplying water to '${tankName}' in block '${blockName}' has started manually at ${new Date().toISOString()}.`;
eventEmitter.emit("sendMotorStartNotification", fcmToken, startMessage); // eventEmitter.emit("sendMotorStartNotification", fcmToken, startMessage);
await Tank.updateOne( await Tank.updateOne(
{ customerId, "connections.inputConnections.motor_id": motorId }, { customerId, "connections.inputConnections.motor_id": motorId },
@ -2197,39 +2197,21 @@ console.log(fcmToken)
//const startTime = req.body.startTime; //const startTime = req.body.startTime;
// Schedule a task to send a notification when the threshold time is reached // Schedule a task to send a notification when the threshold time is reached
motorIntervals[motorId] = setTimeout(async () => { motorIntervals[motorId] = setTimeout(async () => {
try { try {
// Fetch the latest tank and motor data // Perform threshold time logic (e.g., notification, motor stop)
const receiverTank = await Tank.findOne({ console.log(`Threshold time of ${manualThresholdTime} minutes reached for motor ${motorId}`);
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( await Tank.updateOne(
{ customerId, "connections.inputConnections.motor_id": motorId }, { customerId, "connections.inputConnections.motor_id": motorId },
{ $set: { "connections.inputConnections.$.motor_stop_status": "1" } } { $set: { "connections.inputConnections.$.motor_stop_status": "1" } }
); );
clearTimeout(motorIntervals[motorId]); // Clear timeout
clearTimeout(motorIntervals[motorId]);
delete motorIntervals[motorId]; delete motorIntervals[motorId];
} catch (error) { } catch (error) {
console.error("Error handling threshold time notification:", error); console.error("Error in threshold time handling:", error);
} }
}, thresholdTimeMs) }, thresholdTimeMs);
const stopCriteria = const stopCriteria =
motorOnType === "time" motorOnType === "time"
? `${req.body.manual_threshold_time} minutes` ? `${req.body.manual_threshold_time} minutes`
@ -2246,12 +2228,14 @@ console.log(fcmToken)
startTime, startTime,
motorOnType, motorOnType,
stopCriteria, stopCriteria,
stopTime ,manual_threshold_time manual_threshold_time
); );
await Tank.updateOne( await Tank.updateOne(
{ customerId, "connections.inputConnections.motor_id": motorId }, { customerId, "connections.inputConnections.motor_id": motorId },
{ $set: { "connections.inputConnections.$.motor_stop_status": "2" } } { $set: { "connections.inputConnections.$.motor_stop_status": "2",
"connections.inputConnections.$.manual_threshold_time": manual_threshold_time,
"connections.inputConnections.$.threshold_type": "time" } }
); );
reply.code(200).send({ message: "Motor started successfully." }); reply.code(200).send({ message: "Motor started successfully." });
@ -2275,8 +2259,8 @@ console.log(fcmToken)
} 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 // Emit stop notification
const stopMessage = `The motor supplying water to '${tankName}' in block '${blockName}' was stopped manually at ${stopTime}.`; // const stopMessage = `The motor supplying water to '${tankName}' in block '${blockName}' was stopped manually at ${stopTime}.`;
eventEmitter.emit("sendMotorStopNotification", fcmToken, stopMessage); // eventEmitter.emit("sendMotorStopNotification", fcmToken, stopMessage);
await Tank.updateOne( await Tank.updateOne(
{ customerId, "connections.inputConnections.motor_id": motorId }, { customerId, "connections.inputConnections.motor_id": motorId },

Loading…
Cancel
Save