From 2881cf88c60a1a6df316935a2f4814bddba09099 Mon Sep 17 00:00:00 2001 From: Bhaskara Kishore Date: Wed, 5 Apr 2023 12:24:38 +0530 Subject: [PATCH] create a IOT device post and get apis --- src/controllers/tanksController.js | 42 +++++++++++++++++++++++-- src/models/tanks.js | 16 ++++++++-- src/routes/tanksRoute.js | 49 ++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 4 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 4c7a7fcf..577b04c0 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1,5 +1,5 @@ //const Tank = require("../models/tanks"); -const { Tank, MotorData } = require('../models/tanks') +const { Tank, MotorData, IotData } = require('../models/tanks') const User = require("../models/User"); const boom = require("boom"); @@ -510,4 +510,42 @@ exports.consumption = async (req, reply) => { } catch (err) { throw boom.boomify(err); } -}; \ No newline at end of file +}; + + +exports.IotDevice = async (req, reply) => { + try { + const { hardwareId, tankHeight, maxLevel, minLevel, mode } = req.body; + + // create a new tank document + const tank = new IotData({ hardwareId, tankHeight, maxLevel, minLevel, mode }); + + // save the document to MongoDB + await tank.save(); + + // send a success response + reply.code(200).send({ message: 'Data saved successfully' }); + } catch (err) { + // send an error response + reply.code(500).send({ error: err.message }); + } +} + + + + +exports.getIotD = async(req, reply) => { + try { + await IotData.find({hardwareId: req.query.hardwareId}) + .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); + } +} \ No newline at end of file diff --git a/src/models/tanks.js b/src/models/tanks.js index b1edeb69..508f688d 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -39,9 +39,21 @@ const motordataSchema = new mongoose.Schema({ }); + + +const IOttankSchema = new mongoose.Schema({ + hardwareId: { type: String, required: true }, + tankHeight: { type: String, required: true }, + maxLevel: { type: String, required: true }, + minLevel: { type: String, required: true }, + mode: { type: Number, required: true } + }); + + + const Tank = mongoose.model("Tank", tanksSchema); const MotorData = mongoose.model("MotorData", motordataSchema); - +const IotData = mongoose.model("IotData", IOttankSchema); module.exports = { - Tank, MotorData + Tank, MotorData,IotData } diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index e96e30ec..d90089bf 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -280,6 +280,55 @@ module.exports = function (fastify, opts, next) { handler: tanksController.consumption, }); + + + fastify.route({ + method: "POST", + url: "/api/APIWrite", + schema: { + tags: ["Tank"], + description: "This is for cretae IOT Device", + summary: "This is for Create IOT Device.", + + body: { + type: "object", + properties: { + hardwareId: { type: "string" }, + tankHeight: { type: "string"}, + maxLevel: { type: "string" }, + minLevel: { type: "string" }, + + mode: { type: "string" }, + }, + }, + security: [ + { + basicAuth: [], + }, + ], + + }, + handler: tanksController.IotDevice, + + }); + + fastify.get("/api/APIRead", { + schema: { + tags: ["Tank"], + description: "This is for Get IOT Data", + summary: "This is for to Get IOT Data", + querystring: { + hardwareId: {type: 'string'} + }, + security: [ + { + basicAuth: [], + }, + ], + }, + preHandler: fastify.auth([fastify.authenticate]), + handler: tanksController.getIotD, + }); next();