made changes in tanks controller

master
varun 3 years ago
parent d7be8badb0
commit 8702c293c7

@ -142,54 +142,52 @@ exports.getTank = async (req, reply) => {
//}; //};
exports.updateTanklevels = async (req, reply) => { exports.updateTanklevels = async (req, reply) => {
try { try {
const customerId = req.params.customerId; const customerId = req.params.customerId;
const tank = await Tank.find({customerId}); const tanks = await Tank.find({ customerId });
const intervals = {};
for (var i=0; i < tank.length; i++) {
const tankname = tank[i].tankName for (const tank of tanks) {
const capacity = (tank[i].capacity).replace(/,/g, "") const tankName = tank.tankName;
let capacity = parseInt(tank.capacity.replace(/,/g, ''), 10);
let Number = capacity-100 //Math.floor(Math.random() * capacity)
let waterLevel = capacity - 100; // initial water level
const intervalId = setInterval(async function () {
const newWaterLevel = Math.floor(waterLevel / 1.5);
//console.log(Number)
setInterval(async function () { const result = await Tank.findOneAndUpdate(
{ customerId, tankName },
// const randomNumber = Math.floor(Math.random() * capacity) { $set: { waterlevel: newWaterLevel } }
//const randomNumber = Math.floor(Math.random() * (5500 - 1000) + 1000); );
//console.log(tankname) console.log(result);
const randomNumber = Math.floor(Number/1.5)
if (newWaterLevel === 0) {
clearInterval(intervals[tankName]);
const tank_info =await Tank.findOneAndUpdate({customerId:customerId,tankName:tankname},{$set:{waterlevel:randomNumber}}) console.log(`Stopped updating ${tankName}`);
console.log(tank_info) return;
Number = randomNumber }
//return Number
}, 2000);
}
return { message: 'Water level will be updated every 2 seconds' };
}
catch (err) { waterLevel = newWaterLevel;
}, 2000);
intervals[tankName] = intervalId;
}
return { message: 'Water level updates started' };
} catch (err) {
throw boom.boomify(err); throw boom.boomify(err);
} }
}; };
exports.getTanklevels = async (req, reply) => { exports.getTanklevels = async (req, reply) => {
try { try {
const customerId = req.params.customerId; const customerId = req.params.customerId;

Loading…
Cancel
Save