From d381d9553336565bb6f33dd3b64d13df9f11bd39 Mon Sep 17 00:00:00 2001 From: varun Date: Fri, 3 Mar 2023 00:35:25 -0500 Subject: [PATCH] made changes in start and stop,created consumption --- src/controllers/tanksController.js | 55 ++++++++++++++++++------------ src/routes/tanksRoute.js | 30 ++++++++++++++++ 2 files changed, 63 insertions(+), 22 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 9a5ee63e..c4fca91c 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -225,11 +225,6 @@ exports.updateTanklevels1 = async (req, reply) => { }; - - - - - exports.getTanklevels = async (req, reply) => { try { const customerId = req.params.customerId; @@ -249,17 +244,7 @@ exports.getTanklevels = async (req, reply) => { } }; -const changingfrom_tankwaterlevel = async (customerId,initial_update,supplier_tank_info) => { - //console.log(customerId,"0") - //console.log(update_level,"1") - //console.log(fromSump,"2") - - var result = await Tank.findOneAndUpdate( - {customerId:customerId, tankName: supplier_tank_info.tankName}, { $set: { waterlevel: initial_update } } - ); - return result.waterlevel; -}; @@ -277,10 +262,12 @@ exports.motorAction = async (req, reply) => { supplier_tank_type = req.body.from_type if(supplier_tank_type==="sump"){ + 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}); - initial_update = parseInt(supplier_tank_info.capacity.replace(/,/g, ''), 10)-200; - - await changingfrom_tankwaterlevel(customerId,initial_update,supplier_tank_info); + + //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 () { @@ -306,13 +293,13 @@ exports.motorAction = async (req, reply) => { } }, 2000); - } if(supplier_tank_type==="bore"){ + 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}); - initial_update = parseInt(supplier_tank_info.capacity.replace(/,/g, ''), 10)-200; - 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 () { @@ -357,4 +344,28 @@ exports.motorAction = async (req, reply) => { }; - \ No newline at end of file + +exports.consumption = async (req, reply) => { + try { + const customerId = req.params.customerId; + const tanks = await Tank.find({ customerId }); + + for (const tank of tanks) { + const tankId = tank._id; + let capacity = parseInt(tank.capacity.replace(/,/g, ''), 10); + let waterLevel = capacity*2+1700; // initial water level + + + const newWaterLevel = Math.floor(waterLevel); + + + console.log(newWaterLevel); + + } + + return { message: 'Water level updates started' }; + } catch (err) { + throw boom.boomify(err); + } +}; + diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index cef09249..b2e71d9a 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -247,6 +247,36 @@ module.exports = function (fastify, opts, next) { handler: tanksController.motorAction, }); + + fastify.route({ + method: "GET", + url: "/api/consumption/:customerId", + schema: { + tags: ["Tank"], + summary: "This is for getting consumption", + params: { + required: ["customerId"], + type: "object", + properties: { + customerId: { + type: "string", + description: "customerId", + }, + }, + }, + // querystring: { + // tankName: {type: 'string'} + // }, + security: [ + { + basicAuth: [], + }, + ], + }, + preHandler: fastify.auth([fastify.authenticate]), + handler: tanksController.consumption, + }); + next();