"""Shared device operations used by REST and mobile adapters.""" from __future__ import annotations import hashlib import json import threading from app.config.store import validate_config class DeviceControl: def __init__(self, store, display, power_monitor, performance_mode): self.store = store self.display = display self.power_monitor = power_monitor self.performance_mode = performance_mode self.lock = threading.RLock() @staticmethod def revision(value): return hashlib.sha256(json.dumps(value, sort_keys=True, ensure_ascii=False, separators=(",", ":"), allow_nan=False).encode("utf-8")).hexdigest() def update_config(self, values): with self.lock: config = validate_config({**self.store.config, **values}) if "low_voltage_protection_enabled" in values: enabled = config["low_voltage_protection_enabled"] self.store.update({"low_voltage_protection_enabled": enabled}) self.power_monitor.set_protection_enabled(enabled) if enabled: self.power_monitor.sample_now() if "orientation" in values: self.display.set_orientation(config["orientation"]) if "brightness" in values: self.display.set_brightness(config["brightness"]) if "matrix_refresh_rate_limit_hz" in values: self.display.set_refresh_rate_limit(config["matrix_refresh_rate_limit_hz"]) if "performance_mode_enabled" in values: self.performance_mode.transact(config["performance_mode_enabled"], lambda enabled: self.store.update({"performance_mode_enabled": enabled})) remaining = {key: config[key] for key in ( "default_font", "default_text_size", "preview_refresh_interval_ms", "custom_test_color", "workspace_order", "animation_preview_max_concurrent") if key in values} if remaining: self.store.update(remaining) return self.store.config