Keep sending chat action

This commit is contained in:
Davide Depau 2024-08-11 21:00:41 +02:00
parent f49141ccf2
commit d60dad1c87

22
bot.py
View file

@ -68,6 +68,7 @@ class Bot:
self, self,
adjust_perspective=True, adjust_perspective=True,
timeout: float = 10.0, timeout: float = 10.0,
chat_action_fn=None,
) -> List[cv2.typing.MatLike]: ) -> List[cv2.typing.MatLike]:
privacy_mode = self.tapo.getPrivacyMode() privacy_mode = self.tapo.getPrivacyMode()
@ -99,9 +100,11 @@ class Bot:
aruco_params = aruco.DetectorParameters() aruco_params = aruco.DetectorParameters()
t0 = time.time() t0 = time.time()
last_chat_action = 0
print("Taking image with ArUco markers...") print("Taking image with ArUco markers...")
while len(aruco_corners) < 4: while len(aruco_corners) < 4:
if time.time() - t0 > timeout: delta = time.time() - t0
if delta > timeout:
a = self.last_aruco_corners.copy() a = self.last_aruco_corners.copy()
a.update(aruco_corners) a.update(aruco_corners)
aruco_corners = a aruco_corners = a
@ -117,6 +120,11 @@ class Bot:
self.tapo.setDayNightMode("auto") self.tapo.setDayNightMode("auto")
return [pretty_image] 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() ret, annotated_image = vcap.read()
if not ret: if not ret:
continue continue
@ -189,9 +197,15 @@ class Bot:
return [warped, annotated_image] return [warped, annotated_image]
async def _photo_command(self, chat_id: int, adjust_perspective: bool = True): async def _photo_command(self, chat_id: int, adjust_perspective: bool = True):
async def chat_action_fn():
await self.bot.send_chat_action(chat_id=chat_id, action="upload_photo") 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] jpegs = [cv2.imencode(".jpg", photo)[1].tobytes() for photo in photos]
media = [ media = [
@ -319,7 +333,7 @@ class Bot:
) )
async def take_photo( 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]: ) -> List[cv2.typing.MatLike]:
item_state = await self._get_item_state() item_state = await self._get_item_state()
if item_state == "OFF": if item_state == "OFF":
@ -328,7 +342,7 @@ class Bot:
try: try:
return await asyncio.get_event_loop().run_in_executor( 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: finally:
if item_state == "OFF": if item_state == "OFF":