From 910ff604df8cdbd9663e7425ef217c470f4a9693 Mon Sep 17 00:00:00 2001 From: varun Date: Mon, 8 Jan 2024 05:51:18 -0500 Subject: [PATCH] write motor status for sensor --- src/controllers/tanksController.js | 52 +++++++++--------------------- 1 file changed, 16 insertions(+), 36 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index fe2aeeb4..35fe470f 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1004,8 +1004,6 @@ exports.consumption = async (req, reply) => { // } // }; - - exports.calculateCapacity = async (req, reply) => { try { const shape = req.body.shape; @@ -1076,8 +1074,6 @@ exports.calculateCapacity = async (req, reply) => { } }; - - // exports.IotDevice = async (req, reply) => { // try { // const { hardwareId, mode, tanks } = req.body; @@ -1168,8 +1164,7 @@ exports.IotDevice = async (req, reply) => { // Find the corresponding tank in tanksSchema const existingTank = await Tank.findOne({ hardwareId, tankhardwareId }); - - + if (existingTank) { // Update the waterlevel using the tankHeight value @@ -1289,8 +1284,6 @@ exports.getLatestData = async (req, reply) => { }; - - exports.checkStatusofIot = async (req, reply) => { try { // get a list of unique hardware IDs in the collection @@ -1352,8 +1345,6 @@ exports.totalwaterLevelSum = async (request, reply) => { } - - exports.startUpdateLoop = async (request, reply) => { const updateInterval = 5000; @@ -1420,7 +1411,6 @@ exports.updatewaterlevelsatmidnight = async (req, reply) => { }; - exports.deletemotordatarecordsbefore7days = async (req, reply) => { try { // Schedule the task to run every day at 10 seconds past the minute @@ -1443,16 +1433,6 @@ exports.deletemotordatarecordsbefore7days = async (req, reply) => { }; - - - - - - - - - - exports.motorstatus = async (req, reply) => { try { @@ -1507,7 +1487,6 @@ exports.readMotorStatus = async (req, reply) => { 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 @@ -1515,20 +1494,18 @@ exports.writeMotorStatus = async (req, reply) => { const existingRecord = await Tank.findOne({ motor_id: motor_id }); if (existingRecord && (existingRecord.motor_stop_status === '1' || existingRecord.motor_stop_status === '2')) { - if (existingRecord.motor_stop_status === status) { - if (existingRecord.motor_status !== status) { - const result = await Tank.findOneAndUpdate( - { motor_id: motor_id }, - { $set: { motor_status: status } }, - { 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: status }); - } + 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, message: 'Status and motor_stop_status are not the same.' }); + reply.send({ status_code: 200, motor_status: newMotorStatus }); } } else { reply.send({ status_code: 200, message: 'Motor stop status is not "on" or "off".' }); @@ -1563,6 +1540,7 @@ exports.changeMotorStatus = async (req, reply) => { } }; + exports.motortemperature = async (req, reply) => { try { @@ -1584,4 +1562,6 @@ exports.motortemperature = async (req, reply) => { catch (err) { throw boom.boomify(err); } -}; \ No newline at end of file +}; + +