From 39404e6cfba925d3e820ee34638b77f044e6515f Mon Sep 17 00:00:00 2001 From: varun Date: Tue, 17 Oct 2023 06:10:16 -0400 Subject: [PATCH] write motor status --- src/controllers/tanksController.js | 49 +++++++++++++++++++++- src/routes/tanksRoute.js | 66 +++++++++++++++++++++++++++++- 2 files changed, 113 insertions(+), 2 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 88fb50f6..f3598eb7 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1346,7 +1346,7 @@ exports.totalwaterLevelSum = async (request, reply) => { } ]); - const result = waterlevelSum[0]?totalWaterlevel : 0 + const result = waterlevelSum[0]?.totalWaterlevel ?? 0; reply.send({ waterlevelSum: result }); } @@ -1567,4 +1567,51 @@ exports.motortemperature = async (req, reply) => { catch (err) { throw boom.boomify(err); } +}; + +exports.readMotorStatus = async (req, reply) => { + try { + const { motor_id, action } = req.body; + + // Perform any necessary logic based on action (1: Start, 2: Stop) + + // For example, you can update a database or trigger an action + + const motorInfo = await Tank.findOne({ motor_id: motor_id }); + const motor_stp_status = motorInfo.motor_stop_status + if (motor_stp_status === "1"){ + + } + + reply.send({ status_code: 200, message: `Motor ${motor_id} is set to ${action === '1' ? 'start' : 'stop'}` }); + } catch (err) { + throw boom.boomify(err); + } +}; + +exports.writeMotorStatus = async (req, reply) => { + try { + const { motor_id, status, current, temp } = req.body; + + // Perform any necessary logic to handle motor status update from the device + + // For example, update a database with the new status, current, and temp values + let result; + + if (status === 'on') { + result = await Tank.findOneAndUpdate( + { motor_id: motor_id }, + { $set: { motor_status: "2" } } + ); + } else if (status === 'off') { + result = await Tank.findOneAndUpdate( + { motor_id: motor_id }, + { $set: { motor_stop_status: "1" } } + ); + } + + reply.send({ status_code: 200, message: `Motor ${motor_id} status updated to ${status}` }); + } catch (err) { + throw boom.boomify(err); + } }; \ No newline at end of file diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 74908da0..42aeaf51 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -686,7 +686,71 @@ 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'], + // }, + // // 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(); }