diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index e4c4ac31..7addce2f 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,TankWaterLevel } = require('../models/tanks') +const { Tank, MotorData, IotData,MotorIot,TankWaterLevel,TankConsumptionSchema } = require('../models/tanks') const User = require("../models/User"); const boom = require("boom"); @@ -2177,18 +2177,33 @@ cron.schedule('0 0 * * *', updatewaterlevelsatmidnight, { timezone: "Asia/Kolkata" }); -const updateWaterConsumptiontillmidnight = async () => { + + + + + + +const updatetotalConsumptiontillmidnight = async () => { console.log('Cron job triggered at:', moment().tz('Asia/Kolkata').format()); try { const tanks = await Tank.find({}); for (const tank of tanks) { - + const waterlevel_at_midnight = parseInt((tank.waterlevel_at_midnight).replace(/,/g, ''), 10); + const total_water_added_from_midnight = parseInt((tank.total_water_added_from_midnight).replace(/,/g, ''), 10); + const waterlevel = parseInt((tank.waterlevel).replace(/,/g, ''), 10); + const totalconsumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel + const newTankConsumption = new TankConsumptionSchema({ + customerId: tank.customerId, + tankName: tank.tankName, + tankLocation: tank.tankLocation, + consumption: totalconsumption.toString(), + time: new Date().toISOString() + }); - tank.waterlevel_at_midnight = tank.waterlevel; - tank.total_water_added_from_midnight = "0"; - await tank.save(); - console.log(`Updated tank ${tank._id} waterlevel_at_midnight to ${tank.waterlevel}`); + // Save the new document + await newTankConsumption.save(); + } console.log('Waterlevel noted in waterlevel_at_midnight'); } catch (error) { @@ -2197,11 +2212,14 @@ const updateWaterConsumptiontillmidnight = async () => { }; // Schedule the task to run every day at 13:49 IST (1:49 PM IST) -cron.schedule('55 23 * * *', updateWaterConsumptiontillmidnight, { +cron.schedule('55 23 * * *', updatetotalConsumptiontillmidnight, { timezone: "Asia/Kolkata" }); + + + exports.deletemotordatarecordsbefore7days = async (req, reply) => { try { // Schedule the task to run every day at 10 seconds past the minute diff --git a/src/models/tanks.js b/src/models/tanks.js index c93d5914..903c4b57 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -144,14 +144,23 @@ const tankWaterLevelSchema = new mongoose.Schema({ time: { type: String } }); - +const tankconsumptionSchema = new mongoose.Schema({ + customerId: { type: String }, + tankName: { type: String }, + tankLocation: { type: String }, + consumption: { 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); +const TankConsumptionSchema = mongoose.model("TankConsumptionSchema", tankconsumptionSchema); + module.exports = { - Tank, MotorData,IotData,TankWaterLevel + Tank, MotorData,IotData,TankWaterLevel,TankConsumptionSchema }