From cfe1351d6333842530a30a1f723a277f2661f959 Mon Sep 17 00:00:00 2001 From: varun Date: Thu, 15 Jun 2023 06:36:50 -0400 Subject: [PATCH] changes regarding iot data --- src/controllers/tanksController.js | 74 ++++++++++++++++++++---------- src/models/tanks.js | 4 +- 2 files changed, 54 insertions(+), 24 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 0aefd7cb..36580270 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -956,43 +956,71 @@ exports.calculateCapacity = async (req, reply) => { } }; - exports.IotDevice = async (req, reply) => { try { const { hardwareId, mode, tanks } = req.body; - // create a new tank document with the current date and time + // Find the existing IOttank document with the provided hardwareId + let ottank = await IotData.findOne({ hardwareId }); + + // If no existing document is found, create a new one + if (!ottank) { + ottank = new IotData({ hardwareId, mode, tanks: [], date: '', time: '' }); + } + + // Update the date and time of the IOttank document 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' }); - - // Create an array of tank documents - const tankDocuments = tanks.map(tank => ({ - tankhardwareId: tank.tankhardwareId, - tankHeight: tank.tankHeight, - maxLevel: tank.maxLevel, - minLevel: tank.minLevel - - })); + ottank.date = currentDate.toISOString(); + ottank.time = currentDate.toLocaleTimeString('en-IN', { hour12: false, timeZone: 'Asia/Kolkata' }); + + // Remove excess tank records for each tankhardwareId + for (const tank of ottank.tanks) { + if (tank.tankhardwareId === tank.tankhardwareId) { + const tankRecords = ottank.tanks.filter(t => t.tankhardwareId === tank.tankhardwareId); + if (tankRecords.length > 3) { + tankRecords.sort((a, b) => new Date(b.date) - new Date(a.date)); + const excessRecords = tankRecords.slice(3); + ottank.tanks = ottank.tanks.filter(t => !excessRecords.includes(t)); + } + } + } - // create a new IOttank document with the provided data - const ottank = new IotData({ hardwareId, mode, tanks: tankDocuments, date, time }); + // Iterate through the tanks received in the request + for (const tank of tanks) { + // Find the existing tank document with the provided tankhardwareId + const existingTank = ottank.tanks.find(t => t.tankhardwareId === tank.tankhardwareId); + + if (existingTank) { + // Update the existing tank document + existingTank.tankHeight = tank.tankHeight; + existingTank.maxLevel = tank.maxLevel; + existingTank.minLevel = tank.minLevel; + existingTank.date = ottank.date; + existingTank.time = ottank.time; + } else { + // Create a new tank document and add it to the IOttank document + ottank.tanks.push({ + tankhardwareId: tank.tankhardwareId, + tankHeight: tank.tankHeight, + maxLevel: tank.maxLevel, + minLevel: tank.minLevel, + date: ottank.date, + time: ottank.time + }); + } + } - // save the document to MongoDB + // Save the updated or new IOttank document to MongoDB 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 - reply.code(200).send({ latestOttank }); + // Send the updated or new IOttank document + reply.code(200).send({ latestOttank: ottank }); } catch (err) { - // send an error response + // Send an error response reply.code(500).send({ error: err.message }); } }; - exports.getIotD = async(req, reply) => { try { await IotData.find({hardwareId: req.query.hardwareId}) diff --git a/src/models/tanks.js b/src/models/tanks.js index 2b279362..359d0faf 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -85,7 +85,9 @@ const tankSchema = new mongoose.Schema({ tankhardwareId: { type: String }, tankHeight: { type: String, required: true }, maxLevel: { type: String, required: true }, - minLevel: { type: String, required: true } + minLevel: { type: String, required: true }, + date: { type: String, required: true }, + time: { type: String, required: true } }); const IOttankSchema = new mongoose.Schema({