|
|
|
@ -2874,7 +2874,6 @@ async function calculateTotalPumpedWater(customerId, motorId, start_instance_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.motorAction = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { customerId } = req.params;
|
|
|
|
@ -2900,7 +2899,7 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
|
let motorStopStatus = action === "start" ? "2" : "1";
|
|
|
|
|
const blockName = req.body.from || "Unknown Block";
|
|
|
|
|
const tankName = req.body.to || "Unknown Tank";
|
|
|
|
|
|
|
|
|
|
const stopTime = req.body.stopTime
|
|
|
|
|
if (action === "start") {
|
|
|
|
|
|
|
|
|
|
if (motorIntervals[motorId]) {
|
|
|
|
@ -2983,7 +2982,45 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
|
console.log(thresholdTime,"thresholdTime")
|
|
|
|
|
console.log("motor stopping because it entered this condition")
|
|
|
|
|
// Emit the threshold time notification
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
console.log("motor stopping because it entered this condition")
|
|
|
|
|
|
|
|
|
|
const tank = await Tank.findOne(
|
|
|
|
|
{ customerId, "connections.inputConnections.motor_id": motorId },
|
|
|
|
|
{ "connections.inputConnections.$": 1 } // Fetch only relevant motor connection
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (tank && tank.connections.inputConnections[0].motor_stop_status === "1") {
|
|
|
|
|
console.log("⚠️ Motor already stopped. Skipping notification.");
|
|
|
|
|
} else {
|
|
|
|
|
console.log("🚀 Sending threshold time notification...");
|
|
|
|
|
|
|
|
|
|
eventEmitter.emit(
|
|
|
|
|
"sendThresholdTimeNotification",
|
|
|
|
|
customerId,
|
|
|
|
|
fcmToken,
|
|
|
|
|
manual_threshold_time,
|
|
|
|
|
motorId,
|
|
|
|
|
tankName,
|
|
|
|
|
blockName
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
// eventEmitter.emit(
|
|
|
|
|
// "sendThresholdTimeNotification",
|
|
|
|
|
// customerId,
|
|
|
|
|
// fcmToken,
|
|
|
|
|
// manual_threshold_time,
|
|
|
|
|
// motorId,
|
|
|
|
|
// tankName,
|
|
|
|
|
// blockName
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
reply.code(200).send({ message: "Motor stopped successfully." });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error in handleMotorStop:", error);
|
|
|
|
|
reply.code(500).send({ error: "Internal Server Error" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// eventEmitter.emit(
|
|
|
|
|
// "sendThresholdTimeNotification",
|
|
|
|
|
// customerId,
|
|
|
|
@ -2994,32 +3031,32 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
|
// blockName
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
const notificationKey = `${customerId}_${motorId}_threshold`;
|
|
|
|
|
// const notificationKey = `${customerId}_${motorId}_threshold`;
|
|
|
|
|
|
|
|
|
|
// Check if the notification has already been sent
|
|
|
|
|
if (!notificationTracker.get(notificationKey)) {
|
|
|
|
|
console.log("Sending threshold time notification...");
|
|
|
|
|
// // Check if the notification has already been sent
|
|
|
|
|
// 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(
|
|
|
|
@ -3032,8 +3069,6 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
|
"connections.inputConnections.$.manual_threshold_time": null,
|
|
|
|
|
"connections.inputConnections.$.manual_threshold_percentage": null,
|
|
|
|
|
"connections.inputConnections.$.stopTime": currentTime,
|
|
|
|
|
"connections.inputConnections.$.start_instance_id": null,
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
@ -3059,19 +3094,11 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
|
console.log(motorData,"motorData")
|
|
|
|
|
if (motorData) {
|
|
|
|
|
console.log("got into if")
|
|
|
|
|
|
|
|
|
|
const receiverTank = await Tank.findOne({ customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() });
|
|
|
|
|
const receiverFinalWaterLevel = parseInt(receiverTank.waterlevel, 10);
|
|
|
|
|
console.log(receiverFinalWaterLevel,"receiverFinalWaterLevel")
|
|
|
|
|
const quantityDelivered = receiverFinalWaterLevel - parseInt(motorData.receiverInitialwaterlevel, 10);
|
|
|
|
|
console.log(quantityDelivered,"quantityDelivered")
|
|
|
|
|
const water_pumped_till_now = parseInt(receiverTank.total_water_added_from_midnight, 10);
|
|
|
|
|
const totalwaterpumped = quantityDelivered + water_pumped_till_now;
|
|
|
|
|
const start = moment(motorData.startTime, 'DD-MMM-YYYY - HH:mm');
|
|
|
|
|
const stop = moment(currentTime, 'DD-MMM-YYYY - HH:mm');
|
|
|
|
|
const duration = moment.duration(stop.diff(start));
|
|
|
|
|
const runtime = Math.floor(duration.asMinutes()); // runtime in minutes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await Tank.findOneAndUpdate(
|
|
|
|
|
{ customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() },
|
|
|
|
@ -3084,8 +3111,7 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
|
$set: {
|
|
|
|
|
stopTime: currentTime,
|
|
|
|
|
receiverfinalwaterlevel: receiverFinalWaterLevel.toString(),
|
|
|
|
|
quantity_delivered: quantityDelivered.toString(),
|
|
|
|
|
runtime: runtime.toString()
|
|
|
|
|
quantity_delivered: quantityDelivered.toString()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
@ -3141,7 +3167,7 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
const supplier_threshold = supplier_waterLevel-desired_percentage
|
|
|
|
|
console.log(supplier_threshold,"supplier_threshold")
|
|
|
|
|
|
|
|
|
|
for await (const tank of Tank.find({ "connections.inputConnections.motor_id": motorId })) {
|
|
|
|
|
this.publishMotorStopStatus(motorId, motorStopStatus);
|
|
|
|
|
for await (const tank of Tank.find({ "connections.inputConnections.motor_id": motorId })) {
|
|
|
|
|
const index = tank.connections.inputConnections.findIndex(connection => connection.motor_id === motorId);
|
|
|
|
@ -3205,12 +3231,25 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, 20000);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else if (action === "stop") {
|
|
|
|
|
await stopMotor(motorId, customerId, start_instance_id);
|
|
|
|
|
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" });
|
|
|
|
|
}
|
|
|
|
|
this.publishMotorStopStatus(motorId, motorStopStatus);
|
|
|
|
|
reply.code(200).send({ message: "Motor stopped successfully." });
|
|
|
|
|
}
|
|
|
|
@ -3238,15 +3277,12 @@ async function stopMotor(motorId, customerId, start_instance_id) {
|
|
|
|
|
delete motorIntervals[motorId];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
eventEmitter.emit("motorStop", customerId, [], "", "", currentTime, "Mobile APP", 0, "", motorId, "");
|
|
|
|
|
// eventEmitter.emit("motorStop", customerId, [], "", "", currentTime, "Mobile APP", 0, "", motorId, "");
|
|
|
|
|
|
|
|
|
|
const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id });
|
|
|
|
|
if (motorData) {
|
|
|
|
|
const start = moment(motorData.startTime, 'DD-MMM-YYYY - HH:mm');
|
|
|
|
|
const stop = moment(currentTime, 'DD-MMM-YYYY - HH:mm');
|
|
|
|
|
const duration = moment.duration(stop.diff(start));
|
|
|
|
|
const runtime = Math.floor(duration.asMinutes()); // runtime in minutes
|
|
|
|
|
|
|
|
|
|
const startTime = moment(motorData.startTime, 'DD-MMM-YYYY - HH:mm');
|
|
|
|
|
const runtime = moment.duration(moment(currentTime, 'DD-MMM-YYYY - HH:mm').diff(startTime)).asSeconds();
|
|
|
|
|
|
|
|
|
|
const receiverTank = await Tank.findOne({ customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() });
|
|
|
|
|
const receiverFinalWaterLevel = parseInt(receiverTank.waterlevel.replace(/,/g, ''), 10);
|
|
|
|
@ -3262,6 +3298,7 @@ async function stopMotor(motorId, customerId, start_instance_id) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const monitorWaterLevels = async () => {
|
|
|
|
|
try {
|
|
|
|
|
// console.log("⏳ Monitoring water levels...");
|
|
|
|
|