From 321f3e5a38d0ea21a2e19a342a3d0f94ea0ece0d Mon Sep 17 00:00:00 2001 From: varun Date: Fri, 24 May 2024 05:01:50 -0400 Subject: [PATCH] storing waterlevels for every 15 minutes --- src/controllers/tanksController.js | 27 ++++++++++++++++++++++++++- src/models/tanks.js | 11 +++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 22ce0747..d291a0fa 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1,5 +1,5 @@ //const Tank = require("../models/tanks"); -const { Tank, MotorData, IotData,MotorIot } = require('../models/tanks') +const { Tank, MotorData, IotData,MotorIot,TankWaterLevel } = require('../models/tanks') const User = require("../models/User"); const boom = require("boom"); @@ -2373,3 +2373,28 @@ exports.update_auto_percentage = async (req, reply) => { throw boom.boomify(error); } }; + + +//storing water level for every 15 minutes + +const storeWaterLevels = async () => { + try { + const tanks = await Tank.find({}); + const currentTime = new Date().toISOString(); + + const waterLevelRecords = tanks.map(tank => ({ + customerId: tank.customerId, + tankName: tank.tankName, + tankLocation: tank.tankLocation, + waterlevel: tank.waterlevel, + time: currentTime + })); + + await TankWaterLevel.insertMany(waterLevelRecords); + console.log('Water levels stored successfully'); + } catch (error) { + console.error('Error storing water levels:', error); + } +}; + +setInterval(storeWaterLevels, 15 * 60 * 1000); // Run every 15 minutes diff --git a/src/models/tanks.js b/src/models/tanks.js index 3e6e3532..defcd2d6 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -134,15 +134,22 @@ const IOttankSchema = new mongoose.Schema({ }); +const tankWaterLevelSchema = new mongoose.Schema({ + customerId: { type: String }, + tankName: { type: String }, + tankLocation: { type: String }, + waterlevel: { type: String }, + time: { type: String } +}); const Tank = mongoose.model("Tank", tanksSchema); const MotorData = mongoose.model("MotorData", motordataSchema); - +const TankWaterLevel = mongoose.model("TankWaterLevel", tankWaterLevelSchema); const IotData = mongoose.model("IotData", IOttankSchema); module.exports = { - Tank, MotorData,IotData + Tank, MotorData,IotData,TankWaterLevel }