made changes in start and stop and conmsumption

master
varun 2 years ago
parent 198275668e
commit 92ae56ce5f

@ -260,12 +260,13 @@ exports.getTanklevels = async (req, reply) => {
}; };
const intervals = {}; const intervals = {};
let start_time="" let sump_water_levels=[];
let supplier_tanks = [];
exports.motorAction = async (req, reply) => { exports.motorAction = async (req, reply) => {
try { try {
7
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
@ -273,20 +274,37 @@ exports.motorAction = async (req, reply) => {
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 supplier_tank = req.body.from const supplier_tank = req.body.from
const supplier_tank_type = (req.body.from_type).toLowerCase() const supplier_tank_type = (req.body.from_type).toLowerCase()
const receiver_type = (req.body.to_type).toLowerCase() const receiver_type = (req.body.to_type).toLowerCase()
console.log(supplier_tank) // console.log(supplier_tank)
// const suplr_tank_info1 = await Tank.findOne({ customerId ,tankName:supplier_tank,tankLocation:supplier_tank_type});
const interval_variable = supplier_tank+receiver_tank const interval_variable = supplier_tank+receiver_tank
let currentTank = supplier_tanks.find(tank => tank.supplier_tank === supplier_tank);
let currentSump = sump_water_levels.find(tank => tank.supplier_tank === supplier_tank);
if(action === "start"){ if(action === "start"){
start_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'})
console.log(start_time) if (!currentTank) {
currentTank = {
supplier_tank: supplier_tank,
start_time: new Date().toLocaleString('en-US', { timeZone: 'Asia/Kolkata' })
};
supplier_tanks.push(currentTank);
}
// start_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'})
console.log(supplier_tanks)
// const stop_at = req.body.stop_at // const stop_at = req.body.stop_at
if(supplier_tank_type==="sump" && receiver_type === "overhead"){ if(supplier_tank_type==="sump" && receiver_type === "overhead"){
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" } });
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});
@ -294,7 +312,23 @@ exports.motorAction = async (req, reply) => {
//const initial_update = parseInt(supplier_tank_info1.waterlevel.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});
const sump_water_level= parseInt(supplier_tank_info.waterlevel.replace(/,/g, ''), 10)
const receiver_tank_info2 = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:(req.body.to_type).toLowerCase()});
const water_added_from_midnight = parseInt((receiver_tank_info2.total_water_added_from_midnight).replace(/,/g, ''), 10)
if (!currentSump) {
currentSump = {
supplier_tank: supplier_tank,
supplier_initial_waterlevel:sump_water_level,
receiver_tank_total_water_added_from_midnight:water_added_from_midnight
};
sump_water_levels.push(currentSump);
}
console.log(sump_water_levels)
const overheadTank = await Tank.findOne({ customerId, tankName: receiver_tank, tankLocation: receiver_type }); const overheadTank = await Tank.findOne({ customerId, tankName: receiver_tank, tankLocation: receiver_type });
const connection = overheadTank.connections.inputConnections.find((conn) => conn.inputConnections === supplier_tank); const connection = overheadTank.connections.inputConnections.find((conn) => conn.inputConnections === supplier_tank);
@ -424,8 +458,6 @@ exports.motorAction = async (req, reply) => {
if(supplier_tank_type==="sump" && receiver_type === "sump"){ if(supplier_tank_type==="sump" && 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)
@ -574,10 +606,47 @@ exports.motorAction = async (req, reply) => {
} }
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'})
clearInterval(intervals[interval_variable]); // Clear the interval for this tank clearInterval(intervals[interval_variable]); // Clear the interval for this tank
delete intervals[interval_variable]; delete intervals[interval_variable];
const stopTime = new Date().toLocaleString('en-US', { timeZone: 'Asia/Kolkata' });
// console.log(currentTank.start_time)
const startTime = currentTank.start_time;
// const duration = calculateDuration(startTime, stopTime);
// Store the duration or perform any required operations
supplier_tanks = supplier_tanks.filter(tank => tank.supplier_tank !== supplier_tank);
// console.log(supplier_tanks)
// storing data of how amny water supplied from sump to overhead to calculate the consumption
const suplr_tank_info2 = await Tank.findOne({ customerId ,tankName:supplier_tank,tankLocation:supplier_tank_type});
let water_added_from_midnight1=0
if (supplier_tank_type === "sump") {
// const rcvr_info2 = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:receiver_type});
console.log(currentSump.receiver_tank_total_water_added_from_midnight,"5")
water_added_from_midnight1=currentSump.receiver_tank_total_water_added_from_midnight
console.log(water_added_from_midnight1)
}
const sump_water_level1 =currentSump.supplier_initial_waterlevel
console.log(sump_water_level1,"1")
// console.log(water_added_from_midnight)
const sump_final_water_level= parseInt(suplr_tank_info2.waterlevel.replace(/,/g, ''), 10)
console.log(sump_final_water_level,"2")
sump_water_levels = sump_water_levels.filter(tank => tank.supplier_tank !== supplier_tank);
const quantity_delivered = sump_water_level1-sump_final_water_level
if (supplier_tank_type === "sump") {
final_added_water=water_added_from_midnight1+quantity_delivered
await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { total_water_added_from_midnight: final_added_water } })
}
const overheadTank = await Tank.findOne({ customerId, tankName: receiver_tank, tankLocation: receiver_type }); const overheadTank = await Tank.findOne({ customerId, tankName: receiver_tank, tankLocation: receiver_type });
const connection = overheadTank.connections.inputConnections.find((conn) => conn.inputConnections === supplier_tank); const connection = overheadTank.connections.inputConnections.find((conn) => conn.inputConnections === supplier_tank);
@ -601,17 +670,18 @@ exports.motorAction = async (req, reply) => {
); );
} }
motorData = { //saving the motor run time and data
motorData = {
customerId:customerId, customerId:customerId,
supplierTank : supplier_tank, supplierTank : supplier_tank,
supplier_type: supplier_tank_type, supplier_type: supplier_tank_type,
receiverTank: receiver_tank, receiverTank: receiver_tank,
receiver_type: receiver_type, receiver_type: receiver_type,
startTime: start_time, startTime: startTime,
stopTime: stop_time, stopTime: stopTime,
quantity_delivered:quantity_delivered
@ -627,8 +697,9 @@ exports.motorAction = async (req, reply) => {
motorData.receiverTank = receiver_tank; motorData.receiverTank = receiver_tank;
motorData.supplier_type = supplier_type; motorData.supplier_type = supplier_type;
motorData.receiver_type = receiver_type; motorData.receiver_type = receiver_type;
motorData.startTime = start_time; motorData.startTime = startTime;
motorData.stopTime = stop_time; motorData.stopTime = stopTime;
motorData.quantity_delivered = quantity_delivered;
} }
@ -640,7 +711,7 @@ exports.motorAction = async (req, reply) => {
// reply.send({ status_code: 200, "start time": start_time, data: motor_data}); // reply.send({ status_code: 200, "start time": start_time, data: motor_data});
console.log(start_time) // console.log(start_time)
// return motor_data // return motor_data
@ -650,7 +721,7 @@ exports.motorAction = async (req, reply) => {
// 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,data: motor_data}); reply.send({ status_code: 200, "stop time": stopTime,data: motor_data});
} else { } else {
throw new Error("Invalid action"); throw new Error("Invalid action");
} }

@ -43,6 +43,7 @@ const tanksSchema = new mongoose.Schema({
tankLocation: { type: String, default: null }, tankLocation: { type: String, default: null },
motor_status: { type: String, default: "0" }, motor_status: { type: String, default: "0" },
waterlevel_at_midnight:{ type: String,default:"0" }, waterlevel_at_midnight:{ type: String,default:"0" },
total_water_added_from_midnight:{ type: String,default:"0" },
connections: { connections: {
source: { type: String }, source: { type: String },
inputConnections: [ inputConnections: [
@ -74,7 +75,7 @@ const motordataSchema = new mongoose.Schema({
stopTime: { type: String, default: null }, stopTime: { type: String, default: null },
supplier_type: { type: String, default: null }, supplier_type: { type: String, default: null },
receiver_type: { type: String, default: null }, receiver_type: { type: String, default: null },
quantity_delivered:{ type: String, default: null },
}); });

Loading…
Cancel
Save