|
|
@ -743,3 +743,46 @@ exports.getLatestData = async (req, reply) => {
|
|
|
|
reply.code(500).send({ error: err.message });
|
|
|
|
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 });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|