2022-07-25 23:51:30 +01:00
|
|
|
import json
|
2022-10-24 22:42:16 +01:00
|
|
|
import logging
|
2022-07-25 23:51:30 +01:00
|
|
|
import time
|
2022-07-27 19:39:33 +01:00
|
|
|
from enum import IntEnum
|
2022-07-25 23:51:30 +01:00
|
|
|
from threading import Thread
|
|
|
|
|
|
|
|
from .baseclient import ObsClient
|
|
|
|
from .callback import Callback
|
|
|
|
|
|
|
|
"""
|
|
|
|
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
|
|
|
|
"""
|
|
|
|
|
2022-07-27 19:39:33 +01:00
|
|
|
Subs = IntEnum(
|
|
|
|
"Subs",
|
|
|
|
"general config scenes inputs transitions filters outputs sceneitems mediainputs vendors ui",
|
|
|
|
start=0,
|
|
|
|
)
|
2022-07-25 23:51:30 +01:00
|
|
|
|
2022-07-27 19:39:33 +01:00
|
|
|
|
2022-07-29 02:42:44 +01:00
|
|
|
class EventClient:
|
2022-10-24 22:42:16 +01:00
|
|
|
logger = logging.getLogger("events.eventclient")
|
2022-07-25 23:51:30 +01:00
|
|
|
DELAY = 0.001
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
2022-07-28 10:00:24 +01:00
|
|
|
defaultkwargs = {
|
|
|
|
"subs": (
|
|
|
|
(1 << Subs.general)
|
|
|
|
| (1 << Subs.config)
|
|
|
|
| (1 << Subs.scenes)
|
|
|
|
| (1 << Subs.inputs)
|
|
|
|
| (1 << Subs.transitions)
|
|
|
|
| (1 << Subs.filters)
|
|
|
|
| (1 << Subs.outputs)
|
|
|
|
| (1 << Subs.sceneitems)
|
|
|
|
| (1 << Subs.mediainputs)
|
|
|
|
| (1 << Subs.vendors)
|
|
|
|
| (1 << Subs.ui)
|
|
|
|
)
|
|
|
|
}
|
2022-07-27 19:49:37 +01:00
|
|
|
kwargs = defaultkwargs | kwargs
|
2022-07-25 23:51:30 +01:00
|
|
|
self.base_client = ObsClient(**kwargs)
|
2022-10-24 22:42:16 +01:00
|
|
|
if self.base_client.authenticate():
|
|
|
|
self.logger.info("Successfully identified client with the server")
|
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
|
|
|
|
2022-07-27 20:49:45 +01:00
|
|
|
def subscribe(self):
|
2022-07-25 23:51:30 +01:00
|
|
|
worker = Thread(target=self.trigger, daemon=True)
|
|
|
|
worker.start()
|
|
|
|
|
|
|
|
def trigger(self):
|
|
|
|
"""
|
|
|
|
Continuously listen for events.
|
|
|
|
|
|
|
|
Triggers a callback on event received.
|
|
|
|
"""
|
2022-07-27 20:49:45 +01:00
|
|
|
self.running = True
|
2022-07-25 23:51:30 +01:00
|
|
|
while self.running:
|
2022-10-24 22:42:16 +01:00
|
|
|
event = json.loads(self.base_client.ws.recv())
|
|
|
|
self.logger.debug(f"Event received {event}")
|
|
|
|
type_, data = (
|
|
|
|
event["d"].get("eventType"),
|
|
|
|
event["d"].get("eventData"),
|
2022-07-28 11:55:05 +01:00
|
|
|
)
|
2022-10-24 22:42:16 +01:00
|
|
|
self.callback.trigger(type_, data if data else {})
|
2022-07-25 23:51:30 +01:00
|
|
|
time.sleep(self.DELAY)
|
|
|
|
|
|
|
|
def unsubscribe(self):
|
|
|
|
"""
|
|
|
|
stop listening for events
|
|
|
|
"""
|
|
|
|
self.running = False
|
2022-09-26 10:58:02 +01:00
|
|
|
self.base_client.ws.close()
|