|
|
|
@ -2203,64 +2203,71 @@ eventEmitter.on('sendThresholdTimeNotification', async (fcmTokens, message) => {
|
|
|
|
|
// }
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 🚀 Motor Start Notification
|
|
|
|
|
eventEmitter.on(
|
|
|
|
|
"sendMotorStartNotification",
|
|
|
|
|
async (hw_Id, customerId, fcmTokens, waterLevel, blockName, tankName, motorOnType, stopCriteria, typeOfWater, manualThresholdTime) => {
|
|
|
|
|
eventEmitter.on("sendMotorStartNotification", async (hw_Id, customerId, fcmTokens, waterLevel, blockName, tankName, motorOnType, stopCriteria, typeOfWater, manualThresholdTime) => {
|
|
|
|
|
try {
|
|
|
|
|
const formattedTime = new Date().toLocaleTimeString("en-IN", { timeZone: "Asia/Kolkata" });
|
|
|
|
|
|
|
|
|
|
if (motorOnType !== "forced_manual") {
|
|
|
|
|
console.log(`⚠️ Skipping notification: Motor was started in **${motorOnType}** mode.`);
|
|
|
|
|
const normalizedMotorOnType = motorOnType.toLowerCase();
|
|
|
|
|
if (normalizedMotorOnType !== "forced_manual") {
|
|
|
|
|
console.log(`⚠️ Skipping notification: Motor started in **${motorOnType}** mode.`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const stopConditionMessage = stopCriteria === "manual"
|
|
|
|
|
? `⚠️ Pump will stop **manually**.`
|
|
|
|
|
: `🚨 Pump will stop when the water level reaches **${manualThresholdTime}%**.`;
|
|
|
|
|
if (!Array.isArray(fcmTokens) || fcmTokens.length === 0) {
|
|
|
|
|
console.warn(`⚠️ No valid FCM tokens found for Customer ID: ${customerId}`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const stopConditionMessage = stopCriteria === "level"
|
|
|
|
|
? `🚨 Pump will stop when the water level reaches **${manualThresholdTime}%**.`
|
|
|
|
|
: `⚠️ Pump will stop **manually**.`;
|
|
|
|
|
|
|
|
|
|
const message = `🚰 **Motor Started** 🚀\n` +
|
|
|
|
|
`👤 **Customer ID:** ${customerId}\n` +
|
|
|
|
|
`🔹 **Motor Name:** ${tankName} - ${blockName} - ${typeOfWater}\n` +
|
|
|
|
|
`🏢 **Block Name:** ${blockName}\n` +
|
|
|
|
|
`💧 **Water Level:** ${waterLevel}%\n` +
|
|
|
|
|
`📱 **Mode:** **Physically Started**\n` +
|
|
|
|
|
`🕒 **Pump started at:** ${formattedTime}\n` +
|
|
|
|
|
stopConditionMessage;
|
|
|
|
|
`📱 **Mode:** **Forced Manual**\n` +
|
|
|
|
|
`🕒 **Pump started at:** ${formattedTime}\n`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await sendNotification(hw_Id, customerId, fcmTokens, "Motor Started 🚀", message);
|
|
|
|
|
console.log(`✅ Motor start notification sent for Customer ID: ${customerId}`);
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(`❌ Error in sendMotorStartNotification for Customer ID: ${customerId}`, error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 🛑 Motor Stop Notification
|
|
|
|
|
eventEmitter.on(
|
|
|
|
|
"sendMotorStopNotification",
|
|
|
|
|
async (hw_Id, customerId, fcmTokens, waterLevel, blockName, tankName, motorOnType, typeOfWater) => {
|
|
|
|
|
eventEmitter.on("sendMotorStopNotification", async (hw_Id, customerId, fcmTokens, waterLevel, blockName, tankName, motorOnType, typeOfWater) => {
|
|
|
|
|
try {
|
|
|
|
|
const formattedTime = new Date().toLocaleTimeString("en-IN", { timeZone: "Asia/Kolkata" });
|
|
|
|
|
|
|
|
|
|
const normalizedMotorOnType = motorOnType.toLowerCase();
|
|
|
|
|
if (normalizedMotorOnType !== "forced_manual") {
|
|
|
|
|
console.log(`⚠️ Skipping notification: Motor stopped in **${motorOnType}** mode.`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Array.isArray(fcmTokens) || fcmTokens.length === 0) {
|
|
|
|
|
console.warn(`⚠️ No valid FCM tokens found for Customer ID: ${customerId}`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const message = `🛑 **Motor Stopped** ❌\n` +
|
|
|
|
|
`👤 **Customer ID:** ${customerId}\n` +
|
|
|
|
|
`🔹 **Motor Name:** ${tankName} - ${blockName} - ${typeOfWater}\n` +
|
|
|
|
|
`🏢 **Block Name:** ${blockName}\n` +
|
|
|
|
|
`💧 **Water Level:** ${waterLevel}%\n` +
|
|
|
|
|
`📱 **Mode:** **Physically Stopped**\n` +
|
|
|
|
|
`📱 **Mode:** **Forced Manual**\n` +
|
|
|
|
|
`🕒 **Pump stopped at:** ${formattedTime}`;
|
|
|
|
|
|
|
|
|
|
await sendNotification(hw_Id, customerId, fcmTokens, "Motor Stopped ❌", message);
|
|
|
|
|
console.log(`✅ Motor stop notification sent for Customer ID: ${customerId}`);
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(`❌ Error in sendMotorStopNotification for Customer ID: ${customerId}`, error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// eventEmitter.on('sendLowWaterNotification', (fcmTokens, message) => {
|
|
|
|
|
// const notificationMessage = `Warning: Water level is low in the tank.
|
|
|
|
@ -2566,96 +2573,79 @@ const emitWithTimestamp = (eventName, fcmTokens, motorId, waterLevel) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const sendNotification = async (customerId, fcmIds, title, body) => {
|
|
|
|
|
const sendNotification = async (hw_Id, customerId, fcmIds, title, body) => {
|
|
|
|
|
try {
|
|
|
|
|
if (!customerId) {
|
|
|
|
|
throw new Error("Customer ID is required.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!customerId) throw new Error("Customer ID is required.");
|
|
|
|
|
if (!Array.isArray(fcmIds) || fcmIds.length === 0) {
|
|
|
|
|
throw new Error("No FCM tokens provided or invalid format.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const flatTokens = fcmIds.flat ? fcmIds.flat() : fcmIds;
|
|
|
|
|
if (flatTokens.length === 0) {
|
|
|
|
|
throw new Error("Flattened FCM token list is empty.");
|
|
|
|
|
}
|
|
|
|
|
if (flatTokens.length === 0) throw new Error("Flattened FCM token list is empty.");
|
|
|
|
|
|
|
|
|
|
console.log(`📨 Preparing to send notification to Customer ID: ${customerId}`);
|
|
|
|
|
|
|
|
|
|
// Fetch users based on customerId
|
|
|
|
|
// Fetch users with FCM tokens & preferences
|
|
|
|
|
const users = await User.find({ customerId }).select("fcmIds notificationPreference lastNotificationSent");
|
|
|
|
|
|
|
|
|
|
const promises = users.map(async (user) => {
|
|
|
|
|
const { fcmIds: userFcmIds, notificationPreference, lastNotificationSent } = user;
|
|
|
|
|
|
|
|
|
|
if (!Array.isArray(userFcmIds)) {
|
|
|
|
|
console.log(`Invalid fcmIds for customer ID: ${customerId}`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!Array.isArray(userFcmIds)) return console.log(`⚠️ Invalid fcmIds for customer ID: ${customerId}`);
|
|
|
|
|
|
|
|
|
|
const validTokens = flatTokens.filter(token => userFcmIds.includes(token));
|
|
|
|
|
|
|
|
|
|
if (validTokens.length === 0) {
|
|
|
|
|
console.log(`No matching FCM tokens for customer ID: ${customerId}`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (validTokens.length === 0) return console.log(`⚠️ No matching FCM tokens for customer ID: ${customerId}`);
|
|
|
|
|
|
|
|
|
|
// Handle notification preferences
|
|
|
|
|
if (notificationPreference === "never") {
|
|
|
|
|
console.log(`Notifications disabled for customer ID: ${customerId}`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (notificationPreference === "never") return console.log(`🔕 Notifications disabled for Customer ID: ${customerId}`);
|
|
|
|
|
|
|
|
|
|
const now = new Date();
|
|
|
|
|
const lastSent = new Date(lastNotificationSent || 0);
|
|
|
|
|
|
|
|
|
|
let minInterval = 0;
|
|
|
|
|
|
|
|
|
|
switch (notificationPreference) {
|
|
|
|
|
case "6_hours":
|
|
|
|
|
minInterval = 6 * 60 * 60 * 1000; // 6 hours
|
|
|
|
|
minInterval = 6 * 60 * 60 * 1000;
|
|
|
|
|
break;
|
|
|
|
|
case "8_hours":
|
|
|
|
|
minInterval = 8 * 60 * 60 * 1000; // 8 hours
|
|
|
|
|
minInterval = 8 * 60 * 60 * 1000;
|
|
|
|
|
break;
|
|
|
|
|
case "1_month":
|
|
|
|
|
minInterval = 30 * 24 * 60 * 60 * 1000; // 1 month
|
|
|
|
|
minInterval = 30 * 24 * 60 * 60 * 1000;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if enough time has passed for non-"always" preferences
|
|
|
|
|
if (notificationPreference !== "always" && now - lastSent < minInterval) {
|
|
|
|
|
console.log(`Skipping notification for customer ID: ${customerId} due to preference (${notificationPreference}).`);
|
|
|
|
|
return;
|
|
|
|
|
return console.log(`⏳ Skipping notification for Customer ID: ${customerId} due to preference (${notificationPreference}).`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Send notifications
|
|
|
|
|
console.log(`🚀 Sending notification to Customer ID: ${customerId}, Tokens: ${validTokens.length}`);
|
|
|
|
|
|
|
|
|
|
const notificationPromises = validTokens.map(async (token) => {
|
|
|
|
|
try {
|
|
|
|
|
const response = await admin.messaging().send({
|
|
|
|
|
notification: { title, body },
|
|
|
|
|
token,
|
|
|
|
|
data: { target: "/tank_levels" },
|
|
|
|
|
notification: { title, body },
|
|
|
|
|
data: { hw_Id, target: "/tank_levels" },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log(`Notification sent successfully to token: ${token}`);
|
|
|
|
|
console.log("FCM Response:", response);
|
|
|
|
|
console.log("Titel:", title);
|
|
|
|
|
console.log("Body:", body);
|
|
|
|
|
console.log("Data:", data);
|
|
|
|
|
console.log(`✅ Notification sent successfully to token: ${token}`);
|
|
|
|
|
console.log("📬 FCM Response:", response);
|
|
|
|
|
console.log(`📡 Sending notification to Customer ID: ${customerId}`);
|
|
|
|
|
console.log(`🔍 FCM Tokens:`, fcmTokens);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(`Failed to send notification to token: ${token}`, error);
|
|
|
|
|
console.error(`❌ Failed to send notification to token: ${token}`, error);
|
|
|
|
|
|
|
|
|
|
if (error.code === "messaging/registration-token-not-registered") {
|
|
|
|
|
await User.updateOne({ customerId }, { $pull: { fcmIds: token } });
|
|
|
|
|
console.log(`Removed invalid token: ${token}`);
|
|
|
|
|
console.log(`🗑️ Removed invalid token: ${token}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await Promise.all(notificationPromises);
|
|
|
|
|
|
|
|
|
|
// Update lastNotificationSent timestamp if preference is not "always"
|
|
|
|
|
if (notificationPreference !== "always") {
|
|
|
|
|
await User.updateOne({ customerId }, { $set: { lastNotificationSent: now } });
|
|
|
|
|
}
|
|
|
|
@ -2664,10 +2654,15 @@ const sendNotification = async (customerId, fcmIds, title, body) => {
|
|
|
|
|
await Promise.all(promises);
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error sending notifications:", error);
|
|
|
|
|
console.error("❌ Error sending notifications:", error);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const sendDailyConsumptionNotification = async () => {
|
|
|
|
|
try {
|
|
|
|
|
const now = new Date();
|
|
|
|
@ -2712,19 +2707,30 @@ const sendDailyConsumptionNotification = async () => {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Standardized mapping for water types
|
|
|
|
|
const typeMapping = {
|
|
|
|
|
"bore water": "Bore Water",
|
|
|
|
|
"bore": "Bore Water",
|
|
|
|
|
"drinking": "Drinking Water",
|
|
|
|
|
"drink": "Drinking Water",
|
|
|
|
|
"DRINK": "Drinking Water"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Calculate consumption per water type
|
|
|
|
|
let totalConsumption = 0;
|
|
|
|
|
let consumptionByType = {};
|
|
|
|
|
|
|
|
|
|
for (const record of consumptions) {
|
|
|
|
|
const { typeofwater, consumption } = record;
|
|
|
|
|
const consumptionValue = parseInt(consumption, 10) || 0;
|
|
|
|
|
let { typeofwater, consumption } = record;
|
|
|
|
|
typeofwater = (typeofwater || "").trim().toLowerCase(); // Normalize case
|
|
|
|
|
const standardType = typeMapping[typeofwater] || typeofwater; // Use mapped name or original
|
|
|
|
|
|
|
|
|
|
if (!consumptionByType[typeofwater]) {
|
|
|
|
|
consumptionByType[typeofwater] = 0;
|
|
|
|
|
const consumptionValue = parseInt(consumption, 10) || 0;
|
|
|
|
|
if (!consumptionByType[standardType]) {
|
|
|
|
|
consumptionByType[standardType] = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
consumptionByType[typeofwater] += consumptionValue;
|
|
|
|
|
consumptionByType[standardType] += consumptionValue;
|
|
|
|
|
totalConsumption += consumptionValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -2766,6 +2772,7 @@ const sendDailyConsumptionNotification = async () => {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Schedule the function to run every minute to check for user notification times
|
|
|
|
|
cron.schedule("* * * * *", async () => {
|
|
|
|
|
console.log("🔄 Running daily consumption notification check...");
|
|
|
|
@ -6336,58 +6343,411 @@ client.on('connect', () => {
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// client.on("message", async (topic, message) => {
|
|
|
|
|
// console.log(`📩 Message received on topic ${topic}:`, message.toString());
|
|
|
|
|
|
|
|
|
|
// if (topic === "water/iot-data") {
|
|
|
|
|
// try {
|
|
|
|
|
// const data = JSON.parse(message.toString());
|
|
|
|
|
// const { hw_Id, Motor_status, tanks } = data.objects;
|
|
|
|
|
|
|
|
|
|
// const currentDate = new Date();
|
|
|
|
|
// const time = currentDate.toLocaleTimeString("en-IN", { hour12: false, timeZone: "Asia/Kolkata" });
|
|
|
|
|
|
|
|
|
|
// for (const tank of tanks) {
|
|
|
|
|
// const { Id: tankhardwareId, level: tankHeight } = tank;
|
|
|
|
|
// const existingTank = await Tank.findOne({ hardwareId: hw_Id, tankhardwareId });
|
|
|
|
|
|
|
|
|
|
// if (!existingTank) continue;
|
|
|
|
|
|
|
|
|
|
// const customerId = existingTank.customerId;
|
|
|
|
|
// const tankName = existingTank.tankName;
|
|
|
|
|
|
|
|
|
|
// console.log(`🔍 Checking FCM tokens for Customer ID: ${customerId}`);
|
|
|
|
|
|
|
|
|
|
// const users = await User.find({ customerId }).select("fcmIds");
|
|
|
|
|
// let fcmTokens = users.flatMap(user => user.fcmIds).filter(token => token);
|
|
|
|
|
|
|
|
|
|
// console.log(`📡 Found ${fcmTokens.length} FCM tokens for Customer ID: ${customerId}`);
|
|
|
|
|
|
|
|
|
|
// if (!Array.isArray(fcmTokens) || fcmTokens.length === 0) {
|
|
|
|
|
// console.warn(`⚠️ No valid FCM tokens found for Customer ID: ${customerId}`);
|
|
|
|
|
// continue;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// const motorTank = await Tank.findOne({ "connections.inputConnections.motor_id": hw_Id });
|
|
|
|
|
|
|
|
|
|
// if (!motorTank) {
|
|
|
|
|
// console.warn(`⚠️ Motor not found for motor_id: ${hw_Id}`);
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// const inputConnection = motorTank.connections.inputConnections.find(conn => conn.motor_id === hw_Id);
|
|
|
|
|
// console.log(`📢 Emitting 'sendMotorStartNotification' for hw_Id: ${hw_Id}`);
|
|
|
|
|
|
|
|
|
|
// if (inputConnection) {
|
|
|
|
|
// const blockName = motorTank.blockName || "N/A";
|
|
|
|
|
// console.log(`🚀 blockName`, blockName);
|
|
|
|
|
|
|
|
|
|
// console.log("🔍 Debugging Condition Check:");
|
|
|
|
|
// console.log("🛠️ inputConnection.motor_stop_status:", inputConnection.motor_stop_status);
|
|
|
|
|
// console.log("🛠️ Motor_status:", Motor_status);
|
|
|
|
|
// console.log("🛠️ inputConnection.motor_on_type:", inputConnection.motor_on_type);
|
|
|
|
|
|
|
|
|
|
// if (inputConnection.motor_stop_status == 1 && Motor_status == 2) { // Removed forced_manual check
|
|
|
|
|
// inputConnection.motor_stop_status = "2";
|
|
|
|
|
// inputConnection.motor_on_type = "forced_manual";
|
|
|
|
|
// inputConnection.startTime = new Date().toISOString();
|
|
|
|
|
|
|
|
|
|
// console.log("🚀 Motor started in FORCED_MANUAL mode.");
|
|
|
|
|
// console.log("📢 Emitting sendMotorStartNotification event...");
|
|
|
|
|
|
|
|
|
|
// eventEmitter.emit(
|
|
|
|
|
// "sendMotorStartNotification",
|
|
|
|
|
// hw_Id,
|
|
|
|
|
// customerId,
|
|
|
|
|
// fcmTokens,
|
|
|
|
|
// inputConnection.water_level || 0,
|
|
|
|
|
// blockName,
|
|
|
|
|
// tankName,
|
|
|
|
|
// "forced_manual",
|
|
|
|
|
// inputConnection.stop_criteria,
|
|
|
|
|
// inputConnection.typeOfWater,
|
|
|
|
|
// inputConnection.manual_threshold_time
|
|
|
|
|
// );
|
|
|
|
|
// await motorTank.markModified("connections.inputConnections");
|
|
|
|
|
// await motorTank.save(); // Ensure the change is saved
|
|
|
|
|
// console.log("💾 motorTank saved successfully.");
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// console.log("🛠️ Debugging Motor Stop Condition:");
|
|
|
|
|
// console.log("🛠️ inputConnection.motor_stop_status:", inputConnection.motor_stop_status);
|
|
|
|
|
// console.log("🛠️ Motor_status:", Motor_status);
|
|
|
|
|
|
|
|
|
|
// if (inputConnection.motor_stop_status == 2 && Motor_status == 1) {
|
|
|
|
|
// console.log("🛑 Motor stopping... Updating motor_stop_status.");
|
|
|
|
|
|
|
|
|
|
// inputConnection.motor_stop_status = 1;
|
|
|
|
|
// inputConnection.stopTime = new Date().toISOString();
|
|
|
|
|
|
|
|
|
|
// console.log("📢 Emitting sendMotorStopNotification event...");
|
|
|
|
|
|
|
|
|
|
// eventEmitter.emit(
|
|
|
|
|
// "sendMotorStopNotification",
|
|
|
|
|
// hw_Id,
|
|
|
|
|
// customerId,
|
|
|
|
|
// fcmTokens,
|
|
|
|
|
// inputConnection.water_level || 0,
|
|
|
|
|
// blockName,
|
|
|
|
|
// tankName,
|
|
|
|
|
// "forced_manual",
|
|
|
|
|
// inputConnection.typeOfWater
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// await motorTank.markModified("connections.inputConnections");
|
|
|
|
|
// await motorTank.save();
|
|
|
|
|
// console.log("💾 motorTank saved successfully.");
|
|
|
|
|
// } else {
|
|
|
|
|
// console.log("❌ Stop condition not met. No notification sent.");
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// console.log(`✅ Data processed successfully for hardwareId: ${hw_Id}`);
|
|
|
|
|
// } catch (err) {
|
|
|
|
|
// console.error(`❌ Error processing message: ${err.message}`);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// client.on("message", async (topic, message) => {
|
|
|
|
|
// console.log(`📩 Message received on topic ${topic}:`, message.toString());
|
|
|
|
|
|
|
|
|
|
// if (topic === "water/iot-data") {
|
|
|
|
|
// try {
|
|
|
|
|
// const data = JSON.parse(message.toString());
|
|
|
|
|
// const { hw_Id, Motor_status, tanks } = data.objects;
|
|
|
|
|
|
|
|
|
|
// for (const tank of tanks) {
|
|
|
|
|
// const { Id: tankhardwareId } = tank;
|
|
|
|
|
// const existingTank = await Tank.findOne({ hardwareId: hw_Id, tankhardwareId });
|
|
|
|
|
|
|
|
|
|
// if (!existingTank) continue;
|
|
|
|
|
|
|
|
|
|
// const customerId = existingTank.customerId;
|
|
|
|
|
// const tankName = existingTank.tankName;
|
|
|
|
|
|
|
|
|
|
// console.log(`🔍 Checking FCM tokens for Customer ID: ${customerId}`);
|
|
|
|
|
|
|
|
|
|
// const users = await User.find({ customerId }).select("fcmIds");
|
|
|
|
|
// let fcmTokens = users.flatMap(user => user.fcmIds).filter(token => token);
|
|
|
|
|
|
|
|
|
|
// console.log(`📡 Found ${fcmTokens.length} FCM tokens for Customer ID: ${customerId}`);
|
|
|
|
|
|
|
|
|
|
// if (!Array.isArray(fcmTokens) || fcmTokens.length === 0) {
|
|
|
|
|
// console.warn(`⚠️ No valid FCM tokens found for Customer ID: ${customerId}`);
|
|
|
|
|
// continue;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// const motorTank = await Tank.findOne({ "connections.inputConnections.motor_id": hw_Id });
|
|
|
|
|
|
|
|
|
|
// if (!motorTank) {
|
|
|
|
|
// console.warn(`⚠️ Motor not found for motor_id: ${hw_Id}`);
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// const inputConnection = motorTank.connections.inputConnections.find(conn => conn.motor_id === hw_Id);
|
|
|
|
|
|
|
|
|
|
// if (!inputConnection) {
|
|
|
|
|
// console.warn(`⚠️ No inputConnection found for motor_id: ${hw_Id}`);
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// console.log(`📢 Processing motor logic for hw_Id: ${hw_Id}`);
|
|
|
|
|
// const blockName = motorTank.blockName || "N/A";
|
|
|
|
|
|
|
|
|
|
// console.log("🔍 Debugging Condition Check:");
|
|
|
|
|
// console.log("🛠️ inputConnection.motor_stop_status:", inputConnection.motor_stop_status);
|
|
|
|
|
// console.log("🛠️ Motor_status:", Motor_status);
|
|
|
|
|
// console.log("🛠️ inputConnection.motor_on_type:", inputConnection.motor_on_type);
|
|
|
|
|
|
|
|
|
|
// /** 🔥 MOTOR START LOGIC */
|
|
|
|
|
// if (Motor_status === 2 && inputConnection.motor_stop_status === 1) {
|
|
|
|
|
// // Only update if motor_stop_status is still 1 (motor was previously OFF)
|
|
|
|
|
// inputConnection.motor_stop_status = 2;
|
|
|
|
|
// inputConnection.motor_on_type = "forced_manual";
|
|
|
|
|
// inputConnection.startTime = new Date().toISOString();
|
|
|
|
|
|
|
|
|
|
// console.log("🚀 Motor started. Updating motor_stop_status to 2.");
|
|
|
|
|
// console.log("📢 Emitting sendMotorStartNotification event...");
|
|
|
|
|
|
|
|
|
|
// eventEmitter.emit(
|
|
|
|
|
// "sendMotorStartNotification",
|
|
|
|
|
// hw_Id,
|
|
|
|
|
// customerId,
|
|
|
|
|
// fcmTokens,
|
|
|
|
|
// inputConnection.water_level || 0,
|
|
|
|
|
// blockName,
|
|
|
|
|
// tankName,
|
|
|
|
|
// "forced_manual",
|
|
|
|
|
// inputConnection.stop_criteria,
|
|
|
|
|
// inputConnection.typeOfWater,
|
|
|
|
|
// inputConnection.manual_threshold_time
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// await motorTank.markModified("connections.inputConnections");
|
|
|
|
|
// await motorTank.save();
|
|
|
|
|
// console.log("💾 motorTank saved successfully after start.");
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// /** 🔥 MOTOR STOP LOGIC */
|
|
|
|
|
// console.log("🛠️ Debugging Motor Stop Condition:");
|
|
|
|
|
// console.log("🛠️ inputConnection.motor_stop_status:", inputConnection.motor_stop_status);
|
|
|
|
|
// console.log("🛠️ Motor_status:", Motor_status);
|
|
|
|
|
|
|
|
|
|
// if (Motor_status === 1 && inputConnection.motor_stop_status === 2) {
|
|
|
|
|
// // Only update if motor_stop_status was previously 2 (motor was ON)
|
|
|
|
|
// console.log("🛑 Motor stopping... Updating motor_stop_status to 1.");
|
|
|
|
|
|
|
|
|
|
// inputConnection.motor_stop_status = 1;
|
|
|
|
|
// inputConnection.stopTime = new Date().toISOString();
|
|
|
|
|
|
|
|
|
|
// console.log("📢 Emitting sendMotorStopNotification event...");
|
|
|
|
|
|
|
|
|
|
// eventEmitter.emit(
|
|
|
|
|
// "sendMotorStopNotification",
|
|
|
|
|
// hw_Id,
|
|
|
|
|
// customerId,
|
|
|
|
|
// fcmTokens,
|
|
|
|
|
// inputConnection.water_level || 0,
|
|
|
|
|
// blockName,
|
|
|
|
|
// tankName,
|
|
|
|
|
// "forced_manual",
|
|
|
|
|
// inputConnection.typeOfWater
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// await motorTank.markModified("connections.inputConnections");
|
|
|
|
|
// await motorTank.save();
|
|
|
|
|
// console.log("💾 motorTank saved successfully after stop.");
|
|
|
|
|
// } else {
|
|
|
|
|
// console.log("⚠️ Stop condition not met. No notification sent.");
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// console.log(`✅ Data processed successfully for hardwareId: ${hw_Id}`);
|
|
|
|
|
// } catch (err) {
|
|
|
|
|
// console.error(`❌ Error processing message: ${err.message}`);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Handling incoming MQTT messages
|
|
|
|
|
client.on('message', async (topic, message) => {
|
|
|
|
|
console.log(`📩 Message received on topic ${topic}:`, message.toString());
|
|
|
|
|
console.log(`Message received on topic ${topic}:`, message.toString());
|
|
|
|
|
|
|
|
|
|
if (topic === 'water/iot-data') {
|
|
|
|
|
try {
|
|
|
|
|
const data = JSON.parse(message.toString());
|
|
|
|
|
const { hw_Id, Motor_status, tanks } = data.objects;
|
|
|
|
|
const { hw_Id, Motor_status, tanks } = data.objects; // Updated variable names according to new format
|
|
|
|
|
|
|
|
|
|
// Get the current date and time in the required format
|
|
|
|
|
const currentDate = new Date();
|
|
|
|
|
const date = currentDate.toISOString();
|
|
|
|
|
const time = currentDate.toLocaleTimeString('en-IN', { hour12: false, timeZone: 'Asia/Kolkata' });
|
|
|
|
|
const date = currentDate.toISOString(); // ISO string for date
|
|
|
|
|
const time = currentDate.toLocaleTimeString('en-IN', { hour12: false, timeZone: 'Asia/Kolkata' }); // Time in 'HH:MM:SS'
|
|
|
|
|
|
|
|
|
|
// Create array of tank documents with current date and time
|
|
|
|
|
const tankDocuments = tanks.map(tank => ({
|
|
|
|
|
tankhardwareId: tank.Id, // Updated to match the new format
|
|
|
|
|
tankHeight: tank.level, // Updated to match the new format
|
|
|
|
|
date,
|
|
|
|
|
time
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
for (const tank of tanks) {
|
|
|
|
|
const { Id: tankhardwareId, level: tankHeight } = tank;
|
|
|
|
|
const existingTank = await Tank.findOne({ hardwareId: hw_Id, tankhardwareId });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Save IoT data for the received tanks
|
|
|
|
|
const iotTankData = new IotData({
|
|
|
|
|
hardwareId: hw_Id, // Updated variable name
|
|
|
|
|
Motor_status,
|
|
|
|
|
tanks: tankDocuments,
|
|
|
|
|
date,
|
|
|
|
|
time
|
|
|
|
|
});
|
|
|
|
|
await iotTankData.save();
|
|
|
|
|
|
|
|
|
|
// Delete excess records (keep only the latest three records)
|
|
|
|
|
const recordsToKeep = 3;
|
|
|
|
|
const recordsToDelete = await IotData.find({ hardwareId: hw_Id }) // Updated variable name
|
|
|
|
|
.sort({ date: -1, time: -1 })
|
|
|
|
|
.skip(recordsToKeep);
|
|
|
|
|
|
|
|
|
|
for (const record of recordsToDelete) {
|
|
|
|
|
await record.remove();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Process each tank to update water level and connections
|
|
|
|
|
for (const tank of tanks) {
|
|
|
|
|
const { Id: tankhardwareId, level: tankHeight } = tank; // Updated to match the new format
|
|
|
|
|
// Find the corresponding tank in the Tank schema using hardwareId and tankhardwareId
|
|
|
|
|
const existingTank = await Tank.findOne({ hardwareId: hw_Id, tankhardwareId }); // Updated variable name
|
|
|
|
|
if (!existingTank) continue;
|
|
|
|
|
|
|
|
|
|
const customerId = existingTank.customerId;
|
|
|
|
|
const tankName = existingTank.tankName;
|
|
|
|
|
const tank_name = existingTank.tankName;
|
|
|
|
|
|
|
|
|
|
console.log(`🔍 Checking FCM tokens for Customer ID: ${customerId}`);
|
|
|
|
|
// Calculate water level using tank height and capacity
|
|
|
|
|
const tankHeightInCm = (parseInt(existingTank.height.replace(/,/g, ''), 10)) * 30.48; // Convert height to cm
|
|
|
|
|
const tank_height = parseInt(tankHeightInCm.toFixed(0), 10);
|
|
|
|
|
const waterLevelHeight = tank_height - tankHeight;
|
|
|
|
|
const waterCapacityPerCm = parseInt(existingTank.waterCapacityPerCm.replace(/,/g, ''), 10);
|
|
|
|
|
|
|
|
|
|
const users = await User.find({ customerId }).select("fcmIds");
|
|
|
|
|
const fcmTokens = users.flatMap(user => user.fcmIds).filter(token => token);
|
|
|
|
|
const waterLevel = parseInt(waterLevelHeight * waterCapacityPerCm, 10); // Calculated water level
|
|
|
|
|
|
|
|
|
|
console.log(`📡 Found ${fcmTokens.length} FCM tokens for Customer ID: ${customerId}`);
|
|
|
|
|
// Update water level in the existing tank
|
|
|
|
|
console.log(tankHeight,"this is located in tank controllers at iot-data mqtt sub ")
|
|
|
|
|
if (tankHeight>0 && waterLevel >= 0) {
|
|
|
|
|
existingTank.waterlevel = waterLevel;
|
|
|
|
|
await existingTank.save();
|
|
|
|
|
|
|
|
|
|
if (!fcmTokens.length) {
|
|
|
|
|
console.warn(`⚠️ No valid FCM tokens found for Customer ID: ${customerId}`);
|
|
|
|
|
// Update linked tanks (input/output connections)
|
|
|
|
|
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 = waterLevel; // Update water level for linked tank
|
|
|
|
|
await linkedTank.save(); // Save updated linked tank
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update motor status
|
|
|
|
|
const status = Motor_status;
|
|
|
|
|
const motorTank = await Tank.findOne({ "connections.inputConnections.motor_id": hw_Id }); // Updated variable name
|
|
|
|
|
|
|
|
|
|
if (!motorTank) {
|
|
|
|
|
console.log('Motor not found for the specified motor_id');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const motorTank = await Tank.findOne({ "connections.inputConnections.motor_id": hw_Id });
|
|
|
|
|
// Find the inputConnection for the motor and update motor status
|
|
|
|
|
const inputConnection = motorTank.connections.inputConnections.find(conn => conn.motor_id === hw_Id); // Updated variable name
|
|
|
|
|
const user = await User.findOne({ customerId: motorTank.customerId }); // Fetch user by customerId
|
|
|
|
|
const allowNotifications = user?.manualStartAndStopNotify ?? true; // Default to true if not set
|
|
|
|
|
|
|
|
|
|
if (!motorTank) {
|
|
|
|
|
console.warn(`⚠️ Motor not found for motor_id: ${hw_Id}`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const inputConnection = motorTank.connections.inputConnections.find(conn => conn.motor_id === hw_Id);
|
|
|
|
|
if (!inputConnection) {
|
|
|
|
|
console.warn(`⚠️ No inputConnection found for motor_id: ${hw_Id}`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (inputConnection) {
|
|
|
|
|
inputConnection.motor_status = status; // Update motor status
|
|
|
|
|
const tankName = motorTank.tankName;
|
|
|
|
|
const customerId = motorTank.customerId;
|
|
|
|
|
console.log("tankName",tankName)
|
|
|
|
|
console.log("customerId", customerId)
|
|
|
|
|
// Ensure fcmTokens are fetched before emitting
|
|
|
|
|
// const user = await User.findOne({ customerId: motorTank.customerId }).select("fcmIds manualStartAndStopNotify");
|
|
|
|
|
// const fcmTokens = user?.fcmIds || []; // Ensure fcmTokens is an array
|
|
|
|
|
// console.log("fcmTokens", fcmTokens)
|
|
|
|
|
|
|
|
|
|
const users = await User.findOne({ customerId : motorTank.customerId}).select("fcmIds");
|
|
|
|
|
// console.log("users", users)
|
|
|
|
|
// let fcmTokens = users.flatMap(user => user.fcmIds).filter(token => token);
|
|
|
|
|
let fcmTokens = users.fcmIds.filter(token => token);
|
|
|
|
|
|
|
|
|
|
console.log(`📡 Found ${fcmTokens.length} FCM tokens for Customer ID: ${customerId}`);
|
|
|
|
|
|
|
|
|
|
if (!Array.isArray(fcmTokens) || fcmTokens.length === 0) {
|
|
|
|
|
console.warn(`⚠️ No valid FCM tokens found for Customer ID: ${customerId}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// const motorTank = await Tank.findOne({ "connections.inputConnections.motor_id": hw_Id });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// const inputConnection = motorTank.connections.inputConnections.find(conn => conn.motor_id === hw_Id);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log(`📢 Processing motor logic for hw_Id: ${hw_Id}`);
|
|
|
|
|
const blockName = motorTank.blockName || "N/A";
|
|
|
|
|
const allowNotifications = (await User.findOne({ customerId }))?.manualStartAndStopNotify ?? true;
|
|
|
|
|
|
|
|
|
|
if (allowNotifications && inputConnection.motor_stop_status === "1" && Motor_status === 2 && inputConnection.motor_on_type !== "forced_manual") {
|
|
|
|
|
inputConnection.motor_stop_status = "2";
|
|
|
|
|
console.log("🔍 Debugging Condition Check:");
|
|
|
|
|
console.log("🛠️ inputConnection.motor_stop_status:", inputConnection.motor_stop_status);
|
|
|
|
|
console.log("🛠️ Motor_status:", status);
|
|
|
|
|
console.log("🛠️inputConnection.motor_status:", inputConnection.motor_status);
|
|
|
|
|
|
|
|
|
|
console.log("🛠️ inputConnection.motor_on_type:", inputConnection.motor_on_type);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (status === 2 && inputConnection.motor_stop_status === "1") {
|
|
|
|
|
// Only update if motor_stop_status is still 1 (motor was previously OFF)
|
|
|
|
|
inputConnection.motor_stop_status = 2;
|
|
|
|
|
inputConnection.motor_on_type = "forced_manual";
|
|
|
|
|
inputConnection.startTime = new Date().toISOString();
|
|
|
|
|
// status = 1;
|
|
|
|
|
// inputConnection.motor_status = 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log("🛠️ IN inputConnection.motor_stop_status:", inputConnection.motor_stop_status);
|
|
|
|
|
console.log("🛠️ Motor_status:", status);
|
|
|
|
|
console.log("🛠️inputConnection.motor_status:", inputConnection.motor_status);
|
|
|
|
|
|
|
|
|
|
console.log("🚀 Motor started. Updating motor_stop_status to 2.");
|
|
|
|
|
console.log("📢 Emitting sendMotorStartNotification event...");
|
|
|
|
|
|
|
|
|
|
console.log(`🚀 Motor started in FORCED_MANUAL mode.`);
|
|
|
|
|
await motorTank.markModified("connections.inputConnections");
|
|
|
|
|
await motorTank.save();
|
|
|
|
|
console.log("💾 motorTank saved successfully after start.");
|
|
|
|
|
|
|
|
|
|
eventEmitter.emit(
|
|
|
|
|
"sendMotorStartNotification",
|
|
|
|
@ -6398,17 +6758,126 @@ client.on('message', async (topic, message) => {
|
|
|
|
|
blockName,
|
|
|
|
|
tankName,
|
|
|
|
|
"forced_manual",
|
|
|
|
|
inputConnection.stop_criteria,
|
|
|
|
|
inputConnection.typeOfWater,
|
|
|
|
|
inputConnection.manual_threshold_time
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (allowNotifications && inputConnection.motor_stop_status === "2" && Motor_status === 1) {
|
|
|
|
|
// if (allowNotifications && inputConnection.motor_stop_status === "1" && status === 2 && inputConnection.motor_on_type !== "forced_manual") {
|
|
|
|
|
// const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm');
|
|
|
|
|
// inputConnection.motor_stop_status = "2";
|
|
|
|
|
// inputConnection.motor_on_type = "forced_manual";
|
|
|
|
|
// inputConnection.startTime = currentTime;
|
|
|
|
|
|
|
|
|
|
// console.log("📢 Emitting sendMotorStartNotification event...");
|
|
|
|
|
|
|
|
|
|
// eventEmitter.emit(
|
|
|
|
|
// "sendMotorStartNotification",
|
|
|
|
|
// hw_Id,
|
|
|
|
|
// customerId,
|
|
|
|
|
// fcmTokens,
|
|
|
|
|
// inputConnection.water_level || 0,
|
|
|
|
|
// blockName,
|
|
|
|
|
// tankName,
|
|
|
|
|
// "forced_manual",
|
|
|
|
|
// inputConnection.stop_criteria,
|
|
|
|
|
// inputConnection.typeOfWater,
|
|
|
|
|
// inputConnection.manual_threshold_time
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// await motorTank.markModified("connections.inputConnections");
|
|
|
|
|
// await motorTank.save();
|
|
|
|
|
// console.log("💾 motorTank saved successfully.");
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// if (allowNotifications && inputConnection.motor_stop_status === "1" && status === 2 && inputConnection.motor_on_type !== "forced_manual") {
|
|
|
|
|
// const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm');
|
|
|
|
|
// inputConnection.motor_stop_status = "2";
|
|
|
|
|
// inputConnection.motor_on_type = "forced_manual";
|
|
|
|
|
// inputConnection.startTime = currentTime;
|
|
|
|
|
|
|
|
|
|
// console.log("🚀 Motor started in FORCED_MANUAL mode.");
|
|
|
|
|
|
|
|
|
|
// console.log("📢 Emitting sendMotorStartNotification event...");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// eventEmitter.emit(
|
|
|
|
|
// "sendMotorStartNotification",
|
|
|
|
|
// hw_Id,
|
|
|
|
|
// customerId,
|
|
|
|
|
// fcmTokens,
|
|
|
|
|
// inputConnection.water_level || 0,
|
|
|
|
|
// blockName,
|
|
|
|
|
// tankName,
|
|
|
|
|
// "forced_manual",
|
|
|
|
|
// inputConnection.stop_criteria,
|
|
|
|
|
// inputConnection.typeOfWater,
|
|
|
|
|
// inputConnection.manual_threshold_time
|
|
|
|
|
// );
|
|
|
|
|
// await motorTank.markModified("connections.inputConnections");
|
|
|
|
|
// await motorTank.save(); // Ensure the change is saved
|
|
|
|
|
// console.log("💾 motorTank saved successfully.");
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// if (allowNotifications && inputConnection.motor_stop_status === "2" && status === 1) {
|
|
|
|
|
// const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm');
|
|
|
|
|
// inputConnection.motor_stop_status = "1";
|
|
|
|
|
// inputConnection.stopTime = currentTime;
|
|
|
|
|
|
|
|
|
|
// console.log("📢 Emitting sendMotorStopNotification event...");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// eventEmitter.emit(
|
|
|
|
|
// "sendMotorStopNotification",
|
|
|
|
|
// hw_Id,
|
|
|
|
|
// customerId,
|
|
|
|
|
// fcmTokens,
|
|
|
|
|
// inputConnection.water_level || 0,
|
|
|
|
|
// blockName,
|
|
|
|
|
// tankName,
|
|
|
|
|
// "forced_manual",
|
|
|
|
|
// inputConnection.typeOfWater
|
|
|
|
|
// );
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// if (allowNotifications && inputConnection.motor_stop_status === "2" && status === 1) {
|
|
|
|
|
// const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm');
|
|
|
|
|
// inputConnection.motor_stop_status = "1";
|
|
|
|
|
// inputConnection.stopTime = currentTime;
|
|
|
|
|
|
|
|
|
|
// console.log("📢 Emitting sendMotorStopNotification event...");
|
|
|
|
|
|
|
|
|
|
// eventEmitter.emit(
|
|
|
|
|
// "sendMotorStopNotification",
|
|
|
|
|
// hw_Id,
|
|
|
|
|
// customerId,
|
|
|
|
|
// fcmTokens,
|
|
|
|
|
// inputConnection.water_level || 0,
|
|
|
|
|
// blockName,
|
|
|
|
|
// tankName,
|
|
|
|
|
// "forced_manual",
|
|
|
|
|
// inputConnection.typeOfWater
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// await motorTank.markModified("connections.inputConnections");
|
|
|
|
|
// await motorTank.save();
|
|
|
|
|
// }
|
|
|
|
|
console.log(`🔍 Checking stop condition: status=${status}, motor_stop_status=${inputConnection.motor_stop_status}`);
|
|
|
|
|
if (status === 2 && inputConnection.motor_stop_status === "2") {
|
|
|
|
|
console.log("🛑 Motor stopping... Updating motor_stop_status to 1.");
|
|
|
|
|
|
|
|
|
|
inputConnection.motor_stop_status = "1";
|
|
|
|
|
inputConnection.stopTime = new Date().toISOString();
|
|
|
|
|
|
|
|
|
|
console.log(`🛑 Motor stopped manually.`);
|
|
|
|
|
await motorTank.markModified("connections.inputConnections");
|
|
|
|
|
await motorTank.save(); // Ensure data is saved before emitting
|
|
|
|
|
|
|
|
|
|
console.log("📢 Emitting sendMotorStopNotification event...");
|
|
|
|
|
eventEmitter.emit(
|
|
|
|
|
"sendMotorStopNotification",
|
|
|
|
|
hw_Id,
|
|
|
|
@ -6422,19 +6891,23 @@ client.on('message', async (topic, message) => {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await motorTank.save();
|
|
|
|
|
}
|
|
|
|
|
await motorTank.save(); // Save the updated tank
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log(`✅ Data processed successfully for hardwareId: ${hw_Id}`);
|
|
|
|
|
console.log('Data processed successfully for hardwareId:', hw_Id); // Updated variable name
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(`❌ Error processing message: ${err.message}`);
|
|
|
|
|
console.error('Error processing message:', err.message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getPendingAndCompletedsurveyOfparticularInstaller = async (request, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { installationId } = request.params;
|
|
|
|
|