# Session Summary: MQTT Auto-Recording Debugging & Theme Implementation ## Overview This session focused on debugging MQTT-triggered auto-recording functionality and implementing dark/light theme toggle in the management dashboard. Multiple issues were identified and resolved related to camera recording, MQTT message handling, and UI theming. --- ## Major Tasks Completed ### 1. Scheduling Module Extraction (Previous Session) - Extracted the Scheduling module into a separate microfrontend (`scheduling-remote`) - Configured Module Federation for the new microfrontend - Updated `docker-compose.yml` to include the new service ### 2. Dark/Light Theme Toggle Implementation - Created `useTheme` hook (`management-dashboard-web-app/src/hooks/useTheme.ts`) - Added theme toggle button to `TopNavbar.tsx` - Applied dark mode classes across multiple components: - `DashboardLayout.tsx` - Main content area - `Login.tsx` - Login page background - `DataEntry.tsx` - Widgets, text, borders - `UserManagement.tsx` - Stats cards, tables, forms - `ExperimentPhases.tsx` - Phase cards, badges - `index.css` - Body background - `index.html` - Inline script to prevent FOUC (Flash of Unstyled Content) ### 3. MQTT Auto-Recording Debugging #### Issues Identified: 1. **Manual recording buttons not working** - "Record" buttons for cameras couldn't start manual recording 2. **Automatic recording unavailable** - MQTT-triggered auto-recording wasn't functioning 3. **Camera2 initialization error** - Error code 32774 when initializing camera2 4. **Button visibility issues** - "Restart Camera" and "Stop Streaming" buttons appeared transparent #### Fixes Applied: ##### A. Transparent Buttons Fix **File:** `vision-system-remote/src/components/CameraCard.tsx` - Added inline `style` attributes to "Restart Camera" and "Stop Streaming" buttons - Explicitly set `backgroundColor` and `color` to prevent transparency ##### B. Camera Initialization Error (32774) **File:** `camera-management-api/usda_vision_system/camera/monitor.py` - Enhanced error handling for camera initialization error code 32774 - Added logic to check if camera is already in use by existing recorder/streamer - If error 32774 occurs, camera is marked as "available" with warning instead of failing completely - Provides better diagnostics for cameras that might be in use by another process ##### C. StandaloneAutoRecorder Integration **File:** `camera-management-api/usda_vision_system/recording/standalone_auto_recorder.py` - **Problem:** `StandaloneAutoRecorder` was creating its own MQTT client and camera recorders, causing conflicts - **Solution:** - Modified constructor to accept `camera_manager`, `state_manager`, and `event_system` instances - Removed internal MQTT client setup when using shared instances - Now subscribes to `EventType.MACHINE_STATE_CHANGED` events from the event system - Uses `camera_manager.manual_start_recording()` and `manual_stop_recording()` instead of creating its own recorders - Added comprehensive logging for event reception and recording operations **File:** `camera-management-api/usda_vision_system/main.py` - Updated `USDAVisionSystem` constructor to pass `camera_manager`, `state_manager`, and `event_system` to `StandaloneAutoRecorder` ##### D. Missing Constants Import **File:** `camera-management-api/usda_vision_system/camera/recorder.py` - **Error:** `NameError: name 'CAMERA_TEST_CAPTURE_TIMEOUT' is not defined` - **Fix:** Added missing imports: - `CAMERA_GET_BUFFER_TIMEOUT` - `CAMERA_INIT_TIMEOUT` - `CAMERA_TEST_CAPTURE_TIMEOUT` - `DEFAULT_VIDEO_FPS` - `BRIEF_PAUSE_SLEEP` - All imported from `.constants` ##### E. Recording Routes Enhancement **File:** `camera-management-api/usda_vision_system/api/routes/recording_routes.py` - Made `StartRecordingRequest` parameter optional for `start_recording` endpoint - Added logic to create default `StartRecordingRequest` if `request` is `None` - Added INFO-level logging for recording attempts and success/failure - Improved error messages for `manual_start_recording` failures ##### F. MQTT Logging Improvements **Files:** - `camera-management-api/usda_vision_system/mqtt/client.py` - `camera-management-api/usda_vision_system/mqtt/handlers.py` **Changes:** - Changed MQTT message logging from `DEBUG` to `INFO` level for visibility in production - Added detailed logging for: - Message reception: `📡 MQTT MESSAGE RECEIVED - Topic: {topic}, Payload: '{payload}'` - Message processing: `📡 Processing MQTT message for machine '{machine_name}': '{payload}'` - Payload normalization: `📡 Normalized payload '{payload}' -> '{normalized_payload}'` - Event publishing: `📡 Publishing MACHINE_STATE_CHANGED event` - Connection status: `🔗 MQTT CONNECTED` with broker details - Subscription confirmation: `📋 MQTT subscribed to {count} topics` ##### G. MQTT Test Script **File:** `camera-management-api/test_mqtt_simple.py` (NEW) - Created standalone MQTT test script to verify connectivity and message reception - Connects to MQTT broker and subscribes to configured topics - Displays received messages in real-time with timestamps - Validates payload format (on/off/true/false/1/0) - Shows connection statistics ### 4. Configuration Updates **File:** `camera-management-api/config.json` - Confirmed `log_level` is set to `INFO` - `auto_recording_enabled` is set to `true` --- ## Key Files Modified ### Frontend (React/TypeScript) 1. `management-dashboard-web-app/src/hooks/useTheme.ts` - NEW 2. `management-dashboard-web-app/src/components/TopNavbar.tsx` 3. `management-dashboard-web-app/src/components/DashboardLayout.tsx` 4. `management-dashboard-web-app/src/components/Login.tsx` 5. `management-dashboard-web-app/src/components/DataEntry.tsx` 6. `management-dashboard-web-app/src/components/UserManagement.tsx` 7. `management-dashboard-web-app/src/components/ExperimentPhases.tsx` 8. `management-dashboard-web-app/src/index.css` 9. `management-dashboard-web-app/index.html` 10. `vision-system-remote/src/components/CameraCard.tsx` ### Backend (Python/FastAPI) 1. `camera-management-api/usda_vision_system/recording/standalone_auto_recorder.py` 2. `camera-management-api/usda_vision_system/main.py` 3. `camera-management-api/usda_vision_system/camera/recorder.py` 4. `camera-management-api/usda_vision_system/camera/monitor.py` 5. `camera-management-api/usda_vision_system/api/routes/recording_routes.py` 6. `camera-management-api/usda_vision_system/mqtt/client.py` 7. `camera-management-api/usda_vision_system/mqtt/handlers.py` 8. `camera-management-api/test_mqtt_simple.py` - NEW --- ## Current Status ### ✅ Completed - Dark/light theme toggle fully implemented - Transparent buttons fixed - Camera initialization error handling improved - `StandaloneAutoRecorder` properly integrated with shared instances - Missing constants imported - Recording routes enhanced with better logging - MQTT logging improved to INFO level - MQTT test script created ### 🔄 Testing Needed - **MQTT Auto-Recording**: Needs verification that MQTT messages trigger recording - Test script available: `camera-management-api/test_mqtt_simple.py` - Monitor logs for MQTT message flow: `docker compose logs api -f | grep -i "mqtt\|📡"` - Check MQTT status: `curl http://localhost:8000/mqtt/status` - Check recent events: `curl http://localhost:8000/mqtt/events?limit=10` ### 📋 Expected MQTT Message Flow When a machine turns on/off, the following should appear in logs: 1. `📡 MQTT MESSAGE RECEIVED` - Message received from broker 2. `📡 Processing MQTT message` - Message being processed 3. `📡 Normalized payload` - Payload normalization 4. `✅ Published MACHINE_STATE_CHANGED event` - Event published to event system 5. `📡 AUTO-RECORDER: Received MACHINE_STATE_CHANGED event` - Auto-recorder received event 6. `✅ Started auto-recording` - Recording started --- ## MQTT Configuration From `config.json`: ```json { "mqtt": { "broker_host": "192.168.1.110", "broker_port": 1883, "username": null, "password": null, "topics": { "vibratory_conveyor": "vision/vibratory_conveyor/state", "blower_separator": "vision/blower_separator/state" } } } ``` Machine-to-Camera Mapping (from `standalone_auto_recorder.py`): - `vibratory_conveyor` → `camera2` - `blower_separator` → `camera1` --- ## Testing Commands ### Test MQTT Connectivity ```bash # Option 1: Standalone test script docker compose exec api python test_mqtt_simple.py # Option 2: Check MQTT status via API curl -s http://localhost:8000/mqtt/status | python -m json.tool # Option 3: Monitor API logs for MQTT messages docker compose logs api -f | grep -i "mqtt\|📡" # Option 4: Check recent MQTT events curl -s http://localhost:8000/mqtt/events?limit=10 | python -m json.tool ``` ### Restart API Container ```bash docker compose restart api ``` --- ## Architecture Notes ### Event Flow for Auto-Recording 1. **MQTT Message Received** → `MQTTClient._on_message()` 2. **Message Processed** → `MQTTMessageHandler.handle_message()` 3. **State Updated** → `StateManager.update_machine_state()` 4. **Event Published** → `EventSystem.publish(EventType.MACHINE_STATE_CHANGED)` 5. **Event Received** → `StandaloneAutoRecorder._on_machine_state_changed()` 6. **Recording Started/Stopped** → `CameraManager.manual_start_recording()` / `manual_stop_recording()` ### Key Components - **MQTTClient**: Connects to MQTT broker, subscribes to topics, receives messages - **MQTTMessageHandler**: Processes MQTT messages, normalizes payloads, updates state - **EventSystem**: Publishes/subscribes to internal events - **StandaloneAutoRecorder**: Subscribes to `MACHINE_STATE_CHANGED` events, triggers recording - **CameraManager**: Manages camera operations including recording --- ## Notes for Future Sessions 1. **MQTT Testing**: If auto-recording still doesn't work, verify: - MQTT broker is reachable from API container - Topics match exactly (case-sensitive) - Messages are being published to correct topics - Payload format is recognized (on/off/true/false/1/0) 2. **Camera Initialization**: Error 32774 typically means camera is in use. The system now handles this gracefully but may need investigation if cameras are not accessible. 3. **Theme Persistence**: Theme preference is stored in `localStorage` and persists across sessions. 4. **Log Level**: Currently set to `INFO` in `config.json` for comprehensive event tracking. --- ## Session Date Session completed with focus on MQTT debugging and enhanced logging for troubleshooting auto-recording functionality.