|
|
|
@ -1,4 +1,6 @@
|
|
|
|
|
const Tank = require("../models/tanks");
|
|
|
|
|
//const Tank = require("../models/tanks");
|
|
|
|
|
const { Tank, MotorData } = require('../models/tanks')
|
|
|
|
|
|
|
|
|
|
const User = require("../models/User");
|
|
|
|
|
const boom = require("boom");
|
|
|
|
|
const fastify = require("fastify")({
|
|
|
|
@ -33,7 +35,7 @@ console.log(req.params);
|
|
|
|
|
capacity: req.body.capacity,
|
|
|
|
|
typeOfWater: req.body.typeOfWater,
|
|
|
|
|
|
|
|
|
|
tankLocation:req.body.tankLocation,
|
|
|
|
|
tankLocation:(req.body.tankLocation).toLowerCase(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var tank_name = req.body.tankName
|
|
|
|
@ -56,7 +58,7 @@ console.log(req.params);
|
|
|
|
|
tank.capacity = usertobeInserted.capacity;
|
|
|
|
|
tank.typeOfWater = usertobeInserted.typeOfWater;
|
|
|
|
|
|
|
|
|
|
tank.tankLocation = usertobeInserted.tankLocation;
|
|
|
|
|
tank.tankLocation = (usertobeInserted.tankLocation).toLowerCase();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const insertedTank = await tank.save();
|
|
|
|
@ -154,20 +156,23 @@ exports.updateTanklevels = async (req, reply) => {
|
|
|
|
|
let waterLevel = capacity - 100; // initial water level
|
|
|
|
|
|
|
|
|
|
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(
|
|
|
|
|
{ _id: tankId },
|
|
|
|
|
{ $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;
|
|
|
|
|
}, 2000);
|
|
|
|
@ -197,14 +202,14 @@ exports.updateTanklevels1 = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
let waterLevel = capacity - 100; // initial water level
|
|
|
|
|
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(
|
|
|
|
|
{ customerId, tankName, },
|
|
|
|
|
{ $set: { waterlevel: newWaterLevel } }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
console.log(result);
|
|
|
|
|
// console.log(result);
|
|
|
|
|
|
|
|
|
|
if (newWaterLevel === 0) {
|
|
|
|
|
clearInterval(intervals[tankName]);
|
|
|
|
@ -235,7 +240,6 @@ exports.getTanklevels = async (req, reply) => {
|
|
|
|
|
reply.send({ status_code: 200, data: updated_data});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return { message: 'success' };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -250,69 +254,58 @@ exports.getTanklevels = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
exports.motorAction = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
//let start_time,stop_time
|
|
|
|
|
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_tank_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:req.body.to_type});
|
|
|
|
|
const receiver_capacity = receiver_tank_info.capacity
|
|
|
|
|
const desired_water_percentage = parseInt((req.body.percentage).replace(/,/g, ''), 10)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(action === "start"){
|
|
|
|
|
// start_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'})
|
|
|
|
|
|
|
|
|
|
supplier_tank = req.body.from
|
|
|
|
|
supplier_tank_type = req.body.from_type
|
|
|
|
|
receiver_type = req.body.to_type
|
|
|
|
|
console.log(supplier_tank)
|
|
|
|
|
|
|
|
|
|
// if(supplier_tank_type==="sump" && receiver_type === "overhead"){
|
|
|
|
|
// const supplier_tank_info1 = await Tank.findOne({ customerId ,tankName:supplier_tank});
|
|
|
|
|
// initial_update = parseInt(supplier_tank_info1.capacity.replace(/,/g, ''), 10)-200;
|
|
|
|
|
// await Tank.findOneAndUpdate({customerId, tankName: supplier_tank}, { $set: { waterlevel: initial_update } });
|
|
|
|
|
// const supplier_tank_info = await Tank.findOne({ customerId ,tankName:supplier_tank});
|
|
|
|
|
|
|
|
|
|
//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);
|
|
|
|
|
// const supplier_capacity = parseInt(supplier_tank_info.capacity.replace(/,/g, ''), 10)
|
|
|
|
|
|
|
|
|
|
// Check if updating should stop
|
|
|
|
|
// if ((newSupplierWaterLevel/supplier_capacity)*100 <= 5 || (newWaterLevel/receiver_capacity)*100 >= 95) {
|
|
|
|
|
// clearInterval(intervalId)
|
|
|
|
|
// console.log("end");
|
|
|
|
|
// } else {
|
|
|
|
|
// Update water levels in database
|
|
|
|
|
// supplier_waterlevel = newSupplierWaterLevel;
|
|
|
|
|
// receiver_waterlevel = newWaterLevel;
|
|
|
|
|
// console.log((newSupplierWaterLevel/supplier_capacity)*100)
|
|
|
|
|
// console.log((newWaterLevel/receiver_capacity)*100)
|
|
|
|
|
// await Promise.all([
|
|
|
|
|
// Tank.findOneAndUpdate({customerId, tankName: receiver_tank}, { $set: { waterlevel: newWaterLevel } }),
|
|
|
|
|
// Tank.findOneAndUpdate({customerId, tankName: supplier_tank}, { $set: { waterlevel: newSupplierWaterLevel } })
|
|
|
|
|
// ]);
|
|
|
|
|
// }
|
|
|
|
|
//}, 2000);
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
if(supplier_tank_type==="sump" && receiver_type === "overhead"){
|
|
|
|
|
await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { motor_status: "1" } });
|
|
|
|
|
|
|
|
|
|
if(supplier_tank_type==="bore" && receiver_type === "sump"){
|
|
|
|
|
const receiver_capacity = receiver_tank_info.capacity
|
|
|
|
|
const supplier_tank_info1 = await Tank.findOne({ customerId ,tankName:supplier_tank,tankLocation:supplier_tank_type});
|
|
|
|
|
console.log(supplier_tank_info1)
|
|
|
|
|
initial_update = parseInt(supplier_tank_info1.capacity.replace(/,/g, ''), 10)-200;
|
|
|
|
|
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});
|
|
|
|
|
|
|
|
|
|
// 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+225;
|
|
|
|
|
const rcvr_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:receiver_type});
|
|
|
|
|
const newWaterLevel = receiver_waterlevel + 200//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)
|
|
|
|
|
|
|
|
|
|
// Check if updating should stop
|
|
|
|
|
if ((newWaterLevel/receiver_capacity)*100 >= 97) {
|
|
|
|
|
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)
|
|
|
|
|
await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { motor_status: "0" } });
|
|
|
|
|
|
|
|
|
|
console.log("end");
|
|
|
|
|
} else {
|
|
|
|
|
// Update water levels in database
|
|
|
|
|
|
|
|
|
|
supplier_waterlevel = newSupplierWaterLevel;
|
|
|
|
|
receiver_waterlevel = newWaterLevel;
|
|
|
|
|
|
|
|
|
|
console.log((newSupplierWaterLevel/supplier_capacity)*100)
|
|
|
|
|
console.log((newWaterLevel/receiver_capacity)*100)
|
|
|
|
|
await Promise.all([
|
|
|
|
|
Tank.findOneAndUpdate({customerId, tankName: receiver_tank}, { $set: { waterlevel: newWaterLevel } }),
|
|
|
|
|
Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { waterlevel: newWaterLevel } }),
|
|
|
|
|
Tank.findOneAndUpdate({customerId, tankName: supplier_tank,tankLocation:supplier_tank_type}, { $set: { waterlevel: newSupplierWaterLevel } })
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}, 2000);
|
|
|
|
@ -321,49 +314,139 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(supplier_tank_type==="sump" && receiver_type === "overhead"){
|
|
|
|
|
const supplier_tank_info1 = await Tank.findOne({ customerId ,tankName:supplier_tank});
|
|
|
|
|
initial_update = parseInt(supplier_tank_info1.capacity.replace(/,/g, ''), 10)/2;
|
|
|
|
|
await Tank.findOneAndUpdate({customerId, tankName: supplier_tank}, { $set: { waterlevel: initial_update } });
|
|
|
|
|
const supplier_tank_info = await Tank.findOne({ customerId ,tankName:supplier_tank});
|
|
|
|
|
// if(supplier_tank_type==="sump" && receiver_type === "overhead"){
|
|
|
|
|
// await Tank.findOneAndUpdate({customerId, tankName: receiver_tank}, { $set: { motor_status: 1 } });
|
|
|
|
|
|
|
|
|
|
let supplier_waterlevel = parseInt(supplier_tank_info.waterlevel.replace(/,/g, ''), 10)
|
|
|
|
|
// // console.log(rcvr_info.motor_status)
|
|
|
|
|
// const supplier_tank_info1 = await Tank.findOne({ customerId ,tankName:supplier_tank});
|
|
|
|
|
// initial_update = parseInt(supplier_tank_info1.capacity.replace(/,/g, ''), 10)/2;
|
|
|
|
|
|
|
|
|
|
// await Tank.findOneAndUpdate({customerId, tankName: supplier_tank}, { $set: { waterlevel: initial_update } });
|
|
|
|
|
// const supplier_tank_info = await Tank.findOne({ customerId ,tankName:supplier_tank});
|
|
|
|
|
|
|
|
|
|
// 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 () {
|
|
|
|
|
// const rcvr_info = await Tank.findOne({ customerId ,tankName:receiver_tank});
|
|
|
|
|
// const supplier_capacity = parseInt(supplier_tank_info.capacity.replace(/,/g, ''), 10)
|
|
|
|
|
// // Calculate new water levels
|
|
|
|
|
// const newWaterLevel = receiver_waterlevel + 200//Math.floor(supplier_waterlevel * 0.1);
|
|
|
|
|
// const newSupplierWaterLevel = Math.min(supplier_capacity, supplier_waterlevel + 350);// Math.floor(supplier_waterlevel * 0.15));
|
|
|
|
|
// // console.log(newWaterLevel)
|
|
|
|
|
// // console.log(newSupplierWaterLevel)
|
|
|
|
|
// // console.log(rcvr_info.motor_status)
|
|
|
|
|
// // console.log(rcvr_info.tankName)
|
|
|
|
|
|
|
|
|
|
// // Check if updating should stop
|
|
|
|
|
// if ( (newWaterLevel/receiver_capacity)*100 >= 95 || (newWaterLevel/receiver_capacity)*100 >= desired_water_percentage || rcvr_info.motor_status === 0) {
|
|
|
|
|
|
|
|
|
|
// clearInterval(intervalId)
|
|
|
|
|
// await Tank.findOneAndUpdate({customerId, tankName: receiver_tank}, { $set: { motor_status: 0 } });
|
|
|
|
|
|
|
|
|
|
// console.log("end");
|
|
|
|
|
// } else {
|
|
|
|
|
// // Update water levels in database
|
|
|
|
|
// supplier_waterlevel = newSupplierWaterLevel;
|
|
|
|
|
// receiver_waterlevel = newWaterLevel;
|
|
|
|
|
// // console.log((newSupplierWaterLevel/supplier_capacity)*100)
|
|
|
|
|
// // console.log((newWaterLevel/receiver_capacity)*100)
|
|
|
|
|
// await Promise.all([
|
|
|
|
|
// Tank.findOneAndUpdate({customerId, tankName: receiver_tank}, { $set: { waterlevel: newWaterLevel } }),
|
|
|
|
|
// Tank.findOneAndUpdate({customerId, tankName: supplier_tank}, { $set: { waterlevel: newSupplierWaterLevel } })
|
|
|
|
|
// ]);
|
|
|
|
|
// }
|
|
|
|
|
// }, 2000);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
if(supplier_tank_type==="bore" && receiver_type === "sump"){
|
|
|
|
|
const receiver_capacity = parseInt(receiver_tank_info.capacity.replace(/,/g, ''), 10)
|
|
|
|
|
console.log(receiver_capacity,"0")
|
|
|
|
|
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)
|
|
|
|
|
console.log(receiver_waterlevel,"1")
|
|
|
|
|
intervalId = setInterval(async function () {
|
|
|
|
|
const supplier_capacity = parseInt(supplier_tank_info.capacity.replace(/,/g, ''), 10)
|
|
|
|
|
// Calculate new water levels
|
|
|
|
|
const newWaterLevel = receiver_waterlevel + Math.floor(supplier_waterlevel * 0.1);
|
|
|
|
|
const newSupplierWaterLevel = Math.min(supplier_capacity, supplier_waterlevel + Math.floor(supplier_waterlevel * 0.15));
|
|
|
|
|
|
|
|
|
|
const rcvr_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:receiver_type});
|
|
|
|
|
console.log(rcvr_info.motor_status)
|
|
|
|
|
const newWaterLevel = receiver_waterlevel+200;
|
|
|
|
|
console.log(newWaterLevel,"2")
|
|
|
|
|
// Check if updating should stop
|
|
|
|
|
if ((newSupplierWaterLevel/supplier_capacity)*100 <= 5 || (newWaterLevel/receiver_capacity)*100 >= 95) {
|
|
|
|
|
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" } });
|
|
|
|
|
clearInterval(intervalId)
|
|
|
|
|
console.log("end");
|
|
|
|
|
} else {
|
|
|
|
|
// Update water levels in database
|
|
|
|
|
supplier_waterlevel = newSupplierWaterLevel;
|
|
|
|
|
|
|
|
|
|
receiver_waterlevel = newWaterLevel;
|
|
|
|
|
console.log((newSupplierWaterLevel/supplier_capacity)*100)
|
|
|
|
|
console.log((newWaterLevel/receiver_capacity)*100)
|
|
|
|
|
|
|
|
|
|
console.log((newWaterLevel/receiver_capacity)*100,"4")
|
|
|
|
|
await Promise.all([
|
|
|
|
|
Tank.findOneAndUpdate({customerId, tankName: receiver_tank}, { $set: { waterlevel: newWaterLevel } }),
|
|
|
|
|
Tank.findOneAndUpdate({customerId, tankName: supplier_tank}, { $set: { waterlevel: newSupplierWaterLevel } })
|
|
|
|
|
Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { waterlevel: newWaterLevel } }),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}, 2000);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
console.log(customerId,req.body.from,req.body.from_type,receiver_tank,req.body.to_type,)
|
|
|
|
|
motorData = {
|
|
|
|
|
|
|
|
|
|
customerId:customerId,
|
|
|
|
|
supplierTank : req.body.from,
|
|
|
|
|
supplier_type: req.body.from_type,
|
|
|
|
|
receiverTank: receiver_tank,
|
|
|
|
|
receiver_type: req.body.to_type,
|
|
|
|
|
startTime: req.body.startTime,
|
|
|
|
|
stopTime: req.body.stopTime,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
var motorData = new MotorData(motorData);
|
|
|
|
|
|
|
|
|
|
checkFormEncoding = isUserFormUrlEncoded(req);
|
|
|
|
|
if (checkFormEncoding.isUserFormUrlEncoded) {
|
|
|
|
|
usertobeInserted = checkFormEncoding.motorData;
|
|
|
|
|
console.log("thsi true url string");
|
|
|
|
|
motorData.customerId = customerId;
|
|
|
|
|
motorData.supplierTank = req.body.from;
|
|
|
|
|
motorData.receiverTank = receiver_tank;
|
|
|
|
|
motorData.supplier_type = req.body.from_type;
|
|
|
|
|
motorData.receiver_type = req.body.to_type;
|
|
|
|
|
motorData.startTime = usertobeInserted.startTime;
|
|
|
|
|
motorData.stopTime = usertobeInserted.stopTime;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
const motor_data = await motorData.save();
|
|
|
|
|
reply.send({ status_code: 200, data: motor_data });
|
|
|
|
|
return motor_data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// reply.send({ status_code: 200, "start time": start_time});
|
|
|
|
|
//console.log(start_time)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (action === "stop") {
|
|
|
|
|
clearInterval(intervalId);
|
|
|
|
|
// stop_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'})
|
|
|
|
|
// console.log(stop_time)
|
|
|
|
|
// clearInterval(intervalId);
|
|
|
|
|
await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:req.body.to_type}, { $set: { motor_status: "0" } });
|
|
|
|
|
|
|
|
|
|
// reply.send({ status_code: 200, "stop time": stop_time});
|
|
|
|
|
} 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}`);
|
|
|
|
|
|
|
|
|
|