diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 78a7e982..041f792b 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -3097,7 +3097,7 @@ exports.motorAction = async (req, reply) => { } ); - + clearInterval(motorIntervals[motorId]); // Stop the motor if condition met @@ -5896,113 +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, - reconnectPeriod: 2000, -}); +const activeClients = new Map(); // Store active clients per hw_Id -const subscribedTopics = new Set(); -const activeDevices = new Set(); -const DEVICE_TIMEOUT = 10 * 60 * 1000; // 10 minutes +// 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}`); - 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 }); - 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}`); -client.on('offline', () => { - console.log('āš ļø MQTT Broker Offline. Attempting to reconnect...'); - if (!client.connected) client.reconnect(); -}); + 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('close', () => { - console.log('āš ļø MQTT Connection Closed. Attempting to reconnect...'); - if (!client.connected) client.reconnect(); -}); + newClient.on('message', async (topic, message) => { + console.log(`šŸ“© Message received on topic ${topic}: ${message.toString()}`); -client.on('error', (err) => console.error('āŒ MQTT Error:', err)); + try { + const data = JSON.parse(message.toString()); -client.on('message', async (topic, message) => { - console.log(`šŸ“© Message received on topic ${topic}: ${message.toString()}`); + if (topic === 'water/iot-data/announce' && isTemp) { + if (!data.objects || !data.objects.hw_Id) { + console.error("āŒ Invalid announcement format. Missing hw_Id."); + return; + } - try { - const data = JSON.parse(message.toString()); + const hw_Id = data.objects.hw_Id; + console.log(`šŸ”„ New Device Found! hw_Id: ${hw_Id}`); - if (topic === 'water/iot-data/announce') { - if (!data.objects || !data.objects.hw_Id) { - console.error("āŒ Invalid announcement format. Missing hw_Id."); - return; + // 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)); + } } - const hw_Id = data.objects.hw_Id; - const deviceTopic = `water/iot-data/${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)); - - 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); - - } + if (topic.startsWith('water/iot-data/')) { + setImmediate(() => { + console.log(`šŸš€ Processing IoT Data for topic: ${topic}`); + const deviceHwId = topic.split('/')[2]; + processIotData(deviceHwId, data); }); } - return; + } 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}`); + }); +} - 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)); - }); - } - - } catch (err) { - console.error('āŒ Error processing message:', err.message); - } -}); async function processIotData(hw_Id, data) { try { @@ -6015,7 +6000,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 => ({ @@ -6025,19 +6010,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(); - // **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) - ]); + // 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); - const tankOperations = tanks.map(async (tank) => { + for (const record of recordsToDelete) { + await record.remove(); + } + + // 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; @@ -6048,70 +6045,72 @@ 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)); - console.log("motorIntervals:", motorIntervals); -} -// Call logSets every 30 seconds -setInterval(logSets, 30000); +// 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);