consumption

master
varun 1 year ago
parent 7890881ed8
commit 556931e871

@ -837,9 +837,7 @@ exports.consumption = async (request, reply) => {
const tankname = tank.tankName
const consumption = (waterlevel_at_midnight+total_water_added_from_midnight)-waterlevel
const newWaterLevel1 = Math.floor(consumption);
const newWaterLevel=Math.abs(newWaterLevel1)
tankData.push({ tankname, waterLevel: newWaterLevel });
@ -874,9 +872,73 @@ exports.consumption = async (request, reply) => {
};
function parseDateTime(dateTimeStr) {
const [datePart, timePart] = dateTimeStr.split(' - ');
const [day, month, year] = datePart.split('-');
const monthIndex = new Date(Date.parse(month +" 1, 2024")).getMonth(); // Parse the month name to get its index
const [hours, minutes] = timePart.split(':');
return new Date(year, monthIndex, day, hours, minutes);
}
exports.consumption = async (request, reply) => {
try {
const { customerId } = request.params;
const { startDate, stopDate } = request.body;
const start = parseDateTime(startDate);
const end = parseDateTime(stopDate);
const tanks = await Tank.find({ customerId, tankLocation: "overhead" });
const tankData = [];
for (const tank of tanks) {
const tankId = tank._id;
const waterlevel_at_midnight = parseInt(tank.waterlevel_at_midnight.replace(/,/g, ''), 10);
const total_water_added_from_midnight = parseInt(tank.total_water_added_from_midnight.replace(/,/g, ''), 10);
const waterlevel = parseInt(tank.waterlevel.replace(/,/g, ''), 10);
const tankname = tank.tankName;
const tankConsumptions = await TankConsumptionSchema.find({
customerId,
tankName: tank.tankName,
tankLocation: tank.tankLocation,
time: {
$gte: start,
$lte: end
}
});
const total_consumption_from_records = tankConsumptions.reduce((acc, record) => {
return acc + parseInt(record.consumption, 10);
}, 0);
const consumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel + total_consumption_from_records;
tankData.push({ tankname, totalConsumption: consumption });
}
reply.send({ status_code: 200, tankData });
} catch (err) {
throw boom.boomify(err);
}
};
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
//const moment = require('moment'); // Import moment.js for date/time operations
const formatDate = (date) => {
const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
const day = String(date.getDate()).padStart(2, '0');
const month = months[date.getMonth()];
const year = date.getFullYear();
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
return `${day}-${month}-${year} - ${hours}:${minutes}`;
};
exports.motorAction = async (req, reply) => {
try {
@ -1017,11 +1079,13 @@ exports.motorAction = async (req, reply) => {
const totalwaterpumped = quantityDelivered + water_pumped_till_now
await Tank.findOneAndUpdate({customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase()}, { $set: { total_water_added_from_midnight: totalwaterpumped } })
const stopTime = formatDate(new Date());
await MotorData.updateOne(
{ customerId, motor_id: motorId, start_instance_id: start_instance_id },
{
$set: {
stopTime: new Date().toISOString(),
stopTime:stopTime,
receiverfinalwaterlevel: receiverFinalWaterLevel.toString(),
quantity_delivered: quantityDelivered.toString()
}
@ -1108,11 +1172,14 @@ exports.motorAction = async (req, reply) => {
const receiverFinalWaterLevel = parseInt(receiverTank.waterlevel, 10);
const quantityDelivered = receiverFinalWaterLevel - parseInt(motorData.receiverInitialwaterlevel, 10);
const stopTime = formatDate(new Date());
await MotorData.updateOne(
{ customerId, motor_id: motorId, start_instance_id: start_instance_id },
{
$set: {
stopTime: new Date().toISOString(),
stopTime:stopTime,
receiverfinalwaterlevel: receiverFinalWaterLevel.toString(),
quantity_delivered: quantityDelivered.toString()
}

@ -327,6 +327,16 @@ module.exports = function (fastify, opts, next) {
// querystring: {
// tankName: {type: 'string'}
// },
body: {
type: "object",
// required: ['phone'],
properties: {
startDate:{ type: "string" },
stopDate:{type:"string"},
},
},
security: [
{
basicAuth: [],

Loading…
Cancel
Save