master^2
Varun 4 months ago
parent 9be08103c0
commit dc0a602199

@ -6426,6 +6426,7 @@ setInterval(logSets, 30000);
const sendMotorNotifications = async () => { const sendMotorNotifications = async () => {
// console.log("🔄 Checking for motor notifications..."); // 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);

@ -946,7 +946,8 @@ exports.getSuppliers = async (req, reply) => {
type_of_water, type_of_water,
capacity: requestedCapacityStr, capacity: requestedCapacityStr,
quantity: requestedQuantityStr, quantity: requestedQuantityStr,
date date,
time
} = req.body; } = req.body;
const parseCapacity = (value) => parseFloat((value || "0").toString().replace(/,/g, "")); const parseCapacity = (value) => parseFloat((value || "0").toString().replace(/,/g, ""));

@ -30,6 +30,7 @@ module.exports = function (fastify, opts, next) {
capacity: { type: "string" }, capacity: { type: "string" },
quantity: { type: "string" }, quantity: { type: "string" },
date: { type: "string" }, date: { type: "string" },
time: { type: "string" },
}, },
}, },

Loading…
Cancel
Save