108 lines
3.5 KiB
Python
108 lines
3.5 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
# -*- coding: utf-8 -*-
|
||
|
|
"""
|
||
|
|
Módulo de configuración para la bocina ESP32
|
||
|
|
"""
|
||
|
|
|
||
|
|
import os
|
||
|
|
import configparser
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
# ==================== RUTAS ====================
|
||
|
|
BASE_DIR = Path(__file__).resolve().parent.parent.parent
|
||
|
|
CONFIG_DIR = BASE_DIR / "config" / "speaker_iot"
|
||
|
|
CONFIG_FILE = CONFIG_DIR / "settings.ini"
|
||
|
|
|
||
|
|
# ==================== CONFIGURACIÓN POR DEFECTO ====================
|
||
|
|
DEFAULT_CONFIG = {
|
||
|
|
"ESP32": {
|
||
|
|
"ip": "192.168.15.128",
|
||
|
|
"puerto": "81"
|
||
|
|
},
|
||
|
|
"Audio": {
|
||
|
|
"duracion_ms": "2000",
|
||
|
|
"tono_base": "440",
|
||
|
|
"amplitud": "16000"
|
||
|
|
},
|
||
|
|
"General": {
|
||
|
|
"timeout": "5",
|
||
|
|
"reconectar": "true"
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
class ConfiguracionBocina:
|
||
|
|
"""Gestor de configuración para la bocina"""
|
||
|
|
|
||
|
|
def __init__(self):
|
||
|
|
self.config = configparser.ConfigParser()
|
||
|
|
self._cargar_configuracion()
|
||
|
|
|
||
|
|
def _cargar_configuracion(self):
|
||
|
|
"""Carga la configuración desde el archivo o crea uno por defecto"""
|
||
|
|
if CONFIG_FILE.exists():
|
||
|
|
self.config.read(CONFIG_FILE, encoding='utf-8')
|
||
|
|
else:
|
||
|
|
self._crear_configuracion_default()
|
||
|
|
|
||
|
|
def _crear_configuracion_default(self):
|
||
|
|
"""Crea archivo de configuración por defecto"""
|
||
|
|
# Crear directorio si no existe
|
||
|
|
CONFIG_DIR.mkdir(parents=True, exist_ok=True)
|
||
|
|
|
||
|
|
# Cargar valores por defecto
|
||
|
|
for seccion, valores in DEFAULT_CONFIG.items():
|
||
|
|
self.config[seccion] = valores
|
||
|
|
|
||
|
|
# Guardar archivo
|
||
|
|
with open(CONFIG_FILE, 'w', encoding='utf-8') as f:
|
||
|
|
self.config.write(f)
|
||
|
|
|
||
|
|
print(f"📝 Configuración creada en: {CONFIG_FILE}")
|
||
|
|
|
||
|
|
def obtener_ip(self) -> str:
|
||
|
|
"""Obtiene la IP de la bocina"""
|
||
|
|
return self.config.get("ESP32", "ip", fallback="192.168.15.128")
|
||
|
|
|
||
|
|
def obtener_puerto(self) -> int:
|
||
|
|
"""Obtiene el puerto de la bocina"""
|
||
|
|
return self.config.getint("ESP32", "puerto", fallback=81)
|
||
|
|
|
||
|
|
def obtener_duracion(self) -> int:
|
||
|
|
"""Obtiene duración del audio en ms"""
|
||
|
|
return self.config.getint("Audio", "duracion_ms", fallback=2000)
|
||
|
|
|
||
|
|
def obtener_tono_base(self) -> int:
|
||
|
|
"""Obtiene frecuencia base del tono"""
|
||
|
|
return self.config.getint("Audio", "tono_base", fallback=440)
|
||
|
|
|
||
|
|
def obtener_amplitud(self) -> int:
|
||
|
|
"""Obtiene amplitud del audio"""
|
||
|
|
return self.config.getint("Audio", "amplitud", fallback=16000)
|
||
|
|
|
||
|
|
def obtener_timeout(self) -> int:
|
||
|
|
"""Obtiene timeout en segundos"""
|
||
|
|
return self.config.getint("General", "timeout", fallback=5)
|
||
|
|
|
||
|
|
def reconectar_auto(self) -> bool:
|
||
|
|
"""Obtiene si debe reconectar automáticamente"""
|
||
|
|
return self.config.getboolean("General", "reconectar", fallback=True)
|
||
|
|
|
||
|
|
def actualizar_ip(self, nueva_ip: str):
|
||
|
|
"""Actualiza la IP de la bocina"""
|
||
|
|
self.config.set("ESP32", "ip", nueva_ip)
|
||
|
|
with open(CONFIG_FILE, 'w', encoding='utf-8') as f:
|
||
|
|
self.config.write(f)
|
||
|
|
print(f"✅ IP actualizada a: {nueva_ip}")
|
||
|
|
|
||
|
|
def mostrar_configuracion(self):
|
||
|
|
"""Muestra la configuración actual"""
|
||
|
|
print("\n📡 Configuración actual:")
|
||
|
|
print(f" IP: {self.obtener_ip()}:{self.obtener_puerto()}")
|
||
|
|
print(f" Duración: {self.obtener_duracion()}ms")
|
||
|
|
print(f" Tono base: {self.obtener_tono_base()}Hz")
|
||
|
|
print(f" Timeout: {self.obtener_timeout()}s")
|
||
|
|
|
||
|
|
|
||
|
|
# Instancia global
|
||
|
|
config = ConfiguracionBocina()
|