led-controller/api/routes.py

58 lines
2.3 KiB
Python

from fastapi import APIRouter, HTTPException
from .models import ColorUpdate, PositionUpdate, CreateBar, DeleteBar
from .settings_manager import SettingsManager
router = APIRouter(prefix="/api")
settings_manager = SettingsManager()
@router.get("/settings")
async def get_settings():
try:
return settings_manager.load_settings()
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error reading settings: {str(e)}")
@router.post("/settings/color")
async def update_color(color_update: ColorUpdate):
try:
settings_manager.update_color(color_update.barId, color_update.color)
return {"success": True, "message": f"Color updated for {color_update.barId}"}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error updating settings: {str(e)}")
@router.post("/settings/position")
async def update_position(position_update: PositionUpdate):
try:
settings_manager.update_position(position_update.barId, position_update.x, position_update.y)
return {"success": True, "message": f"Position updated for {position_update.barId}"}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error updating settings: {str(e)}")
@router.post("/settings/create")
async def create_bar(create_bar: CreateBar):
try:
if settings_manager.bar_exists(create_bar.barId):
raise HTTPException(status_code=400, detail=f"Bar {create_bar.barId} already exists")
settings_manager.create_bar(
create_bar.barId,
create_bar.url,
create_bar.color,
create_bar.x,
create_bar.y
)
return {"success": True, "message": f"Bar {create_bar.barId} created"}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error creating bar: {str(e)}")
@router.delete("/settings/delete")
async def delete_bar(delete_bar: DeleteBar):
try:
if not settings_manager.bar_exists(delete_bar.barId):
raise HTTPException(status_code=404, detail=f"Bar {delete_bar.barId} not found")
settings_manager.delete_bar(delete_bar.barId)
return {"success": True, "message": f"Bar {delete_bar.barId} deleted"}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error deleting bar: {str(e)}")