38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
import os
|
|
from time import sleep
|
|
from threading import Thread
|
|
|
|
from flask import Blueprint, request, jsonify
|
|
import paho.mqtt.publish as mqtt_publish
|
|
|
|
|
|
blueprint = Blueprint('detection', __name__)
|
|
|
|
|
|
def set_camera_detection(camera: str, value: bool, delay: int = 0) -> None:
|
|
sleep(delay)
|
|
|
|
mqtt_publish.single(f'frigate/{camera}/detect/set', 'ON' if value else 'OFF', hostname='mqtt', port=1883, auth={'username': os.environ['MQTT_USERNAME'], 'password': os.environ['MQTT_PASSWORD']})
|
|
|
|
|
|
@blueprint.route('/camera/<string:camera>/detect', methods=['POST'])
|
|
def camera_detect_POST(camera):
|
|
if not request.json:
|
|
return jsonify({
|
|
'status': 'failure',
|
|
'description': 'Request body needs to be in JSON format'
|
|
}), 400
|
|
|
|
value = request.json.get('value', True)
|
|
set_camera_detection(camera, value)
|
|
|
|
duration = request.json.get('duration', 0)
|
|
if duration > 0:
|
|
# Start sleeping thread to revert value after duration
|
|
thread = Thread(target=set_camera_detection, args=(camera, not value, duration * 60))
|
|
thread.start()
|
|
|
|
return jsonify({
|
|
'status': 'success'
|
|
}), 200
|