|
|
@ -121,6 +121,8 @@ exports.getTank = async (req, reply) => {
|
|
|
|
throw boom.boomify(err);
|
|
|
|
throw boom.boomify(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//exports.getTanklevels = async (req, reply) => {
|
|
|
|
//exports.getTanklevels = async (req, reply) => {
|
|
|
|
// try {
|
|
|
|
// try {
|
|
|
|
// const customerId = req.params.customerId;
|
|
|
|
// const customerId = req.params.customerId;
|
|
|
@ -150,7 +152,7 @@ exports.updateTanklevels = async (req, reply) => {
|
|
|
|
for (const tank of tanks) {
|
|
|
|
for (const tank of tanks) {
|
|
|
|
const tankId = tank._id;
|
|
|
|
const tankId = tank._id;
|
|
|
|
let capacity = parseInt(tank.capacity.replace(/,/g, ''), 10);
|
|
|
|
let capacity = parseInt(tank.capacity.replace(/,/g, ''), 10);
|
|
|
|
let waterLevel = capacity - 100; // initial water level
|
|
|
|
let waterLevel = parseInt(tank.waterlevel.replace(/,/g, ''), 10);
|
|
|
|
|
|
|
|
|
|
|
|
const intervalId = setInterval(async function () {
|
|
|
|
const intervalId = setInterval(async function () {
|
|
|
|
const newWaterLevel = Math.floor(waterLevel - 200);
|
|
|
|
const newWaterLevel = Math.floor(waterLevel - 200);
|
|
|
@ -197,17 +199,17 @@ exports.updateTanklevels1 = async (req, reply) => {
|
|
|
|
const tank_type = tank.tankLocation
|
|
|
|
const tank_type = tank.tankLocation
|
|
|
|
let capacity = parseInt(tank.capacity.replace(/,/g, ''), 10);
|
|
|
|
let capacity = parseInt(tank.capacity.replace(/,/g, ''), 10);
|
|
|
|
|
|
|
|
|
|
|
|
let waterLevel = capacity - 100; // initial water level
|
|
|
|
const waterLevel = parseInt(tank.waterLevel.replace(/,/g, ''), 10);
|
|
|
|
|
|
|
|
//capacity - 100; // initial water level
|
|
|
|
const intervalId = setInterval(async function () {
|
|
|
|
const intervalId = setInterval(async function () {
|
|
|
|
|
|
|
|
const motor_status = tank.motor_status
|
|
|
|
|
|
|
|
if(motor_status === "0"){
|
|
|
|
const newWaterLevel = Math.floor(waterLevel - 200);
|
|
|
|
const newWaterLevel = Math.floor(waterLevel - 200);
|
|
|
|
console.log(tank.tankName,newWaterLevel)
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (newWaterLevel === 0) {
|
|
|
|
if (newWaterLevel === 0) {
|
|
|
|
clearInterval(intervals[tankName]);
|
|
|
|
clearInterval(intervals[tankName]);
|
|
|
|
console.log(`Stopped updating ${tankName}`);
|
|
|
|
console.log(`Stopped updating ${tankName}`);
|
|
|
@ -215,6 +217,31 @@ exports.updateTanklevels1 = async (req, reply) => {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
waterLevel = newWaterLevel;
|
|
|
|
waterLevel = newWaterLevel;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else{
|
|
|
|
|
|
|
|
if(tank.tankLocation==="overhead",motor_status==="1"){
|
|
|
|
|
|
|
|
tank_waterlevel = math.floor(waterLevel + 250)
|
|
|
|
|
|
|
|
const supplier_water= await findOne({customerId:req.params.customerId,tankName:req.body.from,tankLocation:req.body.from_type})
|
|
|
|
|
|
|
|
const supplier_waterlevel = parseInt(supplier_water.waterlevel .replace(/,/g, ''), 10);
|
|
|
|
|
|
|
|
const newSupplierWaterLevel = math.floor(supplier_waterlevel - 250)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await Tank.findOneAndUpdate({customerId:req.params.customerId,tankName:req.body.from,tankLocation:req.body.from_type}, { $set: { waterlevel: newSupplierWaterLevel } })
|
|
|
|
|
|
|
|
await tank.save()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(tank.tankLocation==="sump",motor_status==="1"){
|
|
|
|
|
|
|
|
const sumpwaterlevel = math.floor(waterLevel + 250)
|
|
|
|
|
|
|
|
await tank.save()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// console.log(result);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}, 2000);
|
|
|
|
}, 2000);
|
|
|
|
|
|
|
|
|
|
|
|
intervals[tankName] = intervalId;
|
|
|
|
intervals[tankName] = intervalId;
|
|
|
@ -247,8 +274,6 @@ exports.getTanklevels = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.motorAction = async (req, reply) => {
|
|
|
|
exports.motorAction = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
//let start_time,stop_time
|
|
|
|
//let start_time,stop_time
|
|
|
@ -258,14 +283,15 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
const receiver_tank_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:(req.body.to_type).toLowerCase()});
|
|
|
|
const receiver_tank_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:(req.body.to_type).toLowerCase()});
|
|
|
|
const receiver_capacity = parseInt((receiver_tank_info.capacity).replace(/,/g, ''), 10)
|
|
|
|
const receiver_capacity = parseInt((receiver_tank_info.capacity).replace(/,/g, ''), 10)
|
|
|
|
const desired_water_percentage = parseInt((req.body.percentage).replace(/,/g, ''), 10)
|
|
|
|
const desired_water_percentage = parseInt((req.body.percentage).replace(/,/g, ''), 10)
|
|
|
|
|
|
|
|
const intervals = {};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(action === "start"){
|
|
|
|
if(action === "start"){
|
|
|
|
// start_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'})
|
|
|
|
start_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'})
|
|
|
|
|
|
|
|
|
|
|
|
supplier_tank = req.body.from
|
|
|
|
const supplier_tank = req.body.from
|
|
|
|
supplier_tank_type = (req.body.from_type).toLowerCase()
|
|
|
|
const supplier_tank_type = (req.body.from_type).toLowerCase()
|
|
|
|
receiver_type = (req.body.to_type).toLowerCase()
|
|
|
|
const receiver_type = (req.body.to_type).toLowerCase()
|
|
|
|
console.log(supplier_tank)
|
|
|
|
console.log(supplier_tank)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -276,14 +302,15 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
const supplier_tank_info1 = await Tank.findOne({ customerId ,tankName:supplier_tank,tankLocation:supplier_tank_type});
|
|
|
|
const supplier_tank_info1 = await Tank.findOne({ customerId ,tankName:supplier_tank,tankLocation:supplier_tank_type});
|
|
|
|
console.log(supplier_tank_info1)
|
|
|
|
console.log(supplier_tank_info1)
|
|
|
|
initial_update = parseInt(supplier_tank_info1.capacity.replace(/,/g, ''), 10)-200;
|
|
|
|
//const initial_update = parseInt(supplier_tank_info1.waterlevel.replace(/,/g, ''), 10)-200;
|
|
|
|
await Tank.findOneAndUpdate({customerId, tankName: supplier_tank,tankLocation:supplier_tank_type}, { $set: { waterlevel: initial_update } });
|
|
|
|
// await Tank.findOneAndUpdate({customerId, tankName: supplier_tank,tankLocation:supplier_tank_type}, { $set: { waterlevel: initial_update } });
|
|
|
|
const supplier_tank_info = await Tank.findOne({ customerId ,tankName:supplier_tank,tankLocation:supplier_tank_type});
|
|
|
|
const supplier_tank_info = await Tank.findOne({ customerId ,tankName:supplier_tank,tankLocation:supplier_tank_type});
|
|
|
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
|
|
|
console.log(supplier_waterlevel)
|
|
|
|
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 () {
|
|
|
|
intervals[receiver_tank] = setInterval(async function () {
|
|
|
|
// Calculate new water levels
|
|
|
|
// Calculate new water levels
|
|
|
|
const rcvr_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:receiver_type});
|
|
|
|
const rcvr_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:receiver_type});
|
|
|
|
const newWaterLevel = receiver_waterlevel + 350//Math.floor(supplier_waterlevel * 0.1);
|
|
|
|
const newWaterLevel = receiver_waterlevel + 350//Math.floor(supplier_waterlevel * 0.1);
|
|
|
@ -292,10 +319,11 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
// Check if updating should stop
|
|
|
|
// Check if updating should stop
|
|
|
|
if ((newSupplierWaterLevel/supplier_capacity)*100 <= 5 || (newWaterLevel/receiver_capacity)*100 >= 95 || (newWaterLevel/receiver_capacity)*100 >= desired_water_percentage || rcvr_info.motor_status === "0") {
|
|
|
|
if ((newSupplierWaterLevel/supplier_capacity)*100 <= 5 || (newWaterLevel/receiver_capacity)*100 >= 95 || (newWaterLevel/receiver_capacity)*100 >= desired_water_percentage || rcvr_info.motor_status === "0") {
|
|
|
|
clearInterval(intervalId)
|
|
|
|
clearInterval(intervals[receiver_tank]); // Clear the interval for this tank
|
|
|
|
|
|
|
|
delete intervals[receiver_tank];
|
|
|
|
await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { motor_status: "0" } });
|
|
|
|
await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { motor_status: "0" } });
|
|
|
|
|
|
|
|
|
|
|
|
console.log("end");
|
|
|
|
console.log("end for"+receiver_tank);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// Update water levels in database
|
|
|
|
// Update water levels in database
|
|
|
|
supplier_waterlevel = newSupplierWaterLevel;
|
|
|
|
supplier_waterlevel = newSupplierWaterLevel;
|
|
|
@ -361,29 +389,31 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
if(supplier_tank_type==="bore" && receiver_type === "sump"){
|
|
|
|
if(supplier_tank_type==="bore" && receiver_type === "sump"){
|
|
|
|
const receiver_capacity = parseInt(receiver_tank_info.capacity.replace(/,/g, ''), 10)
|
|
|
|
const receiver_capacity = parseInt(receiver_tank_info.capacity.replace(/,/g, ''), 10)
|
|
|
|
console.log(receiver_capacity,"0")
|
|
|
|
console.log(receiver_capacity,"0",receiver_tank_info.tankName)
|
|
|
|
await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { motor_status: "1" } });
|
|
|
|
await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { motor_status: "1" } });
|
|
|
|
|
|
|
|
|
|
|
|
let receiver_waterlevel = parseInt(receiver_tank_info.waterlevel.replace(/,/g, ''), 10)
|
|
|
|
let receiver_waterlevel = parseInt(receiver_tank_info.waterlevel.replace(/,/g, ''), 10)
|
|
|
|
console.log(receiver_waterlevel,"1")
|
|
|
|
console.log(receiver_waterlevel,"1")
|
|
|
|
intervalId = setInterval(async function () {
|
|
|
|
intervals[receiver_tank] = setInterval(async function () {
|
|
|
|
// Calculate new water levels
|
|
|
|
// Calculate new water levels
|
|
|
|
const rcvr_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:receiver_type});
|
|
|
|
const rcvr_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:receiver_type});
|
|
|
|
|
|
|
|
console.log(rcvr_info)
|
|
|
|
console.log(rcvr_info.motor_status)
|
|
|
|
console.log(rcvr_info.motor_status)
|
|
|
|
const newWaterLevel = receiver_waterlevel+450;
|
|
|
|
const newWaterLevel = receiver_waterlevel+450;
|
|
|
|
console.log(newWaterLevel,"2")
|
|
|
|
console.log(newWaterLevel,"2",receiver_tank_info.tankName)
|
|
|
|
// Check if updating should stop
|
|
|
|
// Check if updating should stop
|
|
|
|
if ((newWaterLevel/receiver_capacity)*100 >= 97 || (newWaterLevel/receiver_capacity)*100 >= desired_water_percentage || rcvr_info.motor_status === "0" ) {
|
|
|
|
if ((newWaterLevel/receiver_capacity)*100 >= 97 || (newWaterLevel/receiver_capacity)*100 >= desired_water_percentage || rcvr_info.motor_status === "0" ) {
|
|
|
|
|
|
|
|
|
|
|
|
await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { motor_status: "0" } });
|
|
|
|
await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { motor_status: "0" } });
|
|
|
|
clearInterval(intervalId)
|
|
|
|
clearInterval(intervals[receiver_tank]); // Clear the interval for this tank
|
|
|
|
console.log("end");
|
|
|
|
delete intervals[receiver_tank];
|
|
|
|
|
|
|
|
console.log("end for" + receiver_tank);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// Update water levels in database
|
|
|
|
// Update water levels in database
|
|
|
|
|
|
|
|
|
|
|
|
receiver_waterlevel = newWaterLevel;
|
|
|
|
receiver_waterlevel = newWaterLevel;
|
|
|
|
|
|
|
|
|
|
|
|
console.log((newWaterLevel/receiver_capacity)*100,"4")
|
|
|
|
console.log((newWaterLevel/receiver_capacity)*100,"4",receiver_tank_info.tankName)
|
|
|
|
await Promise.all([
|
|
|
|
await Promise.all([
|
|
|
|
Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { waterlevel: newWaterLevel } }),
|
|
|
|
Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { waterlevel: newWaterLevel } }),
|
|
|
|
]);
|
|
|
|
]);
|
|
|
@ -422,24 +452,25 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const motor_data = await motorData.save();
|
|
|
|
const motor_data = await motorData.save();
|
|
|
|
reply.send({ status_code: 200, data: motor_data });
|
|
|
|
// reply.send({ status_code: 200, data: motor_data });
|
|
|
|
return motor_data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// reply.send({ status_code: 200, "start time": start_time});
|
|
|
|
reply.send({ status_code: 200, "start time": start_time, data: motor_data});
|
|
|
|
//console.log(start_time)
|
|
|
|
console.log(start_time)
|
|
|
|
|
|
|
|
return motor_data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
else if (action === "stop") {
|
|
|
|
else if (action === "stop") {
|
|
|
|
// stop_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'})
|
|
|
|
stop_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'})
|
|
|
|
// console.log(stop_time)
|
|
|
|
// console.log(stop_time)
|
|
|
|
// clearInterval(intervalId);
|
|
|
|
// clearInterval(intervalId);
|
|
|
|
await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:(req.body.to_type).toLowerCase()}, { $set: { motor_status: "0" } });
|
|
|
|
await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:(req.body.to_type).toLowerCase()}, { $set: { motor_status: "0" } });
|
|
|
|
|
|
|
|
|
|
|
|
// reply.send({ status_code: 200, "stop time": stop_time});
|
|
|
|
reply.send({ status_code: 200, "stop time": stop_time});
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
throw new Error("Invalid action");
|
|
|
|
throw new Error("Invalid action");
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -453,7 +484,6 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.consumption = async (req, reply) => {
|
|
|
|
exports.consumption = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const customerId = req.params.customerId;
|
|
|
|
const customerId = req.params.customerId;
|
|
|
|