Refactor event creation and ending functions to accept a JSON body in addition to query parameters

This commit is contained in:
Sergey Krashevich 2023-06-26 15:49:46 +03:00
parent 080a2f5bc8
commit c2ceea8e38
No known key found for this signature in database
GPG Key ID: 625171324E7D3856
2 changed files with 13 additions and 18 deletions

View File

@ -8,12 +8,9 @@ import random
import string import string
from multiprocessing.queues import Queue from multiprocessing.queues import Queue
from typing import Optional from typing import Optional
from flask import (
current_app,
jsonify,
request,
)
import cv2 import cv2
from flask import Response, current_app, jsonify, request
from frigate.config import CameraConfig, FrigateConfig from frigate.config import CameraConfig, FrigateConfig
from frigate.const import CLIPS_DIR from frigate.const import CLIPS_DIR
@ -134,7 +131,7 @@ class ExternalEventProcessor:
return base64.b64encode(jpg.tobytes()).decode("utf-8") return base64.b64encode(jpg.tobytes()).decode("utf-8")
def create_event(camera_name, label): def create_event(camera_name: str, label: str, body: dict[str, any] = {}) -> Response:
if not camera_name or not current_app.frigate_config.cameras.get(camera_name): if not camera_name or not current_app.frigate_config.cameras.get(camera_name):
return jsonify( return jsonify(
{"success": False, "message": f"{camera_name} is not a valid camera."}, 404 {"success": False, "message": f"{camera_name} is not a valid camera."}, 404
@ -143,18 +140,16 @@ def create_event(camera_name, label):
if not label: if not label:
return jsonify({"success": False, "message": f"{label} must be set."}, 404) return jsonify({"success": False, "message": f"{label} must be set."}, 404)
json: dict[str, any] = request.get_json(silent=True) or {}
try: try:
frame = current_app.detected_frames_processor.get_current_frame(camera_name) frame = current_app.detected_frames_processor.get_current_frame(camera_name)
event_id = current_app.external_processor.create_manual_event( event_id = current_app.external_processor.create_manual_event(
camera_name, camera_name,
label, label,
json.get("sub_label", None), body.get("sub_label", None),
json.get("duration", 30), body.get("duration", 30),
json.get("include_recording", True), body.get("include_recording", True),
json.get("draw", {}), body.get("draw", {}),
frame, frame,
) )
except Exception as e: except Exception as e:
@ -173,11 +168,9 @@ def create_event(camera_name, label):
) )
def end_event(event_id): def end_event(event_id: str, body: dict[str, any] = {}) -> Response:
json: dict[str, any] = request.get_json(silent=True) or {}
try: try:
end_time = json.get("end_time", datetime.now().timestamp()) end_time = body.get("end_time", datetime.now().timestamp())
current_app.external_processor.finish_manual_event(event_id, end_time) current_app.external_processor.finish_manual_event(event_id, end_time)
except Exception: except Exception:
return jsonify( return jsonify(

View File

@ -850,12 +850,14 @@ def events():
@bp.route("/events/<camera_name>/<label>/create", methods=["POST"]) @bp.route("/events/<camera_name>/<label>/create", methods=["POST"])
def create_event_handler(camera_name, label): def create_event_handler(camera_name, label):
return create_event(camera_name, label) json: dict[str, any] = request.get_json(silent=True) or {}
return create_event(camera_name, label, json)
@bp.route("/events/<event_id>/end", methods=["PUT"]) @bp.route("/events/<event_id>/end", methods=["PUT"])
def end_event_handler(event_id): def end_event_handler(event_id):
return end_event(event_id) json: dict[str, any] = request.get_json(silent=True) or {}
return end_event(event_id, json)
@bp.route("/config") @bp.route("/config")