|
|
|
@ -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);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|