2022-07-25 23:51:30 +01:00
|
|
|
import json
|
2022-10-24 22:42:16 +01:00
|
|
|
import logging
|
2024-01-05 09:57:08 +00:00
|
|
|
import threading
|
2022-07-25 23:51:30 +01:00
|
|
|
|
2024-01-05 09:57:08 +00:00
|
|
|
from websocket import WebSocketConnectionClosedException, WebSocketTimeoutException
|
2023-06-19 17:45:49 +01:00
|
|
|
|
2022-07-25 23:51:30 +01:00
|
|
|
from .baseclient import ObsClient
|
|
|
|
from .callback import Callback
|
2023-06-28 17:56:56 +01:00
|
|
|
from .error import OBSSDKError, OBSSDKTimeoutError
|
2022-12-05 16:41:34 +00:00
|
|
|
from .subs import Subs
|
2022-07-25 23:51:30 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
A class to interact with obs-websocket events
|
|
|
|
defined in official github repo
|
|
|
|
https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#events
|
|
|
|
"""
|
|
|
|
|
2023-06-22 22:17:20 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2022-07-27 19:39:33 +01:00
|
|
|
|
2022-07-29 02:42:44 +01:00
|
|
|
class EventClient:
|
2022-07-25 23:51:30 +01:00
|
|
|
def __init__(self, **kwargs):
|
2023-06-22 22:17:20 +01:00
|
|
|
self.logger = logger.getChild(self.__class__.__name__)
|
2022-10-26 11:08:24 +01:00
|
|
|
defaultkwargs = {"subs": Subs.LOW_VOLUME}
|
2022-07-27 19:49:37 +01:00
|
|
|
kwargs = defaultkwargs | kwargs
|
2022-07-25 23:51:30 +01:00
|
|
|
self.base_client = ObsClient(**kwargs)
|
2023-06-28 17:56:56 +01:00
|
|
|
try:
|
|
|
|
success = self.base_client.authenticate()
|
|
|
|
self.logger.info(
|
|
|
|
f"Successfully identified {self} with the server using RPC version:{success['negotiatedRpcVersion']}"
|
|
|
|
)
|
|
|
|
except OBSSDKError as e:
|
|
|
|
self.logger.error(f"{type(e).__name__}: {e}")
|
|
|
|
raise
|
2022-07-25 23:51:30 +01:00
|
|
|
self.callback = Callback()
|
2022-07-27 20:49:45 +01:00
|
|
|
self.subscribe()
|
2022-07-25 23:51:30 +01:00
|
|
|
|
2024-01-05 09:57:08 +00:00
|
|
|
def __enter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, exc_traceback):
|
|
|
|
self.disconnect()
|
|
|
|
|
2022-10-25 05:28:50 +01:00
|
|
|
def __repr__(self):
|
2022-12-05 18:18:10 +00:00
|
|
|
return type(
|
|
|
|
self
|
2023-06-19 17:45:49 +01:00
|
|
|
).__name__ + "(host='{host}', port={port}, password='{password}', subs={subs}, timeout={timeout})".format(
|
2022-12-05 18:18:10 +00:00
|
|
|
**self.base_client.__dict__,
|
2022-12-05 16:41:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def __str__(self):
|
2022-10-25 05:28:50 +01:00
|
|
|
return type(self).__name__
|
|
|
|
|
2022-07-27 20:49:45 +01:00
|
|
|
def subscribe(self):
|
2024-01-07 11:19:33 +00:00
|
|
|
self.base_client.ws.settimeout(None)
|
2024-01-05 09:57:08 +00:00
|
|
|
stop_event = threading.Event()
|
|
|
|
self.worker = threading.Thread(
|
|
|
|
target=self.trigger, daemon=True, args=(stop_event,)
|
|
|
|
)
|
|
|
|
self.worker.start()
|
2022-07-25 23:51:30 +01:00
|
|
|
|
2024-01-05 09:57:08 +00:00
|
|
|
def trigger(self, stop_event):
|
2022-07-25 23:51:30 +01:00
|
|
|
"""
|
|
|
|
Continuously listen for events.
|
|
|
|
|
|
|
|
Triggers a callback on event received.
|
|
|
|
"""
|
2024-01-05 09:57:08 +00:00
|
|
|
while not stop_event.is_set():
|
2023-06-19 17:45:49 +01:00
|
|
|
try:
|
2024-01-05 09:57:08 +00:00
|
|
|
if r := self.base_client.ws.recv():
|
|
|
|
event = json.loads(r)
|
|
|
|
self.logger.debug(f"Event received {event}")
|
|
|
|
type_, data = (
|
|
|
|
event["d"].get("eventType"),
|
|
|
|
event["d"].get("eventData"),
|
|
|
|
)
|
|
|
|
self.callback.trigger(type_, data if data else {})
|
2023-06-19 17:45:49 +01:00
|
|
|
except WebSocketTimeoutException as e:
|
|
|
|
self.logger.exception(f"{type(e).__name__}: {e}")
|
|
|
|
raise OBSSDKTimeoutError("Timeout while waiting for event") from e
|
2024-01-07 12:35:20 +00:00
|
|
|
except (WebSocketConnectionClosedException, OSError) as e:
|
2024-01-05 09:57:08 +00:00
|
|
|
self.logger.debug(f"{type(e).__name__} terminating the event thread")
|
|
|
|
stop_event.set()
|
|
|
|
|
|
|
|
def disconnect(self):
|
|
|
|
"""stop listening for events"""
|
2022-07-25 23:51:30 +01:00
|
|
|
|
2022-09-26 10:58:02 +01:00
|
|
|
self.base_client.ws.close()
|
2024-01-05 09:57:08 +00:00
|
|
|
self.worker.join()
|
|
|
|
|
|
|
|
unsubscribe = disconnect
|