|
|
|
@ -153,8 +153,6 @@ exports.updateTanklevels = async (req, reply) => {
|
|
|
|
|
const tankName = tank.tankName;
|
|
|
|
|
let capacity = parseInt(tank.capacity.replace(/,/g, ''), 10);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let waterLevel = capacity - 100; // initial water level
|
|
|
|
|
const intervalId = setInterval(async function () {
|
|
|
|
|
const newWaterLevel = Math.floor(waterLevel / 1.5);
|
|
|
|
@ -209,8 +207,135 @@ exports.getTanklevels = async (req, reply) => {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const changingfrom_tankwaterlevel = async (customerId,initial_update,supplier_tank_info) => {
|
|
|
|
|
//console.log(customerId,"0")
|
|
|
|
|
//console.log(update_level,"1")
|
|
|
|
|
//console.log(fromSump,"2")
|
|
|
|
|
|
|
|
|
|
var result = await Tank.findOneAndUpdate(
|
|
|
|
|
{customerId:customerId, tankName: supplier_tank_info.tankName}, { $set: { waterlevel: initial_update } }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return result.waterlevel;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.motorAction1 = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const action = req.body.action
|
|
|
|
|
let from, to_capacity, sump_capacity, update_level, intervalId;
|
|
|
|
|
|
|
|
|
|
if (action === "start") {
|
|
|
|
|
const customerId = req.params.customerId;
|
|
|
|
|
const to = req.body.to
|
|
|
|
|
const from_type = req.body.from_type
|
|
|
|
|
|
|
|
|
|
if (from_type === "sump") {
|
|
|
|
|
from = req.body.from
|
|
|
|
|
const [tankInfo, fromSump] = await Promise.all([
|
|
|
|
|
Tank.findOne({customerId, tankName: to}),
|
|
|
|
|
Tank.findOne({customerId, tankName: from})
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
to_capacity = parseInt(tankInfo.capacity.replace(/,/g, ''), 10);
|
|
|
|
|
sump_capacity = parseInt(fromSump.capacity.replace(/,/g, ''), 10);
|
|
|
|
|
update_level = sump_capacity - 200
|
|
|
|
|
console.log(to_capacity,sump_capacity,update_level)
|
|
|
|
|
var b_id = await changingfrom_tankwaterlevel(customerId,update_level,fromSump)
|
|
|
|
|
//await Tank.findOneAndUpdate({customerId, tankName: from}, { $set: { waterlevel: update_level } })
|
|
|
|
|
|
|
|
|
|
intervalId = setInterval(async function () {
|
|
|
|
|
let sumpWaterLevel = fromSump.waterlevel
|
|
|
|
|
let toWaterLevel = tankInfo.waterlevel
|
|
|
|
|
console.log( Math.floor(sumpWaterLevel * 0.10),"0")
|
|
|
|
|
console.log(toWaterLevel,"1")
|
|
|
|
|
|
|
|
|
|
//const [sumpWaterLevel, toWaterLevel] = await Promise.all([
|
|
|
|
|
|
|
|
|
|
// tankInfo.waterlevel
|
|
|
|
|
//]);
|
|
|
|
|
|
|
|
|
|
const newWaterLevel =parseInt(toWaterLevel.replace(/,/g, ''), 10) + Math.floor(sumpWaterLevel * 0.1);
|
|
|
|
|
const newSumpWaterLevel = sumpWaterLevel - Math.floor(sumpWaterLevel * 0.1)
|
|
|
|
|
console.log(newWaterLevel,"3")
|
|
|
|
|
console.log(newSumpWaterLevel,"4")
|
|
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
|
Tank.findOneAndUpdate({customerId, tankName: to}, { $set: { waterlevel: newWaterLevel } }),
|
|
|
|
|
Tank.findOneAndUpdate({customerId, tankName: from}, { $set: { waterlevel: newSumpWaterLevel } })
|
|
|
|
|
]);
|
|
|
|
|
}, 2000);
|
|
|
|
|
}
|
|
|
|
|
} else if (action === "stop") {
|
|
|
|
|
clearInterval(intervalId);
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error("Invalid action");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { message: 'Water level updates started' };
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw new Error(`Failed to start/stop water level updates: ${err.message}`);
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.motorAction = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const customerId = req.params.customerId;
|
|
|
|
|
const action = req.body.action
|
|
|
|
|
const receiver_tank = req.body.to
|
|
|
|
|
const receiver_tank_info = await Tank.findOne({ customerId ,tankName:receiver_tank});
|
|
|
|
|
const receiver_capacity = receiver_tank_info.capacity
|
|
|
|
|
|
|
|
|
|
if(action === "start"){
|
|
|
|
|
supplier_tank = req.body.from
|
|
|
|
|
supplier_tank_type = req.body.from_type
|
|
|
|
|
|
|
|
|
|
if(supplier_tank_type==="sump"){
|
|
|
|
|
const supplier_tank_info = await Tank.findOne({ customerId ,tankName:supplier_tank});
|
|
|
|
|
initial_update = parseInt(supplier_tank_info.capacity.replace(/,/g, ''), 10)-200;
|
|
|
|
|
await changingfrom_tankwaterlevel(customerId,initial_update,supplier_tank_info);
|
|
|
|
|
let supplier_waterlevel = parseInt(supplier_tank_info.waterlevel.replace(/,/g, ''), 10)
|
|
|
|
|
let receiver_waterlevel = parseInt(receiver_tank_info.waterlevel.replace(/,/g, ''), 10)
|
|
|
|
|
intervalId = setInterval(async function () {
|
|
|
|
|
// Calculate new water levels
|
|
|
|
|
const newWaterLevel = receiver_waterlevel + Math.floor(supplier_waterlevel * 0.1);
|
|
|
|
|
const newSupplierWaterLevel = supplier_waterlevel - Math.floor(supplier_waterlevel * 0.1);
|
|
|
|
|
|
|
|
|
|
// Check if updating should stop
|
|
|
|
|
if (newSupplierWaterLevel <= 0 || newWaterLevel >= receiver_capacity) {
|
|
|
|
|
clearInterval(intervalId);
|
|
|
|
|
} else {
|
|
|
|
|
// Update water levels in database
|
|
|
|
|
supplier_waterlevel = newSupplierWaterLevel;
|
|
|
|
|
receiver_waterlevel = newWaterLevel;
|
|
|
|
|
await Promise.all([
|
|
|
|
|
Tank.findOneAndUpdate({customerId, tankName: receiver_tank}, { $set: { waterlevel: newWaterLevel } }),
|
|
|
|
|
Tank.findOneAndUpdate({customerId, tankName: supplier_tank}, { $set: { waterlevel: newSupplierWaterLevel } })
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}, 2000);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else if (action === "stop") {
|
|
|
|
|
clearInterval(intervalId);
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error("Invalid action");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { message: 'Water level updates started' };
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw new Error(`Failed to start/stop water level updates: ${err.message}`);
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|