From 63273d6dcad4f9daf0e0f830b3ef0b16bc72790d Mon Sep 17 00:00:00 2001 From: varun Date: Mon, 17 Apr 2023 01:55:26 -0400 Subject: [PATCH] this is to check all iots --- src/controllers/tanksController.js | 43 ++++++++++++++++++++++++++++++ src/routes/tanksRoute.js | 22 +++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 34449653..29d57e72 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -743,3 +743,46 @@ exports.getLatestData = async (req, reply) => { reply.code(500).send({ error: err.message }); } }; + + + + +exports.checkStatusofIot = async (req, reply) => { + try { + // get a list of unique hardware IDs in the collection + const hardwareIds = await IotData.distinct('hardwareId'); + + // create an empty object to store the time differences for each hardware ID + const timeDiffs = {}; + + // loop over each hardware ID and calculate the time difference between the latest two records + for (const hardwareId of hardwareIds) { + // get the latest two records for the current hardware ID + const latestTanks = await IotData.find({ hardwareId }).sort({ date: -1, time: -1 }).limit(2); + + // if the number of records for the current hardware ID is less than two, skip to the next ID + if (latestTanks.length < 2) { + continue; + } + + // calculate the time difference between the latest and previous records for the current hardware ID + const latestDate = new Date(latestTanks[0].date); + const previousDate = new Date(latestTanks[1].date); + const latestTime = latestTanks[0].time.split('.')[0]; // remove milliseconds + const previousTime = latestTanks[1].time.split('.')[0]; // remove milliseconds + latestDate.setHours(parseInt(latestTime.substring(0, 2)), parseInt(latestTime.substring(3, 5)), parseInt(latestTime.substring(6, 8))); + previousDate.setHours(parseInt(previousTime.substring(0, 2)), parseInt(previousTime.substring(3, 5)), parseInt(previousTime.substring(6, 8))); + const timeDiff = (latestDate.getTime() - previousDate.getTime()) / 1000; // convert from milliseconds to seconds + + // store the time difference for the current hardware ID + timeDiffs[hardwareId] = timeDiff; + } + + // send the time differences for all hardware IDs + reply.code(200).send({ timeDiffs }); + + } catch (err) { + // send an error response + reply.code(500).send({ error: err.message }); + } +}; diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index e019b106..2a6a40b8 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -402,6 +402,28 @@ module.exports = function (fastify, opts, next) { }); + fastify.route({ + method: "GET", + url: "/api/checkStatusofIot", + schema: { + tags: ["Tank"], + summary: "This is for checking the status of iots", + + // querystring: { + // tankName: {type: 'string'} + // }, + security: [ + { + basicAuth: [], + }, + ], + }, + preHandler: fastify.auth([fastify.authenticate]), + handler: tanksController.checkStatusofIot, + }); + + + next(); }