diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 32e1177d..52c13d5b 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -2820,7 +2820,7 @@ const monitorWaterLevels = async () => { }; // Schedule the task to run every 30 minutes -setInterval(monitorWaterLevels, 30 * 60 * 1000); +// setInterval(monitorWaterLevels, 30 * 60 * 1000); const motorIntervals = {}; async function calculateTotalPumpedWater(customerId, motorId, start_instance_id) { @@ -4958,38 +4958,38 @@ exports.totalwaterLevelSum = async (request, reply) => { } -exports.startUpdateLoop = async (request, reply) => { - const updateInterval = 5000; +// exports.startUpdateLoop = async (request, reply) => { +// const updateInterval = 5000; - setInterval(async () => { - try { - const iotTank = await IotData.findOne({ hardwareId: request.body.hardwareId }); - if (!iotTank) { - console.log(`IOTtank not found for hardwareId ${request.body.hardwareId}`); - return; - } +// setInterval(async () => { +// try { +// const iotTank = await IotData.findOne({ hardwareId: request.body.hardwareId }); +// if (!iotTank) { +// console.log(`IOTtank not found for hardwareId ${request.body.hardwareId}`); +// return; +// } - const currentWaterlevel = Number(iotTank.tankHeight) * 200; - const tank = await Tank.findOne({ hardwareId: iotTank.hardwareId }); +// const currentWaterlevel = Number(iotTank.tankHeight) * 200; +// const tank = await Tank.findOne({ hardwareId: iotTank.hardwareId }); - let combinedWaterlevel; - if (tank) { - combinedWaterlevel = currentWaterlevel + Number(tank.waterlevel); - } else { - combinedWaterlevel = currentWaterlevel; - } +// let combinedWaterlevel; +// if (tank) { +// combinedWaterlevel = currentWaterlevel + Number(tank.waterlevel); +// } else { +// combinedWaterlevel = currentWaterlevel; +// } - await Tank.updateOne({ hardwareId: iotTank.hardwareId }, { $set: { waterlevel: combinedWaterlevel } }); +// await Tank.updateOne({ hardwareId: iotTank.hardwareId }, { $set: { waterlevel: combinedWaterlevel } }); - console.log(`Waterlevel updated successfully for hardwareId ${iotTank.hardwareId}`); - console.log(`Previous waterlevel: ${tank ? tank.waterlevel : 0}`); - console.log(`Current waterlevel: ${currentWaterlevel}`); - console.log(`Combined waterlevel: ${combinedWaterlevel}`); - } catch (err) { - console.error(err); - } - }, updateInterval); -}; +// console.log(`Waterlevel updated successfully for hardwareId ${iotTank.hardwareId}`); +// console.log(`Previous waterlevel: ${tank ? tank.waterlevel : 0}`); +// console.log(`Current waterlevel: ${currentWaterlevel}`); +// console.log(`Combined waterlevel: ${combinedWaterlevel}`); +// } catch (err) { +// console.error(err); +// } +// }, updateInterval); +// }; @@ -6792,72 +6792,72 @@ exports.sendUserAutomaticStartAndStop = async (request, reply) => { // } // }; -const calculateWaterLevelAndNotify = async () => { - try { - const now = moment(); - const currentTime = now.format("HH:mm"); // Current time in HH:mm format +// const calculateWaterLevelAndNotify = async () => { +// try { +// const now = moment(); +// const currentTime = now.format("HH:mm"); // Current time in HH:mm format - console.log(`Current time: ${currentTime}`); +// console.log(`Current time: ${currentTime}`); - // Get all users who have allowed notifications and have set a notification time - const users = await User.find({ allowNotifications: true, notificationTime: currentTime }); +// // Get all users who have allowed notifications and have set a notification time +// const users = await User.find({ allowNotifications: true, notificationTime: currentTime }); - if (users.length === 0) { - console.log("No users to notify at this time."); - return; - } +// if (users.length === 0) { +// console.log("No users to notify at this time."); +// return; +// } - for (const user of users) { - const { customerId, fcmIds } = user; +// for (const user of users) { +// const { customerId, fcmIds } = user; - if (!Array.isArray(fcmIds) || fcmIds.length === 0) { - console.log(`No valid FCM tokens for customer ID: ${customerId}`); - continue; - } +// if (!Array.isArray(fcmIds) || fcmIds.length === 0) { +// console.log(`No valid FCM tokens for customer ID: ${customerId}`); +// continue; +// } + +// // Get tanks associated with the user +// const tanks = await Tank.find({ customerId }); - // Get tanks associated with the user - const tanks = await Tank.find({ customerId }); +// for (const tank of tanks) { +// const { +// tankName, +// tankLocation, +// typeOfWater, +// capacity, +// waterlevel, +// waterlevel_at_midnight, +// } = tank; - for (const tank of tanks) { - const { - tankName, - tankLocation, - typeOfWater, - capacity, - waterlevel, - waterlevel_at_midnight, - } = tank; - - // Remove commas before parsing numbers - const tankCapacity = parseFloat(capacity.replace(/,/g, '')) || 0; - const currentWaterLevel = parseFloat(waterlevel.replace(/,/g, '')) || 0; - const midnightWaterLevel = parseFloat(waterlevel_at_midnight.replace(/,/g, '')) || 0; - - if (tankCapacity === 0) { - console.log(`Skipping tank ${tankName} due to zero capacity`); - continue; - } +// // Remove commas before parsing numbers +// const tankCapacity = parseFloat(capacity.replace(/,/g, '')) || 0; +// const currentWaterLevel = parseFloat(waterlevel.replace(/,/g, '')) || 0; +// const midnightWaterLevel = parseFloat(waterlevel_at_midnight.replace(/,/g, '')) || 0; - const currentWaterLevelPercentage = ((currentWaterLevel / tankCapacity) * 100).toFixed(2); - const waterUsedSinceMidnight = midnightWaterLevel - currentWaterLevel; - const waterUsedPercentageSinceMidnight = ((waterUsedSinceMidnight / tankCapacity) * 100).toFixed(2); +// if (tankCapacity === 0) { +// console.log(`Skipping tank ${tankName} due to zero capacity`); +// continue; +// } - let notificationBody = - `🛢️ Tank Name: ${tankName}\n` + - `🏢 Location: ${tankLocation}\n` + - `💧 Type of Water: ${typeOfWater}\n` + - `Current Water Level: ${currentWaterLevel} liters (${currentWaterLevelPercentage}%)\n`; +// const currentWaterLevelPercentage = ((currentWaterLevel / tankCapacity) * 100).toFixed(2); +// const waterUsedSinceMidnight = midnightWaterLevel - currentWaterLevel; +// const waterUsedPercentageSinceMidnight = ((waterUsedSinceMidnight / tankCapacity) * 100).toFixed(2); - await sendNotification(customerId, fcmIds, "Water Level Update", notificationBody); - console.log("Notification sent for tank:", tankName); - } - } +// let notificationBody = +// `🛢️ Tank Name: ${tankName}\n` + +// `🏢 Location: ${tankLocation}\n` + +// `💧 Type of Water: ${typeOfWater}\n` + +// `Current Water Level: ${currentWaterLevel} liters (${currentWaterLevelPercentage}%)\n`; - console.log("Water level notifications processed."); - } catch (err) { - console.error("Error in water level calculation:", err); - } -}; +// await sendNotification(customerId, fcmIds, "Water Level Update", notificationBody); +// console.log("Notification sent for tank:", tankName); +// } +// } + +// console.log("Water level notifications processed."); +// } catch (err) { +// console.error("Error in water level calculation:", err); +// } +// }; // const calculateLowWaterLevelAndNotify = async () => { // try { // const now = moment(); @@ -7161,14 +7161,14 @@ exports.listofactiveandinactivetankstatus = async (req, reply) => { }; -exports.notificationTiming = async (req, reply) => { - const { customerId, notificationPreference } = req.body; +// exports.notificationTiming = async (req, reply) => { +// const { customerId, notificationPreference } = req.body; - if (!["never", "always", "6_hours", "8_hours", "1_month"].includes(notificationPreference)) { - return reply.status(400).send({ message: "Invalid preference" }); - } +// if (!["never", "always", "6_hours", "8_hours", "1_month"].includes(notificationPreference)) { +// return reply.status(400).send({ message: "Invalid preference" }); +// } - await User.updateOne({ customerId }, { notificationPreference }); +// await User.updateOne({ customerId }, { notificationPreference }); - return reply.send({ message: "Preference updated successfully" }); -} \ No newline at end of file +// return reply.send({ message: "Preference updated successfully" }); +// } \ No newline at end of file diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 2a12feb4..f4854c0a 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -742,48 +742,48 @@ module.exports = function (fastify, opts, next) { }); - fastify.route({ - method: 'POST', - url: '/update-waterlevel', - schema: { - tags: ['Tank'], - description: 'This is for updating waterlevel of a tank based on its IOTtank document', - summary: 'This is for updating waterlevel of a tank', - body: { - type: 'object', - properties: { - hardwareId: { type: 'string' }, - }, - required: ['hardwareId'], - }, - response: { - 200: { - type: 'object', - properties: { - message: { type: 'string' }, - }, - }, - 404: { - type: 'object', - properties: { - message: { type: 'string' }, - }, - }, - 500: { - type: 'object', - properties: { - message: { type: 'string' }, - }, - }, - }, - security: [ - { - basicAuth: [], - }, - ], - }, - handler: tanksController.startUpdateLoop, - }); + // fastify.route({ + // method: 'POST', + // url: '/update-waterlevel', + // schema: { + // tags: ['Tank'], + // description: 'This is for updating waterlevel of a tank based on its IOTtank document', + // summary: 'This is for updating waterlevel of a tank', + // body: { + // type: 'object', + // properties: { + // hardwareId: { type: 'string' }, + // }, + // required: ['hardwareId'], + // }, + // response: { + // 200: { + // type: 'object', + // properties: { + // message: { type: 'string' }, + // }, + // }, + // 404: { + // type: 'object', + // properties: { + // message: { type: 'string' }, + // }, + // }, + // 500: { + // type: 'object', + // properties: { + // message: { type: 'string' }, + // }, + // }, + // }, + // security: [ + // { + // basicAuth: [], + // }, + // ], + // }, + // handler: tanksController.startUpdateLoop, + // }); // fastify.get("/api/updatewaterlevelsatmidnight", { // schema: { @@ -1419,36 +1419,36 @@ module.exports = function (fastify, opts, next) { handler: tanksController.sendUserAutomaticStartAndStop, }); - fastify.route({ - method: "POST", - url: "/api/sendNotificationDailyPreference", - schema: { - tags: ["Tank"], - summary: "This is for time based notification preferences", - body: { - type: "object", - properties: { - customerId: { - type: "string", + // fastify.route({ + // method: "POST", + // url: "/api/sendNotificationDailyPreference", + // schema: { + // tags: ["Tank"], + // summary: "This is for time based notification preferences", + // body: { + // type: "object", + // properties: { + // customerId: { + // type: "string", - }, - notificationPreference: { - type: "string", - }, - // allowNotifications: { - // type: "boolean" - // } - }, - }, - security: [ - { - basicAuth: [], - }, - ], - }, - //preHandler: fastify.auth([fastify.authenticate]), - handler: tanksController.notificationTiming, - }); + // }, + // notificationPreference: { + // type: "string", + // }, + // // allowNotifications: { + // // type: "boolean" + // // } + // }, + // }, + // security: [ + // { + // basicAuth: [], + // }, + // ], + // }, + // //preHandler: fastify.auth([fastify.authenticate]), + // handler: tanksController.notificationTiming, + // }); fastify.route({