create a IOT device post and get apis

master
Bhaskara Kishore 3 years ago
parent 0e0398d5c8
commit 2881cf88c6

@ -1,5 +1,5 @@
//const Tank = require("../models/tanks"); //const Tank = require("../models/tanks");
const { Tank, MotorData } = require('../models/tanks') const { Tank, MotorData, IotData } = require('../models/tanks')
const User = require("../models/User"); const User = require("../models/User");
const boom = require("boom"); const boom = require("boom");
@ -510,4 +510,42 @@ exports.consumption = async (req, reply) => {
} catch (err) { } catch (err) {
throw boom.boomify(err); throw boom.boomify(err);
} }
}; };
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);
}
}

@ -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 Tank = mongoose.model("Tank", tanksSchema);
const MotorData = mongoose.model("MotorData", motordataSchema); const MotorData = mongoose.model("MotorData", motordataSchema);
const IotData = mongoose.model("IotData", IOttankSchema);
module.exports = { module.exports = {
Tank, MotorData Tank, MotorData,IotData
} }

@ -280,6 +280,55 @@ module.exports = function (fastify, opts, next) {
handler: tanksController.consumption, 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(); next();

Loading…
Cancel
Save