ashok 1 year ago
commit 4b3d47ed4d

@ -1453,7 +1453,30 @@ exports.motorAction = async (req, reply) => {
if (action === "start") {
if (req.body.threshold_type === "time") {
// (Existing code for time-based thresholds)
const receiver_tank_info7 = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_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)
});
await newMotorData.save();
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_time`]: req.body.manual_threshold_time, [`connections.inputConnections.${index}.startTime`]: req.body.startTime,[`connections.inputConnections.${index}.start_instance_id`]: start_instance_id } }
);
}
}
// Start monitoring water level based on threshold time
const thresholdTime = moment().add(req.body.manual_threshold_time, 'minutes').toDate();
@ -1514,7 +1537,52 @@ exports.motorAction = async (req, reply) => {
}
}, 60000);
} else if (req.body.threshold_type === "litres") {
// (Existing code for litres-based thresholds)
console.log("entered litres")
const receiver_tank_info7 = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_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)
});
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 })) {
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 } }
);
}
}
// Start monitoring water level based on litres
const intervalId = setInterval(async () => {

Loading…
Cancel
Save