From 9f7eae7f95305fc526b682ecb9827b286f3a026e Mon Sep 17 00:00:00 2001 From: varun Date: Tue, 14 Feb 2023 02:15:00 -0500 Subject: [PATCH] working with water level --- src/controllers/tanksController.js | 43 +++++++++++++++++++++++------- src/routes/tanksRoute.js | 6 ++--- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 2107ff18..86d137e6 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -114,17 +114,42 @@ exports.getTank = async (req, reply) => { throw boom.boomify(err); } }; +//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); + // } +//}; + 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); - + const tank = await Tank.find({customerId}); + + for (var i=0; i < tank.length; i++) { + const tankname = tank[i].tankName + const capacity = tank[i].capacity + //console.log(capacity) + setInterval(async function () { + const randomNumber = Math.floor(Math.random() * capacity) + //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' }; } @@ -132,6 +157,4 @@ exports.getTanklevels = async (req, reply) => { throw boom.boomify(err); } }; - - diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 3b1261c2..ad569d1d 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -158,9 +158,9 @@ module.exports = function (fastify, opts, next) { }, }, }, - querystring: { - tankName: {type: 'string'} - }, + // querystring: { + // tankName: {type: 'string'} + // }, security: [ { basicAuth: [],