Kaera umschaltbar
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
#!/usr/bin/env python3
|
||||
import http.server
|
||||
import json
|
||||
import os
|
||||
import urllib.parse
|
||||
import socketserver
|
||||
import subprocess
|
||||
import threading
|
||||
@@ -9,10 +12,40 @@ BOUNDARY = b'--frame'
|
||||
LATEST_JPEG = None
|
||||
LATEST_LOCK = threading.Lock()
|
||||
|
||||
WIDTH = 1280
|
||||
HEIGHT = 720
|
||||
FPS = 10
|
||||
PORT = 8081
|
||||
CONFIG_FILE = os.path.join(os.path.dirname(__file__), '..', 'config', 'camera_settings.json')
|
||||
DEFAULT_CAMERA_SETTINGS = {
|
||||
'width': 1280,
|
||||
'height': 720,
|
||||
'fps': 10,
|
||||
}
|
||||
|
||||
|
||||
def load_camera_settings():
|
||||
settings = dict(DEFAULT_CAMERA_SETTINGS)
|
||||
try:
|
||||
with open(CONFIG_FILE, 'r', encoding='utf-8') as handle:
|
||||
raw_settings = json.load(handle)
|
||||
except (FileNotFoundError, json.JSONDecodeError, OSError, ValueError, TypeError):
|
||||
return settings
|
||||
|
||||
try:
|
||||
width = int(raw_settings.get('width', settings['width']))
|
||||
height = int(raw_settings.get('height', settings['height']))
|
||||
fps = int(raw_settings.get('fps', settings['fps']))
|
||||
except (TypeError, ValueError):
|
||||
return settings
|
||||
|
||||
settings['width'] = max(320, min(2592, width))
|
||||
settings['height'] = max(240, min(1944, height))
|
||||
settings['fps'] = max(1, min(30, fps))
|
||||
return settings
|
||||
|
||||
|
||||
CAMERA_SETTINGS = load_camera_settings()
|
||||
WIDTH = CAMERA_SETTINGS['width']
|
||||
HEIGHT = CAMERA_SETTINGS['height']
|
||||
FPS = CAMERA_SETTINGS['fps']
|
||||
|
||||
|
||||
def camera_reader():
|
||||
@@ -65,14 +98,18 @@ class ReusableThreadingTCPServer(socketserver.ThreadingTCPServer):
|
||||
|
||||
|
||||
class Handler(http.server.BaseHTTPRequestHandler):
|
||||
def normalized_path(self):
|
||||
return urllib.parse.urlsplit(self.path).path
|
||||
|
||||
def do_HEAD(self):
|
||||
if self.path in ('/', '/index.html'):
|
||||
path = self.normalized_path()
|
||||
if path in ('/', '/index.html'):
|
||||
self.send_response(200)
|
||||
self.send_header('Content-Type', 'text/html; charset=utf-8')
|
||||
self.end_headers()
|
||||
return
|
||||
|
||||
if self.path == '/stream.mjpg':
|
||||
if path == '/stream.mjpg':
|
||||
self.send_response(200)
|
||||
self.send_header('Age', '0')
|
||||
self.send_header('Cache-Control', 'no-cache, private')
|
||||
@@ -84,7 +121,8 @@ class Handler(http.server.BaseHTTPRequestHandler):
|
||||
self.send_error(404)
|
||||
|
||||
def do_GET(self):
|
||||
if self.path in ('/', '/index.html'):
|
||||
path = self.normalized_path()
|
||||
if path in ('/', '/index.html'):
|
||||
body = b'<html><body><img src="/stream.mjpg" /></body></html>'
|
||||
self.send_response(200)
|
||||
self.send_header('Content-Type', 'text/html; charset=utf-8')
|
||||
@@ -93,7 +131,7 @@ class Handler(http.server.BaseHTTPRequestHandler):
|
||||
self.wfile.write(body)
|
||||
return
|
||||
|
||||
if self.path != '/stream.mjpg':
|
||||
if path != '/stream.mjpg':
|
||||
self.send_error(404)
|
||||
return
|
||||
|
||||
|
||||
Reference in New Issue
Block a user