const Tank = require("../models/tanks"); const User = require("../models/User"); const boom = require("boom"); const fastify = require("fastify")({ logger: true, }); exports.addTanks = async (req, reply) => { try { var customerId = req.params.customerId //const username = req.params.username; console.log(req.params); //const {username} = loginObject.user.username; //console.log(loginObject.user.username) // const userInfo = await User.findOne({ username: username.toString() }); // const updateData = req.body; // console.log("This is the reply in the handler after the validations", reply); tankData = { customerId: customerId, tankName: req.body.tankName, blockName: req.body.blockName, capacity: req.body.capacity, typeOfWater: req.body.typeOfWater, tankLocation:req.body.tankLocation, }; var tank_name = req.body.tankName var i_tank = await Tank.findOne({ tankName: tank_name,customerId:customerId}) if(i_tank){ throw new Error('tankname already exists'); } else { var tank = new Tank(tankData); checkFormEncoding = isUserFormUrlEncoded(req); if (checkFormEncoding.isUserFormUrlEncoded) { usertobeInserted = checkFormEncoding.tank; console.log("thsi true url string"); tank.customerId = usertobeInserted.customerId tank.tankName = usertobeInserted.tankName; tank.blockName = usertobeInserted.blockName; tank.capacity = usertobeInserted.capacity; tank.typeOfWater = usertobeInserted.typeOfWater; tank.tankLocation = usertobeInserted.tankLocation; } } const insertedTank = await tank.save(); return insertedTank; } catch (err) { throw boom.boomify(err); } }; //update selected tank exports.updateTanksInfo = async (req, reply) => { try { var customerId = req.params.customerId; var tankName = req.query.tankName; const tank = req.body; const { ...updateData } = tank; const update = await Tank.findOneAndUpdate({ tankName: tankName,customerId:customerId, }, updateData, { new: true }); console.log(update.username) //return update; reply.send({ status_code: 200, data: update }); } catch (err) { throw boom.boomify(err); } }; //delete selected tank exports.deleteTanksInfo = async (req, reply) => { try { var customerId = req.params.customerId; var tankName = req.query.tankName; const tank = await Tank.findOneAndDelete({ tankName: tankName,customerId:customerId }); reply.send({ status_code: 200, data: tank}); // return tank; } catch (err) { throw boom.boomify(err); } }; //get tanks data by passing username exports.getTank = async (req, reply) => { try { await Tank.find({customerId: req.query.customerId}) .exec() .then((docs) => { reply.send({ status_code: 200, data: docs, count: docs.length }); }) .catch((err) => { console.log(err); reply.send({ error: err }); }); } 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); // 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 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' }; } catch (err) { throw boom.boomify(err); } };