this is to check all iots

master
varun 3 years ago
parent 0be274c3ce
commit 63273d6dca

@ -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 });
}
};

@ -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();
}

Loading…
Cancel
Save