From d006a23e65c34dc78c98d260715bd28c878a6818 Mon Sep 17 00:00:00 2001 From: varun Date: Thu, 8 Jun 2023 05:52:08 -0400 Subject: [PATCH] iot devices for multiple --- src/controllers/tanksController.js | 37 +++++++++++++++--------------- src/models/tanks.js | 21 ++++++++++------- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 7081800e..90b33f0b 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -959,33 +959,32 @@ exports.calculateCapacity = async (req, reply) => { exports.IotDevice = async (req, reply) => { try { - const { hardwareId, tankHeight, maxLevel, minLevel, mode,tankhardwareId } = req.body; + 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' }); - const tank = new IotData({ hardwareId, tankHeight, maxLevel, minLevel, mode, date, time, tankhardwareId}); + const date = currentDate.toISOString(); // save the date as an ISO string + const time = currentDate.toLocaleTimeString('en-IN', { hour12: false, timeZone: 'Asia/Kolkata' }); - // save the document to MongoDB - await tank.save(); + // Create an array of tank documents + const tankDocuments = tanks.map(tank => ({ + tankhardwareId: tank.tankhardwareId, + tankHeight: tank.tankHeight, + maxLevel: tank.maxLevel, + minLevel: tank.minLevel + })); - // get all the tank documents for the current hardwareId sorted in descending order of date and time - const tanks = await IotData.find({ hardwareId,tankhardwareId }).sort({ date: -1, time: -1 }); + // create a new IOttank document with the provided data + const ottank = new IotData({ hardwareId, mode, tanks: tankDocuments, date, time }); - // if the number of documents for the current hardwareId is greater than three, remove the oldest documents until there are only three left - if (tanks.length > 3) { - const oldestDocuments = tanks.slice(3); - for (const doc of oldestDocuments) { - await IotData.deleteOne({ _id: doc._id }); - } - } + // save the document to MongoDB + await ottank.save(); - // get the latest three tank documents for the current hardwareId sorted in descending order of date and time - const latestTanks = await IotData.find({ hardwareId,tankhardwareId }).sort({ date: -1, time: -1 }).limit(3); + // 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 three documents in descending order of date and time - reply.code(200).send({ latestTanks: latestTanks.reverse() }); + // send the latest document + reply.code(200).send({ latestOttank }); } catch (err) { // send an error response reply.code(500).send({ error: err.message }); diff --git a/src/models/tanks.js b/src/models/tanks.js index 19cb4c05..f66a2033 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -81,15 +81,20 @@ const motordataSchema = new mongoose.Schema({ +const tankSchema = new mongoose.Schema({ + tankhardwareId: { type: String }, + tankHeight: { type: String, required: true }, + maxLevel: { type: String, required: true }, + minLevel: { type: String, required: true } +}); + const IOttankSchema = new mongoose.Schema({ - hardwareId: { type: String}, - tankHeight: { type: String, required: true }, - maxLevel: { type: String, required: true }, - minLevel: { type: String, required: true }, - date: { type: String, required: true }, - time: { type: String, required: true }, - mode: { type: Number, required: true } - }); + hardwareId: { type: String }, + mode: { type: Number, required: true }, + tanks: [tankSchema], + date: { type: String, required: true }, + time: { type: String, required: true } +});