76 lines
1.6 KiB
Python
76 lines
1.6 KiB
Python
import logging
|
|
from datetime import UTC, datetime
|
|
|
|
from flask import Flask, Response, abort, jsonify, render_template, send_from_directory
|
|
|
|
from src.camera import camera
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
logging.basicConfig(level=logging.WARNING)
|
|
|
|
|
|
@app.get("/heartbeat")
|
|
def heartbeat() -> tuple[Response, int]:
|
|
return (
|
|
jsonify(
|
|
{
|
|
"status": "ok",
|
|
"timestamp": datetime.now(UTC).isoformat(),
|
|
}
|
|
),
|
|
200,
|
|
)
|
|
|
|
|
|
@app.get("/")
|
|
def index() -> str:
|
|
return render_template("index.html")
|
|
|
|
|
|
@app.post("/camera/start")
|
|
def camera_start() -> tuple[Response, int]:
|
|
camera.start()
|
|
return jsonify({"status": "started"}), 200
|
|
|
|
|
|
@app.post("/camera/stop")
|
|
def camera_stop() -> tuple[Response, int]:
|
|
camera.stop()
|
|
return jsonify({"status": "stopped"}), 200
|
|
|
|
|
|
@app.get("/camera/status")
|
|
def camera_status() -> tuple[Response, int]:
|
|
return (
|
|
jsonify(
|
|
{
|
|
"running": camera.running,
|
|
"ready": camera.wait_until_ready(timeout=0),
|
|
}
|
|
),
|
|
200,
|
|
)
|
|
|
|
|
|
@app.get("/camera/hls/<path:filename>")
|
|
def hls_segment(filename: str) -> Response:
|
|
hls_dir = camera.hls_dir
|
|
if not hls_dir.exists():
|
|
abort(404)
|
|
|
|
# set correct MIME types for HLS files
|
|
if filename.endswith(".m3u8"):
|
|
mimetype = "application/vnd.apple.mpegurl"
|
|
elif filename.endswith(".ts"):
|
|
mimetype = "video/mp2t"
|
|
else:
|
|
abort(404)
|
|
|
|
return send_from_directory(str(hls_dir), filename, mimetype=mimetype)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app.run(host="0.0.0.0", port=5000, debug=True, threaded=True)
|