From 321f3e5a38d0ea21a2e19a342a3d0f94ea0ece0d Mon Sep 17 00:00:00 2001 From: varun Date: Fri, 24 May 2024 05:01:50 -0400 Subject: [PATCH 1/2] 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 } From bded7a1e594733454a9148ecc99c2fef9dd1b40c Mon Sep 17 00:00:00 2001 From: varun Date: Mon, 27 May 2024 04:19:59 -0400 Subject: [PATCH 2/2] This is for getting tank levels --- src/controllers/tanksController.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index d291a0fa..702b6253 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -2377,10 +2377,14 @@ exports.update_auto_percentage = async (req, reply) => { //storing water level for every 15 minutes +const getFormattedISTTime = () => { + return moment().tz('Asia/Kolkata').format('DD-MM-YYYY hh:mm:ss A'); +}; + const storeWaterLevels = async () => { try { const tanks = await Tank.find({}); - const currentTime = new Date().toISOString(); + const currentTime = getFormattedISTTime(); const waterLevelRecords = tanks.map(tank => ({ customerId: tank.customerId, @@ -2397,4 +2401,4 @@ const storeWaterLevels = async () => { } }; -setInterval(storeWaterLevels, 15 * 60 * 1000); // Run every 15 minutes +setInterval(storeWaterLevels, 15 * 60 * 1000);