|
|
|
|
@ -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;
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
// for (const tank of tanks) {
|
|
|
|
|
// const {
|
|
|
|
|
// tankName,
|
|
|
|
|
// tankLocation,
|
|
|
|
|
// typeOfWater,
|
|
|
|
|
// capacity,
|
|
|
|
|
// waterlevel,
|
|
|
|
|
// waterlevel_at_midnight,
|
|
|
|
|
// } = tank;
|
|
|
|
|
|
|
|
|
|
const currentWaterLevelPercentage = ((currentWaterLevel / tankCapacity) * 100).toFixed(2);
|
|
|
|
|
const waterUsedSinceMidnight = midnightWaterLevel - currentWaterLevel;
|
|
|
|
|
const waterUsedPercentageSinceMidnight = ((waterUsedSinceMidnight / tankCapacity) * 100).toFixed(2);
|
|
|
|
|
// // 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;
|
|
|
|
|
|
|
|
|
|
let notificationBody =
|
|
|
|
|
`🛢️ Tank Name: ${tankName}\n` +
|
|
|
|
|
`🏢 Location: ${tankLocation}\n` +
|
|
|
|
|
`💧 Type of Water: ${typeOfWater}\n` +
|
|
|
|
|
`Current Water Level: ${currentWaterLevel} liters (${currentWaterLevelPercentage}%)\n`;
|
|
|
|
|
// if (tankCapacity === 0) {
|
|
|
|
|
// console.log(`Skipping tank ${tankName} due to zero capacity`);
|
|
|
|
|
// continue;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
await sendNotification(customerId, fcmIds, "Water Level Update", notificationBody);
|
|
|
|
|
console.log("Notification sent for tank:", tankName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// const currentWaterLevelPercentage = ((currentWaterLevel / tankCapacity) * 100).toFixed(2);
|
|
|
|
|
// const waterUsedSinceMidnight = midnightWaterLevel - currentWaterLevel;
|
|
|
|
|
// const waterUsedPercentageSinceMidnight = ((waterUsedSinceMidnight / tankCapacity) * 100).toFixed(2);
|
|
|
|
|
|
|
|
|
|
console.log("Water level notifications processed.");
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error("Error in water level calculation:", err);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
// let notificationBody =
|
|
|
|
|
// `🛢️ Tank Name: ${tankName}\n` +
|
|
|
|
|
// `🏢 Location: ${tankLocation}\n` +
|
|
|
|
|
// `💧 Type of Water: ${typeOfWater}\n` +
|
|
|
|
|
// `Current Water Level: ${currentWaterLevel} liters (${currentWaterLevelPercentage}%)\n`;
|
|
|
|
|
|
|
|
|
|
// 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" });
|
|
|
|
|
}
|
|
|
|
|
// return reply.send({ message: "Preference updated successfully" });
|
|
|
|
|
// }
|