diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index d4c2a219..aee0a795 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -960,7 +960,7 @@ exports.calculateCapacity = async (req, reply) => { -exports.IotDevice1 = async (req, reply) => { +exports.IotDevice = async (req, reply) => { try { const { hardwareId, mode, tanks } = req.body; @@ -1010,21 +1010,35 @@ exports.IotDevice1 = async (req, reply) => { -exports.getIotD = async(req, reply) => { +// exports.getIotD = async(req, reply) => { +// try { +// await IotData.find({hardwareId: req.query.hardwareId}) +// .exec() +// .then((docs) => { +// reply.send({ status_code: 200, data: docs, count: docs.length }); +// }) +// .catch((err) => { +// console.log(err); +// reply.send({ error: err }); +// }); +// } catch (err) { +// throw boom.boomify(err); +// } +// } + +exports.getIotD = async (req, reply) => { try { - await IotData.find({hardwareId: req.query.hardwareId}) - .exec() - .then((docs) => { - reply.send({ status_code: 200, data: docs, count: docs.length }); - }) - .catch((err) => { - console.log(err); - reply.send({ error: err }); - }); + const latestRecords = await IotData.find({ hardwareId: req.query.hardwareId }) + .sort({ date: -1, time: -1 }) // Sort by date and time in descending order + .limit(3) // Limit the result to 3 records + .exec(); + + reply.send({ status_code: 200, data: latestRecords, count: latestRecords.length }); } catch (err) { + console.error(err); throw boom.boomify(err); } -} +}; exports.getLatestData = async (req, reply) => { @@ -1209,32 +1223,3 @@ exports.deletemotordatarecordsbefore7days = async (req, reply) => { } }; - -exports.IotDevice = async (req, reply) => { - try { - // Schedule the task to run every day at 10 seconds past the minute - const { hardwareId, mode, tanks } = req.body; - - // create a new tank document with the current date and time - const currentDate = new Date(); - const date = currentDate.toISOString(); // save the date as an ISO string - const time = currentDate.toLocaleTimeString('en-IN', { hour12: false, timeZone: 'Asia/Kolkata' }); - - // Create an array of tank documents - const tankDocuments = tanks.map(tank => ({ - tankhardwareId: tank.tankhardwareId, - tankHeight: tank.tankHeight, - maxLevel: tank.maxLevel, - minLevel: tank.minLevel, - date: date, - time: time - })); - reply.send({ status_code: 200,hardwareId:hardwareId, mode:mode, tanks:tanks,date: date, - time: time }); - - - } catch (err) { - throw boom.boomify(err); - } -}; - diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index af20d261..5a789e9c 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -383,7 +383,7 @@ module.exports = function (fastify, opts, next) { }, ], }, - preHandler: fastify.auth([fastify.authenticate]), + //preHandler: fastify.auth([fastify.authenticate]), handler: tanksController.getIotD, });