changes in motoraction

master^2
Varun 9 months ago
parent 40eae962e0
commit 88a9ca0501

@ -2239,13 +2239,14 @@ console.log(fcmToken)
// Perform stop operations in the background // Perform stop operations in the background
(async () => { (async () => {
await delay(300000);
const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id: start_instance_id }); const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id: start_instance_id });
if (motorData) { if (motorData) {
const supplierTank = await Tank.findOne({ customerId, tankName: motorData.supplierTank, tankLocation: motorData.supplier_type.toLowerCase() });
const supplierFinalWaterLevel = parseInt(supplierTank.waterlevel, 10);
const receiverTank = await Tank.findOne({ customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() }); const receiverTank = await Tank.findOne({ customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() });
const receiverFinalWaterLevel = parseInt(receiverTank.waterlevel, 10); const receiverFinalWaterLevel = parseInt(receiverTank.waterlevel, 10);
const quantityDelivered = receiverFinalWaterLevel - parseInt(motorData.receiverInitialwaterlevel, 10); const quantityDelivered = parseInt(motorData.supplierInitialwaterlevel, 10)-supplierFinalWaterLevel;
const water_pumped_till_now = parseInt(receiverTank.total_water_added_from_midnight, 10); const water_pumped_till_now = parseInt(receiverTank.total_water_added_from_midnight, 10);
const totalwaterpumped = quantityDelivered + water_pumped_till_now; const totalwaterpumped = quantityDelivered + water_pumped_till_now;
@ -2260,6 +2261,7 @@ console.log(fcmToken)
$set: { $set: {
stopTime: req.body.stopTime, stopTime: req.body.stopTime,
receiverfinalwaterlevel: receiverFinalWaterLevel.toString(), receiverfinalwaterlevel: receiverFinalWaterLevel.toString(),
supplierfinalwaterlevel:supplierFinalWaterLevel.toString(),
quantity_delivered: quantityDelivered.toString() quantity_delivered: quantityDelivered.toString()
} }
} }
@ -2280,6 +2282,7 @@ console.log(fcmToken)
if (req.body.threshold_type === "time") { if (req.body.threshold_type === "time") {
// Create a new MotorData entry // Create a new MotorData entry
const receiverTank = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() }); const receiverTank = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() });
const supplierTank = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() });
const newMotorData = new MotorData({ const newMotorData = new MotorData({
customerId, customerId,
motor_id: motorId, motor_id: motorId,
@ -2289,6 +2292,7 @@ console.log(fcmToken)
supplier_type: req.body.from_type, supplier_type: req.body.from_type,
receiver_type: req.body.to_type, receiver_type: req.body.to_type,
startTime: req.body.startTime, startTime: req.body.startTime,
supplierInitialwaterlevel: parseInt(supplierTank.waterlevel, 10),
receiverInitialwaterlevel: parseInt(receiverTank.waterlevel, 10) receiverInitialwaterlevel: parseInt(receiverTank.waterlevel, 10)
}); });
await newMotorData.save(); await newMotorData.save();
@ -2341,14 +2345,15 @@ console.log(fcmToken)
delete motorIntervals[motorId]; delete motorIntervals[motorId];
this.publishMotorStopStatus(motorId, "1"); this.publishMotorStopStatus(motorId, "1");
await delay(300000);
const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id: start_instance_id }); const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id: start_instance_id });
if (motorData) { if (motorData) {
const receiverTank = await Tank.findOne({ customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() }); const supplierTank = await Tank.findOne({ customerId, tankName: motorData.supplierTank, tankLocation: motorData.supplier_type.toLowerCase() });
const receiverFinalWaterLevel = parseInt(receiverTank.waterlevel, 10); const supplierFinalWaterLevel = parseInt(supplierTank.waterlevel, 10);
const quantityDelivered = receiverFinalWaterLevel - parseInt(motorData.receiverInitialwaterlevel, 10); const quantityDelivered = parseInt(motorData.supplierInitialwaterlevel, 10)-supplierFinalWaterLevel;
const water_pumped_till_now = parseInt(receiverTank.total_water_added_from_midnight, 10); const water_pumped_till_now = parseInt(receiverTank.total_water_added_from_midnight, 10);
const totalwaterpumped = quantityDelivered + water_pumped_till_now; const totalwaterpumped = quantityDelivered + water_pumped_till_now;
await Tank.findOneAndUpdate( await Tank.findOneAndUpdate(
{ customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() }, { customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() },
@ -2360,6 +2365,7 @@ console.log(fcmToken)
{ {
$set: { $set: {
stopTime: req.body.stopTime, stopTime: req.body.stopTime,
supplierfinalwaterlevel: supplierFinalWaterLevel.toString(),
receiverfinalwaterlevel: receiverFinalWaterLevel.toString(), receiverfinalwaterlevel: receiverFinalWaterLevel.toString(),
quantity_delivered: quantityDelivered.toString() quantity_delivered: quantityDelivered.toString()
} }
@ -2375,7 +2381,113 @@ console.log(fcmToken)
}, 30000); // Check every minute }, 30000); // Check every minute
} }
}else if (req.body.threshold_type === "litres") {
console.log("entered litres")
const receiver_tank_info7 = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() });
const supplier_tank_info7 = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() });
const newMotorData = new MotorData({
customerId: customerId,
motor_id: motorId,
start_instance_id: start_instance_id,
supplierTank: req.body.from,
receiverTank: req.body.to,
supplier_type: req.body.from_type,
receiver_type: req.body.to_type,
startTime: req.body.startTime,
receiverInitialwaterlevel:parseInt(receiver_tank_info7.waterlevel, 10),
supplierInitialwaterlevel:parseInt(supplier_tank_info7.waterlevel, 10)
});
await newMotorData.save();
// If threshold type is percentage, calculate percentage threshold
const receiver_tank_info = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() });
const supplier_tank_info = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() });
if (!receiver_tank_info) {
throw new Error("Receiver tank not found.");
}
if (!supplier_tank_info) {
throw new Error("Supplierr tank not found.");
}
const supplier_capacity = parseInt(supplier_tank_info.capacity, 10);
const supplier_waterLevel = parseInt(supplier_tank_info.waterlevel, 10);
const capacity = parseInt(receiver_tank_info.capacity, 10);
const waterLevel = parseInt(receiver_tank_info.waterlevel, 10);
const desired_percentage = parseInt(req.body.manual_threshold_litres.replace(/,/g, ''), 10);
console.log(desired_percentage)
const threshold_water_level = waterLevel+desired_percentage;
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);
if (index !== -1) {
await Tank.updateOne(
{ customerId, "connections.inputConnections.motor_id": motorId },
{ $set: { [`connections.inputConnections.${index}.manual_threshold_percentage`]: supplier_threshold.toString(), [`connections.inputConnections.${index}.startTime`]: req.body.startTime } }
);
}
}
// Update water level threshold
// Start monitoring water level based on threshold percentage
motorIntervals[motorId] = setInterval(async () => {
// Check if water level has reached the threshold percentage
const supplier_tank_info1 = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() });
const current_water_level = parseInt(supplier_tank_info1.waterlevel, 10);
if (current_water_level <= supplier_threshold) {
// Stop the motor pump
await Tank.updateOne(
{ customerId, "connections.inputConnections.motor_id": motorId },
{
$set: {
"connections.inputConnections.$.motor_stop_status": "1",
"connections.inputConnections.$.threshold_type": null,
"connections.inputConnections.$.manual_threshold_time": null,
"connections.inputConnections.$.manual_threshold_percentage": null
}
}
);
clearInterval(motorIntervals[motorId]); // Clear interval
delete motorIntervals[motorId];
this.publishMotorStopStatus(motorId, "1");
await delay(300000);
const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id: start_instance_id });
if (motorData) {
const receiverTank = await Tank.findOne({ customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() });
const receiverFinalWaterLevel = parseInt(receiverTank.waterlevel, 10);
const quantityDelivered = receiverFinalWaterLevel - parseInt(motorData.receiverInitialwaterlevel, 10);
const stopTime = formatDate(new Date());
await MotorData.updateOne(
{ customerId, motor_id: motorId, start_instance_id: start_instance_id },
{
$set: {
stopTime:stopTime,
receiverfinalwaterlevel: receiverFinalWaterLevel.toString(),
quantity_delivered: quantityDelivered.toString()
}
}
);
}
}
}, 20000);
} }
}
} }
// Respond with success message // Respond with success message
reply.code(200).send({ message: `Motor ${action === "start" ? "started" : "stopped"} successfully.` }); reply.code(200).send({ message: `Motor ${action === "start" ? "started" : "stopped"} successfully.` });

@ -116,14 +116,16 @@ const motordataSchema = new mongoose.Schema({
start_instance_id:{type:String,default:null}, start_instance_id:{type:String,default:null},
supplierTank: { type: String, default: null }, supplierTank: { type: String, default: null },
receiverTank: { type: String, default: null }, receiverTank: { type: String, default: null },
receiverInitialwaterlevel: { type: String, default: "0" }, supplierInitialwaterlevel: { type: String, default: "0" },
receiverfinalwaterlevel: { type: String, default: "0" }, supplierfinalwaterlevel: { type: String, default: "0" },
startTime: { type: String, default: null }, startTime: { type: String, default: null },
stopTime: { type: String, default: null }, stopTime: { type: String, default: null },
runtime:{type:String, default:"0"}, runtime:{type:String, default:"0"},
supplier_type: { type: String, default: null }, supplier_type: { type: String, default: null },
receiver_type: { type: String, default: null }, receiver_type: { type: String, default: null },
quantity_delivered:{ type: String, default: null }, quantity_delivered:{ type: String, default: null },
receiverInitialwaterlevel: { type: String, default: "0" },
receiverfinalwaterlevel: { type: String, default: "0" },
}); });

Loading…
Cancel
Save