Skip to content

Commit

Permalink
Generalize BackendBase so it can be subclassed for other device types
Browse files Browse the repository at this point in the history
  • Loading branch information
nocarryr committed Feb 23, 2017
1 parent fc8012c commit e5dbb91
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 42 deletions.
2 changes: 1 addition & 1 deletion vidhubcontrol/backends/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .base import BackendBase, Preset
from .base import VidhubBackendBase, Preset
from .dummy import DummyBackend
from .telnet import TelnetBackend
78 changes: 41 additions & 37 deletions vidhubcontrol/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,20 @@
from pydispatch.properties import ListProperty, DictProperty

class BackendBase(Dispatcher):
crosspoints = ListProperty()
output_labels = ListProperty()
input_labels = ListProperty()
crosspoint_control = ListProperty()
output_label_control = ListProperty()
input_label_control = ListProperty()
presets = ListProperty()
device_name = Property()
device_model = Property()
device_id = Property()
device_version = Property()
num_outputs = Property(0)
num_inputs = Property(0)
connected = Property(False)
running = Property(False)
prelude_parsed = Property(False)
_events_ = ['on_preset_added', 'on_preset_stored', 'on_preset_active']
def __init__(self, **kwargs):
self.device_name = kwargs.get('device_name')
self.client = None
self.event_loop = kwargs.get('event_loop', asyncio.get_event_loop())
self.bind(
num_outputs=self.on_num_outputs,
num_inputs=self.on_num_inputs,
output_labels=self.on_prop_feedback,
input_labels=self.on_prop_feedback,
crosspoints=self.on_prop_feedback,
output_label_control=self.on_prop_control,
input_label_control=self.on_prop_control,
crosspoint_control=self.on_prop_control,
device_id=self.on_device_id,
)
self.bind(device_id=self.on_device_id)
if self.device_id is None:
self.device_id = kwargs.get('device_id')
presets = kwargs.get('presets', [])
for pst_data in presets:
pst_data['backend'] = self
preset = Preset(**pst_data)
self.presets.append(preset)
preset.bind(
on_preset_stored=self.on_preset_stored,
active=self.on_preset_active,
)
self.connect_fut = asyncio.ensure_future(self.connect(), loop=self.event_loop)
@classmethod
async def create_async(cls, **kwargs):
obj = cls(**kwargs)
Expand Down Expand Up @@ -78,6 +48,46 @@ async def do_disconnect(self):
raise NotImplementedError()
async def get_status(self):
raise NotImplementedError()
def on_device_id(self, instance, value, **kwargs):
if value is None:
return
if self.device_name is None:
self.device_name = value
self.unbind(self.on_device_id)

class VidhubBackendBase(BackendBase):
crosspoints = ListProperty()
output_labels = ListProperty()
input_labels = ListProperty()
crosspoint_control = ListProperty()
output_label_control = ListProperty()
input_label_control = ListProperty()
presets = ListProperty()
num_outputs = Property(0)
num_inputs = Property(0)
_events_ = ['on_preset_added', 'on_preset_stored', 'on_preset_active']
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.bind(
num_outputs=self.on_num_outputs,
num_inputs=self.on_num_inputs,
output_labels=self.on_prop_feedback,
input_labels=self.on_prop_feedback,
crosspoints=self.on_prop_feedback,
output_label_control=self.on_prop_control,
input_label_control=self.on_prop_control,
crosspoint_control=self.on_prop_control,
)
presets = kwargs.get('presets', [])
for pst_data in presets:
pst_data['backend'] = self
preset = Preset(**pst_data)
self.presets.append(preset)
preset.bind(
on_preset_stored=self.on_preset_stored,
active=self.on_preset_active,
)
self.connect_fut = asyncio.ensure_future(self.connect(), loop=self.event_loop)
async def set_crosspoint(self, out_idx, in_idx):
raise NotImplementedError()
async def set_crosspoints(self, *args):
Expand Down Expand Up @@ -157,12 +167,6 @@ def on_prop_control(self, instance, value, **kwargs):
coro = getattr(self, coro_name)
args = [(key, value[key]) for key in keys]
tx_fut = asyncio.run_coroutine_threadsafe(coro(*args), loop=self.event_loop)
def on_device_id(self, instance, value, **kwargs):
if value is None:
return
if self.device_name is None:
self.device_name = value
self.unbind(self.on_device_id)

class Preset(Dispatcher):
name = Property()
Expand Down
4 changes: 2 additions & 2 deletions vidhubcontrol/backends/dummy.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import asyncio
import logging

from . import BackendBase
from . import VidhubBackendBase

class DummyBackend(BackendBase):
class DummyBackend(VidhubBackendBase):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.device_id = kwargs.get('device_id', 'dummy')
Expand Down
4 changes: 2 additions & 2 deletions vidhubcontrol/backends/telnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
from pydispatch import Property

from vidhubcontrol import aiotelnetlib
from . import BackendBase
from . import VidhubBackendBase

logger = logging.getLogger(__name__)

class TelnetBackend(BackendBase):
class TelnetBackend(VidhubBackendBase):
DEFAULT_PORT = 9990
SECTION_NAMES = [
'PROTOCOL PREAMBLE:',
Expand Down

0 comments on commit e5dbb91

Please sign in to comment.