diff --git a/src/controllers/admincontroller.js b/src/controllers/admincontroller.js index 1ce9557f..2066f17f 100644 --- a/src/controllers/admincontroller.js +++ b/src/controllers/admincontroller.js @@ -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) => { @@ -68,6 +70,21 @@ 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 }) + } + } + + + \ No newline at end of file diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 29d57e72..ac54eb01 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -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; @@ -192,72 +192,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) => { @@ -786,3 +720,7 @@ exports.checkStatusofIot = async (req, reply) => { reply.code(500).send({ error: err.message }); } }; + + + + diff --git a/src/routes/adminRoute.js b/src/routes/adminRoute.js index 9d17c75b..5976ec5f 100644 --- a/src/routes/adminRoute.js +++ b/src/routes/adminRoute.js @@ -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(); }; diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 2a6a40b8..0bd6aef7 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -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: [],