You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
from flask import Flask, Response, request, jsonify
|
|
from camera import Camera
|
|
from database import log_event, get_all_events
|
|
import threading
|
|
|
|
app = Flask(__name__)
|
|
|
|
# Хранилище камер и событий детекции
|
|
cameras = {}
|
|
motion_detected = []
|
|
|
|
@app.route('/add_camera', methods=['POST'])
|
|
def add_camera():
|
|
camera_id = request.json['id']
|
|
if camera_id not in cameras:
|
|
cameras[camera_id] = Camera(camera_id)
|
|
threading.Thread(target=cameras[camera_id].start_stream, args=(motion_detected,)).start()
|
|
return jsonify({"message": "Camera added"}), 200
|
|
return jsonify({"message": "Camera already exists"}), 400
|
|
|
|
@app.route('/remove_camera', methods=['POST'])
|
|
def remove_camera():
|
|
camera_id = request.json['id']
|
|
if camera_id in cameras:
|
|
cameras[camera_id].stop_stream()
|
|
del cameras[camera_id]
|
|
return jsonify({"message": "Camera removed"}), 200
|
|
return jsonify({"message": "Camera not found"}), 404
|
|
|
|
@app.route('/stream/<camera_id>')
|
|
def stream(camera_id):
|
|
if camera_id in cameras:
|
|
return Response(cameras[camera_id].get_frame(), mimetype='multipart/x-mixed-replace; boundary=frame')
|
|
return "Camera not found", 404
|
|
|
|
@app.route('/events', methods=['GET'])
|
|
def get_events():
|
|
return jsonify(get_all_events()), 200
|
|
|
|
@app.route('/motion', methods=['GET'])
|
|
def motion():
|
|
return jsonify(motion_detected), 200
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host='0.0.0.0', port=5000)
|