2024-02-19 14:26:59 +01:00
|
|
|
"""Facilitates communication between processes."""
|
|
|
|
|
|
|
|
from enum import Enum
|
|
|
|
from typing import Optional
|
|
|
|
|
2024-06-21 23:30:19 +02:00
|
|
|
from .zmq_proxy import Publisher, Subscriber
|
2024-02-19 14:26:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
class DetectionTypeEnum(str, Enum):
|
|
|
|
all = ""
|
2024-04-02 15:07:50 +02:00
|
|
|
api = "api"
|
2024-02-19 14:26:59 +01:00
|
|
|
video = "video"
|
|
|
|
audio = "audio"
|
|
|
|
|
|
|
|
|
2024-06-21 23:30:19 +02:00
|
|
|
class DetectionPublisher(Publisher):
|
2024-02-19 14:26:59 +01:00
|
|
|
"""Simplifies receiving video and audio detections."""
|
|
|
|
|
2024-06-21 23:30:19 +02:00
|
|
|
topic_base = "detection/"
|
2024-02-19 14:26:59 +01:00
|
|
|
|
|
|
|
def __init__(self, topic: DetectionTypeEnum) -> None:
|
2024-06-21 23:30:19 +02:00
|
|
|
topic = topic.value
|
|
|
|
super().__init__(topic)
|
2024-02-19 14:26:59 +01:00
|
|
|
|
|
|
|
|
2024-06-21 23:30:19 +02:00
|
|
|
class DetectionSubscriber(Subscriber):
|
|
|
|
"""Simplifies receiving video and audio detections."""
|
2024-02-19 14:26:59 +01:00
|
|
|
|
2024-06-21 23:30:19 +02:00
|
|
|
topic_base = "detection/"
|
2024-02-19 14:26:59 +01:00
|
|
|
|
2024-06-21 23:30:19 +02:00
|
|
|
def __init__(self, topic: DetectionTypeEnum) -> None:
|
|
|
|
topic = topic.value
|
|
|
|
super().__init__(topic)
|
|
|
|
|
|
|
|
def check_for_update(
|
|
|
|
self, timeout: float = None
|
|
|
|
) -> Optional[tuple[DetectionTypeEnum, any]]:
|
|
|
|
return super().check_for_update(timeout)
|
|
|
|
|
|
|
|
def _return_object(self, topic: str, payload: any) -> any:
|
|
|
|
if payload is None:
|
|
|
|
return (None, None)
|
|
|
|
return (DetectionTypeEnum[topic[len(self.topic_base) :]], payload)
|