diff --git a/vban_cmd/base.py b/vban_cmd/base.py index 1adaa12..80e35e1 100644 --- a/vban_cmd/base.py +++ b/vban_cmd/base.py @@ -1,4 +1,3 @@ -import dataclasses import socket import time from abc import ABCMeta, abstractmethod @@ -6,6 +5,7 @@ from enum import IntEnum from threading import Thread from typing import Iterable, NoReturn, Optional, Union +from .misc import Event from .packet import ( HEADER_SIZE, RegisterRTHeader, @@ -48,7 +48,8 @@ class VbanCmd(metaclass=ABCMeta): ) self.running = True self.subject = Subject() - self.cache = {} + self.cache = dict() + self.event = Event(self.subs) @abstractmethod def __str__(self): @@ -187,6 +188,7 @@ class VbanCmd(metaclass=ABCMeta): pass def _updates(self) -> NoReturn: + print(f"Listening for {', '.join(self.event.get())} events") self.cache["strip_level"], self.cache["bus_level"] = self._get_levels( self.public_packet ) @@ -197,13 +199,13 @@ class VbanCmd(metaclass=ABCMeta): self._strip_buf, self._bus_buf = self._get_levels(self._pp) self._pdirty = self._pp.pdirty(self.public_packet) - if self.ldirty: + if self.event.ldirty and self.ldirty: self.cache["strip_level"] = self._strip_buf self.cache["bus_level"] = self._bus_buf self.subject.notify("ldirty") if self.public_packet != self._pp: self._public_packet = self._pp - if self.pdirty: + if self.event.pdirty and self.pdirty: self.subject.notify("pdirty") elapsed = time.time() - start if self.ratelimit - elapsed > 0: diff --git a/vban_cmd/factory.py b/vban_cmd/factory.py index 90419fe..f669416 100644 --- a/vban_cmd/factory.py +++ b/vban_cmd/factory.py @@ -58,6 +58,9 @@ class FactoryBase(VbanCmd): """Base class for factories, subclasses VbanCmd.""" def __init__(self, kind_id: str, **kwargs): + defaultsubs = {"pdirty": True, "ldirty": False} + if "subs" in kwargs: + defaultsubs = defaultsubs | kwargs.pop("subs") defaultkwargs = { "ip": None, "port": 6980, @@ -66,6 +69,7 @@ class FactoryBase(VbanCmd): "channel": 0, "ratelimit": 0.01, "sync": False, + "subs": defaultsubs, } kwargs = defaultkwargs | kwargs self.kind = kindmap(kind_id) diff --git a/vban_cmd/misc.py b/vban_cmd/misc.py new file mode 100644 index 0000000..9e23b48 --- /dev/null +++ b/vban_cmd/misc.py @@ -0,0 +1,40 @@ +class Event: + def __init__(self, subs: dict): + self.subs = subs + + def info(self, msg): + info = ( + f"{msg} events", + f"Now listening for {', '.join(self.get())} events", + ) + print("\n".join(info)) + + @property + def pdirty(self): + return self.subs["pdirty"] + + @property + def mdirty(self): + return self.subs["mdirty"] + + @property + def midi(self): + return self.subs["midi"] + + @property + def ldirty(self): + return self.subs["ldirty"] + + def get(self) -> list: + return [k for k, v in self.subs.items() if v] + + def any(self) -> bool: + return any(self.subs.values()) + + def add(self, event): + self.subs[event] = True + self.info(f"{event} added to") + + def remove(self, event): + self.subs[event] = False + self.info(f"{event} removed from")