From 85e053747959e7188320e9f4f63370f9befa0491 Mon Sep 17 00:00:00 2001 From: Varun Date: Tue, 18 Mar 2025 14:13:47 +0530 Subject: [PATCH 1/2] changes --- src/controllers/tanksController.js | 192 ++++++++++++++--------------- 1 file changed, 93 insertions(+), 99 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 49151590..8db3656e 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -2997,7 +2997,7 @@ exports.motorAction = async (req, reply) => { } ); - + clearInterval(motorIntervals[motorId]); // Stop the motor if condition met @@ -5772,48 +5772,44 @@ require('dotenv').config(); // **Persistent MQTT Connection** const client = mqtt.connect('mqtt://35.207.198.4:1883', { clientId: `mqtt_server_${Math.random().toString(16).substr(2, 8)}`, - clean: false, - reconnectPeriod: 2000, + clean: false, // Ensures MQTT retains subscriptions + reconnectPeriod: 2000, // Reconnect every 2 seconds }); const subscribedTopics = new Set(); -const activeDevices = new Set(); -const DEVICE_TIMEOUT = 10 * 60 * 1000; // 10 minutes +const activeDevices = new Set(); // Keep track of active devices client.on('connect', () => { console.log('āœ… Connected to MQTT broker'); + // **Ensure re-subscriptions after reconnect** subscribedTopics.forEach(topic => { client.subscribe(topic, { qos: 1 }, (err) => { - if (err) console.error(`āŒ Error resubscribing to ${topic}:`, err); - else console.log(`šŸ”„ Resubscribed to ${topic}`); + if (err) { + console.error(`āŒ Error resubscribing to ${topic}:`, err); + } else { + console.log(`šŸ”„ Resubscribed to ${topic}`); + } }); }); + // **Subscribe to new device announcements** client.subscribe('water/iot-data/announce', { qos: 1 }, (err) => { - if (err) console.error('āŒ Error subscribing to announcement topic:', err); - else console.log('šŸ“” Subscribed to water/iot-data/announce'); + if (err) { + console.error('āŒ Error subscribing to announcement topic:', err); + } else { + console.log('šŸ“” Subscribed to water/iot-data/announce'); + } }); }); -client.on('offline', () => { - console.log('āš ļø MQTT Broker Offline. Attempting to reconnect...'); - if (!client.connected) client.reconnect(); -}); - -client.on('close', () => { - console.log('āš ļø MQTT Connection Closed. Attempting to reconnect...'); - if (!client.connected) client.reconnect(); -}); - -client.on('error', (err) => console.error('āŒ MQTT Error:', err)); - client.on('message', async (topic, message) => { console.log(`šŸ“© Message received on topic ${topic}: ${message.toString()}`); try { const data = JSON.parse(message.toString()); + // **Handle device announcements** if (topic === 'water/iot-data/announce') { if (!data.objects || !data.objects.hw_Id) { console.error("āŒ Invalid announcement format. Missing hw_Id."); @@ -5825,58 +5821,42 @@ client.on('message', async (topic, message) => { if (!subscribedTopics.has(deviceTopic)) { client.subscribe(deviceTopic, { qos: 1 }, (err) => { - if (err) console.error(`āŒ Error subscribing to ${deviceTopic}:`, err); - else { + if (err) { + console.error(`āŒ Error subscribing to ${deviceTopic}:`, err); + } else { console.log(`āœ… Subscribed to ${deviceTopic}`); subscribedTopics.add(deviceTopic); activeDevices.add(hw_Id); console.log('šŸ“” Active Devices:', Array.from(activeDevices)); - setTimeout(() => { - if (subscribedTopics.has(deviceTopic) && activeDevices.has(hw_Id)) { // Check if still active - console.log(`šŸ”„ Unsubscribing from inactive device: ${deviceTopic}`); - client.unsubscribe(deviceTopic); - subscribedTopics.delete(deviceTopic); - activeDevices.delete(hw_Id); - } - }, DEVICE_TIMEOUT); - + // āœ… **Now also process data** + processIotData(hw_Id, data); } }); + } else { + console.log(`šŸ”„ Already subscribed to ${deviceTopic}, processing data.`); + processIotData(hw_Id, data); } return; } + // **Process IoT Data for device topics** if (topic.startsWith('water/iot-data/')) { setImmediate(() => { - console.log(`šŸš€ Processing IoT Data for topic: ${topic}`); - - // Extract hw_Id from received data first, fallback to topic split - const hw_Id = data.objects?.hw_Id || topic.split('/')[2]; - - if (!hw_Id) { - console.error("āŒ hw_Id missing in received data:", JSON.stringify(data, null, 2)); - return; - } - - console.log("Extracted hw_Id:", hw_Id); - - // Ensure data is valid before processing - if (!data || !data.objects) { - console.error("āŒ Invalid data received:", JSON.stringify(data, null, 2)); - return; - } - - // Process IoT data asynchronously - setImmediate(() => processIotData(hw_Id, data)); + console.log(`šŸš€ Entering processIotData() for topic: ${topic}`); + const hw_Id = topic.split('/')[2]; + processIotData(hw_Id, data); }); } - } catch (err) { console.error('āŒ Error processing message:', err.message); } }); +client.on('error', (err) => console.error('āŒ MQTT Error:', err)); +client.on('close', () => console.log('āš ļø MQTT Connection Closed.')); +client.on('offline', () => console.log('āš ļø MQTT Broker Offline.')); + async function processIotData(hw_Id, data) { try { console.log(`šŸ“” Processing IoT Data for hw_Id: ${hw_Id}`, JSON.stringify(data, null, 2)); @@ -5888,7 +5868,7 @@ async function processIotData(hw_Id, data) { const { Motor_status, tanks } = data.objects; const currentDate = new Date(); - const date = currentDate.toISOString(); + const date = currentDate.toISOString(); // ISO string for date const time = currentDate.toLocaleTimeString('en-IN', { hour12: false, timeZone: 'Asia/Kolkata' }); const tankDocuments = tanks.map(tank => ({ @@ -5898,19 +5878,31 @@ async function processIotData(hw_Id, data) { time })); - const iotTankData = new IotData({ hardwareId: hw_Id, Motor_status, tanks: tankDocuments, date, time }); + const iotTankData = new IotData({ + hardwareId: hw_Id, + 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 }) + .sort({ date: -1, time: -1 }) + .skip(recordsToKeep); - // **Save IoT Data & Clean Up Old Records in Parallel** - await Promise.all([ - iotTankData.save(), - IotData.deleteMany({ hardwareId: hw_Id }).sort({ date: -1, time: -1 }).skip(3) - ]); + for (const record of recordsToDelete) { + await record.remove(); + } - const tankOperations = tanks.map(async (tank) => { + // Process each tank + for (const tank of tanks) { const { Id: tankhardwareId, level: tankHeight } = tank; const existingTank = await Tank.findOne({ hardwareId: hw_Id, tankhardwareId }); - if (!existingTank) return; + if (!existingTank) continue; const customerId = existingTank.customerId; const tank_name = existingTank.tankName; @@ -5921,62 +5913,64 @@ async function processIotData(hw_Id, data) { const waterCapacityPerCm = parseInt(existingTank.waterCapacityPerCm.replace(/,/g, ''), 10); const waterLevel = parseInt(waterLevelHeight * waterCapacityPerCm, 10); - existingTank.waterlevel = waterLevel; - await existingTank.save(); - - // **Process 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; - await linkedTank.save(); + console.log(`🚰 Tank [${tankhardwareId}] - Level: ${tankHeight}, Calculated Water Level: ${waterLevel}`); + + if (tankHeight > 0 && waterLevel >= 0) { + existingTank.waterlevel = waterLevel; + await existingTank.save(); + + 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; + await linkedTank.save(); + } } } } } - }); - - await Promise.all(tankOperations); + } - // **Update Motor Status** + // Update motor status + const status = Motor_status; const motorTank = await Tank.findOne({ "connections.inputConnections.motor_id": hw_Id }); - console.log(motorTank,"motortank") - if (motorTank) { - const inputConnection = motorTank.connections.inputConnections.find(conn => conn.motor_id === hw_Id); - if (inputConnection) { - console.log("it entered inputconnection",Motor_status,inputConnection.motor_status ) - inputConnection.motor_status = Motor_status; + if (!motorTank) { + console.log('āš ļø Motor not found for specified motor_id'); + return; + } - if (inputConnection.motor_stop_status === "1" && Motor_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; - inputConnection.start_instance_id = `${hw_Id}${currentTime}`; - } + const inputConnection = motorTank.connections.inputConnections.find(conn => conn.motor_id === hw_Id); + if (inputConnection) { + inputConnection.motor_status = status; - if (inputConnection.motor_stop_status === "2" && Motor_status === 1) { - const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm'); - inputConnection.motor_stop_status = "1"; - inputConnection.motor_on_type = "manual"; - inputConnection.stopTime = currentTime; - } + if (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; + } - await motorTank.save(); + if (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.motor_on_type = "manual"; + inputConnection.stopTime = currentTime; } - } else { - console.log('āš ļø Motor not found for specified motor_id'); + + await motorTank.save(); } console.log(`āœ… Data processed successfully for hw_Id: ${hw_Id}`); + } catch (err) { console.error('āŒ Error processing IoT data:', err.message); } } + function logSets() { console.log("Subscribed Topics:", Array.from(subscribedTopics)); console.log("Active Devices:", Array.from(activeDevices)); From 992eab6eba3e2768280582ee578eab305c082edc Mon Sep 17 00:00:00 2001 From: Varun Date: Tue, 18 Mar 2025 15:26:23 +0530 Subject: [PATCH 2/2] changes --- src/controllers/tanksController.js | 159 +++++++++++++++-------------- 1 file changed, 82 insertions(+), 77 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index f40ef152..041f792b 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -5896,93 +5896,98 @@ exports.getBlockData = async (req, reply) => { const mqtt = require('mqtt'); require('dotenv').config(); -// **Persistent MQTT Connection** -const client = mqtt.connect('mqtt://35.207.198.4:1883', { - clientId: `mqtt_server_${Math.random().toString(16).substr(2, 8)}`, - clean: false, // Ensures MQTT retains subscriptions - reconnectPeriod: 2000, // Reconnect every 2 seconds -}); +const activeClients = new Map(); // Store active clients per hw_Id -const subscribedTopics = new Set(); -const activeDevices = new Set(); // Keep track of active devices +// Start a persistent temp client to listen for multiple devices +const tempClientId = `mqtt_temp_${Math.random().toString(16).substr(2, 8)}`; +const tempClient = connectMQTT(tempClientId, true); -client.on('connect', () => { - console.log('āœ… Connected to MQTT broker'); +// Function to create an MQTT client +function connectMQTT(clientId, isTemp = false, hw_Id = null) { + console.log(`šŸ”„ Connecting to MQTT with clientId: ${clientId}`); - // **Ensure re-subscriptions after reconnect** - subscribedTopics.forEach(topic => { - client.subscribe(topic, { qos: 1 }, (err) => { - if (err) { - console.error(`āŒ Error resubscribing to ${topic}:`, err); - } else { - console.log(`šŸ”„ Resubscribed to ${topic}`); - } - }); + const newClient = mqtt.connect('mqtt://35.207.198.4:1883', { + clientId, + clean: false, // Ensures session persistence + reconnectPeriod: 2000, // Reconnect every 2 seconds + keepalive: 60, // Maintain connection }); - // **Subscribe to new device announcements** - client.subscribe('water/iot-data/announce', { qos: 1 }, (err) => { - if (err) { - console.error('āŒ Error subscribing to announcement topic:', err); - } else { - console.log('šŸ“” Subscribed to water/iot-data/announce'); + newClient.on('connect', () => { + console.log(`āœ… Connected to MQTT broker as ${clientId}`); + + if (isTemp) { + // Temporary client listens for multiple device announcements + newClient.subscribe('water/iot-data/announce', { qos: 1 }, (err) => { + if (err) { + console.error('āŒ Error subscribing to announcement topic:', err); + } else { + console.log('šŸ“” Subscribed to water/iot-data/announce'); + } + }); + } else if (hw_Id) { + // If this is the actual device client, subscribe to its topic + const deviceTopic = `water/iot-data/${hw_Id}`; + newClient.subscribe(deviceTopic, { qos: 1 }, (err) => { + if (err) { + console.error(`āŒ Error subscribing to ${deviceTopic}:`, err); + } else { + console.log(`āœ… Subscribed to ${deviceTopic}`); + logActiveClients(); // Log clients after each new device connects + } + }); } }); -}); -client.on('message', async (topic, message) => { - console.log(`šŸ“© Message received on topic ${topic}: ${message.toString()}`); + newClient.on('message', async (topic, message) => { + console.log(`šŸ“© Message received on topic ${topic}: ${message.toString()}`); - try { - const data = JSON.parse(message.toString()); + try { + const data = JSON.parse(message.toString()); - // **Handle device announcements** - if (topic === 'water/iot-data/announce') { - if (!data.objects || !data.objects.hw_Id) { - console.error("āŒ Invalid announcement format. Missing hw_Id."); - return; - } + if (topic === 'water/iot-data/announce' && isTemp) { + if (!data.objects || !data.objects.hw_Id) { + console.error("āŒ Invalid announcement format. Missing hw_Id."); + return; + } - const hw_Id = data.objects.hw_Id; - const deviceTopic = `water/iot-data/${hw_Id}`; + const hw_Id = data.objects.hw_Id; + console.log(`šŸ”„ New Device Found! hw_Id: ${hw_Id}`); - if (!subscribedTopics.has(deviceTopic)) { - client.subscribe(deviceTopic, { qos: 1 }, (err) => { - if (err) { - console.error(`āŒ Error subscribing to ${deviceTopic}:`, err); - } else { - console.log(`āœ… Subscribed to ${deviceTopic}`); - subscribedTopics.add(deviceTopic); - activeDevices.add(hw_Id); - console.log('šŸ“” Active Devices:', Array.from(activeDevices)); - - // āœ… **Now also process data** - processIotData(hw_Id, data); - } + // If this device hasn't been connected yet, create a client for it + if (!activeClients.has(hw_Id)) { + console.log(`šŸ”„ Creating new MQTT client for hw_Id: ${hw_Id}`); + activeClients.set(hw_Id, connectMQTT(`mqtt_client_${hw_Id}`, false, hw_Id)); + } + } + + if (topic.startsWith('water/iot-data/')) { + setImmediate(() => { + console.log(`šŸš€ Processing IoT Data for topic: ${topic}`); + const deviceHwId = topic.split('/')[2]; + processIotData(deviceHwId, data); }); - } else { - console.log(`šŸ”„ Already subscribed to ${deviceTopic}, processing data.`); - processIotData(hw_Id, data); } - return; + } catch (err) { + console.error('āŒ Error processing message:', err.message); } + }); - // **Process IoT Data for device topics** - if (topic.startsWith('water/iot-data/')) { - setImmediate(() => { - console.log(`šŸš€ Entering processIotData() for topic: ${topic}`); - const hw_Id = topic.split('/')[2]; - processIotData(hw_Id, data); - }); - } - } catch (err) { - console.error('āŒ Error processing message:', err.message); - } -}); + newClient.on('error', (err) => console.error('āŒ MQTT Error:', err)); + newClient.on('close', () => console.log(`āš ļø MQTT Connection Closed for ${clientId}`)); + newClient.on('offline', () => console.log(`āš ļø MQTT Broker Offline for ${clientId}`)); + + return newClient; +} + +// Function to log active MQTT clients +function logActiveClients() { + console.log(`\nšŸ“” Active MQTT Clients:`); + activeClients.forEach((client, hw_Id) => { + console.log(` šŸ”¹ Client ID: mqtt_client_${hw_Id}`); + }); +} -client.on('error', (err) => console.error('āŒ MQTT Error:', err)); -client.on('close', () => console.log('āš ļø MQTT Connection Closed.')); -client.on('offline', () => console.log('āš ļø MQTT Broker Offline.')); async function processIotData(hw_Id, data) { try { @@ -6098,14 +6103,14 @@ async function processIotData(hw_Id, data) { } -function logSets() { - console.log("Subscribed Topics:", Array.from(subscribedTopics)); - console.log("Active Devices:", Array.from(activeDevices)); - console.log("motorIntervals:", motorIntervals); -} +// function logSets() { +// console.log("Subscribed Topics:", Array.from(subscribedTopics)); +// console.log("Active Devices:", Array.from(activeDevices)); +// console.log("motorIntervals:", motorIntervals); +// } -// Call logSets every 30 seconds -setInterval(logSets, 30000); +// // Call logSets every 30 seconds +// setInterval(logSets, 30000);