# πŸ—οΈ Arquitectura del Sistema Integrado ## πŸ“Š Diagrama General ``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ SISTEMA COMPLETO β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ SERVICIO β”‚ β”‚ DASHBOARD β”‚ β”‚ β”‚ β”‚ PRINCIPAL │◄────── Comparte ───────►│ WEB β”‚ β”‚ β”‚ β”‚ (main.py) β”‚ contexto β”‚ (dashboard) β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ Polling cada 5s β”‚ API REST β”‚ β”‚ β–Ό β–Ό β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ PROCESAMIENTO AUTOMÁTICO β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ β”‚ β”‚ AUDIO β”‚ β”‚ PDF β”‚ β”‚ TXT β”‚ β”‚ WEBDAV β”‚ β”‚ β”‚ β”‚ β”‚ β”‚Whisper+IAβ”‚ β”‚ OCR+IA β”‚ β”‚ IA β”‚ β”‚ Sync β”‚ β”‚ β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”‚ β–Ό β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ NEXTCLOUD β”‚ β”‚ β”‚ β”‚ (WebDAV) β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` ## πŸ”„ Flujo de Datos ### 1. Servicio Principal (main.py) ``` Inicio β†’ start_dashboard() β†’ main() [Bucle infinito] ↓ ↓ ↓ Dashboard Hilo separado Polling 5s Web (5000) (daemon) ↓ Check Archivos ↓ Procesar ↓ Subir a Nextcloud ``` ### 2. Dashboard Web (dashboard.py) ``` HTTP Request β†’ Flask App β†’ API Endpoints ↓ ↓ ↓ localhost:5000 Routing file_manager ↓ ↓ ↓ Browser Python Code Operar archivos ``` ## πŸ“‘ ComunicaciΓ³n entre Componentes ### Imports Compartidos ```python # main.py importa del dashboard import dashboard dashboard.app.run() # En hilo separado # dashboard.py importa de main from main import ( AUDIO_EXTENSIONS, LOCAL_DOWNLOADS_PATH, load_processed_files, process_audio_file ) ``` ### Estado Compartido - βœ… Variables de configuraciΓ³n - βœ… Funciones de procesamiento - βœ… Registro de archivos procesados - βœ… ConexiΓ³n WebDAV a Nextcloud ## 🎯 Modos de EjecuciΓ³n ### Modo 1: Servicio Completo ```bash python3 main.py ``` ``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ THREAD PRINCIPAL β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ main() - Bucle principal β”‚ β”‚ β”‚ β”‚ - Polling Nextcloud β”‚ β”‚ β”‚ β”‚ - Procesar archivos β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ threading.Thread β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ DASHBOARD THREAD (daemon) β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ Flask Web Server β”‚ β”‚ β”‚ β”‚ - Puerto 5000 β”‚ β”‚ β”‚ β”‚ - API REST β”‚ β”‚ β”‚ β”‚ - Interfaz web β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` ### Modo 2: Solo Dashboard ```bash python3 main.py dashboard-only ``` ``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ MAIN THREAD β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ Flask Web Server ONLY β”‚ β”‚ β”‚ β”‚ - Puerto 5000 β”‚ β”‚ β”‚ β”‚ - Sin bucle principal β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` ## πŸ—‚οΈ Estructura de Archivos ``` /home/ren/cbc/ β”œβ”€β”€ main.py # Servicio principal + integra dashboard β”œβ”€β”€ dashboard.py # AplicaciΓ³n Flask independiente β”œβ”€β”€ test_dashboard.py # Script de pruebas β”œβ”€β”€ templates/ β”‚ └── index.html # Interfaz web del dashboard β”œβ”€β”€ downloads/ # Archivos temporales locales β”œβ”€β”€ processed_files.txt # Registro de procesados β”œβ”€β”€ QUICKSTART.md # GuΓ­a de inicio rΓ‘pido β”œβ”€β”€ DASHBOARD_INSTRUCTIONS.md # Manual detallado └── ARQUITECTURA.md # Este archivo ``` ## πŸ”Œ API Endpoints del Dashboard | MΓ©todo | Endpoint | FunciΓ³n | |--------|----------|---------| | GET | `/` | PΓ‘gina principal | | GET | `/api/files` | Obtener lista de archivos | | POST | `/api/reprocess` | Reprocesar archivo | | POST | `/api/mark-unprocessed` | Marcar como no procesado | | GET | `/api/refresh` | Refrescar lista | | GET | `/health` | Health check | | GET | `/downloads/` | Descargar archivo | ## πŸš€ Proceso de Inicio ``` 1. Usuario ejecuta: python3 main.py β”‚ β”œβ”€β–Ί main.py inicia β”‚ β”‚ β”‚ β”œβ”€β–Ί acquire_lock() [Evitar mΓΊltiples instancias] β”‚ β”‚ β”‚ β”œβ”€β–Ί start_dashboard() [INICIA DASHBOARD] β”‚ β”‚ β”‚ β”‚ β”‚ β”œβ”€β–Ί import dashboard β”‚ β”‚ β”‚ β”‚ β”‚ β”œβ”€β–Ί threading.Thread(target=run_dashboard) β”‚ β”‚ β”‚ β”‚ β”‚ β”œβ”€β–Ί dashboard_thread.start() β”‚ β”‚ β”‚ β”‚ β”‚ └─► βœ… Dashboard en http://localhost:5000 β”‚ β”‚ β”‚ β”œβ”€β–Ί time.sleep(2) [Pausa para dashboard] β”‚ β”‚ β”‚ └─► main() [INICIA BUCLE PRINCIPAL] β”‚ β”‚ β”‚ └─► while True: β”‚ β”‚ β”‚ β”œβ”€β–Ί Polling Nextcloud β”‚ β”œβ”€β–Ί Verificar archivos nuevos β”‚ β”œβ”€β–Ί Procesar automΓ‘ticamente β”‚ └─► sleep(5) [esperar 5s] β”‚ └─► Servicio corriendo indefinidamente β”‚ β”œβ”€β–Ί Dashboard accesible └─► Procesamiento activo ``` ## πŸ” CaracterΓ­sticas de Seguridad 1. **Threading Daemon** - Dashboard se cierra automΓ‘ticamente con main.py - No impide el cierre del programa 2. **Lock File** - Evita mΓΊltiples instancias de main.py - ProtecciΓ³n automΓ‘tica via `fcntl` 3. **Error Handling** - Dashboard puede fallar sin afectar main - Logging detallado de errores 4. **CORS Enabled** - Flask-CORS configurado - Acceso desde cualquier origen ## πŸ’‘ Ventajas de la IntegraciΓ³n ### βœ… Beneficios - **Un solo comando** para todo - **Contexto compartido** (config, funciones) - **Cierre automΓ‘tico** (hilo daemon) - **Logs unificados** (consola ΓΊnica) - **Sin dependencias externas** (todo en main.py) ### ⚑ Rendimiento - **Dashboard**: ~5-10MB RAM - **Main**: Variable segΓΊn procesamiento - **ComunicaciΓ³n**: Directa (mismo proceso) - **Latencia**: MΓ­nima (sin red) ### πŸ› οΈ Mantenimiento - **CΓ³digo unificado** en main.py - **Menos archivos** de configuraciΓ³n - **Debugging simplificado** (una sola consola) - **ActualizaciΓ³n fΓ‘cil** (un solo archivo) ## πŸŽ‰ Resumen El sistema estΓ‘ **completamente integrado**: - βœ… Un solo comando: `python3 main.py` - βœ… Dashboard automΓ‘tico en puerto 5000 - βœ… Servicio principal procesando 24/7 - βœ… Interfaz web moderna y responsive - βœ… API REST completa - βœ… GestiΓ³n de archivos en tiempo real **Β‘Simplemente ejecuta `python3 main.py` y visita http://localhost:5000!** πŸš€