From 7477b29eb3e70b4420b846f898b3553c865cfa3a Mon Sep 17 00:00:00 2001 From: bhaskar Date: Tue, 17 Oct 2023 14:41:52 +0530 Subject: [PATCH 1/3] read and write the motor status --- src/controllers/tanksController.js | 54 +++++++++++++++++++++++- src/routes/tanksRoute.js | 66 ++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 1 deletion(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 26841904..6fda48a7 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1567,4 +1567,56 @@ exports.motortemperature = async (req, reply) => { catch (err) { throw boom.boomify(err); } -}; \ No newline at end of file +}; + + + + +exports.readMotorStatus = async (request, reply) => { + try { + const { motor_id, action } = request.query; + + const tank = await Tank.findOne({ motor_id }); + + if (!tank) { + return reply.code(404).send({ error: 'Tank not found' }); + } + + if (action === '1') { + tank.motor_status = '1'; + } else if (action === '2') { + tank.motor_status = '0'; + } else { + return reply.code(400).send({ error: 'Invalid action' }); + } + + await tank.save(); + + return tank; + } catch (error) { + request.log.error(error); + return reply.code(500).send({ error: 'Internal Server Error' }); + } +} + +exports.writeMotorStatus = async (request, reply) => { + try { + const { motor_id, status, current, temp } = request.body; + + const tank = await Tank.findOne({ motor_id }); + + if (!tank) { + return reply.code(404).send({ error: 'Tank not found' }); + } + + tank.motor_status = status; + tank.motor_temperature = temp || tank.motor_temperature; + + await tank.save(); + + return tank; + } catch (error) { + request.log.error(error); + return reply.code(500).send({ error: 'Internal Server Error' }); + } +} \ No newline at end of file diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 74908da0..a0878b2d 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -686,6 +686,72 @@ module.exports = function (fastify, opts, next) { }); + + + + fastify.post('/api/motor/write', { + schema: { + tags: ["Tank"], + description: "This is to Write the motor status", + summary: "This is to Write the motor status", + body: { + type: 'object', + required: ['motor_id', 'status'], + properties: { + motor_id: { type: 'string' }, + status: { type: 'string', enum: ['on', 'off'] }, + current: { type: 'string' }, + temp: { type: 'string' }, + }, + }, + // response: { + // 200: { + // type: 'object', + // properties: { + // // Define your response properties here + // motor_id: { type: 'string' }, + // status: { type: 'string' }, + // current: { type: 'string' }, + // temp: { type: 'string' }, + // // Add other properties as needed + // }, + // }, + // }, + }, + handler: tanksController.writeMotorStatus + + }); + + + fastify.get('/api/motor/read', { + schema: { + tags: ["Tank"], + description: "This is to Read the motor status", + summary: "This is to Read the motor status", + querystring: { + type: 'object', + properties: { + motor_id: { type: 'string' }, + action: { type: 'string', enum: ['1', '2'] }, + }, + required: ['motor_id', 'action'], + }, + // response: { + // 200: { + // type: 'object', + // properties: { + // // Define your response properties here + // motor_id: { type: 'string' }, + // motor_status: { type: 'string' }, + // motor_speed: { type: 'string' }, + // motor_temperature: { type: 'string' }, + // // Add other properties as needed + // }, + // }, + // }, + }, + handler: tanksController.readMotorStatus + }); next(); } From bd50f4c8e645a39c2b2d6936e2ad0cc8132a623b Mon Sep 17 00:00:00 2001 From: bhaskar Date: Tue, 17 Oct 2023 14:44:03 +0530 Subject: [PATCH 2/3] read and write motor status --- src/controllers/tanksController.js | 3 ++- src/routes/tanksRoute.js | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 6fda48a7..c07bfa30 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1619,4 +1619,5 @@ exports.writeMotorStatus = async (request, reply) => { request.log.error(error); return reply.code(500).send({ error: 'Internal Server Error' }); } -} \ No newline at end of file +} + diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index a0878b2d..ad6edc6c 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -752,6 +752,8 @@ module.exports = function (fastify, opts, next) { }, handler: tanksController.readMotorStatus }); + + next(); } From ebb0fc10505a1f8011a2fea26f882b4fcca7b875 Mon Sep 17 00:00:00 2001 From: bhaskar Date: Mon, 4 Dec 2023 14:58:03 +0530 Subject: [PATCH 3/3] read and write changes --- src/routes/tanksRoute.js | 126 +++++++++++++++++++-------------------- 1 file changed, 63 insertions(+), 63 deletions(-) diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 19376aa7..ddd6d096 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -638,69 +638,69 @@ module.exports = function (fastify, opts, next) { - fastify.post('/api/motor/write', { - schema: { - tags: ["Tank"], - description: "This is to Write the motor status", - summary: "This is to Write the motor status", - body: { - type: 'object', - required: ['motor_id', 'status'], - properties: { - motor_id: { type: 'string' }, - status: { type: 'string', enum: ['on', 'off'] }, - current: { type: 'string' }, - temp: { type: 'string' }, - }, - }, - // response: { - // 200: { - // type: 'object', - // properties: { - // // Define your response properties here - // motor_id: { type: 'string' }, - // status: { type: 'string' }, - // current: { type: 'string' }, - // temp: { type: 'string' }, - // // Add other properties as needed - // }, - // }, - // }, - }, - handler: tanksController.writeMotorStatus - - }); - - - fastify.get('/api/motor/read', { - schema: { - tags: ["Tank"], - description: "This is to Read the motor status", - summary: "This is to Read the motor status", - querystring: { - type: 'object', - properties: { - motor_id: { type: 'string' }, - action: { type: 'string', enum: ['1', '2'] }, - }, - required: ['motor_id', 'action'], - }, - // response: { - // 200: { - // type: 'object', - // properties: { - // // Define your response properties here - // motor_id: { type: 'string' }, - // motor_status: { type: 'string' }, - // motor_speed: { type: 'string' }, - // motor_temperature: { type: 'string' }, - // // Add other properties as needed - // }, - // }, - // }, - }, - handler: tanksController.readMotorStatus - }); + // fastify.post('/api/motor/write', { + // schema: { + // tags: ["Tank"], + // description: "This is to Write the motor status", + // summary: "This is to Write the motor status", + // body: { + // type: 'object', + // required: ['motor_id', 'status'], + // properties: { + // motor_id: { type: 'string' }, + // status: { type: 'string', enum: ['on', 'off'] }, + // current: { type: 'string' }, + // temp: { type: 'string' }, + // }, + // }, + // // response: { + // // 200: { + // // type: 'object', + // // properties: { + // // // Define your response properties here + // // motor_id: { type: 'string' }, + // // status: { type: 'string' }, + // // current: { type: 'string' }, + // // temp: { type: 'string' }, + // // // Add other properties as needed + // // }, + // // }, + // // }, + // }, + // handler: tanksController.writeMotorStatus + + // }); + + + // fastify.get('/api/motor/read', { + // schema: { + // tags: ["Tank"], + // description: "This is to Read the motor status", + // summary: "This is to Read the motor status", + // querystring: { + // type: 'object', + // properties: { + // motor_id: { type: 'string' }, + // action: { type: 'string', enum: ['1', '2'] }, + // }, + // required: ['motor_id', 'action'], + // }, + // // response: { + // // 200: { + // // type: 'object', + // // properties: { + // // // Define your response properties here + // // motor_id: { type: 'string' }, + // // motor_status: { type: 'string' }, + // // motor_speed: { type: 'string' }, + // // motor_temperature: { type: 'string' }, + // // // Add other properties as needed + // // }, + // // }, + // // }, + // }, + // handler: tanksController.readMotorStatus + // });