diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index a2cd01ac..26841904 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1,5 +1,5 @@ //const Tank = require("../models/tanks"); -const { Tank, MotorData, IotData } = require('../models/tanks') +const { Tank, MotorData, IotData,MotorIot } = require('../models/tanks') const User = require("../models/User"); const boom = require("boom"); @@ -1387,6 +1387,7 @@ exports.startUpdateLoop = async (request, reply) => { }, updateInterval); }; + exports.updatewaterlevelsatmidnight = async (req, reply) => { try { // Schedule the task to run every day at 10 seconds past the minute @@ -1441,3 +1442,129 @@ exports.deletemotordatarecordsbefore7days = async (req, reply) => { } }; + + + +exports.startmotoriot = async (req, reply) => { + + try { + var motor_id = req.body.motor_id; + + + + + const result = await Tank.findOneAndUpdate( + { motor_id: motor_id }, + { $set: { motor_status: "2" } } + ); + //return update; + console.log(result) + reply.send({ status_code: 200}); + + + + } + catch (err) { + throw boom.boomify(err); + } +}; + + + + +exports.stopmotoriot = async (req, reply) => { + + try { + var motor_id = req.body.motor_id; + + + + + const result = await Tank.findOneAndUpdate( + { motor_id: motor_id }, + { $set: { motor_status: "1" } } + ); + //return update; + console.log(result) + reply.send({ status_code: 200}); + + + + } + catch (err) { + throw boom.boomify(err); + } +}; + + + +exports.motorstatus = async (req, reply) => { + + try { + const motor_id = req.params.motor_id; + console.log(motor_id) + + const motorInfo = await Tank.findOne({ motor_id: motor_id }); + + console.log(motorInfo) + + + //return update; + + reply.send({ status_code: 200,status:motorInfo.motor_status}); + + + + } + catch (err) { + throw boom.boomify(err); + } +}; + +exports.motorspeed = async (req, reply) => { + + try { + const motor_id = req.body.motor_id; + const speed = req.body.speed; + + + + + const result = await Tank.findOneAndUpdate( + { motor_id: motor_id }, + { $set: { motor_speed: speed } } + ); + //return update; + console.log(result) + reply.send({ status_code: 200}); + + + + } + catch (err) { + throw boom.boomify(err); + } +}; + +exports.motortemperature = async (req, reply) => { + + try { + const motor_id = req.params.motor_id; + console.log(motor_id) + + const motorInfo = await Tank.findOne({ motor_id: motor_id }); + + console.log(motorInfo) + + + //return update; + + reply.send({ status_code: 200,temperature:motorInfo.motor_temperfature}); + + + + } + 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 fe18720a..66750896 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -34,6 +34,7 @@ const tanksSchema = new mongoose.Schema({ hardwareId: { type: String }, tankhardwareId: { type: String }, hardwareId_type: { type: String }, + motor_id:{ type: String ,default: null}, hardwareId_company: { type: String }, customerId: { type: String, default: null }, tankName: { type: String, default: null }, @@ -44,7 +45,9 @@ const tanksSchema = new mongoose.Schema({ typeOfWater: { type: String, default: null }, waterlevel: { type: String, default: "0" }, tankLocation: { type: String, default: null }, - motor_status: { type: String, default: "0" }, + motor_status: { type: String, default: "1" }, + motor_speed: { type: String, default: "100" }, + motor_temperature: { type: String, default: "0" }, waterlevel_at_midnight:{ type: String,default:"0" }, total_water_added_from_midnight:{ type: String,default:"0" }, connections: { @@ -108,6 +111,7 @@ const IOttankSchema = new mongoose.Schema({ const Tank = mongoose.model("Tank", tanksSchema); const MotorData = mongoose.model("MotorData", motordataSchema); + const IotData = mongoose.model("IotData", IOttankSchema); module.exports = { Tank, MotorData,IotData diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index f3532c38..74908da0 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -569,6 +569,124 @@ module.exports = function (fastify, opts, next) { handler: tanksController.getTankmotordata, }); + + + + + fastify.route({ + method: 'PUT', + url: '/api/motors/start', + + schema: { + tags: ["Tank"], + description: "This is to start motor using IOT", + summary: "This is to start motor using IOT", + body: { + type: 'object', + properties: { + motor_id: { type: 'string' }, + + }, + required: ["motor_id"] + }, + + }, + handler: tanksController.startmotoriot + }); + + + fastify.route({ + method: 'PUT', + url: '/api/motors/stop', + + schema: { + tags: ["Tank"], + description: "This is to stop motor using IOT", + summary: "This is to stop motor using IOT", + body: { + type: 'object', + properties: { + motor_id: { type: 'string' }, + + }, + required: ["motor_id"] + }, + + }, + handler: tanksController.stopmotoriot + }); + + + fastify.route({ + method: 'GET', + url: '/api/motors/status/:motor_id', + + schema: { + tags: ["Tank"], + description: "This is to get motor status", + summary: "This is to get motor status", + params: { + required: ["motor_id"], + type: "object", + properties: { + motor_id: { + type: "string", + description: "motor_id", + }, + }, + }, + + }, + handler: tanksController.motorstatus + }); + + fastify.route({ + method: 'PUT', + url: '/api/motors/speed', + + schema: { + tags: ["Tank"], + description: "This is to change motor pumping speed using IOT", + summary: "This is to to change motor pumping speed using IOT", + body: { + type: 'object', + properties: { + motor_id: { type: 'string' }, + speed: { type: 'string' }, + + }, + required: ["motor_id"] + }, + + }, + handler: tanksController.motorspeed + }); + + fastify.route({ + method: 'GET', + url: '/api/motors/temperature/:motor_id', + + schema: { + tags: ["Tank"], + description: "This is to get motor temperature", + summary: "This is to get motor temperature", + params: { + required: ["motor_id"], + type: "object", + properties: { + motor_id: { + type: "string", + description: "motor_id", + }, + }, + }, + + }, + handler: tanksController.motortemperature + }); + + + next(); }