From 85e053747959e7188320e9f4f63370f9befa0491 Mon Sep 17 00:00:00 2001 From: Varun Date: Tue, 18 Mar 2025 14:13:47 +0530 Subject: [PATCH] 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));