diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index f3598eb7..9cf0cb37 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1445,56 +1445,11 @@ 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); - } -}; @@ -1521,69 +1476,20 @@ exports.motorstatus = async (req, reply) => { } }; -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); - } -}; exports.readMotorStatus = async (req, reply) => { try { - const { motor_id, action } = req.body; + const motor_id = req.query.motor_id; // 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"){ + const motor_stp_status = motorInfo.motor_status - } - reply.send({ status_code: 200, message: `Motor ${motor_id} is set to ${action === '1' ? 'start' : 'stop'}` }); + reply.send({ status_code: 200, motor_status:motor_stp_status }); } catch (err) { throw boom.boomify(err); } @@ -1614,4 +1520,32 @@ exports.writeMotorStatus = async (req, reply) => { } catch (err) { throw boom.boomify(err); } -}; \ No newline at end of file +}; + +exports.changeMotorStatus = async (req, reply) => { + try { + const motor_id = req.body.motor_id; + const action =req.body.action + + // 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 + + + + const result = await Tank.findOneAndUpdate( + { motor_id: motor_id }, + { $set: { motor_status: action } }); + + + + reply.send({ status_code: 200}); + } catch (err) { + throw boom.boomify(err); + } +}; + + + + + diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 42aeaf51..3d9a2584 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -573,184 +573,67 @@ module.exports = function (fastify, opts, next) { - fastify.route({ - method: 'PUT', - url: '/api/motors/start', - + fastify.post('/api/motor/write', { schema: { tags: ["Tank"], - description: "This is to start motor using IOT", - summary: "This is to start motor using IOT", + 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' }, }, - required: ["motor_id"] }, - + }, - handler: tanksController.startmotoriot + handler: tanksController.writeMotorStatus + }); - fastify.route({ - method: 'PUT', - url: '/api/motors/stop', - + fastify.get('/api/motor/read', { schema: { tags: ["Tank"], - description: "This is to stop motor using IOT", - summary: "This is to stop motor using IOT", - body: { + description: "This is to Read the motor status", + summary: "This is to Read the motor status", + querystring: { type: 'object', properties: { motor_id: { type: 'string' }, - + }, - required: ["motor_id"] + // required: ['motor_id'], }, - + }, - handler: tanksController.stopmotoriot + handler: tanksController.readMotorStatus }); - 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', - + fastify.post('/api/motor/changeMotorStatus', { schema: { tags: ["Tank"], - description: "This is to change motor pumping speed using IOT", - summary: "This is to to change motor pumping speed using IOT", + description: "This is to change the motor status", + summary: "This is to change the motor status", + body: { type: 'object', properties: { motor_id: { type: 'string' }, - speed: { type: 'string' }, + action: { type: 'string' }, }, required: ["motor_id"] }, }, - handler: tanksController.motorspeed + handler: tanksController.changeMotorStatus }); - 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 - }); - - - 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(); }