added capacity calculator

master
varun 3 years ago
parent 0e0398d5c8
commit fef12c9389

@ -511,3 +511,147 @@ exports.consumption = async (req, reply) => {
throw boom.boomify(err);
}
};
exports.calculateCapacity = async (req, reply) => {
try {
const shape = req.body.shape
if(shape==="rectangle"){
const { length, width, height } = req.body
// Ensure all parameters are valid numbers
if (isNaN(length) || isNaN(width) || isNaN(height)) {
reply.code(400).send('Invalid input parameters')
return
}
// Calculate the capacity of the water tank in liters
const capacity = length * width * height * 1000
reply.send({ status_code: 200, capacity: capacity});
return { message: 'success' };
}
if(shape==="horizontalcylinder"){
console.log("hii1")
const { length,diameter } = req.body
// Ensure all parameters are valid numbers
if (isNaN(length) || isNaN(diameter)) {
reply.code(400).send('Invalid input parameters')
return
}
// Calculate the capacity of the water tank in liters
const radius = diameter / 2
const volume = Math.PI * Math.pow(radius, 2) * length
const capacity = volume * 1000
reply.send({ status_code: 200, capacity: capacity});
return { message: 'success' };
}
if(shape==="verticalcylinder"){
console.log("hii2")
const { height,diameter } = req.body
// Ensure all parameters are valid numbers
if (isNaN(height) || isNaN(diameter)) {
reply.code(400).send('Invalid input parameters')
return
}
// Calculate the capacity of the water tank in liters
const radius = diameter / 2
const volume = Math.PI * Math.pow(radius, 2) * height
const capacity = volume * 1000
reply.send({ status_code: 200, capacity: capacity});
return { message: 'success' };
}
if(shape==="horizontaloval"){
console.log("hii3")
const { length, width, height } = req.body
// Ensure all parameters are valid numbers
if (isNaN(length) || isNaN(width) || isNaN(height)) {
reply.code(400).send('Invalid input parameters')
return
}
// Calculate the capacity of the water tank in liters
const radius = height / 2
const a = width - height
const area = Math.PI * radius * radius + 2 * radius * a
const volume = area * length
const capacity = volume * 1000
// Format the result with two decimal places and comma-separated thousands
const formattedCapacity = capacity.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,')
reply.send({ status_code: 200, capacity: formattedCapacity});
return { message: 'success' };
}
if(shape=== "verticaloval"){
console.log("hii4")
const { length, width, height } = req.body
// Ensure all parameters are valid numbers
if (isNaN(length) || isNaN(width) || isNaN(height)) {
reply.code(400).send('Invalid input parameters')
return
}
// Calculate the capacity of the water tank in liters
const radius = width / 2
const a = height - width
const area = Math.PI * radius * radius + 2 * radius * a
const volume = area * length
const capacity = volume * 1000
// Format the result with two decimal places and comma-separated thousands
const formattedCapacity = capacity.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,')
reply.send({ status_code: 200, capacity: formattedCapacity});
return { message: 'success' };
}
if(shape==="horizontalellipse"){
const { length, width, height } = req.body
// Ensure all parameters are valid numbers
if (isNaN(length) || isNaN(width) || isNaN(height)) {
reply.code(400).send('Invalid input parameters')
return
}
// Calculate the capacity of the water tank in liters
const radius1 = length / 2
const radius2 = width / 2
const volume = Math.PI * radius1 * radius2 * height
const capacity = volume * 1000
reply.send({ status_code: 200, capacity: capacity});
return { message: 'success' };
}
}
catch (err) {
throw boom.boomify(err);
}
};

@ -234,6 +234,7 @@ module.exports = function (fastify, opts, next) {
percentage: { type: "string",default: "100" },
startTime:{ type: "string" },
stopTime:{ type: "string" },
},
},
security: [
@ -280,6 +281,39 @@ module.exports = function (fastify, opts, next) {
handler: tanksController.consumption,
});
fastify.route({
method: "PUT",
url: "/api/calculateCapacity",
schema: {
tags: ["Tank"],
summary: "This is for calculating the capacity",
body: {
type: "object",
// required: ['phone'],
properties: {
shape: { type: "string", default: null },
length: { type: "string", default: null },
width: { type: "string", default: null },
height: { type: "string" },
diameter:{ type: "string" },
},
},
security: [
{
basicAuth: [],
},
],
},
// preHandler: [
// fastify.auth([fastify.operatorAuthenticate]),
// validationHandler.validatePhoneFormat,
// ],
preHandler: fastify.auth([fastify.authenticate]),
handler: tanksController.calculateCapacity,
});
next();

Loading…
Cancel
Save