|
|
|
@ -219,22 +219,25 @@ exports.getTanklevels = async (req, reply) => {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const intervals = {};
|
|
|
|
|
let sum_oh_count = 0
|
|
|
|
|
let bore_sump_count = 0
|
|
|
|
|
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,tankLocation:(req.body.to_type).toLowerCase()});
|
|
|
|
|
const receiver_capacity = parseInt((receiver_tank_info.capacity).replace(/,/g, ''), 10)
|
|
|
|
|
const desired_water_percentage = parseInt((req.body.percentage).replace(/,/g, ''), 10)
|
|
|
|
|
const intervals = {};
|
|
|
|
|
|
|
|
|
|
const supplier_tank = req.body.from
|
|
|
|
|
const supplier_tank_type = (req.body.from_type).toLowerCase()
|
|
|
|
|
const receiver_type = (req.body.to_type).toLowerCase()
|
|
|
|
|
console.log(supplier_tank)
|
|
|
|
|
const interval_variable = supplier_tank+receiver_tank
|
|
|
|
|
|
|
|
|
|
if(action === "start"){
|
|
|
|
|
const start_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'})
|
|
|
|
@ -259,26 +262,32 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
|
await overheadTank.save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sum_oh_count++;
|
|
|
|
|
console.log(sum_oh_count)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// await changingfrom_tankwaterlevel(customerId,initial_update,supplier_tank_info);
|
|
|
|
|
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)
|
|
|
|
|
intervals[receiver_tank] = setInterval(async function () {
|
|
|
|
|
intervals[interval_variable] = setInterval(async function () {
|
|
|
|
|
// Calculate new water levels
|
|
|
|
|
const supplier_tank_info2 = await Tank.findOne({ customerId ,tankName:supplier_tank,tankLocation:supplier_tank_type});
|
|
|
|
|
const rcvr_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:receiver_type});
|
|
|
|
|
const newWaterLevel = receiver_waterlevel + 250//Math.floor(supplier_waterlevel * 0.1);
|
|
|
|
|
const newWaterLevel = receiver_waterlevel + (250*sum_oh_count)//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)
|
|
|
|
|
console.log((newSupplierWaterLevel/supplier_capacity)*100)
|
|
|
|
|
// 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") {
|
|
|
|
|
clearInterval(intervals[receiver_tank]); // Clear the interval for this tank
|
|
|
|
|
delete intervals[receiver_tank];
|
|
|
|
|
clearInterval(intervals[interval_variable]); // Clear the interval for this tank
|
|
|
|
|
delete intervals[interval_variable];
|
|
|
|
|
sum_oh_count--;
|
|
|
|
|
if (sum_oh_count===0){
|
|
|
|
|
await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { motor_status: "0" } });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
const overheadTank = await Tank.findOne({ customerId, tankName: receiver_tank, tankLocation: receiver_type });
|
|
|
|
|
const connection = overheadTank.connections.inputConnections.find((conn) => conn.inputConnections === supplier_tank);
|
|
|
|
|
|
|
|
|
@ -359,6 +368,7 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
|
if(supplier_tank_type==="bore" && receiver_type === "sump"){
|
|
|
|
|
const receiver_capacity = parseInt(receiver_tank_info.capacity.replace(/,/g, ''), 10)
|
|
|
|
|
// console.log(receiver_capacity,"0",receiver_tank_info.tankName)
|
|
|
|
|
bore_sump_count++;
|
|
|
|
|
await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { motor_status: "1" } });
|
|
|
|
|
const sumpTank = await Tank.findOne({ customerId, tankName: receiver_tank, tankLocation: receiver_type });
|
|
|
|
|
const connection = sumpTank.connections.inputConnections.find((conn) => conn.inputConnections === supplier_tank);
|
|
|
|
@ -371,19 +381,23 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
let receiver_waterlevel = parseInt(receiver_tank_info.waterlevel.replace(/,/g, ''), 10)
|
|
|
|
|
// console.log(receiver_waterlevel,"1")
|
|
|
|
|
intervals[receiver_tank] = setInterval(async function () {
|
|
|
|
|
intervals[interval_variable] = setInterval(async function () {
|
|
|
|
|
// Calculate new water levels
|
|
|
|
|
const rcvr_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:receiver_type});
|
|
|
|
|
//console.log(rcvr_info)
|
|
|
|
|
//console.log(rcvr_info.motor_status)
|
|
|
|
|
const newWaterLevel = receiver_waterlevel+250;
|
|
|
|
|
const newWaterLevel = receiver_waterlevel+(250*bore_sump_count);
|
|
|
|
|
//console.log(newWaterLevel,"2",receiver_tank_info.tankName)
|
|
|
|
|
// Check if updating should stop
|
|
|
|
|
if ((newWaterLevel/receiver_capacity)*100 >= 97 || (newWaterLevel/receiver_capacity)*100 >= desired_water_percentage || rcvr_info.motor_status === "0" ) {
|
|
|
|
|
|
|
|
|
|
clearInterval(intervals[interval_variable]); // Clear the interval for this tank
|
|
|
|
|
delete intervals[interval_variable];
|
|
|
|
|
bore_sump_count--;
|
|
|
|
|
if (bore_sump_count===0){
|
|
|
|
|
await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { motor_status: "0" } });
|
|
|
|
|
clearInterval(intervals[receiver_tank]); // Clear the interval for this tank
|
|
|
|
|
delete intervals[receiver_tank];
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
const sumpTank = await Tank.findOne({ customerId, tankName: receiver_tank, tankLocation: receiver_type });
|
|
|
|
|
const connection = sumpTank.connections.inputConnections.find((conn) => conn.inputConnections === supplier_tank);
|
|
|
|
|
|
|
|
|
@ -435,6 +449,7 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
const motor_data = await motorData.save();
|
|
|
|
|
console.log(motor_data)
|
|
|
|
|
// reply.send({ status_code: 200, data: motor_data });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -449,6 +464,23 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
else if (action === "stop") {
|
|
|
|
|
const stop_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'})
|
|
|
|
|
clearInterval(intervals[interval_variable]); // Clear the interval for this tank
|
|
|
|
|
delete intervals[interval_variable];
|
|
|
|
|
if (supplier_tank_type === "sump"){
|
|
|
|
|
sum_oh_count--;
|
|
|
|
|
if (sum_oh_count===0){
|
|
|
|
|
await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:(req.body.to_type).toLowerCase()}, { $set: { motor_status: "0" } });
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (supplier_tank_type === "bore"){
|
|
|
|
|
bore_sump_count--;
|
|
|
|
|
if (bore_sump_count===0){
|
|
|
|
|
await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:(req.body.to_type).toLowerCase()}, { $set: { motor_status: "0" } });
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const overheadTank = await Tank.findOne({ customerId, tankName: receiver_tank, tankLocation: receiver_type });
|
|
|
|
|
const connection = overheadTank.connections.inputConnections.find((conn) => conn.inputConnections === supplier_tank);
|
|
|
|
|
|
|
|
|
@ -458,7 +490,8 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
|
}
|
|
|
|
|
// console.log(stop_time)
|
|
|
|
|
// 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});
|
|
|
|
|
} else {
|
|
|
|
|