|
|
|
@ -2863,6 +2863,106 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
|
const tankName = req.body.to || "Unknown Tank";
|
|
|
|
|
|
|
|
|
|
if (action === "start") {
|
|
|
|
|
motorStopStatus = "2";
|
|
|
|
|
const startTime = req.body.startTime;
|
|
|
|
|
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 stopCriteria =
|
|
|
|
|
motorOnType === "time"
|
|
|
|
|
? `${req.body.manual_threshold_time} minutes`
|
|
|
|
|
: `${req.body.manual_threshold_litres} litres`;
|
|
|
|
|
try {
|
|
|
|
|
console.log("enter the start")
|
|
|
|
|
eventEmitter.emit(
|
|
|
|
|
"motorStart",
|
|
|
|
|
customerId,
|
|
|
|
|
fcmToken,
|
|
|
|
|
tankName,
|
|
|
|
|
blockName,
|
|
|
|
|
startTime,
|
|
|
|
|
"Mobile APP",
|
|
|
|
|
manual_threshold_time,
|
|
|
|
|
typeOfWater,
|
|
|
|
|
motorId,
|
|
|
|
|
loggedInUser.phone,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
reply.code(200).send({ message: "Motor started successfully." });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error in handleMotorStart:", error);
|
|
|
|
|
reply.code(500).send({ error: "Internal Server Error" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start checking water level every 30 minutes
|
|
|
|
|
if (!waterLevelCheckInterval) {
|
|
|
|
|
waterLevelCheckInterval = setInterval(async () => {
|
|
|
|
|
await checkWaterLevel(customerId, motorId, fcmToken, receiverTank);
|
|
|
|
|
}, 30 * 60 * 1000); // 30 minutes
|
|
|
|
|
}
|
|
|
|
|
await Tank.updateOne(
|
|
|
|
|
{ customerId, "connections.inputConnections.motor_id": motorId },
|
|
|
|
|
{ $set: { "connections.inputConnections.$.motor_stop_status": "2",
|
|
|
|
|
"connections.inputConnections.$.manual_threshold_time": manual_threshold_time,
|
|
|
|
|
"connections.inputConnections.$.threshold_type": "time",
|
|
|
|
|
"connections.inputConnections.$.motor_on_type": "manual" } }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
reply.code(200).send({ message: "Motor started successfully." });
|
|
|
|
|
|
|
|
|
|
} else if (action === "stop") {
|
|
|
|
|
motorStopStatus = "1"; // If action is stop, set stop status to "1"
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const totalWaterPumped = await calculateTotalPumpedWater(customerId, motorId, start_instance_id);
|
|
|
|
|
|
|
|
|
|
eventEmitter.emit("motorStop", customerId, fcmToken, tankName, blockName, stopTime, "Mobile APP", totalWaterPumped, typeOfWater, motorId,
|
|
|
|
|
loggedInUser.phone,);
|
|
|
|
|
|
|
|
|
|
reply.code(200).send({ message: "Motor stopped successfully." });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error in handleMotorStop:", error);
|
|
|
|
|
reply.code(500).send({ error: "Internal Server Error" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await Tank.updateOne(
|
|
|
|
|
{ customerId, "connections.inputConnections.motor_id": motorId },
|
|
|
|
|
{
|
|
|
|
|
$set: {
|
|
|
|
|
"connections.inputConnections.$.motor_stop_status": "1",
|
|
|
|
|
"connections.inputConnections.$.motor_on_type": motorOnType }
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
// Clear the interval when the motor is stopped
|
|
|
|
|
if (waterLevelCheckInterval) {
|
|
|
|
|
clearInterval(waterLevelCheckInterval);
|
|
|
|
|
waterLevelCheckInterval = null; // Reset the interval ID
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error("Invalid action provided.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If action is stop, immediately update motor status and perform stop operations
|
|
|
|
|
if (action === "stop") {
|
|
|
|
|
console.log("enterted stop")
|
|
|
|
|
await Tank.updateOne(
|
|
|
|
|
{ customerId, "connections.inputConnections.motor_id": motorId },
|
|
|
|
|
{
|
|
|
|
|
$set: {
|
|
|
|
|
"connections.inputConnections.$.motor_stop_status": "1",
|
|
|
|
|
"connections.inputConnections.$.motor_on_type": "manual",
|
|
|
|
|
"connections.inputConnections.$.stopTime": req.body.stopTime,
|
|
|
|
|
"connections.inputConnections.$.threshold_type": null,
|
|
|
|
|
"connections.inputConnections.$.manual_threshold_time": null,
|
|
|
|
|
"connections.inputConnections.$.manual_threshold_percentage": null
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (motorIntervals[motorId]) {
|
|
|
|
|
console.log(`🔄 Clearing all existing intervals for motorId: ${motorId}`);
|
|
|
|
@ -2958,29 +3058,29 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
|
const notificationKey = `${customerId}_${motorId}_threshold`;
|
|
|
|
|
|
|
|
|
|
// Check if the notification has already been sent
|
|
|
|
|
if (!notificationTracker.get(notificationKey)) {
|
|
|
|
|
console.log("Sending threshold time notification...");
|
|
|
|
|
// if (!notificationTracker.get(notificationKey)) {
|
|
|
|
|
// console.log("Sending threshold time notification...");
|
|
|
|
|
|
|
|
|
|
eventEmitter.emit(
|
|
|
|
|
"sendThresholdTimeNotification",
|
|
|
|
|
customerId,
|
|
|
|
|
fcmToken,
|
|
|
|
|
manual_threshold_time,
|
|
|
|
|
motorId,
|
|
|
|
|
tankName,
|
|
|
|
|
blockName
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Mark notification as sent
|
|
|
|
|
notificationTracker.set(notificationKey, true);
|
|
|
|
|
|
|
|
|
|
// Optionally, reset the flag after some time (e.g., 24 hours)
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
notificationTracker.delete(notificationKey);
|
|
|
|
|
}, 24 * 60 * 60 * 1000); // Reset after 24 hours
|
|
|
|
|
} else {
|
|
|
|
|
console.log("Notification already sent, skipping...");
|
|
|
|
|
}
|
|
|
|
|
// eventEmitter.emit(
|
|
|
|
|
// "sendThresholdTimeNotification",
|
|
|
|
|
// customerId,
|
|
|
|
|
// fcmToken,
|
|
|
|
|
// manual_threshold_time,
|
|
|
|
|
// motorId,
|
|
|
|
|
// tankName,
|
|
|
|
|
// blockName
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// // Mark notification as sent
|
|
|
|
|
// notificationTracker.set(notificationKey, true);
|
|
|
|
|
|
|
|
|
|
// // Optionally, reset the flag after some time (e.g., 24 hours)
|
|
|
|
|
// setTimeout(() => {
|
|
|
|
|
// notificationTracker.delete(notificationKey);
|
|
|
|
|
// }, 24 * 60 * 60 * 1000); // Reset after 24 hours
|
|
|
|
|
// } else {
|
|
|
|
|
// console.log("Notification already sent, skipping...");
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm');
|
|
|
|
|
await Tank.updateOne(
|
|
|
|
|