api to attach hardwareId to tank,changes in delete tanks

master
varun 3 years ago
parent 63273d6dca
commit 952b98dbe1

@ -4,6 +4,8 @@ const jwt = require('jsonwebtoken')
const bcrypt = require('bcrypt')
const fastify = require("fastify");
const { Tank, MotorData, IotData } = require('../models/tanks')
exports.adminSignUp = async (request, reply) => {
@ -70,4 +72,19 @@ exports.adminSignUp = async (request, reply) => {
exports.integratingHardwareidToTank = async (request, reply) => {
try {
const { customerId, tankName,tankLocation,hardwareId } = request.body
const tank = await Tank.findOneAndUpdate({customerId, tankName:tankName ,tankLocation:tankLocation.toLowerCase()}, { $set: { hardwareId: hardwareId } });
reply.send({ status_code: 200, message:`${hardwareId} set to ${tankName}` });
} catch (err) {
reply.status(500).send({ message: err.message })
}
}

@ -102,7 +102,7 @@ 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 });
const tank = await Tank.findOneAndDelete({ tankName: tankName,customerId:customerId,tankLocation:req.body.tankLocation.toLowerCase() });
reply.send({ status_code: 200, data: tank});
// return tank;
@ -193,72 +193,6 @@ exports.updateTanklevels = async (req, reply) => {
exports.updateTanklevels1 = async (req, reply) => {
try {
const customerId = req.params.customerId;
const tanks = await Tank.find({ customerId });
const intervals = {};
for (const tank of tanks) {
const tankName = tank.tankName;
const tank_type = tank.tankLocation
let capacity = parseInt(tank.capacity.replace(/,/g, ''), 10);
const waterLevel = parseInt(tank.waterLevel.replace(/,/g, ''), 10);
//capacity - 100; // initial water level
const intervalId = setInterval(async function () {
const motor_status = tank.motor_status
if(motor_status === "0"){
const newWaterLevel = Math.floor(waterLevel - 200);
console.log(tank.tankName,newWaterLevel)
const result = await Tank.findOneAndUpdate(
{ customerId, tankName, },
{ $set: { waterlevel: newWaterLevel } }
);
if (newWaterLevel === 0) {
clearInterval(intervals[tankName]);
console.log(`Stopped updating ${tankName}`);
return;
}
waterLevel = newWaterLevel;
}
else{
if(tank.tankLocation==="overhead",motor_status==="1"){
tank_waterlevel = math.floor(waterLevel + 250)
const supplier_water= await findOne({customerId:req.params.customerId,tankName:req.body.from,tankLocation:req.body.from_type})
const supplier_waterlevel = parseInt(supplier_water.waterlevel .replace(/,/g, ''), 10);
const newSupplierWaterLevel = math.floor(supplier_waterlevel - 250)
await Tank.findOneAndUpdate({customerId:req.params.customerId,tankName:req.body.from,tankLocation:req.body.from_type}, { $set: { waterlevel: newSupplierWaterLevel } })
await tank.save()
}
if(tank.tankLocation==="sump",motor_status==="1"){
const sumpwaterlevel = math.floor(waterLevel + 250)
await tank.save()
}
}
// console.log(result);
}, 2000);
intervals[tankName] = intervalId;
}
return { message: 'Water level updates started' };
} catch (err) {
throw boom.boomify(err);
}
};
exports.getTanklevels = async (req, reply) => {
try {
@ -786,3 +720,7 @@ exports.checkStatusofIot = async (req, reply) => {
reply.code(500).send({ error: err.message });
}
};

@ -46,6 +46,27 @@ fastify.route({
handler: adminController.adminLogin,
});
fastify.post("/api/integratingHardwareidToTank", {
schema: {
description: "This is for integrating hardwareId with tank",
tags: ["Admin"],
summary: "This is for integrating hardwareId with tank",
body: {
type: "object",
properties: {
customerId: { type: "string" },
tankName: { type: "string" },
tankLocation: { type: "string" },
hardwareId: { type: "string" },
},
},
},
handler: adminController.integratingHardwareidToTank,
});
next();
};

@ -114,6 +114,15 @@ module.exports = function (fastify, opts, next) {
querystring: {
tankName: {type: 'string'}
},
body: {
type: "object",
// required: ['phone'],
properties: {
tankLocation: { type: "string", default: null },
},
},
security: [
{
basicAuth: [],

Loading…
Cancel
Save