From d60dad1c87a0179e28f0fcfa7868ef8537d658c0 Mon Sep 17 00:00:00 2001 From: Davide Depau Date: Sun, 11 Aug 2024 21:00:41 +0200 Subject: [PATCH] Keep sending chat action --- bot.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/bot.py b/bot.py index 1f6c417..c43b9f6 100644 --- a/bot.py +++ b/bot.py @@ -68,6 +68,7 @@ class Bot: self, adjust_perspective=True, timeout: float = 10.0, + chat_action_fn=None, ) -> List[cv2.typing.MatLike]: privacy_mode = self.tapo.getPrivacyMode() @@ -99,9 +100,11 @@ class Bot: aruco_params = aruco.DetectorParameters() t0 = time.time() + last_chat_action = 0 print("Taking image with ArUco markers...") while len(aruco_corners) < 4: - if time.time() - t0 > timeout: + delta = time.time() - t0 + if delta > timeout: a = self.last_aruco_corners.copy() a.update(aruco_corners) aruco_corners = a @@ -117,6 +120,11 @@ class Bot: self.tapo.setDayNightMode("auto") return [pretty_image] + if delta - last_chat_action > 3: + last_chat_action = delta + if chat_action_fn: + chat_action_fn() + ret, annotated_image = vcap.read() if not ret: continue @@ -189,9 +197,15 @@ class Bot: return [warped, annotated_image] async def _photo_command(self, chat_id: int, adjust_perspective: bool = True): - await self.bot.send_chat_action(chat_id=chat_id, action="upload_photo") + async def chat_action_fn(): + await self.bot.send_chat_action(chat_id=chat_id, action="upload_photo") - photos = await self.take_photo(adjust_perspective=adjust_perspective) + await chat_action_fn() + + photos = await self.take_photo( + adjust_perspective=adjust_perspective, + chat_action_fn=lambda: asyncio.run(chat_action_fn()) + ) jpegs = [cv2.imencode(".jpg", photo)[1].tobytes() for photo in photos] media = [ @@ -319,7 +333,7 @@ class Bot: ) async def take_photo( - self, adjust_perspective=True, timeout=10.0 + self, adjust_perspective=True, timeout=10.0, chat_action_fn=None ) -> List[cv2.typing.MatLike]: item_state = await self._get_item_state() if item_state == "OFF": @@ -328,7 +342,7 @@ class Bot: try: return await asyncio.get_event_loop().run_in_executor( - self.executor, self._take_photo_blocking, adjust_perspective, timeout + self.executor, self._take_photo_blocking, adjust_perspective, timeout, chat_action_fn ) finally: if item_state == "OFF":