From 860a4e47a76e7e42cc1ca2fdbf8de5023abcb160 Mon Sep 17 00:00:00 2001 From: varun Date: Thu, 11 Jan 2024 06:48:56 -0500 Subject: [PATCH] changed motor status read and write --- src/controllers/tanksController.js | 94 ++++++++++++++++++++++++------ src/routes/tanksRoute.js | 20 +++++++ 2 files changed, 95 insertions(+), 19 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index c1904b40..670ec9db 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1474,42 +1474,98 @@ exports.readMotorStatus = async (req, reply) => { return reply.status(404).send({ status_code: 404, message: 'Tank not found for the specified motor_id' }); } - const motor_stop_status = motorInfo.motor_stop_status; + const motor_status = motorInfo.motor_status; - reply.send({ status_code: 200, motor_stop_status:motor_stop_status }); + reply.send({ status_code: 200, motor_status:motor_status }); } catch (err) { throw boom.boomify(err); } }; + +exports.readMotorStatusFromIot = async (req, reply) => { + try { + + const motor_id = req.query.motor_id; + console.log(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 }); + + if (!motorInfo) { + return reply.status(404).send({ status_code: 404, message: 'Tank not found for the specified motor_id' }); + } + + const motor_status = motorInfo.motor_status; + + + + reply.send({ status_code: 200, motor_status:motor_status }); + } catch (err) { + throw boom.boomify(err); + } +}; + + + +// exports.writeMotorStatus = async (req, reply) => { +// try { +// const motor_id = req.body.motor_id; + +// // 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 existingRecord = await Tank.findOne({ motor_id: motor_id }); + +// if (existingRecord && (existingRecord.motor_stop_status === '1' || existingRecord.motor_stop_status === '2')) { +// const newMotorStatus = existingRecord.motor_stop_status; + +// if (existingRecord.motor_status !== newMotorStatus) { +// const result = await Tank.findOneAndUpdate( +// { motor_id: motor_id }, +// { $set: { motor_status: newMotorStatus } }, +// { new: true } // To return the updated document +// ); + +// reply.send({ status_code: 200, motor_status: result.motor_status }); +// } else { +// reply.send({ status_code: 200, motor_status: newMotorStatus }); +// } +// } else { +// reply.send({ status_code: 200, message: 'Motor stop status is not "on" or "off".' }); +// } +// } catch (err) { +// throw boom.boomify(err); +// } +// }; + exports.writeMotorStatus = async (req, reply) => { try { const motor_id = req.body.motor_id; + const status = req.body.status; // 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 existingRecord = await Tank.findOne({ motor_id: motor_id }); - - if (existingRecord && (existingRecord.motor_stop_status === '1' || existingRecord.motor_stop_status === '2')) { - const newMotorStatus = existingRecord.motor_stop_status; + const result = await Tank.findOneAndUpdate( + { motor_id: motor_id }, + { $set: { motor_status: status } + }); - if (existingRecord.motor_status !== newMotorStatus) { - const result = await Tank.findOneAndUpdate( - { motor_id: motor_id }, - { $set: { motor_status: newMotorStatus } }, - { new: true } // To return the updated document - ); + // Fetch the motor_status for the given motor_id + const updatedMotor = await Tank.findOne({ motor_id: motor_id }); - reply.send({ status_code: 200, motor_status: result.motor_status }); - } else { - reply.send({ status_code: 200, motor_status: newMotorStatus }); - } - } else { - reply.send({ status_code: 200, message: 'Motor stop status is not "on" or "off".' }); - } + // Send the response with motor_stop_status and motor_status + reply.send({ + status_code: 200, + motor_status: status, + // Assuming motor_status is a field in your Tank model + }); } catch (err) { throw boom.boomify(err); } diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 7d25fcc9..7b85b847 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -620,6 +620,26 @@ module.exports = function (fastify, opts, next) { }); + fastify.get('/api/motor/readMotorIotStatus', { + schema: { + tags: ["Tank"], + description: "This is to Read the motor status from Iot", + summary: "This is to Read the motor status from Iot", + querystring: { + type: 'object', + properties: { + motor_id: { type: 'string' }, + + }, + // required: ['motor_id'], + }, + + + + }, + handler: tanksController.readMotorStatusFromIot + }); + fastify.post('/api/motor/changeMotorStatus', { schema: { tags: ["Tank"],