removed percentages growth and decrease system in start and stop,update tank levels, created desired tank level system,and written bore to overhead

master
varun 3 years ago
parent e980d26523
commit 6af9949a28

@ -154,20 +154,23 @@ exports.updateTanklevels = async (req, reply) => {
let waterLevel = capacity - 100; // initial water level let waterLevel = capacity - 100; // initial water level
const intervalId = setInterval(async function () { const intervalId = setInterval(async function () {
const newWaterLevel = Math.floor(waterLevel / 1.5); const newWaterLevel = Math.floor(waterLevel - 200);
if (newWaterLevel <= 0) {
clearInterval(intervals[tankId]);
console.log(`Stopped updating tank with ID ${tankId}`);
return;
}
else{
const result = await Tank.findOneAndUpdate( const result = await Tank.findOneAndUpdate(
{ _id: tankId }, { _id: tankId },
{ $set: { waterlevel: newWaterLevel } } { $set: { waterlevel: newWaterLevel } }
); );
}
console.log(result); // console.log(result);
if (newWaterLevel === 0) {
clearInterval(intervals[tankId]);
console.log(`Stopped updating tank with ID ${tankId}`);
return;
}
waterLevel = newWaterLevel; waterLevel = newWaterLevel;
}, 2000); }, 2000);
@ -197,14 +200,14 @@ exports.updateTanklevels1 = async (req, reply) => {
let waterLevel = capacity - 100; // initial water level let waterLevel = capacity - 100; // initial water level
const intervalId = setInterval(async function () { const intervalId = setInterval(async function () {
const newWaterLevel = Math.floor(waterLevel / 1.5); const newWaterLevel = Math.floor(waterLevel - 200);
console.log(tank.tankName,newWaterLevel)
const result = await Tank.findOneAndUpdate( const result = await Tank.findOneAndUpdate(
{ customerId, tankName, }, { customerId, tankName, },
{ $set: { waterlevel: newWaterLevel } } { $set: { waterlevel: newWaterLevel } }
); );
console.log(result); // console.log(result);
if (newWaterLevel === 0) { if (newWaterLevel === 0) {
clearInterval(intervals[tankName]); clearInterval(intervals[tankName]);
@ -250,59 +253,64 @@ exports.getTanklevels = async (req, reply) => {
exports.motorAction = async (req, reply) => { exports.motorAction = async (req, reply) => {
try { try {
//let start_time,stop_time
const customerId = req.params.customerId; const customerId = req.params.customerId;
const action = req.body.action const action = req.body.action
const receiver_tank = req.body.to const receiver_tank = req.body.to
const receiver_tank_info = await Tank.findOne({ customerId ,tankName:receiver_tank}); const receiver_tank_info = await Tank.findOne({ customerId ,tankName:receiver_tank});
const receiver_capacity = receiver_tank_info.capacity const receiver_capacity = receiver_tank_info.capacity
const desired_water_percentage = parseInt((req.body.percentage).replace(/,/g, ''), 10)
if(action === "start"){ if(action === "start"){
// start_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'})
supplier_tank = req.body.from supplier_tank = req.body.from
supplier_tank_type = req.body.from_type supplier_tank_type = req.body.from_type
receiver_type = req.body.to_type receiver_type = req.body.to_type
// if(supplier_tank_type==="sump" && receiver_type === "overhead"){ if(supplier_tank_type==="sump" && receiver_type === "overhead"){
// const supplier_tank_info1 = await Tank.findOne({ customerId ,tankName:supplier_tank}); const supplier_tank_info1 = await Tank.findOne({ customerId ,tankName:supplier_tank});
// initial_update = parseInt(supplier_tank_info1.capacity.replace(/,/g, ''), 10)-200; initial_update = parseInt(supplier_tank_info1.capacity.replace(/,/g, ''), 10)-200;
// await Tank.findOneAndUpdate({customerId, tankName: supplier_tank}, { $set: { waterlevel: initial_update } }); await Tank.findOneAndUpdate({customerId, tankName: supplier_tank}, { $set: { waterlevel: initial_update } });
// const supplier_tank_info = await Tank.findOne({ customerId ,tankName:supplier_tank}); const supplier_tank_info = await Tank.findOne({ customerId ,tankName:supplier_tank});
//await changingfrom_tankwaterlevel(customerId,initial_update,supplier_tank_info); // await changingfrom_tankwaterlevel(customerId,initial_update,supplier_tank_info);
// let supplier_waterlevel = parseInt(supplier_tank_info.waterlevel.replace(/,/g, ''), 10) let supplier_waterlevel = parseInt(supplier_tank_info.waterlevel.replace(/,/g, ''), 10)
// let receiver_waterlevel = parseInt(receiver_tank_info.waterlevel.replace(/,/g, ''), 10) let receiver_waterlevel = parseInt(receiver_tank_info.waterlevel.replace(/,/g, ''), 10)
// intervalId = setInterval(async function () { intervalId = setInterval(async function () {
// Calculate new water levels // Calculate new water levels
// const newWaterLevel = receiver_waterlevel + Math.floor(supplier_waterlevel * 0.1); const newWaterLevel = receiver_waterlevel + 200//Math.floor(supplier_waterlevel * 0.1);
// const newSupplierWaterLevel = supplier_waterlevel - Math.floor(supplier_waterlevel * 0.1); const newSupplierWaterLevel = supplier_waterlevel - 200//Math.floor(supplier_waterlevel * 0.1);
// const supplier_capacity = parseInt(supplier_tank_info.capacity.replace(/,/g, ''), 10) const supplier_capacity = parseInt(supplier_tank_info.capacity.replace(/,/g, ''), 10)
// Check if updating should stop // Check if updating should stop
// if ((newSupplierWaterLevel/supplier_capacity)*100 <= 5 || (newWaterLevel/receiver_capacity)*100 >= 95) { if ((newSupplierWaterLevel/supplier_capacity)*100 <= 5 || (newWaterLevel/receiver_capacity)*100 >= 95 || (newWaterLevel/receiver_capacity)*100 >= desired_water_percentage) {
// clearInterval(intervalId) clearInterval(intervalId)
// console.log("end"); console.log("end");
// } else { } else {
// Update water levels in database // Update water levels in database
// supplier_waterlevel = newSupplierWaterLevel; supplier_waterlevel = newSupplierWaterLevel;
// receiver_waterlevel = newWaterLevel; receiver_waterlevel = newWaterLevel;
// console.log((newSupplierWaterLevel/supplier_capacity)*100) console.log((newSupplierWaterLevel/supplier_capacity)*100)
// console.log((newWaterLevel/receiver_capacity)*100) console.log((newWaterLevel/receiver_capacity)*100)
// await Promise.all([ await Promise.all([
// Tank.findOneAndUpdate({customerId, tankName: receiver_tank}, { $set: { waterlevel: newWaterLevel } }), Tank.findOneAndUpdate({customerId, tankName: receiver_tank}, { $set: { waterlevel: newWaterLevel } }),
// Tank.findOneAndUpdate({customerId, tankName: supplier_tank}, { $set: { waterlevel: newSupplierWaterLevel } }) Tank.findOneAndUpdate({customerId, tankName: supplier_tank}, { $set: { waterlevel: newSupplierWaterLevel } })
// ]); ]);
// } }
//}, 2000); }, 2000);
// } }
if(supplier_tank_type==="bore" && receiver_type === "sump"){ if(supplier_tank_type==="bore" && receiver_type === "sump"){
const receiver_capacity = receiver_tank_info.capacity const receiver_capacity = receiver_tank_info.capacity
let receiver_waterlevel = parseInt(receiver_tank_info.waterlevel.replace(/,/g, ''), 10) let receiver_waterlevel = parseInt(receiver_tank_info.waterlevel.replace(/,/g, ''), 10)
intervalId = setInterval(async function () { intervalId = setInterval(async function () {
// Calculate new water levels // Calculate new water levels
const newWaterLevel = receiver_waterlevel+225; const newWaterLevel = receiver_waterlevel+200;
// Check if updating should stop // Check if updating should stop
if ((newWaterLevel/receiver_capacity)*100 >= 97) { if ((newWaterLevel/receiver_capacity)*100 >= 97 || (newWaterLevel/receiver_capacity)*100 >= desired_water_percentage ) {
clearInterval(intervalId) clearInterval(intervalId)
console.log("end"); console.log("end");
} else { } else {
@ -321,10 +329,10 @@ exports.motorAction = async (req, reply) => {
if(supplier_tank_type==="sump" && receiver_type === "overhead"){ if(supplier_tank_type==="bore" && receiver_type === "overhead"){
const supplier_tank_info1 = await Tank.findOne({ customerId ,tankName:supplier_tank}); const supplier_tank_info1 = await Tank.findOne({ customerId ,tankName:supplier_tank});
initial_update = parseInt(supplier_tank_info1.capacity.replace(/,/g, ''), 10)/2; // initial_update = parseInt(supplier_tank_info1.capacity.replace(/,/g, ''), 10)/2;
await Tank.findOneAndUpdate({customerId, tankName: supplier_tank}, { $set: { waterlevel: initial_update } }); // await Tank.findOneAndUpdate({customerId, tankName: supplier_tank}, { $set: { waterlevel: initial_update } });
const supplier_tank_info = await Tank.findOne({ customerId ,tankName:supplier_tank}); const supplier_tank_info = await Tank.findOne({ customerId ,tankName:supplier_tank});
let supplier_waterlevel = parseInt(supplier_tank_info.waterlevel.replace(/,/g, ''), 10) let supplier_waterlevel = parseInt(supplier_tank_info.waterlevel.replace(/,/g, ''), 10)
@ -332,19 +340,21 @@ exports.motorAction = async (req, reply) => {
intervalId = setInterval(async function () { intervalId = setInterval(async function () {
const supplier_capacity = parseInt(supplier_tank_info.capacity.replace(/,/g, ''), 10) const supplier_capacity = parseInt(supplier_tank_info.capacity.replace(/,/g, ''), 10)
// Calculate new water levels // Calculate new water levels
const newWaterLevel = receiver_waterlevel + Math.floor(supplier_waterlevel * 0.1); const newWaterLevel = receiver_waterlevel + 200//Math.floor(supplier_waterlevel * 0.1);
const newSupplierWaterLevel = Math.min(supplier_capacity, supplier_waterlevel + Math.floor(supplier_waterlevel * 0.15)); const newSupplierWaterLevel = Math.min(supplier_capacity, supplier_waterlevel + 350);// Math.floor(supplier_waterlevel * 0.15));
console.log(newWaterLevel)
console.log(newSupplierWaterLevel)
// Check if updating should stop // Check if updating should stop
if ((newSupplierWaterLevel/supplier_capacity)*100 <= 5 || (newWaterLevel/receiver_capacity)*100 >= 95) { if ( (newWaterLevel/receiver_capacity)*100 >= 95 || (newWaterLevel/receiver_capacity)*100 >= desired_water_percentage ) {
clearInterval(intervalId) clearInterval(intervalId)
console.log("end"); console.log("end");
} else { } else {
// Update water levels in database // Update water levels in database
supplier_waterlevel = newSupplierWaterLevel; supplier_waterlevel = newSupplierWaterLevel;
receiver_waterlevel = newWaterLevel; receiver_waterlevel = newWaterLevel;
console.log((newSupplierWaterLevel/supplier_capacity)*100) // console.log((newSupplierWaterLevel/supplier_capacity)*100)
console.log((newWaterLevel/receiver_capacity)*100) // console.log((newWaterLevel/receiver_capacity)*100)
await Promise.all([ await Promise.all([
Tank.findOneAndUpdate({customerId, tankName: receiver_tank}, { $set: { waterlevel: newWaterLevel } }), Tank.findOneAndUpdate({customerId, tankName: receiver_tank}, { $set: { waterlevel: newWaterLevel } }),
Tank.findOneAndUpdate({customerId, tankName: supplier_tank}, { $set: { waterlevel: newSupplierWaterLevel } }) Tank.findOneAndUpdate({customerId, tankName: supplier_tank}, { $set: { waterlevel: newSupplierWaterLevel } })
@ -354,16 +364,22 @@ exports.motorAction = async (req, reply) => {
} }
// reply.send({ status_code: 200, "start time": start_time});
//console.log(start_time)
} }
else if (action === "stop") { else if (action === "stop") {
// stop_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'})
// console.log(stop_time)
clearInterval(intervalId); clearInterval(intervalId);
// reply.send({ status_code: 200, "stop time": stop_time});
} else { } else {
throw new Error("Invalid action"); throw new Error("Invalid action");
} }
return { message: 'Water level updates started' }; return { message: 'Water level updates started' };
} catch (err) { } catch (err) {
throw new Error(`Failed to start/stop water level updates: ${err.message}`); throw new Error(`Failed to start/stop water level updates: ${err.message}`);

@ -231,6 +231,7 @@ module.exports = function (fastify, opts, next) {
from_type: { type: "string" }, from_type: { type: "string" },
to_type: { type: "string" }, to_type: { type: "string" },
action: { type: "string" }, action: { type: "string" },
percentage: { type: "string" },
}, },
}, },

Loading…
Cancel
Save