2024-03-23 17:11:32 +01:00
|
|
|
"""Facilitates communication between processes."""
|
|
|
|
|
|
|
|
import zmq
|
|
|
|
|
|
|
|
from frigate.events.types import EventStateEnum, EventTypeEnum
|
|
|
|
|
|
|
|
SOCKET_PUSH_PULL = "ipc:///tmp/cache/events"
|
2024-04-09 01:19:45 +02:00
|
|
|
SOCKET_PUSH_PULL_END = "ipc:///tmp/cache/events_ended"
|
2024-03-23 17:11:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
class EventUpdatePublisher:
|
|
|
|
"""Publishes events (objects, audio, manual)."""
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
self.context = zmq.Context()
|
|
|
|
self.socket = self.context.socket(zmq.PUSH)
|
|
|
|
self.socket.connect(SOCKET_PUSH_PULL)
|
|
|
|
|
|
|
|
def publish(
|
|
|
|
self, payload: tuple[EventTypeEnum, EventStateEnum, str, dict[str, any]]
|
|
|
|
) -> None:
|
|
|
|
"""There is no communication back to the processes."""
|
2024-07-24 16:58:23 +02:00
|
|
|
self.socket.send_json(payload)
|
2024-03-23 17:11:32 +01:00
|
|
|
|
|
|
|
def stop(self) -> None:
|
|
|
|
self.socket.close()
|
|
|
|
self.context.destroy()
|
|
|
|
|
|
|
|
|
|
|
|
class EventUpdateSubscriber:
|
|
|
|
"""Receives event updates."""
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
self.context = zmq.Context()
|
|
|
|
self.socket = self.context.socket(zmq.PULL)
|
|
|
|
self.socket.bind(SOCKET_PUSH_PULL)
|
|
|
|
|
|
|
|
def check_for_update(
|
|
|
|
self, timeout=1
|
|
|
|
) -> tuple[EventTypeEnum, EventStateEnum, str, dict[str, any]]:
|
2024-04-09 01:19:45 +02:00
|
|
|
"""Returns events or None if no update."""
|
|
|
|
try:
|
|
|
|
has_update, _, _ = zmq.select([self.socket], [], [], timeout)
|
|
|
|
|
|
|
|
if has_update:
|
2024-07-24 16:58:23 +02:00
|
|
|
return self.socket.recv_json()
|
2024-04-09 01:19:45 +02:00
|
|
|
except zmq.ZMQError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
def stop(self) -> None:
|
|
|
|
self.socket.close()
|
|
|
|
self.context.destroy()
|
|
|
|
|
|
|
|
|
|
|
|
class EventEndPublisher:
|
|
|
|
"""Publishes events that have ended."""
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
self.context = zmq.Context()
|
|
|
|
self.socket = self.context.socket(zmq.PUSH)
|
|
|
|
self.socket.connect(SOCKET_PUSH_PULL_END)
|
|
|
|
|
|
|
|
def publish(
|
|
|
|
self, payload: tuple[EventTypeEnum, EventStateEnum, str, dict[str, any]]
|
|
|
|
) -> None:
|
|
|
|
"""There is no communication back to the processes."""
|
2024-07-24 16:58:23 +02:00
|
|
|
self.socket.send_json(payload)
|
2024-04-09 01:19:45 +02:00
|
|
|
|
|
|
|
def stop(self) -> None:
|
|
|
|
self.socket.close()
|
|
|
|
self.context.destroy()
|
|
|
|
|
|
|
|
|
|
|
|
class EventEndSubscriber:
|
|
|
|
"""Receives events that have ended."""
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
self.context = zmq.Context()
|
|
|
|
self.socket = self.context.socket(zmq.PULL)
|
|
|
|
self.socket.bind(SOCKET_PUSH_PULL_END)
|
|
|
|
|
|
|
|
def check_for_update(
|
|
|
|
self, timeout=1
|
|
|
|
) -> tuple[EventTypeEnum, EventStateEnum, str, dict[str, any]]:
|
|
|
|
"""Returns events ended or None if no update."""
|
2024-03-23 17:11:32 +01:00
|
|
|
try:
|
|
|
|
has_update, _, _ = zmq.select([self.socket], [], [], timeout)
|
|
|
|
|
|
|
|
if has_update:
|
2024-07-24 16:58:23 +02:00
|
|
|
return self.socket.recv_json()
|
2024-03-23 17:11:32 +01:00
|
|
|
except zmq.ZMQError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
def stop(self) -> None:
|
|
|
|
self.socket.close()
|
|
|
|
self.context.destroy()
|