Added webcontrol API

This commit is contained in:
ashish committed 2023-05-21 19:49:49 -04:00
1 parent a13f099e14
commit ce6b363d4e
11 files changed
+154 -15

No files matched your search

+11
View File
@@ -0,0 +1,11 @@
FROM python:3.11
WORKDIR /code
ENTRYPOINT ["python3", "server.py"]
RUN pip3 install --upgrade pip
COPY requirements.txt .
RUN pip3 install -r requirements.txt
COPY src .
+2
View File
@@ -0,0 +1,2 @@
flask==2.3.2
paho-mqtt==1.6.1
+37
View File
@@ -0,0 +1,37 @@
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
+20
View File
@@ -0,0 +1,20 @@
from flask import Flask, jsonify
import detection
app = Flask(__name__)
app.register_blueprint(detection.blueprint)
@app.route('/', methods=['GET'])
def home_GET():
return jsonify({
'status': 'success',
'name': 'webcontrol',
'description': 'This is a custom webcontrol API for Frigate that allows minimal control over the NVR system through an HTTP API'
}), 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80, debug=False)