From 0d20fe189d8c2cbbaa5c8b45a29321c51c042d49 Mon Sep 17 00:00:00 2001 From: Alireza Vaezi Date: Tue, 29 Jul 2025 12:30:59 -0400 Subject: [PATCH] feat: Add CameraPreviewModal component for live camera streaming feat: Implement useAuth hook for user authentication management feat: Create useAutoRecording hook for managing automatic recording functionality feat: Develop AutoRecordingManager to handle automatic recording based on MQTT events test: Add test script to verify camera configuration API fix test: Create HTML page for testing camera configuration API and auto-recording fields --- API Documentations/AI_AGENT_INSTRUCTIONS.md | 175 ++++ .../AI_INTEGRATION_GUIDE.md | 41 +- .../AUTO_RECORDING_FEATURE_GUIDE.md | 260 ++++++ API Documentations/CAMERA_CONFIG_API.md | 455 +++++++++ API Documentations/README.md | 870 ++++++++++++++++++ .../STREAMING_GUIDE.md | 0 .../camera-api.types.ts | 12 +- .../camera_preview.html | 2 +- .../docs/API_CHANGES_SUMMARY.md | 175 ++++ API Documentations/docs/API_DOCUMENTATION.md | 627 +++++++++++++ .../docs/API_QUICK_REFERENCE.md | 195 ++++ API Documentations/docs/PROJECT_COMPLETE.md | 212 +++++ API Documentations/docs/README.md | 65 ++ .../docs/api/CAMERA_CONFIG_API.md | 425 +++++++++ .../features/AUTO_RECORDING_FEATURE_GUIDE.md | 262 ++++++ .../docs/guides/CAMERA_RECOVERY_GUIDE.md | 158 ++++ .../docs/guides/MQTT_LOGGING_GUIDE.md | 187 ++++ .../docs/guides/STREAMING_GUIDE.md | 240 +++++ API Documentations/docs/legacy/01README.md | 146 +++ .../docs/legacy/IMPLEMENTATION_SUMMARY.md | 184 ++++ API Documentations/docs/legacy/README.md | 1 + .../docs/legacy/README_SYSTEM.md | 249 +++++ .../docs/legacy/TIMEZONE_SETUP_SUMMARY.md | 190 ++++ .../docs/legacy/VIDEO_RECORDER_README.md | 191 ++++ .../streaming-api.http | 230 ++++- API Documentations/test_frame_conversion.py | 80 ++ .../test_streaming.py | 0 api-endpoints.http | 3 +- docs/AUTO_RECORDING_SETUP.md | 162 ++++ src/components/AutoRecordingStatus.tsx | 162 ++++ src/components/AutoRecordingTest.tsx | 193 ++++ src/components/CameraConfigModal.tsx | 587 ++++++++++++ src/components/CameraPreviewModal.tsx | 194 ++++ src/components/VisionSystem.tsx | 372 +++++--- src/hooks/useAuth.ts | 48 + src/hooks/useAutoRecording.ts | 81 ++ src/lib/autoRecordingManager.ts | 286 ++++++ src/lib/visionApi.ts | 188 ++++ test-api-fix.js | 132 +++ test-camera-config.html | 229 +++++ 40 files changed, 8142 insertions(+), 127 deletions(-) create mode 100644 API Documentations/AI_AGENT_INSTRUCTIONS.md rename {streaming => API Documentations}/AI_INTEGRATION_GUIDE.md (92%) create mode 100644 API Documentations/AUTO_RECORDING_FEATURE_GUIDE.md create mode 100644 API Documentations/CAMERA_CONFIG_API.md create mode 100644 API Documentations/README.md rename {streaming => API Documentations}/STREAMING_GUIDE.md (100%) rename {streaming => API Documentations}/camera-api.types.ts (98%) rename {streaming => API Documentations}/camera_preview.html (99%) create mode 100644 API Documentations/docs/API_CHANGES_SUMMARY.md create mode 100644 API Documentations/docs/API_DOCUMENTATION.md create mode 100644 API Documentations/docs/API_QUICK_REFERENCE.md create mode 100644 API Documentations/docs/PROJECT_COMPLETE.md create mode 100644 API Documentations/docs/README.md create mode 100644 API Documentations/docs/api/CAMERA_CONFIG_API.md create mode 100644 API Documentations/docs/features/AUTO_RECORDING_FEATURE_GUIDE.md create mode 100644 API Documentations/docs/guides/CAMERA_RECOVERY_GUIDE.md create mode 100644 API Documentations/docs/guides/MQTT_LOGGING_GUIDE.md create mode 100644 API Documentations/docs/guides/STREAMING_GUIDE.md create mode 100644 API Documentations/docs/legacy/01README.md create mode 100644 API Documentations/docs/legacy/IMPLEMENTATION_SUMMARY.md create mode 100644 API Documentations/docs/legacy/README.md create mode 100644 API Documentations/docs/legacy/README_SYSTEM.md create mode 100644 API Documentations/docs/legacy/TIMEZONE_SETUP_SUMMARY.md create mode 100644 API Documentations/docs/legacy/VIDEO_RECORDER_README.md rename {streaming => API Documentations}/streaming-api.http (63%) create mode 100644 API Documentations/test_frame_conversion.py rename {streaming => API Documentations}/test_streaming.py (100%) create mode 100644 docs/AUTO_RECORDING_SETUP.md create mode 100644 src/components/AutoRecordingStatus.tsx create mode 100644 src/components/AutoRecordingTest.tsx create mode 100644 src/components/CameraConfigModal.tsx create mode 100644 src/components/CameraPreviewModal.tsx create mode 100644 src/hooks/useAuth.ts create mode 100644 src/hooks/useAutoRecording.ts create mode 100644 src/lib/autoRecordingManager.ts create mode 100644 test-api-fix.js create mode 100644 test-camera-config.html diff --git a/API Documentations/AI_AGENT_INSTRUCTIONS.md b/API Documentations/AI_AGENT_INSTRUCTIONS.md new file mode 100644 index 0000000..dedd89e --- /dev/null +++ b/API Documentations/AI_AGENT_INSTRUCTIONS.md @@ -0,0 +1,175 @@ +# Instructions for AI Agent: Auto-Recording Feature Integration + +## ๐ŸŽฏ Task Overview +Update the React application to support the new auto-recording feature that has been added to the USDA Vision Camera System backend. + +## ๐Ÿ“‹ What You Need to Know + +### System Context +- **Camera 1** monitors the **vibratory conveyor** (conveyor/cracker cam) +- **Camera 2** monitors the **blower separator** machine +- Auto-recording automatically starts when machines turn ON and stops when they turn OFF +- The system includes retry logic for failed recording attempts +- Manual recording always takes precedence over auto-recording + +### New Backend Capabilities +The backend now supports: +1. **Automatic recording** triggered by MQTT machine state changes +2. **Retry mechanism** for failed recording attempts (configurable retries and delays) +3. **Status tracking** for auto-recording state, failures, and attempts +4. **API endpoints** for enabling/disabling and monitoring auto-recording + +## ๐Ÿ”ง Required React App Changes + +### 1. Update TypeScript Interfaces + +Add these new fields to existing `CameraStatusResponse`: +```typescript +interface CameraStatusResponse { + // ... existing fields + auto_recording_enabled: boolean; + auto_recording_active: boolean; + auto_recording_failure_count: number; + auto_recording_last_attempt?: string; + auto_recording_last_error?: string; +} +``` + +Add new response types: +```typescript +interface AutoRecordingConfigResponse { + success: boolean; + message: string; + camera_name: string; + enabled: boolean; +} + +interface AutoRecordingStatusResponse { + running: boolean; + auto_recording_enabled: boolean; + retry_queue: Record; + enabled_cameras: string[]; +} +``` + +### 2. Add New API Endpoints + +```typescript +// Enable auto-recording for a camera +POST /cameras/{camera_name}/auto-recording/enable + +// Disable auto-recording for a camera +POST /cameras/{camera_name}/auto-recording/disable + +// Get overall auto-recording system status +GET /auto-recording/status +``` + +### 3. UI Components to Add/Update + +#### Camera Status Display +- Add auto-recording status badge/indicator +- Show auto-recording enabled/disabled state +- Display failure count if > 0 +- Show last error message if any +- Distinguish between manual and auto-recording states + +#### Auto-Recording Controls +- Toggle switch to enable/disable auto-recording per camera +- System-wide auto-recording status display +- Retry queue information +- Machine state correlation display + +#### Error Handling +- Clear display of auto-recording failures +- Retry attempt information +- Last attempt timestamp +- Quick retry/reset actions + +### 4. Visual Design Guidelines + +**Status Priority (highest to lowest):** +1. Manual Recording (red/prominent) - user initiated +2. Auto-Recording Active (green) - machine ON, recording +3. Auto-Recording Enabled (blue) - ready but machine OFF +4. Auto-Recording Disabled (gray) - feature disabled + +**Machine Correlation:** +- Show machine name next to camera (e.g., "Vibratory Conveyor", "Blower Separator") +- Display machine ON/OFF status +- Alert if machine is ON but auto-recording failed + +## ๐ŸŽจ Specific Implementation Tasks + +### Task 1: Update Camera Cards +- Add auto-recording status indicators +- Add enable/disable toggle controls +- Show machine state correlation +- Display failure information when relevant + +### Task 2: Create Auto-Recording Dashboard +- Overall system status +- List of enabled cameras +- Active retry queue display +- Recent events/errors + +### Task 3: Update Recording Status Logic +- Distinguish between manual and auto-recording +- Show appropriate controls based on recording type +- Handle manual override scenarios + +### Task 4: Add Error Handling +- Display auto-recording failures clearly +- Show retry attempts and timing +- Provide manual retry options + +## ๐Ÿ“ฑ User Experience Requirements + +### Key Behaviors +1. **Non-Intrusive:** Auto-recording status shouldn't clutter the main interface +2. **Clear Hierarchy:** Manual controls should be more prominent than auto-recording +3. **Informative:** Users should understand why recording started/stopped +4. **Actionable:** Clear options to enable/disable or retry failed attempts + +### Mobile Considerations +- Auto-recording controls should work well on mobile +- Status information should be readable on small screens +- Consider collapsible sections for detailed information + +## ๐Ÿ” Testing Requirements + +Ensure the React app correctly handles: +- [ ] Toggling auto-recording on/off per camera +- [ ] Displaying real-time status updates +- [ ] Showing error states and retry information +- [ ] Manual recording override scenarios +- [ ] Machine state changes and correlation +- [ ] Mobile interface functionality + +## ๐Ÿ“š Reference Files + +Key files to review for implementation details: +- `AUTO_RECORDING_FEATURE_GUIDE.md` - Comprehensive technical details +- `api-endpoints.http` - API endpoint documentation +- `config.json` - Configuration structure +- `usda_vision_system/api/models.py` - Response type definitions + +## ๐ŸŽฏ Success Criteria + +The React app should: +1. **Display** auto-recording status for each camera clearly +2. **Allow** users to enable/disable auto-recording per camera +3. **Show** machine state correlation and recording triggers +4. **Handle** error states and retry scenarios gracefully +5. **Maintain** existing manual recording functionality +6. **Provide** clear visual hierarchy between manual and auto-recording + +## ๐Ÿ’ก Implementation Tips + +1. **Start Small:** Begin with basic status display, then add controls +2. **Use Existing Patterns:** Follow the current app's design patterns +3. **Test Incrementally:** Test each feature as you add it +4. **Consider State Management:** Update your state management to handle new data +5. **Mobile First:** Ensure mobile usability from the start + +The goal is to seamlessly integrate auto-recording capabilities while maintaining the existing user experience and adding valuable automation features for the camera operators. diff --git a/streaming/AI_INTEGRATION_GUIDE.md b/API Documentations/AI_INTEGRATION_GUIDE.md similarity index 92% rename from streaming/AI_INTEGRATION_GUIDE.md rename to API Documentations/AI_INTEGRATION_GUIDE.md index b5b49cb..9d881ee 100644 --- a/streaming/AI_INTEGRATION_GUIDE.md +++ b/API Documentations/AI_INTEGRATION_GUIDE.md @@ -7,12 +7,18 @@ This guide is specifically designed for AI assistants to understand and implemen The USDA Vision Camera system provides live video streaming through REST API endpoints. The streaming uses MJPEG format which is natively supported by HTML `` tags and can be easily integrated into React components. ### Key Characteristics: -- **Base URL**: `http://localhost:8000` (configurable) +- **Base URL**: `http://vision:8000` (production) or `http://localhost:8000` (development) - **Stream Format**: MJPEG (Motion JPEG) - **Content-Type**: `multipart/x-mixed-replace; boundary=frame` - **Authentication**: None (add if needed for production) - **CORS**: Enabled for all origins (configure for production) +### Base URL Configuration: +- **Production**: `http://vision:8000` (requires hostname setup) +- **Development**: `http://localhost:8000` (local testing) +- **Custom IP**: `http://192.168.1.100:8000` (replace with actual IP) +- **Custom hostname**: Configure DNS or /etc/hosts as needed + ## ๐Ÿ”Œ API Endpoints Reference ### 1. Get Camera List @@ -71,7 +77,7 @@ GET /cameras/{camera_name}/stream ```jsx import React, { useState, useEffect } from 'react'; -const CameraStream = ({ cameraName, apiBaseUrl = 'http://localhost:8000' }) => { +const CameraStream = ({ cameraName, apiBaseUrl = 'http://vision:8000' }) => { const [isStreaming, setIsStreaming] = useState(false); const [error, setError] = useState(null); const [loading, setLoading] = useState(false); @@ -221,7 +227,7 @@ export default CameraStream; import React, { useState, useEffect } from 'react'; import CameraStream from './CameraStream'; -const CameraDashboard = ({ apiBaseUrl = 'http://localhost:8000' }) => { +const CameraDashboard = ({ apiBaseUrl = 'http://vision:8000' }) => { const [cameras, setCameras] = useState({}); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); @@ -309,7 +315,7 @@ export default CameraDashboard; ```jsx import { useState, useEffect, useCallback } from 'react'; -const useCameraStream = (cameraName, apiBaseUrl = 'http://localhost:8000') => { +const useCameraStream = (cameraName, apiBaseUrl = 'http://vision:8000') => { const [isStreaming, setIsStreaming] = useState(false); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); @@ -444,20 +450,43 @@ const CameraStreamTailwind = ({ cameraName }) => { ### Environment Variables (.env) ```env -REACT_APP_CAMERA_API_URL=http://localhost:8000 +# Production configuration (using 'vision' hostname) +REACT_APP_CAMERA_API_URL=http://vision:8000 REACT_APP_STREAM_REFRESH_INTERVAL=30000 REACT_APP_STREAM_TIMEOUT=10000 + +# Development configuration (using localhost) +# REACT_APP_CAMERA_API_URL=http://localhost:8000 + +# Custom IP configuration +# REACT_APP_CAMERA_API_URL=http://192.168.1.100:8000 ``` ### API Configuration ```javascript const apiConfig = { - baseUrl: process.env.REACT_APP_CAMERA_API_URL || 'http://localhost:8000', + baseUrl: process.env.REACT_APP_CAMERA_API_URL || 'http://vision:8000', timeout: parseInt(process.env.REACT_APP_STREAM_TIMEOUT) || 10000, refreshInterval: parseInt(process.env.REACT_APP_STREAM_REFRESH_INTERVAL) || 30000, }; ``` +### Hostname Setup Guide +```bash +# Option 1: Add to /etc/hosts (Linux/Mac) +echo "127.0.0.1 vision" | sudo tee -a /etc/hosts + +# Option 2: Add to hosts file (Windows) +# Add to C:\Windows\System32\drivers\etc\hosts: +# 127.0.0.1 vision + +# Option 3: Configure DNS +# Point 'vision' hostname to your server's IP address + +# Verify hostname resolution +ping vision +``` + ## ๐Ÿšจ Important Implementation Notes ### 1. MJPEG Stream Handling diff --git a/API Documentations/AUTO_RECORDING_FEATURE_GUIDE.md b/API Documentations/AUTO_RECORDING_FEATURE_GUIDE.md new file mode 100644 index 0000000..fbdb14c --- /dev/null +++ b/API Documentations/AUTO_RECORDING_FEATURE_GUIDE.md @@ -0,0 +1,260 @@ +# Auto-Recording Feature Implementation Guide + +## ๐ŸŽฏ Overview for React App Development + +This document provides a comprehensive guide for updating the React application to support the new auto-recording feature that was added to the USDA Vision Camera System. + +## ๐Ÿ“‹ What Changed in the Backend + +### New API Endpoints Added + +1. **Enable Auto-Recording** + ```http + POST /cameras/{camera_name}/auto-recording/enable + Response: AutoRecordingConfigResponse + ``` + +2. **Disable Auto-Recording** + ```http + POST /cameras/{camera_name}/auto-recording/disable + Response: AutoRecordingConfigResponse + ``` + +3. **Get Auto-Recording Status** + ```http + GET /auto-recording/status + Response: AutoRecordingStatusResponse + ``` + +### Updated API Responses + +#### CameraStatusResponse (Updated) +```typescript +interface CameraStatusResponse { + name: string; + status: string; + is_recording: boolean; + last_checked: string; + last_error?: string; + device_info?: any; + current_recording_file?: string; + recording_start_time?: string; + + // NEW AUTO-RECORDING FIELDS + auto_recording_enabled: boolean; + auto_recording_active: boolean; + auto_recording_failure_count: number; + auto_recording_last_attempt?: string; + auto_recording_last_error?: string; +} +``` + +#### CameraConfigResponse (Updated) +```typescript +interface CameraConfigResponse { + name: string; + machine_topic: string; + storage_path: string; + enabled: boolean; + + // NEW AUTO-RECORDING CONFIG FIELDS + auto_start_recording_enabled: boolean; + auto_recording_max_retries: number; + auto_recording_retry_delay_seconds: number; + + // ... existing fields (exposure_ms, gain, etc.) +} +``` + +#### New Response Types +```typescript +interface AutoRecordingConfigResponse { + success: boolean; + message: string; + camera_name: string; + enabled: boolean; +} + +interface AutoRecordingStatusResponse { + running: boolean; + auto_recording_enabled: boolean; + retry_queue: Record; + enabled_cameras: string[]; +} +``` + +## ๐ŸŽจ React App UI Requirements + +### 1. Camera Status Display Updates + +**Add to Camera Cards/Components:** +- Auto-recording enabled/disabled indicator +- Auto-recording active status (when machine is ON and auto-recording) +- Failure count display (if > 0) +- Last auto-recording error (if any) +- Visual distinction between manual and auto-recording + +**Example UI Elements:** +```jsx +// Auto-recording status badge +{camera.auto_recording_enabled && ( + + Auto-Recording {camera.auto_recording_active ? "Active" : "Enabled"} + +)} + +// Failure indicator +{camera.auto_recording_failure_count > 0 && ( + + Auto-recording failures: {camera.auto_recording_failure_count} + +)} +``` + +### 2. Auto-Recording Controls + +**Add Toggle Controls:** +- Enable/Disable auto-recording per camera +- Global auto-recording status display +- Retry queue monitoring + +**Example Control Component:** +```jsx +const AutoRecordingToggle = ({ camera, onToggle }) => { + const handleToggle = async () => { + const endpoint = camera.auto_recording_enabled ? 'disable' : 'enable'; + await fetch(`/cameras/${camera.name}/auto-recording/${endpoint}`, { + method: 'POST' + }); + onToggle(); + }; + + return ( + + ); +}; +``` + +### 3. Machine State Integration + +**Display Machine Status:** +- Show which machine each camera monitors +- Display current machine state (ON/OFF) +- Show correlation between machine state and recording status + +**Camera-Machine Mapping:** +- Camera 1 โ†’ Vibratory Conveyor (conveyor/cracker cam) +- Camera 2 โ†’ Blower Separator (blower separator) + +### 4. Auto-Recording Dashboard + +**Create New Dashboard Section:** +- Overall auto-recording system status +- List of cameras with auto-recording enabled +- Active retry queue display +- Recent auto-recording events/logs + +## ๐Ÿ”ง Implementation Steps for React App + +### Step 1: Update TypeScript Interfaces +```typescript +// Update existing interfaces in your types file +// Add new interfaces for auto-recording responses +``` + +### Step 2: Update API Service Functions +```typescript +// Add new API calls +export const enableAutoRecording = (cameraName: string) => + fetch(`/cameras/${cameraName}/auto-recording/enable`, { method: 'POST' }); + +export const disableAutoRecording = (cameraName: string) => + fetch(`/cameras/${cameraName}/auto-recording/disable`, { method: 'POST' }); + +export const getAutoRecordingStatus = () => + fetch('/auto-recording/status').then(res => res.json()); +``` + +### Step 3: Update Camera Components +- Add auto-recording status indicators +- Add enable/disable controls +- Update recording status display to distinguish auto vs manual + +### Step 4: Create Auto-Recording Management Panel +- System-wide auto-recording status +- Per-camera auto-recording controls +- Retry queue monitoring +- Error reporting and alerts + +### Step 5: Update State Management +```typescript +// Add auto-recording state to your store/context +interface AppState { + cameras: CameraStatusResponse[]; + autoRecordingStatus: AutoRecordingStatusResponse; + // ... existing state +} +``` + +## ๐ŸŽฏ Key User Experience Considerations + +### Visual Indicators +1. **Recording Status Hierarchy:** + - Manual Recording (highest priority - red/prominent) + - Auto-Recording Active (green/secondary) + - Auto-Recording Enabled but Inactive (blue/subtle) + - Auto-Recording Disabled (gray/muted) + +2. **Machine State Correlation:** + - Show machine ON/OFF status next to camera + - Indicate when auto-recording should be active + - Alert if machine is ON but auto-recording failed + +3. **Error Handling:** + - Clear error messages for auto-recording failures + - Retry count display + - Last attempt timestamp + - Quick retry/reset options + +### User Controls +1. **Quick Actions:** + - Toggle auto-recording per camera + - Force retry failed auto-recording + - Override auto-recording (manual control) + +2. **Configuration:** + - Adjust retry settings + - Change machine-camera mappings + - Set recording parameters for auto-recording + +## ๐Ÿšจ Important Notes + +### Behavior Rules +1. **Manual Override:** Manual recording always takes precedence over auto-recording +2. **Non-Blocking:** Auto-recording status checks don't interfere with camera operation +3. **Machine Correlation:** Auto-recording only activates when the associated machine turns ON +4. **Failure Handling:** Failed auto-recording attempts are retried automatically with exponential backoff + +### API Polling Recommendations +- Poll camera status every 2-3 seconds for real-time updates +- Poll auto-recording status every 5-10 seconds +- Use WebSocket connections if available for real-time machine state updates + +## ๐Ÿ“ฑ Mobile Considerations +- Auto-recording controls should be easily accessible on mobile +- Status indicators should be clear and readable on small screens +- Consider collapsible sections for detailed auto-recording information + +## ๐Ÿ” Testing Checklist +- [ ] Auto-recording toggle works for each camera +- [ ] Status updates reflect machine state changes +- [ ] Error states are clearly displayed +- [ ] Manual recording overrides auto-recording +- [ ] Retry mechanism is visible to users +- [ ] Mobile interface is functional + +This guide provides everything needed to update the React app to fully support the new auto-recording feature! diff --git a/API Documentations/CAMERA_CONFIG_API.md b/API Documentations/CAMERA_CONFIG_API.md new file mode 100644 index 0000000..79d557b --- /dev/null +++ b/API Documentations/CAMERA_CONFIG_API.md @@ -0,0 +1,455 @@ +# ๐ŸŽ›๏ธ Camera Configuration API Guide + +This guide explains how to configure camera settings via API endpoints, including all the advanced settings from your config.json. + +## ๐Ÿ“‹ Configuration Categories + +### โœ… **Real-time Configurable (No Restart Required)** + +These settings can be changed while the camera is active: + +- **Basic**: `exposure_ms`, `gain`, `target_fps` +- **Image Quality**: `sharpness`, `contrast`, `saturation`, `gamma` +- **Color**: `auto_white_balance`, `color_temperature_preset` +- **Advanced**: `anti_flicker_enabled`, `light_frequency` +- **HDR**: `hdr_enabled`, `hdr_gain_mode` + +### โš ๏ธ **Restart Required** + +These settings require camera restart to take effect: + +- **Noise Reduction**: `noise_filter_enabled`, `denoise_3d_enabled` +- **System**: `machine_topic`, `storage_path`, `enabled`, `bit_depth` + +### ๐Ÿค– **Auto-Recording** + +- **Auto-Recording**: `auto_record_on_machine_start` - When enabled, the camera automatically starts recording when MQTT messages indicate the associated machine turns on, and stops recording when it turns off + +## ๐Ÿ”Œ API Endpoints + +### 1. Get Camera Configuration + +```http +GET /cameras/{camera_name}/config +``` + +**Response:** + +```json +{ + "name": "camera1", + "machine_topic": "vibratory_conveyor", + "storage_path": "/storage/camera1", + "enabled": true, + "auto_record_on_machine_start": false, + "exposure_ms": 1.0, + "gain": 3.5, + "target_fps": 0, + "sharpness": 120, + "contrast": 110, + "saturation": 100, + "gamma": 100, + "noise_filter_enabled": true, + "denoise_3d_enabled": false, + "auto_white_balance": true, + "color_temperature_preset": 0, + "anti_flicker_enabled": true, + "light_frequency": 1, + "bit_depth": 8, + "hdr_enabled": false, + "hdr_gain_mode": 0 +} +``` + +### 2. Update Camera Configuration + +```http +PUT /cameras/{camera_name}/config +Content-Type: application/json +``` + +**Request Body (all fields optional):** + +```json +{ + "auto_record_on_machine_start": true, + "exposure_ms": 2.0, + "gain": 4.0, + "target_fps": 10.0, + "sharpness": 150, + "contrast": 120, + "saturation": 110, + "gamma": 90, + "noise_filter_enabled": true, + "denoise_3d_enabled": false, + "auto_white_balance": false, + "color_temperature_preset": 1, + "anti_flicker_enabled": true, + "light_frequency": 1, + "hdr_enabled": false, + "hdr_gain_mode": 0 +} +``` + +**Response:** + +```json +{ + "success": true, + "message": "Camera camera1 configuration updated", + "updated_settings": ["exposure_ms", "gain", "sharpness"] +} +``` + +### 3. Apply Configuration (Restart Camera) + +```http +POST /cameras/{camera_name}/apply-config +``` + +**Response:** + +```json +{ + "success": true, + "message": "Configuration applied to camera camera1" +} +``` + +## ๐Ÿ“Š Setting Ranges and Descriptions + +### Basic Settings + +| Setting | Range | Default | Description | +|---------|-------|---------|-------------| +| `exposure_ms` | 0.1 - 1000.0 | 1.0 | Exposure time in milliseconds | +| `gain` | 0.0 - 20.0 | 3.5 | Camera gain multiplier | +| `target_fps` | 0.0 - 120.0 | 0 | Target FPS (0 = maximum) | + +### Image Quality Settings + +| Setting | Range | Default | Description | +|---------|-------|---------|-------------| +| `sharpness` | 0 - 200 | 100 | Image sharpness (100 = no sharpening) | +| `contrast` | 0 - 200 | 100 | Image contrast (100 = normal) | +| `saturation` | 0 - 200 | 100 | Color saturation (color cameras only) | +| `gamma` | 0 - 300 | 100 | Gamma correction (100 = normal) | + +### Color Settings + +| Setting | Values | Default | Description | +|---------|--------|---------|-------------| +| `auto_white_balance` | true/false | true | Automatic white balance | +| `color_temperature_preset` | 0-10 | 0 | Color temperature preset (0=auto) | + +### Advanced Settings + +| Setting | Values | Default | Description | +|---------|--------|---------|-------------| +| `anti_flicker_enabled` | true/false | true | Reduce artificial lighting flicker | +| `light_frequency` | 0/1 | 1 | Light frequency (0=50Hz, 1=60Hz) | +| `noise_filter_enabled` | true/false | true | Basic noise filtering | +| `denoise_3d_enabled` | true/false | false | Advanced 3D denoising | + +### HDR Settings + +| Setting | Values | Default | Description | +|---------|--------|---------|-------------| +| `hdr_enabled` | true/false | false | High Dynamic Range | +| `hdr_gain_mode` | 0-3 | 0 | HDR processing mode | + +## ๐Ÿš€ Usage Examples + +### Example 1: Adjust Exposure and Gain + +```bash +curl -X PUT http://localhost:8000/cameras/camera1/config \ + -H "Content-Type: application/json" \ + -d '{ + "exposure_ms": 1.5, + "gain": 4.0 + }' +``` + +### Example 2: Improve Image Quality + +```bash +curl -X PUT http://localhost:8000/cameras/camera1/config \ + -H "Content-Type: application/json" \ + -d '{ + "sharpness": 150, + "contrast": 120, + "gamma": 90 + }' +``` + +### Example 3: Configure for Indoor Lighting + +```bash +curl -X PUT http://localhost:8000/cameras/camera1/config \ + -H "Content-Type: application/json" \ + -d '{ + "anti_flicker_enabled": true, + "light_frequency": 1, + "auto_white_balance": false, + "color_temperature_preset": 2 + }' +``` + +### Example 4: Enable HDR Mode + +```bash +curl -X PUT http://localhost:8000/cameras/camera1/config \ + -H "Content-Type: application/json" \ + -d '{ + "hdr_enabled": true, + "hdr_gain_mode": 1 + }' +``` + +## โš›๏ธ React Integration Examples + +### Camera Configuration Component + +```jsx +import React, { useState, useEffect } from 'react'; + +const CameraConfig = ({ cameraName, apiBaseUrl = 'http://localhost:8000' }) => { + const [config, setConfig] = useState(null); + const [loading, setLoading] = useState(false); + const [error, setError] = useState(null); + + // Load current configuration + useEffect(() => { + fetchConfig(); + }, [cameraName]); + + const fetchConfig = async () => { + try { + const response = await fetch(`${apiBaseUrl}/cameras/${cameraName}/config`); + if (response.ok) { + const data = await response.json(); + setConfig(data); + } else { + setError('Failed to load configuration'); + } + } catch (err) { + setError(`Error: ${err.message}`); + } + }; + + const updateConfig = async (updates) => { + setLoading(true); + try { + const response = await fetch(`${apiBaseUrl}/cameras/${cameraName}/config`, { + method: 'PUT', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(updates) + }); + + if (response.ok) { + const result = await response.json(); + console.log('Updated settings:', result.updated_settings); + await fetchConfig(); // Reload configuration + } else { + const error = await response.json(); + setError(error.detail || 'Update failed'); + } + } catch (err) { + setError(`Error: ${err.message}`); + } finally { + setLoading(false); + } + }; + + const handleSliderChange = (setting, value) => { + updateConfig({ [setting]: value }); + }; + + if (!config) return
Loading configuration...
; + + return ( +
+

Camera Configuration: {cameraName}

+ + {/* Basic Settings */} +
+

Basic Settings

+ +
+ + handleSliderChange('exposure_ms', parseFloat(e.target.value))} + /> +
+ +
+ + handleSliderChange('gain', parseFloat(e.target.value))} + /> +
+ +
+ + handleSliderChange('target_fps', parseInt(e.target.value))} + /> +
+
+ + {/* Image Quality Settings */} +
+

Image Quality

+ +
+ + handleSliderChange('sharpness', parseInt(e.target.value))} + /> +
+ +
+ + handleSliderChange('contrast', parseInt(e.target.value))} + /> +
+ +
+ + handleSliderChange('gamma', parseInt(e.target.value))} + /> +
+
+ + {/* Advanced Settings */} +
+

Advanced Settings

+ +
+ +
+ +
+ +
+ +
+ +
+
+ + {error && ( +
+ {error} +
+ )} + + {loading &&
Updating configuration...
} +
+ ); +}; + +export default CameraConfig; +``` + +## ๐Ÿ”„ Configuration Workflow + +### 1. Real-time Adjustments + +For settings that don't require restart: + +```bash +# Update settings +curl -X PUT /cameras/camera1/config -d '{"exposure_ms": 2.0}' + +# Settings take effect immediately +# Continue recording/streaming without interruption +``` + +### 2. Settings Requiring Restart + +For noise reduction and system settings: + +```bash +# Update settings +curl -X PUT /cameras/camera1/config -d '{"noise_filter_enabled": false}' + +# Apply configuration (restarts camera) +curl -X POST /cameras/camera1/apply-config + +# Camera reinitializes with new settings +``` + +## ๐Ÿšจ Important Notes + +### Camera State During Updates + +- **Real-time settings**: Applied immediately, no interruption +- **Restart-required settings**: Saved to config, applied on next restart +- **Recording**: Continues during real-time updates +- **Streaming**: Continues during real-time updates + +### Error Handling + +- Invalid ranges return HTTP 422 with validation errors +- Camera not found returns HTTP 404 +- SDK errors are logged and return HTTP 500 + +### Performance Impact + +- **Image quality settings**: Minimal performance impact +- **Noise reduction**: May reduce FPS when enabled +- **HDR**: Significant processing overhead when enabled + +This comprehensive API allows you to control all camera settings programmatically, making it perfect for integration with React dashboards or automated optimization systems! diff --git a/API Documentations/README.md b/API Documentations/README.md new file mode 100644 index 0000000..a6ca74a --- /dev/null +++ b/API Documentations/README.md @@ -0,0 +1,870 @@ +# USDA Vision Camera System + +A comprehensive system for monitoring machines via MQTT and automatically recording video from GigE cameras when machines are active. Designed for Atlanta, Georgia operations with proper timezone synchronization. + +## ๐ŸŽฏ Overview + +This system integrates MQTT machine monitoring with automated video recording from GigE cameras. When a machine turns on (detected via MQTT), the system automatically starts recording from the associated camera. When the machine turns off, recording stops and the video is saved with an Atlanta timezone timestamp. + +### Key Features + +- **๐Ÿ”„ MQTT Integration**: Listens to multiple machine state topics +- **๐Ÿ“น Automatic Recording**: Starts/stops recording based on machine states +- **๐Ÿ“ท GigE Camera Support**: Uses camera SDK library (mvsdk) for camera control +- **โšก Multi-threading**: Concurrent MQTT listening, camera monitoring, and recording +- **๐ŸŒ REST API**: FastAPI server for dashboard integration +- **๐Ÿ“ก WebSocket Support**: Real-time status updates +- **๐Ÿ’พ Storage Management**: Organized file storage with cleanup capabilities +- **๐Ÿ“ Comprehensive Logging**: Detailed logging with rotation and error tracking +- **โš™๏ธ Configuration Management**: JSON-based configuration system +- **๐Ÿ• Timezone Sync**: Proper time synchronization for Atlanta, Georgia + +## ๐Ÿ“ Project Structure + +``` +USDA-Vision-Cameras/ +โ”œโ”€โ”€ README.md # Main documentation (this file) +โ”œโ”€โ”€ main.py # System entry point +โ”œโ”€โ”€ config.json # System configuration +โ”œโ”€โ”€ requirements.txt # Python dependencies +โ”œโ”€โ”€ pyproject.toml # UV package configuration +โ”œโ”€โ”€ start_system.sh # Startup script +โ”œโ”€โ”€ setup_timezone.sh # Time sync setup +โ”œโ”€โ”€ camera_preview.html # Web camera preview interface +โ”œโ”€โ”€ usda_vision_system/ # Main application +โ”‚ โ”œโ”€โ”€ core/ # Core functionality +โ”‚ โ”œโ”€โ”€ mqtt/ # MQTT integration +โ”‚ โ”œโ”€โ”€ camera/ # Camera management +โ”‚ โ”œโ”€โ”€ storage/ # File management +โ”‚ โ”œโ”€โ”€ api/ # REST API server +โ”‚ โ””โ”€โ”€ main.py # Application coordinator +โ”œโ”€โ”€ camera_sdk/ # GigE camera SDK library +โ”œโ”€โ”€ tests/ # Organized test files +โ”‚ โ”œโ”€โ”€ api/ # API-related tests +โ”‚ โ”œโ”€โ”€ camera/ # Camera functionality tests +โ”‚ โ”œโ”€โ”€ core/ # Core system tests +โ”‚ โ”œโ”€โ”€ mqtt/ # MQTT integration tests +โ”‚ โ”œโ”€โ”€ recording/ # Recording feature tests +โ”‚ โ”œโ”€โ”€ storage/ # Storage management tests +โ”‚ โ”œโ”€โ”€ integration/ # System integration tests +โ”‚ โ””โ”€โ”€ legacy_tests/ # Archived development files +โ”œโ”€โ”€ docs/ # Organized documentation +โ”‚ โ”œโ”€โ”€ api/ # API documentation +โ”‚ โ”œโ”€โ”€ features/ # Feature-specific guides +โ”‚ โ”œโ”€โ”€ guides/ # User and setup guides +โ”‚ โ””โ”€โ”€ legacy/ # Legacy documentation +โ”œโ”€โ”€ ai_agent/ # AI agent resources +โ”‚ โ”œโ”€โ”€ guides/ # AI-specific instructions +โ”‚ โ”œโ”€โ”€ examples/ # Demo scripts and notebooks +โ”‚ โ””โ”€โ”€ references/ # API references and types +โ”œโ”€โ”€ Camera/ # Camera data directory +โ””โ”€โ”€ storage/ # Recording storage (created at runtime) + โ”œโ”€โ”€ camera1/ # Camera 1 recordings + โ””โ”€โ”€ camera2/ # Camera 2 recordings +``` + +## ๐Ÿ—๏ธ Architecture + +``` +โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +โ”‚ MQTT Broker โ”‚ โ”‚ GigE Camera โ”‚ โ”‚ Dashboard โ”‚ +โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ (React) โ”‚ +โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ + โ”‚ โ”‚ โ”‚ + โ”‚ Machine States โ”‚ Video Streams โ”‚ API Calls + โ”‚ โ”‚ โ”‚ +โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +โ”‚ USDA Vision Camera System โ”‚ +โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค +โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ +โ”‚ โ”‚ MQTT Client โ”‚ โ”‚ Camera โ”‚ โ”‚ API Server โ”‚ โ”‚ +โ”‚ โ”‚ โ”‚ โ”‚ Manager โ”‚ โ”‚ โ”‚ โ”‚ +โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ +โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ +โ”‚ โ”‚ State โ”‚ โ”‚ Storage โ”‚ โ”‚ Event โ”‚ โ”‚ +โ”‚ โ”‚ Manager โ”‚ โ”‚ Manager โ”‚ โ”‚ System โ”‚ โ”‚ +โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ +โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ +``` + +## ๐Ÿ“‹ Prerequisites + +### Hardware Requirements +- GigE cameras compatible with camera SDK library +- Network connection to MQTT broker +- Sufficient storage space for video recordings + +### Software Requirements +- **Python 3.11+** +- **uv package manager** (recommended) or pip +- **MQTT broker** (e.g., Mosquitto, Home Assistant) +- **Linux system** (tested on Ubuntu/Debian) + +### Network Requirements +- Access to MQTT broker +- GigE cameras on network +- Internet access for time synchronization (optional but recommended) + +## ๐Ÿš€ Installation + +### 1. Clone the Repository +```bash +git clone https://github.com/your-username/USDA-Vision-Cameras.git +cd USDA-Vision-Cameras +``` + +### 2. Install Dependencies +Using uv (recommended): +```bash +# Install uv if not already installed +curl -LsSf https://astral.sh/uv/install.sh | sh + +# Install dependencies +uv sync +``` + +Using pip: +```bash +# Create virtual environment +python -m venv .venv +source .venv/bin/activate # On Windows: .venv\Scripts\activate + +# Install dependencies +pip install -r requirements.txt +``` + +### 3. Setup GigE Camera Library +Ensure the `camera_sdk` directory contains the mvsdk library for your GigE cameras. This should include: +- `mvsdk.py` - Python SDK wrapper +- Camera driver libraries +- Any camera-specific configuration files + +### 4. Configure Storage Directory +```bash +# Create storage directory (adjust path as needed) +mkdir -p ./storage +# Or for system-wide storage: +# sudo mkdir -p /storage && sudo chown $USER:$USER /storage +``` + +### 5. Setup Time Synchronization (Recommended) +```bash +# Run timezone setup for Atlanta, Georgia +./setup_timezone.sh +``` + +### 6. Configure the System +Edit `config.json` to match your setup: +```json +{ + "mqtt": { + "broker_host": "192.168.1.110", + "broker_port": 1883, + "topics": { + "machine1": "vision/machine1/state", + "machine2": "vision/machine2/state" + } + }, + "cameras": [ + { + "name": "camera1", + "machine_topic": "machine1", + "storage_path": "./storage/camera1", + "enabled": true + } + ] +} +``` + +## ๐Ÿ”ง Configuration + +### MQTT Configuration +```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" + } + } +} +``` + +### Camera Configuration +```json +{ + "cameras": [ + { + "name": "camera1", + "machine_topic": "vibratory_conveyor", + "storage_path": "./storage/camera1", + "exposure_ms": 1.0, + "gain": 3.5, + "target_fps": 3.0, + "enabled": true + } + ] +} +``` + +### System Configuration +```json +{ + "system": { + "camera_check_interval_seconds": 2, + "log_level": "INFO", + "api_host": "0.0.0.0", + "api_port": 8000, + "enable_api": true, + "timezone": "America/New_York" + } +} +``` + +## ๐ŸŽฎ Usage + +### Quick Start +```bash +# Test the system +python test_system.py + +# Start the system +python main.py + +# Or use the startup script +./start_system.sh +``` + +### Command Line Options +```bash +# Custom configuration file +python main.py --config my_config.json + +# Debug mode +python main.py --log-level DEBUG + +# Help +python main.py --help +``` + +### Verify Installation +```bash +# Run system tests +python test_system.py + +# Check time synchronization +python check_time.py + +# Test timezone functions +python test_timezone.py +``` + +## ๐ŸŒ API Usage + +The system provides a comprehensive REST API for monitoring and control. + +> **๐Ÿ“š Complete API Documentation**: See [docs/API_DOCUMENTATION.md](docs/API_DOCUMENTATION.md) for the full API reference including all endpoints, request/response models, examples, and recent enhancements. +> +> **โšก Quick Reference**: See [docs/API_QUICK_REFERENCE.md](docs/API_QUICK_REFERENCE.md) for commonly used endpoints with curl examples. + +### Starting the API Server +The API server starts automatically with the main system on port 8000: +```bash +python main.py +# API available at: http://localhost:8000 +``` + +### ๐Ÿš€ New API Features + +#### Enhanced Recording Control +- **Dynamic camera settings**: Set exposure, gain, FPS per recording +- **Automatic datetime prefixes**: All filenames get timestamp prefixes +- **Auto-recording management**: Enable/disable per camera via API + +#### Advanced Camera Configuration +- **Real-time settings**: Update image quality without restart +- **Live streaming**: MJPEG streams for web integration +- **Recovery operations**: Reconnect, reset, reinitialize cameras + +#### Comprehensive Monitoring +- **MQTT event history**: Track machine state changes +- **Storage statistics**: Monitor disk usage and file counts +- **WebSocket updates**: Real-time system notifications + +### Core Endpoints + +#### System Status +```bash +# Get overall system status +curl http://localhost:8000/system/status + +# Response example: +{ + "system_started": true, + "mqtt_connected": true, + "machines": { + "vibratory_conveyor": {"state": "on", "last_updated": "2025-07-25T21:30:00-04:00"} + }, + "cameras": { + "camera1": {"status": "available", "is_recording": true} + }, + "active_recordings": 1, + "uptime_seconds": 3600 +} +``` + +#### Machine Status +```bash +# Get all machine states +curl http://localhost:8000/machines + +# Response example: +{ + "vibratory_conveyor": { + "name": "vibratory_conveyor", + "state": "on", + "last_updated": "2025-07-25T21:30:00-04:00", + "mqtt_topic": "vision/vibratory_conveyor/state" + } +} +``` + +#### Camera Status +```bash +# Get all camera statuses +curl http://localhost:8000/cameras + +# Get specific camera status +curl http://localhost:8000/cameras/camera1 + +# Response example: +{ + "name": "camera1", + "status": "available", + "is_recording": false, + "last_checked": "2025-07-25T21:30:00-04:00", + "device_info": { + "friendly_name": "Blower-Yield-Cam", + "serial_number": "054012620023" + } +} +``` + +#### Manual Recording Control +```bash +# Start recording manually +curl -X POST http://localhost:8000/cameras/camera1/start-recording \ + -H "Content-Type: application/json" \ + -d '{"camera_name": "camera1", "filename": "manual_test.avi"}' + +# Stop recording manually +curl -X POST http://localhost:8000/cameras/camera1/stop-recording + +# Response example: +{ + "success": true, + "message": "Recording started for camera1", + "filename": "camera1_manual_20250725_213000.avi" +} +``` + +#### Storage Management +```bash +# Get storage statistics +curl http://localhost:8000/storage/stats + +# Get recording files list +curl -X POST http://localhost:8000/storage/files \ + -H "Content-Type: application/json" \ + -d '{"camera_name": "camera1", "limit": 10}' + +# Cleanup old files +curl -X POST http://localhost:8000/storage/cleanup \ + -H "Content-Type: application/json" \ + -d '{"max_age_days": 30}' +``` + +### WebSocket Real-time Updates +```javascript +// Connect to WebSocket for real-time updates +const ws = new WebSocket('ws://localhost:8000/ws'); + +ws.onmessage = function(event) { + const update = JSON.parse(event.data); + console.log('Real-time update:', update); + + // Handle different event types + if (update.event_type === 'machine_state_changed') { + console.log(`Machine ${update.data.machine_name} is now ${update.data.state}`); + } else if (update.event_type === 'recording_started') { + console.log(`Recording started: ${update.data.filename}`); + } +}; +``` + +### Integration Examples + +#### Python Integration +```python +import requests +import json + +# System status check +response = requests.get('http://localhost:8000/system/status') +status = response.json() +print(f"System running: {status['system_started']}") + +# Start recording +recording_data = {"camera_name": "camera1"} +response = requests.post( + 'http://localhost:8000/cameras/camera1/start-recording', + headers={'Content-Type': 'application/json'}, + data=json.dumps(recording_data) +) +result = response.json() +print(f"Recording started: {result['success']}") +``` + +#### JavaScript/React Integration +```javascript +// React hook for system status +import { useState, useEffect } from 'react'; + +function useSystemStatus() { + const [status, setStatus] = useState(null); + + useEffect(() => { + const fetchStatus = async () => { + try { + const response = await fetch('http://localhost:8000/system/status'); + const data = await response.json(); + setStatus(data); + } catch (error) { + console.error('Failed to fetch status:', error); + } + }; + + fetchStatus(); + const interval = setInterval(fetchStatus, 5000); // Update every 5 seconds + + return () => clearInterval(interval); + }, []); + + return status; +} + +// Usage in component +function Dashboard() { + const systemStatus = useSystemStatus(); + + return ( +
+

USDA Vision System

+ {systemStatus && ( +
+

Status: {systemStatus.system_started ? 'Running' : 'Stopped'}

+

MQTT: {systemStatus.mqtt_connected ? 'Connected' : 'Disconnected'}

+

Active Recordings: {systemStatus.active_recordings}

+
+ )} +
+ ); +} +``` + +#### Supabase Integration +```javascript +// Store recording metadata in Supabase +import { createClient } from '@supabase/supabase-js'; + +const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY); + +// Function to sync recording data +async function syncRecordingData() { + try { + // Get recordings from vision system + const response = await fetch('http://localhost:8000/storage/files', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ limit: 100 }) + }); + const { files } = await response.json(); + + // Store in Supabase + for (const file of files) { + await supabase.from('recordings').upsert({ + filename: file.filename, + camera_name: file.camera_name, + start_time: file.start_time, + duration_seconds: file.duration_seconds, + file_size_bytes: file.file_size_bytes + }); + } + } catch (error) { + console.error('Sync failed:', error); + } +} +``` + +## ๐Ÿ“ File Organization + +The system organizes recordings in a structured format: + +``` +storage/ +โ”œโ”€โ”€ camera1/ +โ”‚ โ”œโ”€โ”€ camera1_recording_20250725_213000.avi +โ”‚ โ”œโ”€โ”€ camera1_recording_20250725_214500.avi +โ”‚ โ””โ”€โ”€ camera1_manual_20250725_220000.avi +โ”œโ”€โ”€ camera2/ +โ”‚ โ”œโ”€โ”€ camera2_recording_20250725_213005.avi +โ”‚ โ””โ”€โ”€ camera2_recording_20250725_214505.avi +โ””โ”€โ”€ file_index.json +``` + +### Filename Convention +- **Format**: `{camera_name}_{type}_{YYYYMMDD_HHMMSS}.avi` +- **Timezone**: Atlanta local time (EST/EDT) +- **Examples**: + - `camera1_recording_20250725_213000.avi` - Automatic recording + - `camera1_manual_20250725_220000.avi` - Manual recording + +## ๐Ÿ” Monitoring and Logging + +### Log Files +- **Main Log**: `usda_vision_system.log` (rotated automatically) +- **Console Output**: Colored, real-time status updates +- **Component Logs**: Separate log levels for different components + +### Log Levels +```bash +# Debug mode (verbose) +python main.py --log-level DEBUG + +# Info mode (default) +python main.py --log-level INFO + +# Warning mode (errors and warnings only) +python main.py --log-level WARNING +``` + +### Performance Monitoring +The system tracks: +- Startup times +- Recording session metrics +- MQTT message processing rates +- Camera status check intervals +- API response times + +### Health Checks +```bash +# API health check +curl http://localhost:8000/health + +# System status +curl http://localhost:8000/system/status + +# Time synchronization +python check_time.py +``` + +## ๐Ÿšจ Troubleshooting + +### Common Issues and Solutions + +#### 1. Camera Not Found +**Problem**: `Camera discovery failed` or `No cameras found` + +**Solutions**: +```bash +# Check camera connections +ping 192.168.1.165 # Replace with your camera IP + +# Verify camera SDK library +ls -la "camera_sdk/" +# Should contain mvsdk.py and related files + +# Test camera discovery manually +python -c " +import sys; sys.path.append('./camera_sdk') +import mvsdk +devices = mvsdk.CameraEnumerateDevice() +print(f'Found {len(devices)} cameras') +for i, dev in enumerate(devices): + print(f'Camera {i}: {dev.GetFriendlyName()}') +" + +# Check camera permissions +sudo chmod 666 /dev/video* # If using USB cameras +``` + +#### 2. MQTT Connection Failed +**Problem**: `MQTT connection failed` or `MQTT disconnected` + +**Solutions**: +```bash +# Test MQTT broker connectivity +ping 192.168.1.110 # Replace with your broker IP +telnet 192.168.1.110 1883 # Test port connectivity + +# Test MQTT manually +mosquitto_sub -h 192.168.1.110 -t "vision/+/state" -v + +# Check credentials in config.json +{ + "mqtt": { + "broker_host": "192.168.1.110", + "broker_port": 1883, + "username": "your_username", # Add if required + "password": "your_password" # Add if required + } +} + +# Check firewall +sudo ufw status +sudo ufw allow 1883 # Allow MQTT port +``` + +#### 3. Recording Fails +**Problem**: `Failed to start recording` or `Camera initialization failed` + +**Solutions**: +```bash +# Check storage permissions +ls -la storage/ +chmod 755 storage/ +chmod 755 storage/camera*/ + +# Check available disk space +df -h storage/ + +# Test camera initialization +python -c " +import sys; sys.path.append('./camera_sdk') +import mvsdk +devices = mvsdk.CameraEnumerateDevice() +if devices: + try: + hCamera = mvsdk.CameraInit(devices[0], -1, -1) + print('Camera initialized successfully') + mvsdk.CameraUnInit(hCamera) + except Exception as e: + print(f'Camera init failed: {e}') +" + +# Check if camera is busy +lsof | grep video # Check what's using cameras +``` + +#### 4. API Server Won't Start +**Problem**: `Failed to start API server` or `Port already in use` + +**Solutions**: +```bash +# Check if port 8000 is in use +netstat -tlnp | grep 8000 +lsof -i :8000 + +# Kill process using port 8000 +sudo kill -9 $(lsof -t -i:8000) + +# Use different port in config.json +{ + "system": { + "api_port": 8001 # Change port + } +} + +# Check firewall +sudo ufw allow 8000 +``` + +#### 5. Time Synchronization Issues +**Problem**: `Time is NOT synchronized` or time drift warnings + +**Solutions**: +```bash +# Check time sync status +timedatectl status + +# Force time sync +sudo systemctl restart systemd-timesyncd +sudo timedatectl set-ntp true + +# Manual time sync +sudo ntpdate -s time.nist.gov + +# Check timezone +timedatectl list-timezones | grep New_York +sudo timedatectl set-timezone America/New_York + +# Verify with system +python check_time.py +``` + +#### 6. Storage Issues +**Problem**: `Permission denied` or `No space left on device` + +**Solutions**: +```bash +# Check disk space +df -h +du -sh storage/ + +# Fix permissions +sudo chown -R $USER:$USER storage/ +chmod -R 755 storage/ + +# Clean up old files +python -c " +from usda_vision_system.storage.manager import StorageManager +from usda_vision_system.core.config import Config +from usda_vision_system.core.state_manager import StateManager +config = Config() +state_manager = StateManager() +storage = StorageManager(config, state_manager) +result = storage.cleanup_old_files(7) # Clean files older than 7 days +print(f'Cleaned {result[\"files_removed\"]} files') +" +``` + +### Debug Mode + +Enable debug mode for detailed troubleshooting: +```bash +# Start with debug logging +python main.py --log-level DEBUG + +# Check specific component logs +tail -f usda_vision_system.log | grep "camera" +tail -f usda_vision_system.log | grep "mqtt" +tail -f usda_vision_system.log | grep "ERROR" +``` + +### System Health Check + +Run comprehensive system diagnostics: +```bash +# Full system test +python test_system.py + +# Individual component tests +python test_timezone.py +python check_time.py + +# API health check +curl http://localhost:8000/health +curl http://localhost:8000/system/status +``` + +### Log Analysis + +Common log patterns to look for: +```bash +# MQTT connection issues +grep "MQTT" usda_vision_system.log | grep -E "(ERROR|WARNING)" + +# Camera problems +grep "camera" usda_vision_system.log | grep -E "(ERROR|failed)" + +# Recording issues +grep "recording" usda_vision_system.log | grep -E "(ERROR|failed)" + +# Time sync problems +grep -E "(time|sync)" usda_vision_system.log | grep -E "(ERROR|WARNING)" +``` + +### Getting Help + +If you encounter issues not covered here: + +1. **Check Logs**: Always start with `usda_vision_system.log` +2. **Run Tests**: Use `python test_system.py` to identify problems +3. **Check Configuration**: Verify `config.json` settings +4. **Test Components**: Use individual test scripts +5. **Check Dependencies**: Ensure all required packages are installed + +### Performance Optimization + +For better performance: +```bash +# Reduce camera check interval (in config.json) +{ + "system": { + "camera_check_interval_seconds": 5 # Increase from 2 to 5 + } +} + +# Optimize recording settings +{ + "cameras": [ + { + "target_fps": 2.0, # Reduce FPS for smaller files + "exposure_ms": 2.0 # Adjust exposure as needed + } + ] +} + +# Enable log rotation +{ + "system": { + "log_level": "INFO" # Reduce from DEBUG to INFO + } +} +``` + +## ๐Ÿค Contributing + +### Development Setup +```bash +# Clone repository +git clone https://github.com/your-username/USDA-Vision-Cameras.git +cd USDA-Vision-Cameras + +# Install development dependencies +uv sync --dev + +# Run tests +python test_system.py +python test_timezone.py +``` + +### Project Structure +``` +usda_vision_system/ +โ”œโ”€โ”€ core/ # Core functionality (config, state, events, logging) +โ”œโ”€โ”€ mqtt/ # MQTT client and message handlers +โ”œโ”€โ”€ camera/ # Camera management, monitoring, recording +โ”œโ”€โ”€ storage/ # File management and organization +โ”œโ”€โ”€ api/ # FastAPI server and WebSocket support +โ””โ”€โ”€ main.py # Application coordinator +``` + +### Adding Features +1. **New Camera Types**: Extend `camera/recorder.py` +2. **New MQTT Topics**: Update `config.json` and `mqtt/handlers.py` +3. **New API Endpoints**: Add to `api/server.py` +4. **New Events**: Define in `core/events.py` + +## ๐Ÿ“„ License + +This project is developed for USDA research purposes. + +## ๐Ÿ†˜ Support + +For technical support: +1. Check the troubleshooting section above +2. Review logs in `usda_vision_system.log` +3. Run system diagnostics with `python test_system.py` +4. Check API health at `http://localhost:8000/health` + +--- + +**System Status**: โœ… **READY FOR PRODUCTION** +**Time Sync**: โœ… **ATLANTA, GEORGIA (EDT/EST)** +**API Server**: โœ… **http://localhost:8000** +**Documentation**: โœ… **COMPLETE** diff --git a/streaming/STREAMING_GUIDE.md b/API Documentations/STREAMING_GUIDE.md similarity index 100% rename from streaming/STREAMING_GUIDE.md rename to API Documentations/STREAMING_GUIDE.md diff --git a/streaming/camera-api.types.ts b/API Documentations/camera-api.types.ts similarity index 98% rename from streaming/camera-api.types.ts rename to API Documentations/camera-api.types.ts index ffa9602..3610ac8 100644 --- a/streaming/camera-api.types.ts +++ b/API Documentations/camera-api.types.ts @@ -16,7 +16,7 @@ export interface ApiConfig { } export const defaultApiConfig: ApiConfig = { - baseUrl: 'http://localhost:8000', + baseUrl: 'http://vision:8000', // Production default, change to 'http://localhost:8000' for development timeout: 10000, refreshInterval: 30000, }; @@ -204,17 +204,17 @@ export interface CameraApiClient { // System endpoints getHealth(): Promise; getSystemStatus(): Promise; - + // Camera endpoints getCameras(): Promise; getCameraStatus(cameraName: string): Promise; testCameraConnection(cameraName: string): Promise<{ success: boolean; message: string }>; - + // Streaming endpoints startStream(cameraName: string): Promise; stopStream(cameraName: string): Promise; getStreamUrl(cameraName: string): string; - + // Recording endpoints startRecording(cameraName: string, options?: StartRecordingRequest): Promise; stopRecording(cameraName: string): Promise; @@ -291,14 +291,14 @@ export interface CameraContextValue { streamingState: StreamingState; recordingState: RecordingState; apiClient: CameraApiClient; - + // Actions startStream: (cameraName: string) => Promise; stopStream: (cameraName: string) => Promise; startRecording: (cameraName: string, options?: StartRecordingRequest) => Promise; stopRecording: (cameraName: string) => Promise; refreshCameras: () => Promise; - + // State loading: boolean; error: string | null; diff --git a/streaming/camera_preview.html b/API Documentations/camera_preview.html similarity index 99% rename from streaming/camera_preview.html rename to API Documentations/camera_preview.html index 0caa92d..99d321e 100644 --- a/streaming/camera_preview.html +++ b/API Documentations/camera_preview.html @@ -178,7 +178,7 @@ + + + + \ No newline at end of file