Skip to content

Commit

Permalink
fix https error handling in mjpeg camera
Browse files Browse the repository at this point in the history
  • Loading branch information
nlef committed Nov 24, 2024
1 parent 3230247 commit 8d06a7a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
33 changes: 19 additions & 14 deletions bot/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from ffmpegcv import FFmpegReader
from ffmpegcv.stream_info import get_info # type: ignore
import httpx
from httpx import HTTPError
import numpy
from numpy import ndarray
from telegram import Message
Expand Down Expand Up @@ -646,24 +647,28 @@ def _rotate_img(self, img: Image.Image) -> Image.Image:

@cam_light_toggle
def take_photo(self, ndarr: ndarray = None, force_rotate: bool = True) -> BytesIO:
# Todo: speedup coonections?
response = httpx.get(f"{self._host_snapshot}", timeout=5, verify=False)
bio = BytesIO()

os_nice(15)
if response.is_success and response.headers["Content-Type"] == "image/jpeg":
try:
# Todo: speedup coonections?
response = httpx.get(f"{self._host_snapshot}", timeout=5, verify=False)

if force_rotate:
img = self._rotate_img(Image.open(BytesIO(response.content)).convert("RGB"))
img.save(bio, format="JPEG")
img.close()
del img
os_nice(15)
if response.is_success and response.headers["Content-Type"] == "image/jpeg":

if force_rotate:
img = self._rotate_img(Image.open(BytesIO(response.content)).convert("RGB"))
img.save(bio, format="JPEG")
img.close()
del img
else:
bio.write(response.content)
else:
bio.write(response.content)
else:
logger.error("Streamer snapshot get failed\n\n%s", response.status_code)
with open("../imgs/nosignal.png", "rb") as file:
bio.write(file.read())
response.raise_for_status()
except HTTPError as err:
logger.error("Streamer snapshot get failed\n%s", err)
with Image.open("../imgs/nosignal.png").convert("RGB") as img:
img.save(bio, format="JPEG")

os_nice(0)
bio.seek(0)
Expand Down
2 changes: 1 addition & 1 deletion bot/websocket_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ async def notify_status_update(self, message_params):
if "toolhead" in message_params_loc and "position" in message_params_loc["toolhead"]:
# position_z = json_message["params"][0]['toolhead']['position'][2]
pass
if "gcode_move" in message_params_loc and "position" in message_params_loc["gcode_move"]:
if "gcode_move" in message_params_loc and "gcode_position" in message_params_loc["gcode_move"]:
position_z = message_params_loc["gcode_move"]["gcode_position"][2]
self._klippy.printing_height = position_z
self._notifier.schedule_notification(position_z=int(position_z))
Expand Down
2 changes: 1 addition & 1 deletion scripts/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ anyio==4.6.2.post1
emoji==2.14.0
ffmpegcv==0.3.15
idna==3.10
orjson==3.10.11
orjson==3.10.12
Pillow==11.0.0 ; python_version>='3.10'
Pillow==10.4.0 ; python_version<='3.9'
psutil==6.1.0
Expand Down

0 comments on commit 8d06a7a

Please sign in to comment.