From 3b93adf0b2325dae135959a8575b15028fa4f797 Mon Sep 17 00:00:00 2001 From: varun Date: Mon, 13 Feb 2023 06:38:48 -0500 Subject: [PATCH] creating water level randomly --- src/controllers/tanksController.js | 23 ++++++++++++++++++++++- src/models/tanks.js | 2 +- src/routes/tanksRoute.js | 29 +++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index aca0d437..82667347 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -113,4 +113,25 @@ exports.getTank = async (req, reply) => { } catch (err) { throw boom.boomify(err); } -}; \ No newline at end of file +}; +exports.getTanklevels = async (req, reply) => { + try { + const customerId = req.params.customerId; + const tankName = req.query.tankName; + + setInterval(async function () { + const randomNumber = Math.floor(Math.random() * (5500 - 1000) + 1000); + console.log(randomNumber) + console.log( await Tank.findOneAndUpdate({ customerId: customerId, tankName: tankName }, { $set: { waterlevel: randomNumber } })); + }, 2000); + + return { message: 'Water level will be updated every 2 seconds' }; +} + + catch (err) { + throw boom.boomify(err); + } +}; + + + diff --git a/src/models/tanks.js b/src/models/tanks.js index 48e20d3c..b7433b28 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -14,7 +14,7 @@ const tanksSchema = new mongoose.Schema({ blockName: { type: String, default: null }, capacity: { type: String, default: null }, typeOfWater: { type: String, default: null }, - water_level: { type: String, default: null }, + waterlevel: { type: String, default: null }, tankLocation: { type: String, default: null }, connections: { source: { type: String}, diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 2a4db66b..3b1261c2 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -142,6 +142,35 @@ module.exports = function (fastify, opts, next) { handler: tanksController.getTank, }); + fastify.route({ + method: "GET", + url: "/api/getTanklevels/:customerId", + schema: { + tags: ["Tank"], + summary: "This is for getting tank levels", + params: { + required: ["customerId"], + type: "object", + properties: { + customerId: { + type: "string", + description: "customerId", + }, + }, + }, + querystring: { + tankName: {type: 'string'} + }, + security: [ + { + basicAuth: [], + }, + ], + }, + preHandler: fastify.auth([fastify.authenticate]), + handler: tanksController.getTanklevels, + }); + next(); }