|
|
|
@ -2729,90 +2729,6 @@ const notificationSentStatus = {
|
|
|
|
|
|
|
|
|
|
let waterLevelCheckInterval; // To hold the interval ID
|
|
|
|
|
|
|
|
|
|
const checkWaterLevel = async (customerId, motorId, fcmToken, receiverTank) => {
|
|
|
|
|
const currentWaterLevel = parseInt(receiverTank.waterlevel, 10);
|
|
|
|
|
const tankCapacity = parseInt(receiverTank.capacity.replace(/,/g, ''), 10);
|
|
|
|
|
const currentWaterPercentage = (currentWaterLevel / tankCapacity) * 100;
|
|
|
|
|
|
|
|
|
|
// Check for low water levels
|
|
|
|
|
if (currentWaterPercentage <= 20 && !notificationSentStatus.lowWater) {
|
|
|
|
|
// eventEmitter.emit('sendLowWaterNotification', fcmToken, `Water level has dropped below 20%.`);
|
|
|
|
|
notificationSentStatus.lowWater = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check for critically low water levels
|
|
|
|
|
if (currentWaterPercentage <= 10 && !notificationSentStatus.criticallyLowWater) {
|
|
|
|
|
// eventEmitter.emit('sendCriticalLowWaterNotification', fcmToken, `Water level has dropped below 10%.`);
|
|
|
|
|
notificationSentStatus.criticallyLowWater = true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
// const checkWaterLevelsAndNotify = async (customerId, tankName, tankLocation, fcmTokens) => {
|
|
|
|
|
// try {
|
|
|
|
|
// // Fetch the tank details from the database
|
|
|
|
|
// const tank = await Tank.findOne({ customerId, tankName, tankLocation });
|
|
|
|
|
|
|
|
|
|
// if (!tank) {
|
|
|
|
|
// console.error(`Tank not found: ${tankName} at location ${tankLocation}`);
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// // Extract the current water level and capacity
|
|
|
|
|
// const currentWaterLevel = parseInt(tank.waterlevel, 10);
|
|
|
|
|
// const capacity = parseInt(tank.capacity.replace(/,/g, ''), 10);
|
|
|
|
|
|
|
|
|
|
// // Calculate the water level percentage
|
|
|
|
|
// const waterLevelPercentage = (currentWaterLevel / capacity) * 100;
|
|
|
|
|
|
|
|
|
|
// // Thresholds for notifications
|
|
|
|
|
// const thresholds = {
|
|
|
|
|
// criticallyLow: 10,
|
|
|
|
|
// veryLow: 20,
|
|
|
|
|
// low: 30,
|
|
|
|
|
// high: 70,
|
|
|
|
|
// veryHigh: 80,
|
|
|
|
|
// criticallyHigh: 85,
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
// // Check water levels and send notifications
|
|
|
|
|
// if (waterLevelPercentage <= thresholds.criticallyLow && !notificationSentStatus.criticallyLowWater) {
|
|
|
|
|
// eventEmitter.emit('sendCriticalLowWaterNotification', fcmTokens, tank);
|
|
|
|
|
// notificationSentStatus.criticallyLowWater = true;
|
|
|
|
|
// await Tank.updateOne({ customerId, tankName: tank.tankName }, { $set: { notificationSentCritical: true } });
|
|
|
|
|
// } else if (waterLevelPercentage <= thresholds.veryLow && !notificationSentStatus.veryLowWater) {
|
|
|
|
|
// eventEmitter.emit('sendVeryLowWaterNotification', fcmTokens, tank);
|
|
|
|
|
// notificationSentStatus.veryLowWater = true;
|
|
|
|
|
// await Tank.updateOne({ customerId, tankName: tank.tankName }, { $set: { notificationSentVeryLow: true } });
|
|
|
|
|
// } else if (waterLevelPercentage <= thresholds.low && !notificationSentStatus.lowWater) {
|
|
|
|
|
// eventEmitter.emit('sendLowWaterNotification', fcmTokens, tank);
|
|
|
|
|
// notificationSentStatus.lowWater = true;
|
|
|
|
|
// await Tank.updateOne({ customerId, tankName: tank.tankName }, { $set: { notificationSentLow: true } });
|
|
|
|
|
// }
|
|
|
|
|
// // if (waterLevelPercentage <= thresholds.criticallyLow) {
|
|
|
|
|
// // eventEmitter.emit('sendCriticalLowWaterNotification', fcmTokens, tank);
|
|
|
|
|
// // await Tank.updateOne({ customerId, tankName: tank.tankName }, { $set: { notificationSentCritical: true } });
|
|
|
|
|
// // } else if (waterLevelPercentage <= thresholds.veryLow) {
|
|
|
|
|
// // eventEmitter.emit('sendVeryLowWaterNotification', fcmTokens, tank);
|
|
|
|
|
// // await Tank.updateOne({ customerId, tankName: tank.tankName }, { $set: { notificationSentVeryLow: true } });
|
|
|
|
|
// // } else if (waterLevelPercentage <= thresholds.low) {
|
|
|
|
|
// // eventEmitter.emit('sendLowWaterNotification', fcmTokens, tank);
|
|
|
|
|
// // await Tank.updateOne({ customerId, tankName: tank.tankName }, { $set: { notificationSentLow: true } });
|
|
|
|
|
// // }
|
|
|
|
|
// // else if (waterLevelPercentage >= thresholds.criticallyHigh) {
|
|
|
|
|
// // eventEmitter.emit('sendCriticalHighWaterNotification', fcmTokens, tank);
|
|
|
|
|
// // await Tank.updateOne({ customerId, tankName: tank.tankName }, { $set: { notificationSentCriticalHigh: true } });
|
|
|
|
|
// // } else if (waterLevelPercentage >= thresholds.veryHigh) {
|
|
|
|
|
// // eventEmitter.emit('sendVeryHighWaterNotification', fcmTokens, tank);
|
|
|
|
|
// // await Tank.updateOne({ customerId, tankName: tank.tankName }, { $set: { notificationSentVeryHigh: true } });
|
|
|
|
|
// // } else if (waterLevelPercentage >= thresholds.high) {
|
|
|
|
|
// // eventEmitter.emit('sendHighWaterNotification', fcmTokens, tank);
|
|
|
|
|
// // await Tank.updateOne({ customerId, tankName: tank.tankName }, { $set: { notificationSentHigh: true } });
|
|
|
|
|
// // }
|
|
|
|
|
// } catch (error) {
|
|
|
|
|
// console.error(`Error checking water levels for tank ${tankName}:`, error);
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getPumpsAndUsers = async (req, reply) => {
|
|
|
|
@ -2853,40 +2769,6 @@ exports.getPumpsAndUsers = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// const monitorWaterLevels = async () => {
|
|
|
|
|
// try {
|
|
|
|
|
// const tanks = await Tank.find({});
|
|
|
|
|
|
|
|
|
|
// // Iterate through each tank
|
|
|
|
|
// for (const tank of tanks) {
|
|
|
|
|
// // Fetch users associated with the customerId of the tank
|
|
|
|
|
// const users = await User.find({ customerId: tank.customerId });
|
|
|
|
|
// const fcmTokens = users
|
|
|
|
|
// .map(user => user.fcmIds)
|
|
|
|
|
// .filter(fcmIds => fcmIds)
|
|
|
|
|
// .flat(); // Flatten if there are multiple fcmIds for each user
|
|
|
|
|
|
|
|
|
|
// // Ensure that fcmTokens exist before proceeding
|
|
|
|
|
// if (fcmTokens.length > 0) {
|
|
|
|
|
// const customerId = tank.customerId;
|
|
|
|
|
// const tankName = tank.tankName; // Assuming tank has a 'name' field
|
|
|
|
|
// const tankLocation = tank.tankLocation; // Assuming tank has a 'location' field
|
|
|
|
|
|
|
|
|
|
// // Call the function to check water levels and send notifications
|
|
|
|
|
// //await checkWaterLevelsAndNotify(customerId, tankName, tankLocation, fcmTokens);
|
|
|
|
|
// } else {
|
|
|
|
|
// //console.log(`No FCM tokens found for customerId ${tank.customerId}`);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// } catch (error) {
|
|
|
|
|
// console.error('Error monitoring water levels:', error);
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
// // Schedule the task to run every 30 minutes
|
|
|
|
|
// setInterval(monitorWaterLevels, 30 * 60 * 1000);
|
|
|
|
|
|
|
|
|
|
const motorIntervals = {};
|
|
|
|
|
async function calculateTotalPumpedWater(customerId, motorId, start_instance_id) {
|
|
|
|
|
const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id: start_instance_id });
|
|
|
|
@ -3006,6 +2888,8 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
|
const supplierTank = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() });
|
|
|
|
|
console.log("up",supplierTank)
|
|
|
|
|
console.log(supplierTank.waterlevel,"parseInt(supplierTank.waterlevel, 10)")
|
|
|
|
|
const lowWaterThreshold = 20;
|
|
|
|
|
|
|
|
|
|
const currentWaterLevel = parseInt(supplierTank.waterlevel, 10);
|
|
|
|
|
const currentWaterPercentage = (currentWaterLevel / parseInt(supplierTank.capacity.replace(/,/g, ''), 10)) * 100;
|
|
|
|
|
const notificationTracker = new Map();
|
|
|
|
@ -3037,15 +2921,7 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
|
blockName
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
// eventEmitter.emit(
|
|
|
|
|
// "sendThresholdTimeNotification",
|
|
|
|
|
// customerId,
|
|
|
|
|
// fcmToken,
|
|
|
|
|
// manual_threshold_time,
|
|
|
|
|
// motorId,
|
|
|
|
|
// tankName,
|
|
|
|
|
// blockName
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reply.code(200).send({ message: "Motor stopped successfully." });
|
|
|
|
|
} catch (error) {
|
|
|
|
@ -3053,42 +2929,6 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
|
reply.code(500).send({ error: "Internal Server Error" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// eventEmitter.emit(
|
|
|
|
|
// "sendThresholdTimeNotification",
|
|
|
|
|
// customerId,
|
|
|
|
|
// fcmToken,
|
|
|
|
|
// manual_threshold_time,
|
|
|
|
|
// motorId,
|
|
|
|
|
// tankName,
|
|
|
|
|
// blockName
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// const notificationKey = `${customerId}_${motorId}_threshold`;
|
|
|
|
|
|
|
|
|
|
// // Check if the notification has already been sent
|
|
|
|
|
// if (!notificationTracker.get(notificationKey)) {
|
|
|
|
|
// console.log("Sending threshold time notification...");
|
|
|
|
|
|
|
|
|
|
// eventEmitter.emit(
|
|
|
|
|
// "sendThresholdTimeNotification",
|
|
|
|
|
// customerId,
|
|
|
|
|
// fcmToken,
|
|
|
|
|
// manual_threshold_time,
|
|
|
|
|
// motorId,
|
|
|
|
|
// tankName,
|
|
|
|
|
// blockName
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// // Mark notification as sent
|
|
|
|
|
// notificationTracker.set(notificationKey, true);
|
|
|
|
|
|
|
|
|
|
// // Optionally, reset the flag after some time (e.g., 24 hours)
|
|
|
|
|
// setTimeout(() => {
|
|
|
|
|
// notificationTracker.delete(notificationKey);
|
|
|
|
|
// }, 24 * 60 * 60 * 1000); // Reset after 24 hours
|
|
|
|
|
// } else {
|
|
|
|
|
// console.log("Notification already sent, skipping...");
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm');
|
|
|
|
|
await Tank.updateOne(
|
|
|
|
|