From 3e2511f0192f336b8ec03ec42b69cad982948b56 Mon Sep 17 00:00:00 2001 From: Josh Finlay Date: Wed, 8 Jan 2025 10:30:07 +1000 Subject: [PATCH] Enhanced MQTT logging and fixed event loading in frontend - Added detailed MQTT message logging - Added validation for MQTT commands - Fixed frontend event loading to preserve loaded events - Added proper handling of new events while viewing history --- backend/main.py | 11 +++++++++++ backend/mqtt_integration.py | 11 ++++++++--- frontend/src/App.tsx | 18 ++++++++++++++++-- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/backend/main.py b/backend/main.py index 63a8e46..3c4cb6f 100644 --- a/backend/main.py +++ b/backend/main.py @@ -733,6 +733,17 @@ async def init_mqtt(settings: Settings): if settings.mqtt.enabled: ha_mqtt.update_settings(settings.mqtt) ha_mqtt.enable(True) + + # Set up command callback to handle open/close commands + async def command_handler(command: str): + logger.info(f"Processing MQTT command: {command}") + if command in ["OPEN", "CLOSE"]: + should_open = command == "OPEN" + await handle_mqtt_command(should_open) + else: + logger.warning(f"Received unknown MQTT command: {command}") + + ha_mqtt.set_command_callback(command_handler) logger.info("MQTT integration enabled") else: ha_mqtt.enable(False) diff --git a/backend/mqtt_integration.py b/backend/mqtt_integration.py index 8635170..2c527e6 100644 --- a/backend/mqtt_integration.py +++ b/backend/mqtt_integration.py @@ -91,13 +91,18 @@ class HomeAssistantMQTT: def on_message(self, client, topic, payload, qos, properties): """Callback when message received""" try: + decoded_payload = payload.decode() + logger.info(f"MQTT message received - Topic: {topic}, Payload: {decoded_payload}, QoS: {qos}") + # Handle command messages if topic == self.command_topic and self.command_callback: - command = payload.decode().upper() - logger.debug(f"Received command: {command}") + command = decoded_payload.upper() + logger.debug(f"Processing command: {command}") self.command_callback(command) + else: + logger.debug(f"Message received on non-command topic: {topic}") except Exception as e: - logger.error(f"Error handling message: {e}") + logger.error(f"Error handling message: {e}", exc_info=True) def on_subscribe(self, client, mid, qos, properties): """Callback when subscription confirmed""" diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 0f42465..8d9030e 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -66,11 +66,25 @@ function App() { api.getEvents(10, 0) ]); setGateStatus(statusData); - // Only update events if we're on the first page - if (events.length <= 10) { + + // Update only if we're on the first page and don't have more events loaded + if (events.length <= 10 && !hasMoreEvents) { setEvents(eventsData.events); setHasMoreEvents(eventsData.hasMore); setTotalEvents(eventsData.total); + } else { + // If we have more events loaded, just prepend any new events + const newEvents = eventsData.events.filter( + newEvent => !events.some( + existingEvent => + existingEvent.timestamp === newEvent.timestamp && + existingEvent.action === newEvent.action + ) + ); + if (newEvents.length > 0) { + setEvents(prev => [...newEvents, ...prev]); + setTotalEvents(prev => prev + newEvents.length); + } } } catch (err) { console.error('Failed to update status:', err);