From 40ea912908be5f29c935764b719cc0f0f1d6d5b8 Mon Sep 17 00:00:00 2001 From: Varun Date: Tue, 18 Mar 2025 12:58:46 +0530 Subject: [PATCH] changes --- src/controllers/tanksController.js | 64 ++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 12 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index f29c5c54..94d80d0d 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -2864,10 +2864,19 @@ exports.motorAction = async (req, reply) => { if (action === "start") { - if (motorIntervals[motorId]) { - clearInterval(motorIntervals[motorId]); - delete motorIntervals[motorId]; - } + if (motorIntervals[motorId]) { + console.log(`🔄 Clearing all existing intervals for motorId: ${motorId}`); + + // Clear and delete all intervals for the motorId + Object.keys(motorIntervals).forEach(key => { + if (key.startsWith(motorId)) { + clearInterval(motorIntervals[key]); + delete motorIntervals[key]; + } + }); + + console.log(`✅ All intervals cleared for motorId: ${motorId}`); + } const startTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm'); @@ -5822,13 +5831,14 @@ client.on('message', async (topic, message) => { console.log('📡 Active Devices:', Array.from(activeDevices)); setTimeout(() => { - if (subscribedTopics.has(deviceTopic)) { - console.log(`🔄 Unsubscribing from inactive device: ${deviceTopic}`); - client.unsubscribe(deviceTopic); - subscribedTopics.delete(deviceTopic); - activeDevices.delete(hw_Id); + 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); + }, DEVICE_TIMEOUT); + } }); } @@ -5838,10 +5848,28 @@ client.on('message', async (topic, message) => { if (topic.startsWith('water/iot-data/')) { setImmediate(() => { console.log(`🚀 Processing IoT Data for topic: ${topic}`); - const hw_Id = topic.split('/')[2]; + + // 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); } @@ -5912,11 +5940,12 @@ async function processIotData(hw_Id, data) { // **Update 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 (inputConnection.motor_stop_status === "1" && Motor_status === 2 && inputConnection.motor_on_type !== "forced_manual") { @@ -5946,6 +5975,17 @@ 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); +} + +// Call logSets every 30 seconds +setInterval(logSets, 30000); + + +