creating water level randomly

master
varun 3 years ago
parent 45cfc5917e
commit 3b93adf0b2

@ -113,4 +113,25 @@ exports.getTank = async (req, reply) => {
} catch (err) { } catch (err) {
throw boom.boomify(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);
return { message: 'Water level will be updated every 2 seconds' };
}
catch (err) {
throw boom.boomify(err);
}
};

@ -14,7 +14,7 @@ const tanksSchema = new mongoose.Schema({
blockName: { type: String, default: null }, blockName: { type: String, default: null },
capacity: { type: String, default: null }, capacity: { type: String, default: null },
typeOfWater: { 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 }, tankLocation: { type: String, default: null },
connections: { connections: {
source: { type: String}, source: { type: String},

@ -142,6 +142,35 @@ module.exports = function (fastify, opts, next) {
handler: tanksController.getTank, 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(); next();
} }

Loading…
Cancel
Save