From b4f41de1c628c01bbfddfb064d4fbbd61b8f26f9 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Tue, 2 Jul 2024 10:50:27 +0530 Subject: [PATCH] changes --- package.json | 1 - src/controllers/tanksController.js | 824 ++++++++++++++++------------- 2 files changed, 449 insertions(+), 376 deletions(-) diff --git a/package.json b/package.json index aef128eb..a49b9b24 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,6 @@ "fastify-static": "^4.7.0", "fastify-swagger": "^5.2.0", "file-type": "^18.5.0", - "firebase-admin": "^12.2.0", "form-data": "^2.3.3", "formidable": "^2.1.1", "fs": "^0.0.1-security", diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 9e96cc4a..e7820ffd 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,TankConsumptionSchema } = require('../models/tanks') const User = require("../models/User"); const boom = require("boom"); @@ -838,31 +838,85 @@ let supplier_tanks = []; // }; -exports.consumption = async (req, reply) => { +// exports.consumption = async (request, reply) => { +// try { +// const customerId = req.params.customerId; +// const tanks = await Tank.find({ customerId,tankLocation:"overhead"}); +// const tankData = []; +// for (const tank of tanks) { +// const tankId = tank._id; +// 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 tankname = tank.tankName +// const consumption = (waterlevel_at_midnight+total_water_added_from_midnight)-waterlevel + +// tankData.push({ tankname, waterLevel: consumption }); + + +// // console.log(newWaterLevel); + +// } +// reply.send({ status_code: 200, tankData }); + +// return { message: 'Water level updates started' }; +// } catch (err) { +// throw boom.boomify(err); +// } + + + + + +function parseDateTime(dateTimeStr) { + const [datePart, timePart] = dateTimeStr.split(' - '); + const [day, month, year] = datePart.split('-'); + const monthIndex = new Date(Date.parse(month +" 1, 2024")).getMonth(); // Parse the month name to get its index + const [hours, minutes] = timePart.split(':'); + return new Date(year, monthIndex, day, hours, minutes); +} + + + +exports.consumption = async (request, reply) => { try { - const customerId = req.params.customerId; - const tanks = await Tank.find({ customerId,tankLocation:"overhead"}); - const tankData = []; + + const { customerId } = request.params; + const { startDate, stopDate } = request.body; + + const start = parseDateTime(startDate); + const end = parseDateTime(stopDate); + const tanks = await Tank.find({ customerId, tankLocation: "overhead" }); + const tankData = []; + for (const tank of tanks) { const tankId = tank._id; 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 tankname = tank.tankName - const consumption = (waterlevel_at_midnight+total_water_added_from_midnight)-waterlevel - - const newWaterLevel1 = Math.floor(consumption); - const newWaterLevel=Math.abs(newWaterLevel1) - tankData.push({ tankname, waterLevel: newWaterLevel }); + const tankname = tank.tankName; + console.log(waterlevel_at_midnight,total_water_added_from_midnight,waterlevel) + const tankConsumptions = await TankConsumptionSchema.find({ + customerId, + tankName: tank.tankName, + tankLocation: tank.tankLocation, + time: { + $gte: start, + $lte: end + } + }); - - // console.log(newWaterLevel); + const total_consumption_from_records = tankConsumptions.reduce((acc, record) => { + return acc + parseInt(record.consumption, 10); + }, 0); + const consumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel + total_consumption_from_records; + + tankData.push({ tankname, totalConsumption: consumption }); } - reply.send({ status_code: 200, tankData }); - return { message: 'Water level updates started' }; + reply.send({ status_code: 200, tankData }); } catch (err) { throw boom.boomify(err); } @@ -870,173 +924,135 @@ exports.consumption = async (req, reply) => { -//const moment = require('moment'); // Import moment.js for date/time operations - - -const admin = require('firebase-admin'); -// Initialize Firebase Admin SDK (make sure this is done once in your app) -const serviceAccount = require('../arminta-water-notification-firebase-adminsdk-mhjrk-c90bbe0594.json'); +const delay = ms => new Promise(resolve => setTimeout(resolve, ms)); -admin.initializeApp({ - credential: admin.credential.cert(serviceAccount), -}); - -// Helper function to send push notifications -const sendPushNotification = async (registrationToken, title, body) => { - const message = { - notification: { - title: title, - body: body, - }, - token: registrationToken, - }; - - try { - const response = await admin.messaging().send(message); - console.log('FCM response:', response); // Log the FCM response - return response; // Return the FCM response object - } catch (error) { - console.error('FCM error:', error); - throw error; // Throw the error to handle it further up the call stack - } +//const moment = require('moment'); // Import moment.js for date/time operations +const formatDate = (date) => { + const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; + const day = String(date.getDate()).padStart(2, '0'); + const month = months[date.getMonth()]; + const year = date.getFullYear(); + const hours = String(date.getHours()).padStart(2, '0'); + const minutes = String(date.getMinutes()).padStart(2, '0'); + return `${day}-${month}-${year} - ${hours}:${minutes}`; }; -// Example usage in your motorAction function exports.motorAction = async (req, reply) => { try { const customerId = req.params.customerId; const action = req.body.action; const motorId = req.body.motor_id; - const start_instance_id = req.body.start_instance_id; - const startTime = req.body.startTime; - const stopTime = req.body.stopTime; - const threshold_type = req.body.threshold_type; - const manual_threshold_time = req.body.manual_threshold_time; - const manual_threshold_litres = req.body.manual_threshold_litres; - const from = req.body.from; - const to = req.body.to; - const from_type = req.body.from_type; - const to_type = req.body.to_type; - - console.log(startTime); - + const start_instance_id = req.body.start_instance_id + console.log(req.body.startTime) + // Ensure motor_id is provided if (!motorId) { throw new Error("Motor ID is required."); } + // Determine the motor stop status based on the action let motorStopStatus; if (action === "start") { - motorStopStatus = "2"; + motorStopStatus = "2"; // If action is start, set stop status to "2" } else if (action === "stop") { - motorStopStatus = "1"; + motorStopStatus = "1"; // If action is stop, set stop status to "1" } else { throw new Error("Invalid action provided."); } - const users = await User.find({ customerId: customerId }); - const fcmIds = users.map(user => user.fcmId).filter(fcmId => fcmId); - + // Update the motor stop status immediately if action is stop if (action === "stop") { await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId }, - { - $set: { + { + $set: { "connections.inputConnections.$.motor_stop_status": "1", - "connections.inputConnections.$.stopTime": stopTime, + "connections.inputConnections.$.stopTime": req.body.stopTime, "connections.inputConnections.$.threshold_type": null, "connections.inputConnections.$.manual_threshold_time": null, - "connections.inputConnections.$.manual_threshold_percentage": null, - }, + "connections.inputConnections.$.manual_threshold_percentage": null + } } ); await delay(300000); + // Update the existing motor data entry with stop details const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id: start_instance_id }); if (motorData) { const receiverTank = await Tank.findOne({ customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() }); const receiverFinalWaterLevel = parseInt(receiverTank.waterlevel, 10); const quantityDelivered = receiverFinalWaterLevel - parseInt(motorData.receiverInitialwaterlevel, 10); const water_pumped_till_now = parseInt(receiverTank.total_water_added_from_midnight, 10); - const totalwaterpumped = quantityDelivered + water_pumped_till_now; - await Tank.findOneAndUpdate({ customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() }, { $set: { total_water_added_from_midnight: totalwaterpumped } }); + const totalwaterpumped = quantityDelivered + water_pumped_till_now + await Tank.findOneAndUpdate({customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase()}, { $set: { total_water_added_from_midnight: totalwaterpumped } }) await MotorData.updateOne( { customerId, motor_id: motorId, start_instance_id: start_instance_id }, - { - $set: { - stopTime: stopTime, + { + $set: { + stopTime: req.body.stopTime, receiverfinalwaterlevel: receiverFinalWaterLevel.toString(), - quantity_delivered: quantityDelivered.toString(), - }, + quantity_delivered: quantityDelivered.toString() + } } ); - - // Send notification for motor stop - for (const fcmId of fcmIds) { - try { - const response = await sendPushNotification(fcmId, 'Motor Stopped', `Motor has stopped at ${stopTime}. Quantity delivered: ${quantityDelivered} litres.`); - console.log('Notification sent successfully:', response); - } catch (error) { - console.error('Error sending notification:', error); - } - } } + + + } else { + // Update the motor stop status to "2" for start action await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId }, { $set: { "connections.inputConnections.$.motor_stop_status": "2" } } ); - - // Send notification for motor start - for (const fcmId of fcmIds) { - try { - const response = await sendPushNotification(fcmId, 'Motor Started', `Motor has started at ${startTime}.`); - console.log('Notification sent successfully:', response); - } catch (error) { - console.error('Error sending notification:', error); - } - } } + // Check threshold settings if action is start if (action === "start") { - if (threshold_type === "time") { - const receiverTank = await Tank.findOne({ customerId, tankName: to, tankLocation: to_type.toLowerCase() }); + if (req.body.threshold_type === "time") { + // If threshold type is time, update threshold time + // await Tank.updateOne( + // { customerId, "connections.inputConnections.motor_id": motorId }, + // { $set: { "connections.inputConnections.$.manual_threshold_time": req.body.manual_threshold_time,startTime:req.body.startTime } } + // ); + const receiver_tank_info7 = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() }); const newMotorData = new MotorData({ customerId: customerId, motor_id: motorId, start_instance_id: start_instance_id, - supplierTank: from, - receiverTank: to, - supplier_type: from_type, - receiver_type: to_type, - startTime: startTime, - receiverInitialwaterlevel: parseInt(receiverTank.waterlevel, 10), + supplierTank: req.body.from, + receiverTank: req.body.to, + supplier_type: req.body.from_type, + receiver_type: req.body.to_type, + startTime: req.body.startTime, + receiverInitialwaterlevel:parseInt(receiver_tank_info7.waterlevel, 10) }); await newMotorData.save(); - for await (const tank of Tank.find({ "connections.inputConnections.motor_id": motorId })) { + for await (const tank of Tank.find({ "connections.inputConnections.motor_id": motorId })) { const index = tank.connections.inputConnections.findIndex(connection => connection.motor_id === motorId); if (index !== -1) { await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId }, - { $set: { [`connections.inputConnections.${index}.manual_threshold_time`]: manual_threshold_time, [`connections.inputConnections.${index}.startTime`]: startTime, [`connections.inputConnections.${index}.start_instance_id`]: start_instance_id } } + { $set: { [`connections.inputConnections.${index}.manual_threshold_time`]: req.body.manual_threshold_time, [`connections.inputConnections.${index}.startTime`]: req.body.startTime,[`connections.inputConnections.${index}.start_instance_id`]: start_instance_id } } ); } } + - const thresholdTime = moment().add(manual_threshold_time, 'minutes').toDate(); + // Start monitoring water level based on threshold time + const thresholdTime = moment().add(req.body.manual_threshold_time, 'minutes').toDate(); const intervalId = setInterval(async () => { - const splrTank = await Tank.findOne({ customerId, tankName: from, tankLocation: from_type.toLowerCase() }); - const splrWaterLevel = parseInt(splrTank.waterlevel, 10); - const splrCapacity = parseInt(splrTank.capacity, 10); - const splrPercentage = (splrWaterLevel / splrCapacity) * 100; + const splr_tank_info3 = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() }); + const splr_tank_info3_waterlevel = parseInt(splr_tank_info3.waterlevel, 10); + const splr_tank_info3_capacity = parseInt(splr_tank_info3.capacity, 10); + const splr_tank_info3_percentage = (splr_tank_info3_waterlevel / splr_tank_info3_capacity) * 100; + console.log(splr_tank_info3_percentage, "percentage for less than 20"); - console.log(splrPercentage, "percentage for less than 20"); - - if (new Date() >= thresholdTime || splrPercentage <= 20) { + if (new Date() >= thresholdTime || splr_tank_info3_percentage <= 20) { await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId }, { @@ -1044,8 +1060,8 @@ exports.motorAction = async (req, reply) => { "connections.inputConnections.$.motor_stop_status": "1", "connections.inputConnections.$.threshold_type": null, "connections.inputConnections.$.manual_threshold_time": null, - "connections.inputConnections.$.manual_threshold_percentage": null, - }, + "connections.inputConnections.$.manual_threshold_percentage": null + } } ); clearInterval(intervalId); @@ -1058,100 +1074,94 @@ exports.motorAction = async (req, reply) => { const receiverFinalWaterLevel = parseInt(receiverTank.waterlevel, 10); const quantityDelivered = receiverFinalWaterLevel - parseInt(motorData.receiverInitialwaterlevel, 10); const water_pumped_till_now = parseInt(receiverTank.total_water_added_from_midnight, 10); - const totalwaterpumped = quantityDelivered + water_pumped_till_now; - await Tank.findOneAndUpdate({ customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() }, { $set: { total_water_added_from_midnight: totalwaterpumped } }); + const totalwaterpumped = quantityDelivered + water_pumped_till_now + await Tank.findOneAndUpdate({customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase()}, { $set: { total_water_added_from_midnight: totalwaterpumped } }) - const stopTime = new Date().toISOString(); + const stopTime = formatDate(new Date()); await MotorData.updateOne( { customerId, motor_id: motorId, start_instance_id: start_instance_id }, { $set: { - stopTime: stopTime, + stopTime:stopTime, receiverfinalwaterlevel: receiverFinalWaterLevel.toString(), - quantity_delivered: quantityDelivered.toString(), - }, + quantity_delivered: quantityDelivered.toString() + } } ); - - // Send notification for motor stop due to threshold - for (const fcmId of fcmIds) { - try { - const response = await sendPushNotification(fcmId, 'Motor Stopped', `Motor has stopped at ${stopTime}. Quantity delivered: ${quantityDelivered} litres.`); - console.log('Notification sent successfully:', response); - } catch (error) { - console.error('Error sending notification:', error); - } - } - } - - if (splrPercentage <= 20) { - for (const fcmId of fcmIds) { - try { - const response = await sendPushNotification(fcmId, 'Warning', 'Sump tank water level is below 20%.'); - console.log('Notification sent successfully:', response); - } catch (error) { - console.error('Error sending notification:', error); - } - } } } }, 60000); - } else if (threshold_type === "litres") { - const receiverTank = await Tank.findOne({ customerId, tankName: to, tankLocation: to_type.toLowerCase() }); + } else if (req.body.threshold_type === "litres") { + const receiver_tank_info7 = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() }); const newMotorData = new MotorData({ customerId: customerId, - motor_id: motorId, + motor_id: motor_id, start_instance_id: start_instance_id, - supplierTank: from, - receiverTank: to, - supplier_type: from_type, - receiver_type: to_type, - startTime: startTime, - receiverwaterlevel: parseInt(receiverTank.waterlevel, 10), + supplierTank: req.body.from, + receiverTank: req.body.to, + supplier_type: req.body.from_type, + receiver_type: req.body.to_type, + startTime: req.body.startTime, + receiverwaterlevel:parseInt(receiver_tank_info7.waterlevel, 10) }); await newMotorData.save(); + // If threshold type is percentage, calculate percentage threshold + const receiver_tank_info = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() }); + const supplier_tank_info = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() }); + if (!receiver_tank_info) { + throw new Error("Receiver tank not found."); + } + if (!supplier_tank_info) { + throw new Error("Supplierr tank not found."); + } + const supplier_capacity = parseInt(supplier_tank_info.capacity, 10); + const supplier_waterLevel = parseInt(supplier_tank_info.waterlevel, 10); - const receiverTankInfo = await Tank.findOne({ customerId, tankName: to, tankLocation: to_type.toLowerCase() }); - const supplierTankInfo = await Tank.findOne({ customerId, tankName: from, tankLocation: from_type.toLowerCase() }); - if (!receiverTankInfo) throw new Error("Receiver tank not found."); - if (!supplierTankInfo) throw new Error("Supplier tank not found."); + const capacity = parseInt(receiver_tank_info.capacity, 10); + const waterLevel = parseInt(receiver_tank_info.waterlevel, 10); + const desired_percentage = parseInt(req.body.manual_threshold_litres, 10); + const threshold_water_level = waterLevel+desired_percentage; - const supplierCapacity = parseInt(supplierTankInfo.capacity, 10); - const supplierWaterLevel = parseInt(supplierTankInfo.waterlevel, 10); - const capacity = parseInt(receiverTankInfo.capacity, 10); - const waterLevel = parseInt(receiverTankInfo.waterlevel, 10); - const desiredLitres = parseInt(manual_threshold_litres, 10); - const thresholdWaterLevel = waterLevel + desiredLitres; - const supplierThreshold = supplierWaterLevel - desiredLitres; + const supplier_threshold = supplier_waterLevel-desired_percentage + console.log(supplier_threshold,"supplier_threshold") - for await (const tank of Tank.find({ "connections.inputConnections.motor_id": motorId })) { + for await (const tank of Tank.find({ "connections.inputConnections.motor_id": motorId })) { const index = tank.connections.inputConnections.findIndex(connection => connection.motor_id === motorId); if (index !== -1) { await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId }, - { $set: { [`connections.inputConnections.${index}.manual_threshold_percentage`]: supplierThreshold.toString(), [`connections.inputConnections.${index}.startTime`]: startTime } } + { $set: { [`connections.inputConnections.${index}.manual_threshold_percentage`]: supplier_threshold.toString(), [`connections.inputConnections.${index}.startTime`]: req.body.startTime } } ); } } + + + // Update water level threshold + + + // Start monitoring water level based on threshold percentage const intervalId = setInterval(async () => { - const supplierTank = await Tank.findOne({ customerId, tankName: from, tankLocation: from_type.toLowerCase() }); - const currentWaterLevel = parseInt(supplierTank.waterlevel, 10); - if (currentWaterLevel <= supplierThreshold) { + // Check if water level has reached the threshold percentage + const supplier_tank_info1 = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() }); + const current_water_level = parseInt(supplier_tank_info1.waterlevel, 10); + if (current_water_level <= supplier_threshold) { + // Stop the motor pump await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId }, - { - $set: { + { + $set: { "connections.inputConnections.$.motor_stop_status": "1", + "connections.inputConnections.$.threshold_type": null, "connections.inputConnections.$.manual_threshold_time": null, - "connections.inputConnections.$.manual_threshold_percentage": null, - }, + "connections.inputConnections.$.manual_threshold_percentage": null + } } ); - clearInterval(intervalId); + clearInterval(intervalId); // Stop monitoring water level await delay(300000); const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id: start_instance_id }); @@ -1160,202 +1170,148 @@ exports.motorAction = async (req, reply) => { const receiverFinalWaterLevel = parseInt(receiverTank.waterlevel, 10); const quantityDelivered = receiverFinalWaterLevel - parseInt(motorData.receiverInitialwaterlevel, 10); - const stopTime = new Date().toISOString(); + + const stopTime = formatDate(new Date()); await MotorData.updateOne( { customerId, motor_id: motorId, start_instance_id: start_instance_id }, { $set: { - stopTime: stopTime, + stopTime:stopTime, receiverfinalwaterlevel: receiverFinalWaterLevel.toString(), - quantity_delivered: quantityDelivered.toString(), - }, + quantity_delivered: quantityDelivered.toString() + } } ); - - // Send notification for motor stop due to threshold - for (const fcmId of fcmIds) { - try { - const response = await sendPushNotification(fcmId, 'Motor Stopped', `Motor has stopped at ${stopTime}. Quantity delivered: ${quantityDelivered} litres.`); - console.log('Notification sent successfully:', response); - } catch (error) { - console.error('Error sending notification:', error); - } - } } } - }, 20000); + }, 20000); // Check water level every minute } } + // Respond with success message reply.code(200).send({ message: `Motor ${action === "start" ? "started" : "stopped"} successfully.` }); } catch (err) { + // Handle errors throw boom.boomify(err); } }; -// exports.motorAction = async (req, reply) => { -// try { -// const customerId = req.params.customerId; -// const action = req.body.action; -// const motorId = req.body.motor_id; -// console.log(req.body.startTime) -// // Ensure motor_id is provided -// if (!motorId) { -// throw new Error("Motor ID is required."); -// } - -// // Determine the motor stop status based on the action -// let motorStopStatus; -// if (action === "start") { -// motorStopStatus = "2"; // If action is start, set stop status to "2" -// } else if (action === "stop") { -// motorStopStatus = "1"; // If action is stop, set stop status to "1" -// } else { -// throw new Error("Invalid action provided."); -// } - -// // Update the motor stop status immediately if action is stop -// if (action === "stop") { -// await Tank.updateOne( -// { customerId, "connections.inputConnections.motor_id": motorId }, -// { -// $set: { -// "connections.inputConnections.$.motor_stop_status": "1", -// "connections.inputConnections.$.stopTime": req.body.stopTime, -// "connections.inputConnections.$.threshold_type": null, -// "connections.inputConnections.$.manual_threshold_time": null, -// "connections.inputConnections.$.manual_threshold_percentage": null -// } -// } -// ); - +const motorActionAuto = async (req, reply) => { + try { + const customerId = req.params.customerId; + const action = req.body.action; + const motorId = req.body.motor_id; + const motor_on_type = req.body.motor_on_type + if (!motorId) { + throw new Error("Motor ID is required."); + } - -// } else { -// // Update the motor stop status to "2" for start action -// await Tank.updateOne( -// { customerId, "connections.inputConnections.motor_id": motorId }, -// { $set: { "connections.inputConnections.$.motor_stop_status": "2" } } -// ); -// } + let motorStopStatus; -// // Check threshold settings if action is start -// if (action === "start") { -// if (req.body.threshold_type === "time") { -// // If threshold type is time, update threshold time -// // await Tank.updateOne( -// // { customerId, "connections.inputConnections.motor_id": motorId }, -// // { $set: { "connections.inputConnections.$.manual_threshold_time": req.body.manual_threshold_time,startTime:req.body.startTime } } -// // ); - - -// for await (const tank of Tank.find({ "connections.inputConnections.motor_id": motorId })) { -// const index = tank.connections.inputConnections.findIndex(connection => connection.motor_id === motorId); -// if (index !== -1) { -// await Tank.updateOne( -// { customerId, "connections.inputConnections.motor_id": motorId }, -// { $set: { [`connections.inputConnections.${index}.manual_threshold_time`]: req.body.manual_threshold_time, [`connections.inputConnections.${index}.startTime`]: req.body.startTime } } -// ); -// } -// } - + if (action === "start") { + await Tank.updateOne( + { customerId, "connections.inputConnections.motor_id": motorId }, + { + $set: { + "connections.inputConnections.$.motor_stop_status": "2", + "connections.inputConnections.$.startTime": req.body.startTime, + "connections.inputConnections.$.motor_on_type": "auto", + + } + } + ); -// // Start monitoring water level based on threshold time -// const thresholdTime = moment().add(req.body.manual_threshold_time, 'minutes').toDate(); -// const intervalId = setInterval(async () => { -// // Check if threshold time has been reached -// if (new Date() >= thresholdTime) { -// // Stop the motor pump -// await Tank.updateOne( -// { customerId, "connections.inputConnections.motor_id": motorId }, -// { -// $set: { -// "connections.inputConnections.$.motor_stop_status": "1", - -// "connections.inputConnections.$.threshold_type": null, -// "connections.inputConnections.$.manual_threshold_time": null, -// "connections.inputConnections.$.manual_threshold_percentage": null -// } -// } -// ); -// clearInterval(intervalId); // Stop monitoring water level -// } -// }, 60000); // Check water level every minute -// } else if (req.body.threshold_type === "litres") { -// // If threshold type is percentage, calculate percentage threshold -// const receiver_tank_info = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() }); -// const supplier_tank_info = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() }); -// if (!receiver_tank_info) { -// throw new Error("Receiver tank not found."); -// } -// if (!supplier_tank_info) { -// throw new Error("Supplierr tank not found."); -// } -// const supplier_capacity = parseInt(supplier_tank_info.capacity, 10); -// const supplier_waterLevel = parseInt(supplier_tank_info.waterlevel, 10); + ; + } + if (action === "stop") { + await Tank.updateOne( + { customerId, "connections.inputConnections.motor_id": motorId }, + { + $set: { + "connections.inputConnections.$.motor_stop_status": "1", + "connections.inputConnections.$.stopTime": req.body.stopTime, + "connections.inputConnections.$.motor_on_type": null, + } + } + ); + } + + reply.code(200).send({ message: `Motor ${action === "start" ? "started" : "stopped"} successfully.` }); + } catch (err) { + console.error("Error in motorActionAuto:", err); + reply.code(500).send({ error: err.message }); + } +}; -// const capacity = parseInt(receiver_tank_info.capacity, 10); -// const waterLevel = parseInt(receiver_tank_info.waterlevel, 10); -// const desired_percentage = parseInt(req.body.manual_threshold_litres, 10); -// const threshold_water_level = waterLevel+desired_percentage; +const checkAutoMode = async () => { + try { + const tanks = await Tank.find(); -// const supplier_threshold = supplier_waterLevel-desired_percentage + for (const tank of tanks) { + for (const inputConnection of tank.connections.inputConnections) { + if (inputConnection.auto_mode === "active") { + console.log("This is automation for tank: " + tank.tankName); + const waterLevel = parseFloat(tank.waterlevel.replace(/,/g, '')); + const capacity = parseFloat(tank.capacity.replace(/,/g, '')); + const autoMinPercentage = parseFloat(tank.auto_min_percentage); + const autoMaxPercentage = parseFloat(tank.auto_max_percentage); + console.log(waterLevel,capacity,autoMinPercentage,autoMaxPercentage) + + if (isNaN(waterLevel) || isNaN(capacity) || capacity === 0) { + console.error(`Invalid water level or capacity for tank: ${tank.tankName}`); + continue; // Skip this tank if the values are not valid + } -// for await (const tank of Tank.find({ "connections.inputConnections.motor_id": motorId })) { -// const index = tank.connections.inputConnections.findIndex(connection => connection.motor_id === motorId); -// if (index !== -1) { -// await Tank.updateOne( -// { customerId, "connections.inputConnections.motor_id": motorId }, -// { $set: { [`connections.inputConnections.${index}.manual_threshold_percentage`]: supplier_threshold.toString(), [`connections.inputConnections.${index}.startTime`]: req.body.startTime } } -// ); -// } -// } - + const currentPercentage = (waterLevel / capacity) * 100; + console.log("This is automation percentage: " + currentPercentage); + const now = moment().format('DD-MMM-YYYY - HH:mm'); + console.log(now) + if (currentPercentage <= autoMinPercentage) { + await motorActionAuto({ + params: { customerId: tank.customerId }, + body: { + action: "start", + motor_id: inputConnection.motor_id, + motor_on_type: "auto", + startTime: now + } + }, { + code: (statusCode) => ({ send: (response) => console.log(response) }) + }); + } else if (currentPercentage >= autoMaxPercentage && inputConnection.motor_on_type === "auto") { + await motorActionAuto({ + params: { customerId: tank.customerId }, + body: { + action: "stop", + motor_id: inputConnection.motor_id, + motor_on_type: "auto", + stopTime: now + } + }, { + code: (statusCode) => ({ send: (response) => console.log(response) }) + }); + } + } + } + } + } catch (err) { + console.error("Error checking auto mode:", err); + } +}; +// Set the interval to check every 15 seconds (15000 milliseconds) +setInterval(checkAutoMode, 15000); -// // Update water level threshold - -// // Start monitoring water level based on threshold percentage -// const intervalId = setInterval(async () => { -// // Check if water level has reached the threshold percentage -// const supplier_tank_info1 = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() }); -// const current_water_level = parseInt(supplier_tank_info1.waterlevel, 10); -// if (current_water_level <= supplier_threshold) { -// // Stop the motor pump -// await Tank.updateOne( -// { customerId, "connections.inputConnections.motor_id": motorId }, -// { -// $set: { -// "connections.inputConnections.$.motor_stop_status": "1", - -// "connections.inputConnections.$.threshold_type": null, -// "connections.inputConnections.$.manual_threshold_time": null, -// "connections.inputConnections.$.manual_threshold_percentage": null -// } -// } -// ); -// clearInterval(intervalId); // Stop monitoring water level -// } -// }, 20000); // Check water level every minute -// } -// } -// // Respond with success message -// reply.code(200).send({ message: `Motor ${action === "start" ? "started" : "stopped"} successfully.` }); -// } catch (err) { -// // Handle errors -// throw boom.boomify(err); -// } -// }; @@ -1653,6 +1609,7 @@ exports.calculateCapacity = async (req, reply) => { // }; + exports.IotDevice = async (req, reply) => { try { const { hardwareId, mode, tanks } = req.body; @@ -1708,23 +1665,26 @@ exports.IotDevice = async (req, reply) => { const waterCapacityPerCm = parseInt(existingTank.waterCapacityPerCm.replace(/,/g, ''), 10); const water_level = water_level_height * waterCapacityPerCm; - existingTank.waterlevel = water_level; - - // Save the updated tank document - await existingTank.save(); - - // Update linked tanks - for (const outputConnection of existingTank.connections.outputConnections) { - const linkedTank = await Tank.findOne({ customerId, tankName: outputConnection.outputConnections, tankLocation: outputConnection.output_type }); - if (linkedTank) { - for (const inputConnection of linkedTank.connections.inputConnections) { - if (inputConnection.inputConnections === tank_name) { - inputConnection.water_level = water_level; - await linkedTank.save(); + if (water_level >= 0) { + existingTank.waterlevel = water_level; + + // Save the updated tank document + await existingTank.save(); + + // Update linked tanks + for (const outputConnection of existingTank.connections.outputConnections) { + const linkedTank = await Tank.findOne({ customerId, tankName: outputConnection.outputConnections, tankLocation: outputConnection.output_type }); + if (linkedTank) { + for (const inputConnection of linkedTank.connections.inputConnections) { + if (inputConnection.inputConnections === tank_name) { + inputConnection.water_level = water_level; + await linkedTank.save(); + } } } } } + } // Send the latest three documents @@ -1745,6 +1705,7 @@ exports.IotDevice = async (req, reply) => { + // exports.IotDevice3 = async (req, reply) => { // try { // const { hardwareId, mode, tanks } = req.body; @@ -2227,37 +2188,102 @@ exports.startUpdateLoop = async (request, reply) => { }; -exports.updatewaterlevelsatmidnight = async (req, reply) => { +// exports.updatewaterlevelsatmidnight = async (req, reply) => { +// try { +// // Schedule the task to run every day at 10 seconds past the minute +// cron.schedule('0 0 * * *', async () => { +// try { +// const tanks = await Tank.find({ customerId: req.query.customerId }); +// for (const tank of tanks) { +// tank.waterlevel_at_midnight = tank.waterlevel; +// console.log(tank.waterlevel_at_midnight) +// await tank.save(); +// } +// console.log('Waterlevel noted in waterlevel_at_midnight'); +// } catch (error) { +// console.error('Error occurred:', error); +// } +// }); + +// await Tank.find({ customerId: req.query.customerId }) +// .exec() +// .then((docs) => { +// reply.send({ status_code: 200, data: docs, count: docs.length }); +// }) +// .catch((err) => { +// console.log(err); +// reply.send({ error: err }); +// }); +// } catch (err) { +// throw boom.boomify(err); +// } +// }; + + +const updatewaterlevelsatmidnight = async () => { + console.log('Cron job triggered at:', moment().tz('Asia/Kolkata').format()); + try { - // Schedule the task to run every day at 10 seconds past the minute - cron.schedule('0 0 * * *', async () => { - try { - const tanks = await Tank.find({ customerId: req.query.customerId }); - for (const tank of tanks) { - tank.waterlevel_at_midnight = tank.waterlevel; - console.log(tank.waterlevel_at_midnight) - await tank.save(); - } - console.log('Waterlevel noted in waterlevel_at_midnight'); - } catch (error) { - console.error('Error occurred:', error); - } - }); + const tanks = await Tank.find({}); + for (const tank of tanks) { + 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}`); + } + console.log('Waterlevel noted in waterlevel_at_midnight'); + } catch (error) { + console.error('Error occurred:', error); + } +}; - await Tank.find({ customerId: req.query.customerId }) - .exec() - .then((docs) => { - reply.send({ status_code: 200, data: docs, count: docs.length }); - }) - .catch((err) => { - console.log(err); - reply.send({ error: err }); +// Schedule the task to run every day at 13:49 IST (1:49 PM IST) +cron.schedule('0 0 * * *', updatewaterlevelsatmidnight, { + timezone: "Asia/Kolkata" +}); + + + + + + + +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() }); - } catch (err) { - throw boom.boomify(err); + + // Save the new document + await newTankConsumption.save(); + + } + console.log('Waterlevel noted in waterlevel_at_midnight'); + } catch (error) { + console.error('Error occurred:', error); } }; +// Schedule the task to run every day at 13:49 IST (1:49 PM IST) +cron.schedule('55 23 * * *', updatetotalConsumptiontillmidnight, { + timezone: "Asia/Kolkata" +}); + + + + exports.deletemotordatarecordsbefore7days = async (req, reply) => { try { @@ -2447,8 +2473,7 @@ exports.writeMotorStatus = async (req, reply) => { } }; -// exports.writeMotorStatus = async (req, reply) => { -// try { + // const motor_id = req.body.motor_id; // const status = req.body.status; @@ -2543,7 +2568,7 @@ exports.motortemperature = async (req, reply) => { exports.update_auto_mode = async (req, reply) => { try { const customerId = req.params.customerId; - const { motor_id, auto_min_percentage, auto_max_percentage, auto_mode } = req.body; + const { motor_id, auto_mode } = req.body; // Update inputConnections' auto_mode await Tank.updateOne( @@ -2551,9 +2576,25 @@ exports.update_auto_mode = async (req, reply) => { { $set: { "connections.inputConnections.$.auto_mode": auto_mode } } ); + + + reply.send({ status_code: 200, message: "Auto mode and percentages updated successfully." }); + } catch (error) { + throw boom.boomify(error); + } +}; + +exports.update_auto_percentage = async (req, reply) => { + try { + const customerId = req.params.customerId; + const { tankName,tankLocation, auto_min_percentage, auto_max_percentage } = req.body; + + // Update inputConnections' auto_mode + + // Update auto_min_percentage and auto_max_percentage - await Tank.updateMany( - { customerId: customerId }, + await Tank.updateOne( + { customerId: customerId,tankLocation, tankName}, { $set: { "auto_min_percentage": auto_min_percentage, @@ -2568,3 +2609,36 @@ exports.update_auto_mode = 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 = getFormattedISTTime(); + + 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); + + + + +console.log('Cron job scheduled to update water levels at midnight'); \ No newline at end of file