Skip to content

Commit

Permalink
fixed pylint and removed playground
Browse files Browse the repository at this point in the history
  • Loading branch information
exddc committed Jun 24, 2024
1 parent b995a43 commit 96f431a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 130 deletions.
104 changes: 0 additions & 104 deletions agent/playground/camera_web_stream_test.py

This file was deleted.

56 changes: 34 additions & 22 deletions agent/video_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
# Initialize logger
LOGGER = logger.get_module_logger(__name__)


class StreamingOutput(io.BufferedIOBase):
"""Class to handle streaming output."""

def __init__(self):
self.frame = None
self.condition = Condition()
Expand All @@ -26,48 +28,54 @@ def write(self, buf):
self.frame = buf
self.condition.notify_all()


OUTPUT = StreamingOutput()


class StreamingHandler(server.BaseHTTPRequestHandler):
"""Class to handle streaming requests."""

# pylint: disable=invalid-name
def do_GET(self):
"""Handle GET requests."""
if self.path == '/':
LOGGER.info('Request for /. Redirecting to /stream.mjpg')
if self.path == "/":
LOGGER.info("Request for /. Redirecting to /stream.mjpg")
self.send_response(301)
self.send_header('Location', '/stream.mjpg')
self.send_header("Location", "/stream.mjpg")
self.end_headers()
elif self.path == '/stream.mjpg':
LOGGER.info('Request for /stream.mjpg')
elif self.path == "/stream.mjpg":
LOGGER.info("Request for /stream.mjpg")
self.send_response(200)
self.send_header('Age', 0)
self.send_header('Cache-Control', 'no-cache, private')
self.send_header('Pragma', 'no-cache')
self.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=FRAME')
self.send_header("Age", 0)
self.send_header("Cache-Control", "no-cache, private")
self.send_header("Pragma", "no-cache")
self.send_header(
"Content-Type", "multipart/x-mixed-replace; boundary=FRAME"
)
self.end_headers()
try:
while True:
with OUTPUT.condition:
OUTPUT.condition.wait()
frame = OUTPUT.frame
self.wfile.write(b'--FRAME\r\n')
self.send_header('Content-Type', 'image/jpeg')
self.send_header('Content-Length', len(frame))
self.wfile.write(b"--FRAME\r\n")
self.send_header("Content-Type", "image/jpeg")
self.send_header("Content-Length", len(frame))
self.end_headers()
self.wfile.write(frame)
self.wfile.write(b'\r\n')
self.wfile.write(b"\r\n")
except Exception as e:
LOGGER.warning(
'Removed streaming client %s: %s',
self.client_address, str(e))
"Removed streaming client %s: %s", self.client_address, str(e)
)
else:
self.send_error(404)
self.end_headers()


class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer):
"""Class to handle streaming server."""

allow_reuse_address = True
daemon_threads = True

Expand All @@ -79,13 +87,17 @@ def __init__(self, mqtt_client):
"""Initialize the agent with the MQTT client and settings."""
super().__init__(mqtt_client)
self._streaming = False
self._port = int(os.environ.get("VIDEO_STREAM_PORT"))
self._port = int(os.environ.get("VIDEO_STREAM_PORT"))
self._video_width = int(os.environ.get("VIDEO_WIDTH"))
self._video_height = int(os.environ.get("VIDEO_HEIGHT"))
self._address = ("", self._port)
self._record_time = int(os.environ.get("VIDEO_RECORDING_DURATION"))
self._picamera = Picamera2()
self._picamera.configure(self._picamera.create_video_configuration(main={"size": (self._video_width, self._video_height)}))
self._picamera.configure(
self._picamera.create_video_configuration(
main={"size": (self._video_width, self._video_height)}
)
)

def run(self):
"""Subscribe to the mqtt topic and start listening for video messages."""
Expand Down Expand Up @@ -126,21 +138,21 @@ def _start_video_stream(self):
LOGGER.info("Starting video stream")
self._picamera.start_recording(MJPEGEncoder(), FileOutput(OUTPUT))
try:
server = StreamingServer(self._address, StreamingHandler)
server.serve_forever()
__server = StreamingServer(self._address, StreamingHandler)
__server.serve_forever()
# pylint: disable=broad-except
except Exception:
LOGGER.error("Error starting video stream")
self._stop_video_stream()

def _stop_video_stream(self):
"""Stop the video stream."""
LOGGER.info("Stopping video stream")
try:
self._picamera.stop_recording()
self._picamera.stop_recording()
# pylint: disable=broad-except
except Exception as e:
LOGGER.error("Failed to stop video stream: %s", e)
LOGGER.error("Failed to stop video stream: %s", e)
finally:
self._streaming = False

Expand Down
8 changes: 4 additions & 4 deletions agent/web_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

# pylint: disable=import-error
import os
import dotenv
import threading
import dotenv
import waitress
from flask import Flask, render_template, request, redirect, url_for
from flask_assets import Environment, Bundle
Expand Down Expand Up @@ -106,10 +106,9 @@ def _start_web_server(self) -> None:
try:
waitress.serve(self.app, port=self._port)
LOGGER.info("Webserver started on port %i.", self._port)
# pylint: disable=broad-except
except Exception as error:
LOGGER.error("Error starting the webserver: %s", error)



# pylint: disable=unused-argument
def _on_doorbell_message(self, client, userdata, msg):
Expand Down Expand Up @@ -209,6 +208,7 @@ def tail(file_path, lines=25):
lines = file.readlines()[-lines:]
return [line.strip() for line in lines]


if __name__ == "__main__":
import mqtt_agent

Expand All @@ -218,4 +218,4 @@ def tail(file_path, lines=25):
[],
)
)
web_server.run()
web_server.run()

0 comments on commit 96f431a

Please sign in to comment.