update: misc changes
This commit is contained in:
@@ -14,12 +14,12 @@ import threading
|
|||||||
import time
|
import time
|
||||||
from bar_config import LED_BAR_NAMES, DEFAULT_BAR_SETTINGS
|
from bar_config import LED_BAR_NAMES, DEFAULT_BAR_SETTINGS
|
||||||
from color_utils import adjust_brightness
|
from color_utils import adjust_brightness
|
||||||
|
from networking import WebSocketClient as SPIClient # SPI transport client
|
||||||
|
|
||||||
# Configure logging
|
# Configure logging
|
||||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
||||||
|
|
||||||
# Configuration
|
# Configuration
|
||||||
LED_SERVER_URI = "ws://192.168.4.1:80/ws"
|
|
||||||
CONTROL_SERVER_PORT = 8765
|
CONTROL_SERVER_PORT = 8765
|
||||||
SOUND_CONTROL_HOST = "127.0.0.1"
|
SOUND_CONTROL_HOST = "127.0.0.1"
|
||||||
SOUND_CONTROL_PORT = 65433
|
SOUND_CONTROL_PORT = 65433
|
||||||
@@ -40,57 +40,23 @@ PATTERN_NAMES = {
|
|||||||
|
|
||||||
|
|
||||||
class LEDController:
|
class LEDController:
|
||||||
"""Handles communication with LED bars via WebSocket."""
|
"""Handles communication with LED bars via SPI (through ESP32 relay)."""
|
||||||
|
|
||||||
def __init__(self, led_server_uri):
|
def __init__(self, spi_bus: int = 0, spi_device: int = 0, spi_speed_hz: int = 1_000_000):
|
||||||
self.led_server_uri = led_server_uri
|
self.client = SPIClient(bus=spi_bus, device=spi_device, speed_hz=spi_speed_hz)
|
||||||
self.websocket = None
|
|
||||||
self.is_connected = False
|
@property
|
||||||
self.reconnect_task = None
|
def is_connected(self) -> bool:
|
||||||
|
return getattr(self.client, "is_connected", False)
|
||||||
|
|
||||||
async def connect(self):
|
async def connect(self):
|
||||||
"""Connect to LED server."""
|
await self.client.connect()
|
||||||
if self.is_connected and self.websocket:
|
|
||||||
return
|
|
||||||
|
|
||||||
try:
|
|
||||||
logging.info(f"Connecting to LED server at {self.led_server_uri}...")
|
|
||||||
self.websocket = await websockets.connect(self.led_server_uri)
|
|
||||||
self.is_connected = True
|
|
||||||
logging.info("Connected to LED server")
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
logging.error(f"Failed to connect to LED server: {e}")
|
|
||||||
self.is_connected = False
|
|
||||||
self.websocket = None
|
|
||||||
|
|
||||||
|
|
||||||
async def send_data(self, data):
|
async def send_data(self, data):
|
||||||
"""Send data to LED server."""
|
await self.client.send_data(data)
|
||||||
if not self.is_connected or not self.websocket:
|
|
||||||
logging.warning("Not connected to LED server. Attempting to reconnect...")
|
|
||||||
await self.connect()
|
|
||||||
if not self.is_connected:
|
|
||||||
logging.error("Failed to reconnect to LED server. Cannot send data.")
|
|
||||||
return
|
|
||||||
|
|
||||||
try:
|
|
||||||
await self.websocket.send(json.dumps(data))
|
|
||||||
logging.debug(f"Sent to LED server: {data}")
|
|
||||||
except Exception as e:
|
|
||||||
logging.error(f"Failed to send data to LED server: {e}")
|
|
||||||
self.is_connected = False
|
|
||||||
self.websocket = None
|
|
||||||
# Attempt to reconnect
|
|
||||||
await self.connect()
|
|
||||||
|
|
||||||
async def close(self):
|
async def close(self):
|
||||||
"""Close LED server connection."""
|
await self.client.close()
|
||||||
if self.websocket and self.is_connected:
|
|
||||||
await self.websocket.close()
|
|
||||||
self.is_connected = False
|
|
||||||
self.websocket = None
|
|
||||||
logging.info("Disconnected from LED server")
|
|
||||||
|
|
||||||
|
|
||||||
class SoundController:
|
class SoundController:
|
||||||
@@ -119,7 +85,8 @@ class LightingController:
|
|||||||
"""Main lighting control logic."""
|
"""Main lighting control logic."""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.led_controller = LEDController(LED_SERVER_URI)
|
# SPI defaults: bus 0, CE0, 1MHz; adjust here if needed
|
||||||
|
self.led_controller = LEDController(spi_bus=0, spi_device=0, spi_speed_hz=1_000_000)
|
||||||
self.sound_controller = SoundController(SOUND_CONTROL_HOST, SOUND_CONTROL_PORT)
|
self.sound_controller = SoundController(SOUND_CONTROL_HOST, SOUND_CONTROL_PORT)
|
||||||
|
|
||||||
# Lighting state
|
# Lighting state
|
||||||
|
Reference in New Issue
Block a user