storing only 3 records based on hardwareid

master
varun 2 years ago
parent f820e98db9
commit 2a99a49608

@ -958,6 +958,7 @@ exports.calculateCapacity = async (req, reply) => {
}; };
exports.IotDevice = async (req, reply) => { exports.IotDevice = async (req, reply) => {
try { try {
const { hardwareId, mode, tanks } = req.body; const { hardwareId, mode, tanks } = req.body;
@ -973,20 +974,32 @@ exports.IotDevice = async (req, reply) => {
tankHeight: tank.tankHeight, tankHeight: tank.tankHeight,
maxLevel: tank.maxLevel, maxLevel: tank.maxLevel,
minLevel: tank.minLevel, minLevel: tank.minLevel,
date:date, date: date,
time:time time: time
})); }));
// create a new IOttank document with the provided data // create a new IotData document with the provided data
const ottank = new IotData({ hardwareId, mode, tanks: tankDocuments, date, time }); const ottank = new IotData({ hardwareId, mode, tanks: tankDocuments, date, time });
// save the document to MongoDB // save the document to MongoDB
await ottank.save(); await ottank.save();
// get the latest document sorted in descending order of date and time
const latestOttank = await IotData.findOne({ hardwareId }).sort({ date: -1, time: -1 });
// send the latest document // delete previous records except the three latest ones
reply.code(200).send({ latestOttank }); const previousRecords = await IotData.find({ hardwareId })
.sort({ date: -1, time: -1 })
.skip(3); // skip the three latest documents
for (const record of previousRecords) {
await record.remove();
}
// get the latest three documents sorted in descending order of date and time
const latestOttanks = await IotData.find({ hardwareId })
.sort({ date: -1, time: -1 })
.limit(3);
// send the latest documents
reply.code(200).send({ latestOttanks });
} catch (err) { } catch (err) {
// send an error response // send an error response
reply.code(500).send({ error: err.message }); reply.code(500).send({ error: err.message });
@ -994,6 +1007,7 @@ exports.IotDevice = async (req, reply) => {
}; };
exports.getIotD = async(req, reply) => { exports.getIotD = async(req, reply) => {
try { try {
await IotData.find({hardwareId: req.query.hardwareId}) await IotData.find({hardwareId: req.query.hardwareId})

Loading…
Cancel
Save