mirror of
https://github.com/Sosokker/HomieCare.git
synced 2025-12-19 02:04:03 +01:00
Add cameras local storage
This commit is contained in:
parent
db67580aa2
commit
e27ea919b8
1
.gitignore
vendored
1
.gitignore
vendored
@ -184,5 +184,6 @@ dist-ssr
|
||||
*.sln
|
||||
*.sw?
|
||||
|
||||
config.json
|
||||
|
||||
ActionDetector/
|
||||
|
||||
@ -4,11 +4,12 @@ from fastapi import APIRouter, HTTPException
|
||||
from fastapi.responses import StreamingResponse
|
||||
|
||||
from scheme import Camera
|
||||
from utils import save_to_config, read_cameras_from_config
|
||||
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
cameras: list[Camera] = []
|
||||
cameras: list[Camera] = read_cameras_from_config('config.json')
|
||||
|
||||
# --- UTILITY FUNCTIONS ---
|
||||
|
||||
@ -26,6 +27,7 @@ async def add_camera(rtsp_link: str):
|
||||
id = generate_camera_id()
|
||||
camera = Camera(camera_id=id, link=rtsp_link)
|
||||
cameras.append(camera)
|
||||
save_to_config(key="cameras", value=cameras)
|
||||
return {"message": "Camera added successfully", "camera_id": id}
|
||||
else:
|
||||
raise HTTPException(status_code=400, detail="Invalid RTSP link")
|
||||
@ -68,5 +70,6 @@ async def disconnect_camera(camera_id: int) -> dict:
|
||||
for camera in cameras:
|
||||
if camera.camera_id == camera_id:
|
||||
cameras.remove(camera)
|
||||
save_to_config(key="cameras", value=cameras)
|
||||
return {"message": f"Camera {camera_id} disconnected successfully"}
|
||||
raise HTTPException(status_code=404, detail=f"Camera {camera_id} not found")
|
||||
41
StreamServer/src/utils.py
Normal file
41
StreamServer/src/utils.py
Normal file
@ -0,0 +1,41 @@
|
||||
import json
|
||||
from typing import Never
|
||||
from pydantic import BaseModel
|
||||
from scheme import Camera
|
||||
|
||||
|
||||
def serialize_value(value) -> any:
|
||||
if isinstance(value, list):
|
||||
if all(isinstance(item, BaseModel) for item in value):
|
||||
return [item.dict() for item in value]
|
||||
else:
|
||||
return value
|
||||
elif isinstance(value, BaseModel):
|
||||
return value.dict()
|
||||
else:
|
||||
return value
|
||||
|
||||
|
||||
def save_to_config(key, value) -> None:
|
||||
try:
|
||||
with open('config.json', 'r') as file:
|
||||
config_data = json.load(file)
|
||||
except:
|
||||
config_data = {}
|
||||
config_data[key] = serialize_value(value)
|
||||
|
||||
with open('config.json', 'w') as file:
|
||||
json.dump(config_data, file, indent=4)
|
||||
|
||||
|
||||
def read_cameras_from_config(file_path) -> list[Camera] | list[Never]:
|
||||
try:
|
||||
with open(file_path, 'r') as file:
|
||||
config_data = json.load(file)
|
||||
except (FileNotFoundError, json.JSONDecodeError):
|
||||
return []
|
||||
|
||||
cameras_data = config_data.get('cameras', [])
|
||||
cameras = [Camera(**cam_data) for cam_data in cameras_data]
|
||||
|
||||
return cameras
|
||||
Loading…
Reference in New Issue
Block a user