diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 0b58392c..90b580be 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -6426,6 +6426,7 @@ setInterval(logSets, 30000); + const sendMotorNotifications = async () => { // console.log("🔄 Checking for motor notifications..."); @@ -6533,6 +6534,78 @@ exports.getPendingAndCompletedsurveyOfparticularInstaller = async (request, repl +//const mqtt = require('mqtt'); + +// Connect to test MQTT broker +const client2 = mqtt.connect('mqtt://34.100.133.20:1884', { + clientId: `mqtt_test_${Math.random().toString(16).substr(2, 8)}`, + clean: true, + reconnectPeriod: 2000, +}); +// Sets to track active devices +const subscribedTopics2 = new Set(); +const activeDevices2 = new Set(); // Keep track of active devices + +client2.on('connect', () => { + console.log('✅ Connected to TEST MQTT broker'); + + // Subscribe to the announcement topic + client2.subscribe('water/iot-data/announce', { qos: 1 }, (err) => { + if (err) { + console.error('❌ Failed to subscribe to announce topic:', err); + } else { + console.log('📡 Subscribed to water/iot-data/announce'); + } + }); +}); + +client2.on('message', (topic, message) => { + console.log(`📩 [${topic}] ${message.toString()}`); + + try { + const data = JSON.parse(message.toString()); + + // If announcement message received + if (topic === 'water/iot-data/announce') { + if (data.objects && data.objects.hw_Id) { + const hw_Id = data.objects.hw_Id; + const deviceTopic = `water/iot-data/${hw_Id}`; + + if (!subscribedTopics2.has(deviceTopic)) { + client2.subscribe(deviceTopic, { qos: 1 }, (err) => { + if (err) { + console.error(`❌ Failed to subscribe to ${deviceTopic}:`, err); + } else { + console.log(`✅ Subscribed to ${deviceTopic}`); + subscribedTopics2.add(deviceTopic); + } + }); + } else { + console.log(`🔄 Already subscribed to ${deviceTopic}`); + } + } else { + console.error('❌ Invalid announce message, missing hw_Id'); + } + } + } catch (err) { + console.error('❌ Failed to parse message:', err); + } +}); + +client2.on('error', (err) => console.error('❌ MQTT Error:', err)); +client2.on('close', () => console.log('⚠️ MQTT2 Connection Closed.')); +client2.on('offline', () => console.log('⚠️ MQTT Broker Offline.')); + + + function logSets1() { + console.log("Subscribed Topics2:", Array.from(subscribedTopics2)); + console.log("Active Devices2:", Array.from(activeDevices2)); +} + +// Call logSets every 30 seconds +setInterval(logSets1, 30000); + + diff --git a/src/handlers/supplierHandler.js b/src/handlers/supplierHandler.js index 9acc4dce..bf4e5765 100644 --- a/src/handlers/supplierHandler.js +++ b/src/handlers/supplierHandler.js @@ -946,7 +946,8 @@ exports.getSuppliers = async (req, reply) => { type_of_water, capacity: requestedCapacityStr, quantity: requestedQuantityStr, - date + date, + time } = req.body; const parseCapacity = (value) => parseFloat((value || "0").toString().replace(/,/g, "")); diff --git a/src/routes/supplierRoute.js b/src/routes/supplierRoute.js index 540e0769..0eb38114 100644 --- a/src/routes/supplierRoute.js +++ b/src/routes/supplierRoute.js @@ -30,6 +30,8 @@ module.exports = function (fastify, opts, next) { capacity: { type: "string" }, quantity: { type: "string" }, date: { type: "string" }, + time: { type: "string" }, + }, },