Introduce Picture Targets to separate processing from devices
Add PictureTarget entity that bridges PictureSource to output device, separating processing settings from device connection/calibration state. This enables future target types (Art-Net, E1.31) and cleanly decouples "what to stream" from "where to stream." - Add PictureTarget/WledPictureTarget dataclasses and storage - Split ProcessorManager into DeviceState (health) + TargetState (processing) - Add /api/v1/picture-targets endpoints (CRUD, start/stop, settings, metrics) - Simplify device API (remove processing/settings/metrics endpoints) - Auto-migrate existing device settings to picture targets on first startup - Add Targets tab to WebUI with target cards and editor modal - Add en/ru locale keys for targets UI Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -12,14 +12,17 @@ from wled_controller.core.calibration import (
|
||||
calibration_to_dict,
|
||||
create_default_calibration,
|
||||
)
|
||||
from wled_controller.core.processor_manager import DEFAULT_STATE_CHECK_INTERVAL, ProcessingSettings
|
||||
from wled_controller.utils import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class Device:
|
||||
"""Represents a WLED device configuration."""
|
||||
"""Represents a WLED device configuration.
|
||||
|
||||
A device is a holder of connection state and calibration options.
|
||||
Processing settings and picture source assignments live on PictureTargets.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@@ -28,62 +31,28 @@ class Device:
|
||||
url: str,
|
||||
led_count: int,
|
||||
enabled: bool = True,
|
||||
settings: Optional[ProcessingSettings] = None,
|
||||
calibration: Optional[CalibrationConfig] = None,
|
||||
picture_source_id: str = "",
|
||||
created_at: Optional[datetime] = None,
|
||||
updated_at: Optional[datetime] = None,
|
||||
):
|
||||
"""Initialize device.
|
||||
|
||||
Args:
|
||||
device_id: Unique device identifier
|
||||
name: Device name
|
||||
url: WLED device URL
|
||||
led_count: Number of LEDs
|
||||
enabled: Whether device is enabled
|
||||
settings: Processing settings
|
||||
calibration: Calibration configuration
|
||||
picture_source_id: ID of assigned picture source
|
||||
created_at: Creation timestamp
|
||||
updated_at: Last update timestamp
|
||||
"""
|
||||
self.id = device_id
|
||||
self.name = name
|
||||
self.url = url
|
||||
self.led_count = led_count
|
||||
self.enabled = enabled
|
||||
self.settings = settings or ProcessingSettings()
|
||||
self.calibration = calibration or create_default_calibration(led_count)
|
||||
self.picture_source_id = picture_source_id
|
||||
self.created_at = created_at or datetime.utcnow()
|
||||
self.updated_at = updated_at or datetime.utcnow()
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
"""Convert device to dictionary.
|
||||
|
||||
Returns:
|
||||
Dictionary representation
|
||||
"""
|
||||
"""Convert device to dictionary."""
|
||||
return {
|
||||
"id": self.id,
|
||||
"name": self.name,
|
||||
"url": self.url,
|
||||
"led_count": self.led_count,
|
||||
"enabled": self.enabled,
|
||||
"settings": {
|
||||
"display_index": self.settings.display_index,
|
||||
"fps": self.settings.fps,
|
||||
"border_width": self.settings.border_width,
|
||||
"brightness": self.settings.brightness,
|
||||
"gamma": self.settings.gamma,
|
||||
"saturation": self.settings.saturation,
|
||||
"smoothing": self.settings.smoothing,
|
||||
"interpolation_mode": self.settings.interpolation_mode,
|
||||
"state_check_interval": self.settings.state_check_interval,
|
||||
},
|
||||
"calibration": calibration_to_dict(self.calibration),
|
||||
"picture_source_id": self.picture_source_id,
|
||||
"created_at": self.created_at.isoformat(),
|
||||
"updated_at": self.updated_at.isoformat(),
|
||||
}
|
||||
@@ -92,25 +61,9 @@ class Device:
|
||||
def from_dict(cls, data: dict) -> "Device":
|
||||
"""Create device from dictionary.
|
||||
|
||||
Args:
|
||||
data: Dictionary with device data
|
||||
|
||||
Returns:
|
||||
Device instance
|
||||
Backward-compatible: ignores legacy 'settings' and 'picture_source_id'
|
||||
fields that have been migrated to PictureTarget.
|
||||
"""
|
||||
settings_data = data.get("settings", {})
|
||||
settings = ProcessingSettings(
|
||||
display_index=settings_data.get("display_index", 0),
|
||||
fps=settings_data.get("fps", 30),
|
||||
border_width=settings_data.get("border_width", 10),
|
||||
brightness=settings_data.get("brightness", 1.0),
|
||||
gamma=settings_data.get("gamma", 2.2),
|
||||
saturation=settings_data.get("saturation", 1.0),
|
||||
smoothing=settings_data.get("smoothing", 0.3),
|
||||
interpolation_mode=settings_data.get("interpolation_mode", "average"),
|
||||
state_check_interval=settings_data.get("state_check_interval", DEFAULT_STATE_CHECK_INTERVAL),
|
||||
)
|
||||
|
||||
calibration_data = data.get("calibration")
|
||||
calibration = (
|
||||
calibration_from_dict(calibration_data)
|
||||
@@ -118,17 +71,13 @@ class Device:
|
||||
else create_default_calibration(data["led_count"])
|
||||
)
|
||||
|
||||
picture_source_id = data.get("picture_source_id", "")
|
||||
|
||||
return cls(
|
||||
device_id=data["id"],
|
||||
name=data["name"],
|
||||
url=data["url"],
|
||||
led_count=data["led_count"],
|
||||
enabled=data.get("enabled", True),
|
||||
settings=settings,
|
||||
calibration=calibration,
|
||||
picture_source_id=picture_source_id,
|
||||
created_at=datetime.fromisoformat(data.get("created_at", datetime.utcnow().isoformat())),
|
||||
updated_at=datetime.fromisoformat(data.get("updated_at", datetime.utcnow().isoformat())),
|
||||
)
|
||||
@@ -138,11 +87,6 @@ class DeviceStore:
|
||||
"""Persistent storage for WLED devices."""
|
||||
|
||||
def __init__(self, storage_file: str | Path):
|
||||
"""Initialize device store.
|
||||
|
||||
Args:
|
||||
storage_file: Path to JSON storage file
|
||||
"""
|
||||
self.storage_file = Path(storage_file)
|
||||
self._devices: Dict[str, Device] = {}
|
||||
|
||||
@@ -179,6 +123,16 @@ class DeviceStore:
|
||||
logger.error(f"Failed to load devices: {e}")
|
||||
raise
|
||||
|
||||
def load_raw(self) -> dict:
|
||||
"""Load raw JSON data from storage (for migration)."""
|
||||
if not self.storage_file.exists():
|
||||
return {}
|
||||
try:
|
||||
with open(self.storage_file, "r") as f:
|
||||
return json.load(f)
|
||||
except Exception:
|
||||
return {}
|
||||
|
||||
def save(self):
|
||||
"""Save devices to storage file."""
|
||||
try:
|
||||
@@ -189,12 +143,10 @@ class DeviceStore:
|
||||
}
|
||||
}
|
||||
|
||||
# Write to temporary file first
|
||||
temp_file = self.storage_file.with_suffix(".tmp")
|
||||
with open(temp_file, "w") as f:
|
||||
json.dump(data, f, indent=2)
|
||||
|
||||
# Atomic rename
|
||||
temp_file.replace(self.storage_file)
|
||||
|
||||
logger.debug(f"Saved {len(self._devices)} devices to storage")
|
||||
@@ -208,41 +160,19 @@ class DeviceStore:
|
||||
name: str,
|
||||
url: str,
|
||||
led_count: int,
|
||||
settings: Optional[ProcessingSettings] = None,
|
||||
calibration: Optional[CalibrationConfig] = None,
|
||||
picture_source_id: str = "",
|
||||
) -> Device:
|
||||
"""Create a new device.
|
||||
|
||||
Args:
|
||||
name: Device name
|
||||
url: WLED device URL
|
||||
led_count: Number of LEDs
|
||||
settings: Processing settings
|
||||
calibration: Calibration configuration
|
||||
picture_source_id: ID of assigned picture source
|
||||
|
||||
Returns:
|
||||
Created device
|
||||
|
||||
Raises:
|
||||
ValueError: If validation fails
|
||||
"""
|
||||
# Generate unique ID
|
||||
"""Create a new device."""
|
||||
device_id = f"device_{uuid.uuid4().hex[:8]}"
|
||||
|
||||
# Create device
|
||||
device = Device(
|
||||
device_id=device_id,
|
||||
name=name,
|
||||
url=url,
|
||||
led_count=led_count,
|
||||
settings=settings,
|
||||
calibration=calibration,
|
||||
picture_source_id=picture_source_id,
|
||||
)
|
||||
|
||||
# Store
|
||||
self._devices[device_id] = device
|
||||
self.save()
|
||||
|
||||
@@ -250,22 +180,11 @@ class DeviceStore:
|
||||
return device
|
||||
|
||||
def get_device(self, device_id: str) -> Optional[Device]:
|
||||
"""Get device by ID.
|
||||
|
||||
Args:
|
||||
device_id: Device identifier
|
||||
|
||||
Returns:
|
||||
Device or None if not found
|
||||
"""
|
||||
"""Get device by ID."""
|
||||
return self._devices.get(device_id)
|
||||
|
||||
def get_all_devices(self) -> List[Device]:
|
||||
"""Get all devices.
|
||||
|
||||
Returns:
|
||||
List of all devices
|
||||
"""
|
||||
"""Get all devices."""
|
||||
return list(self._devices.values())
|
||||
|
||||
def update_device(
|
||||
@@ -275,73 +194,38 @@ class DeviceStore:
|
||||
url: Optional[str] = None,
|
||||
led_count: Optional[int] = None,
|
||||
enabled: Optional[bool] = None,
|
||||
settings: Optional[ProcessingSettings] = None,
|
||||
calibration: Optional[CalibrationConfig] = None,
|
||||
picture_source_id: Optional[str] = None,
|
||||
) -> Device:
|
||||
"""Update device.
|
||||
|
||||
Args:
|
||||
device_id: Device identifier
|
||||
name: New name (optional)
|
||||
url: New URL (optional)
|
||||
led_count: New LED count (optional)
|
||||
enabled: New enabled state (optional)
|
||||
settings: New settings (optional)
|
||||
calibration: New calibration (optional)
|
||||
picture_source_id: New picture source ID (optional)
|
||||
|
||||
Returns:
|
||||
Updated device
|
||||
|
||||
Raises:
|
||||
ValueError: If device not found or validation fails
|
||||
"""
|
||||
"""Update device."""
|
||||
device = self._devices.get(device_id)
|
||||
if not device:
|
||||
raise ValueError(f"Device {device_id} not found")
|
||||
|
||||
# Update fields
|
||||
if name is not None:
|
||||
device.name = name
|
||||
if url is not None:
|
||||
device.url = url
|
||||
if led_count is not None:
|
||||
device.led_count = led_count
|
||||
# Reset calibration if LED count changed
|
||||
device.calibration = create_default_calibration(led_count)
|
||||
if enabled is not None:
|
||||
device.enabled = enabled
|
||||
if settings is not None:
|
||||
device.settings = settings
|
||||
if calibration is not None:
|
||||
# Validate LED count matches
|
||||
if calibration.get_total_leds() != device.led_count:
|
||||
raise ValueError(
|
||||
f"Calibration LED count ({calibration.get_total_leds()}) "
|
||||
f"does not match device LED count ({device.led_count})"
|
||||
)
|
||||
device.calibration = calibration
|
||||
if picture_source_id is not None:
|
||||
device.picture_source_id = picture_source_id
|
||||
|
||||
device.updated_at = datetime.utcnow()
|
||||
|
||||
# Save
|
||||
self.save()
|
||||
|
||||
logger.info(f"Updated device {device_id}")
|
||||
return device
|
||||
|
||||
def delete_device(self, device_id: str):
|
||||
"""Delete device.
|
||||
|
||||
Args:
|
||||
device_id: Device identifier
|
||||
|
||||
Raises:
|
||||
ValueError: If device not found
|
||||
"""
|
||||
"""Delete device."""
|
||||
if device_id not in self._devices:
|
||||
raise ValueError(f"Device {device_id} not found")
|
||||
|
||||
@@ -351,22 +235,11 @@ class DeviceStore:
|
||||
logger.info(f"Deleted device {device_id}")
|
||||
|
||||
def device_exists(self, device_id: str) -> bool:
|
||||
"""Check if device exists.
|
||||
|
||||
Args:
|
||||
device_id: Device identifier
|
||||
|
||||
Returns:
|
||||
True if device exists
|
||||
"""
|
||||
"""Check if device exists."""
|
||||
return device_id in self._devices
|
||||
|
||||
def count(self) -> int:
|
||||
"""Get number of devices.
|
||||
|
||||
Returns:
|
||||
Device count
|
||||
"""
|
||||
"""Get number of devices."""
|
||||
return len(self._devices)
|
||||
|
||||
def clear(self):
|
||||
|
||||
Reference in New Issue
Block a user