master^2
Varun 7 months ago
parent 0b107a6785
commit 40ea912908

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

Loading…
Cancel
Save