Skip to content

Commit

Permalink
Fix alert snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
tropicoo committed Apr 17, 2022
1 parent 793ec2f commit 7443f01
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
24 changes: 11 additions & 13 deletions hikcamerabot/handlers/event_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import abc
import logging
import os
from typing import Optional, TYPE_CHECKING
from io import BytesIO
from typing import Optional, TYPE_CHECKING, Union

from pyrogram.types import Message
from tenacity import retry, wait_fixed
Expand Down Expand Up @@ -113,35 +114,32 @@ async def _handle(self, event: dict) -> None:
detection_type: Detection = event['detection_type']
alert_count: int = event['alert_count']
resized: bool = event['resized']
photo = event['img']
photo: BytesIO = event['img']
trigger_name: str = DETECTION_SWITCH_MAP[detection_type]['name'].value

caption = f'[{cam.description}] {trigger_name} at {date_} ' \
f'(alert #{alert_count}) {cam.hashtag}\n/cmds_{cam.id}, /list_cams'

async def send_document(photo_: str) -> Message:
if not isinstance(photo_, str):
photo_.filename = f'Full_alert_snapshot_{date_}.jpg'
async def send_document(photo_: Union[BytesIO, str]) -> Message:
return await self._bot.send_document(
chat_id=uid,
document=photo_,
file_name=f'Full_alert_snapshot_{date_}.jpg',
caption=caption)

async def send_photo(photo_: str) -> Message:
async def send_photo(photo_: Union[BytesIO, str]) -> Message:
return await self._bot.send_photo(chat_id=uid, photo=photo_,
caption=caption)

cached = False
cached_id: Optional[str] = None
for uid in self._bot.user_ids:
try:
if resized:
message = await send_photo(photo)
file_id: str = message.photo[0]['file_id']
message = await send_photo(cached_id or photo)
cached_id = message.photo.file_id
else:
message = await send_document(photo)
file_id: str = message.document.file_id
if not cached:
photo = file_id
message = await send_document(cached_id or photo)
cached_id = message.document.file_id
except Exception:
self._log.exception('Failed to send message to user ID %s', uid)

Expand Down
17 changes: 8 additions & 9 deletions hikcamerabot/services/tasks/alarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,17 @@ async def _process_chunks(self) -> None:

if detection_type:
self.service.increase_alert_count()
await self._send_alerts(detection_type)
self._send_alerts(detection_type)
wait_before = int(time.time()) + self.service.alert_delay
else:
raise ChunkLoopError

async def _send_alerts(self, detection_type: Detection) -> None:
def _send_alerts(self, detection_type: Detection) -> None:
self._log.info('Sending alerts for %s', detection_type)
await self._alert_notifier.notify(detection_type)
# TODO: Put to queue and await everything, don't schedule tasks.
self._alert_notifier.notify(detection_type)


# ------------------------------------------------------------------------------ #

class AbstractAlertNotificationTask(metaclass=abc.ABCMeta):

def __init__(self, service: 'AlarmService',
Expand Down Expand Up @@ -120,10 +119,10 @@ async def _run(self) -> None:
class AlarmPicNotificationTask(AbstractAlertNotificationTask):

async def _run(self) -> None:
if self._cam.conf.alert[self._detection_type].sendpic:
await self._take_pic()
if self._cam.conf.alert[self._detection_type.value].sendpic:
await self._send_pic()

async def _take_pic(self) -> None:
async def _send_pic(self) -> None:
resize = not self._cam.conf.alert[self._detection_type.value].fullpic
photo, ts = await self._cam.take_snapshot(resize=resize)
await self._result_queue.put({
Expand All @@ -148,7 +147,7 @@ def __init__(self, service: 'AlarmService') -> None:
self._log = logging.getLogger(self.__class__.__name__)
self._service = service

async def notify(self, detection_type: Detection) -> None:
def notify(self, detection_type: Detection) -> None:
for task_cls in self.ALARM_NOTIFICATION_TASKS:
task = task_cls(service=self._service, detection_type=detection_type)
create_task(
Expand Down

0 comments on commit 7443f01

Please sign in to comment.