led-controller/api/settings_manager.py

52 lines
1.6 KiB
Python

import json
import os
from typing import Dict, Any
class SettingsManager:
def __init__(self, settings_file: str = "settings.json"):
self.settings_file = settings_file
def load_settings(self) -> Dict[str, Any]:
try:
with open(self.settings_file, "r") as f:
return json.load(f)
except FileNotFoundError:
return {}
def save_settings(self, settings: Dict[str, Any]) -> None:
with open(self.settings_file, "w") as f:
json.dump(settings, f, indent=2)
def update_color(self, bar_id: str, color: str) -> None:
settings = self.load_settings()
if bar_id in settings:
settings[bar_id]["color"] = color
self.save_settings(settings)
def update_position(self, bar_id: str, x: int, y: int) -> None:
settings = self.load_settings()
if bar_id in settings:
settings[bar_id]["x"] = x
settings[bar_id]["y"] = y
self.save_settings(settings)
def create_bar(self, bar_id: str, url: str, color: str, x: int, y: int) -> None:
settings = self.load_settings()
settings[bar_id] = {
"url": url,
"color": color,
"x": x,
"y": y
}
self.save_settings(settings)
def delete_bar(self, bar_id: str) -> None:
settings = self.load_settings()
if bar_id in settings:
del settings[bar_id]
self.save_settings(settings)
def bar_exists(self, bar_id: str) -> bool:
settings = self.load_settings()
return bar_id in settings