xair-api-python/xair_api/config.py

208 lines
5.5 KiB
Python
Raw Normal View History

2022-04-05 20:05:55 +01:00
import abc
import logging
2024-02-08 13:44:55 +00:00
from . import kinds, util
2022-04-05 20:05:55 +01:00
from .meta import bool_prop
logger = logging.getLogger(__name__)
2022-04-05 20:05:55 +01:00
class IConfig(abc.ABC):
"""Abstract Base Class for config"""
def __init__(self, remote):
self._remote = remote
self.logger = logger.getChild(self.__class__.__name__)
2022-04-05 20:05:55 +01:00
def getter(self, param: str):
2025-01-15 10:54:52 +00:00
return self._remote.query(f'{self.address}/{param}')
2022-04-05 20:05:55 +01:00
def setter(self, param: str, val: int):
2025-01-15 10:54:52 +00:00
self._remote.send(f'{self.address}/{param}', val)
2022-04-05 20:05:55 +01:00
@abc.abstractmethod
def address(self):
pass
class Config(IConfig):
"""Concrete class for config"""
@classmethod
def make(cls, remote):
"""
Factory function for Config
Returns a Config class of a kind.
"""
LINKS_cls = _make_links_mixins[remote.kind.id_]
2025-01-15 10:54:52 +00:00
MUTEGROUP_cls = type('MuteGroup', (Config.MuteGroup, cls), {})
MONITOR_cls = type('ConfigMonitor', (Config.Monitor, cls), {})
2022-04-05 20:05:55 +01:00
CONFIG_cls = type(
2025-01-15 10:54:52 +00:00
f'Config{remote.kind}',
2022-04-05 20:05:55 +01:00
(cls, LINKS_cls),
{
2025-01-15 10:54:52 +00:00
'mute_group': tuple(MUTEGROUP_cls(remote, i) for i in range(4)),
'monitor': MONITOR_cls(remote),
},
2022-04-05 20:05:55 +01:00
)
return CONFIG_cls(remote)
@property
def address(self) -> str:
2025-01-15 10:54:52 +00:00
return '/config'
2022-04-05 20:05:55 +01:00
@property
def amixenable(self) -> bool:
2025-01-15 10:54:52 +00:00
return self.getter('mute')[0] == 1
2022-04-05 20:05:55 +01:00
@amixenable.setter
def amixenable(self, val: bool):
2025-01-15 10:54:52 +00:00
self.setter('amixenable', 1 if val else 0)
2022-04-05 20:05:55 +01:00
@property
def amixlock(self) -> bool:
2025-01-15 10:54:52 +00:00
return self.getter('amixlock')[0] == 1
2022-04-05 20:05:55 +01:00
@amixlock.setter
def amixlock(self, val: bool):
2025-01-15 10:54:52 +00:00
self.setter('amixlock', 1 if val else 0)
2022-04-05 20:05:55 +01:00
class MuteGroup:
def __init__(self, remote, i):
super(Config.MuteGroup, self).__init__(remote)
self.i = i + 1
2022-04-05 20:05:55 +01:00
@property
def address(self) -> str:
root = super(Config.MuteGroup, self).address
2025-01-15 10:54:52 +00:00
return f'{root}/mute'
@property
def on(self) -> bool:
2025-01-15 10:54:52 +00:00
return self.getter(f'{self.i}')[0] == 1
@on.setter
def on(self, val: bool):
2025-01-15 10:54:52 +00:00
self.setter(f'{self.i}', 1 if val else 0)
2022-04-05 20:05:55 +01:00
class Monitor:
@property
def address(self) -> str:
root = super(Config.Monitor, self).address
2025-01-15 10:54:52 +00:00
return f'{root}/solo'
2022-04-05 20:05:55 +01:00
@property
2024-02-08 15:27:15 +00:00
@util.db_from
2022-04-05 20:05:55 +01:00
def level(self) -> float:
2025-01-15 10:54:52 +00:00
return self.getter('level')[0]
2022-04-05 20:05:55 +01:00
@level.setter
2024-02-08 15:27:15 +00:00
@util.db_to
2022-04-05 20:05:55 +01:00
def level(self, val: float):
2025-01-15 10:54:52 +00:00
self.setter('level', val)
2022-04-05 20:05:55 +01:00
@property
def source(self) -> int:
2025-01-15 10:54:52 +00:00
return int(self.getter('source')[0])
2022-04-05 20:05:55 +01:00
@source.setter
def source(self, val: int):
2025-01-15 10:54:52 +00:00
self.setter('source', val)
2022-04-05 20:05:55 +01:00
@property
def sourcetrim(self) -> float:
2025-01-15 10:54:52 +00:00
return round(util.lin_get(-18, 18, self.getter('sourcetrim')[0]), 1)
2022-04-05 20:05:55 +01:00
@sourcetrim.setter
def sourcetrim(self, val: float):
if not -18 <= val <= 18:
self.logger.warning(
2025-01-15 10:54:52 +00:00
f'sourcetrim got {val}, expected value in range -18.0 to 18.0'
)
2025-01-15 10:54:52 +00:00
self.setter('sourcetrim', util.lin_set(-18, 18, val))
2022-04-05 20:05:55 +01:00
@property
def chmode(self) -> bool:
2025-01-15 10:54:52 +00:00
return self.getter('chmode')[0] == 1
2022-04-05 20:05:55 +01:00
@chmode.setter
def chmode(self, val: bool):
2025-01-15 10:54:52 +00:00
self.setter('chmode', 1 if val else 0)
2022-04-05 20:05:55 +01:00
@property
def busmode(self) -> bool:
2025-01-15 10:54:52 +00:00
return self.getter('busmode')[0] == 1
2022-04-05 20:05:55 +01:00
@busmode.setter
def busmode(self, val: bool):
2025-01-15 10:54:52 +00:00
self.setter('busmode', 1 if val else 0)
2022-04-05 20:05:55 +01:00
@property
def dimgain(self) -> int:
2025-01-15 10:54:52 +00:00
return int(util.lin_get(-40, 0, self.getter('dimatt')[0]))
2022-04-05 20:05:55 +01:00
@dimgain.setter
def dimgain(self, val: int):
if not -40 <= val <= 0:
self.logger.warning(
2025-01-15 10:54:52 +00:00
f'dimgain got {val}, expected value in range -40 to 0'
)
2025-01-15 10:54:52 +00:00
self.setter('dimatt', util.lin_set(-40, 0, val))
2022-04-05 20:05:55 +01:00
@property
def dim(self) -> bool:
2025-01-15 10:54:52 +00:00
return self.getter('dim')[0] == 1
2022-04-05 20:05:55 +01:00
@dim.setter
def dim(self, val: bool):
2025-01-15 10:54:52 +00:00
self.setter('dim', 1 if val else 0)
2022-04-05 20:05:55 +01:00
@property
def mono(self) -> bool:
2025-01-15 10:54:52 +00:00
return self.getter('mono')[0] == 1
2022-04-05 20:05:55 +01:00
@mono.setter
def mono(self, val: bool):
2025-01-15 10:54:52 +00:00
self.setter('mono', 1 if val else 0)
2022-04-05 20:05:55 +01:00
@property
def mute(self) -> bool:
2025-01-15 10:54:52 +00:00
return self.getter('mute')[0] == 1
2022-04-05 20:05:55 +01:00
@mute.setter
def mute(self, val: bool):
2025-01-15 10:54:52 +00:00
self.setter('mute', 1 if val else 0)
2022-04-05 20:05:55 +01:00
@property
def dimfpl(self) -> bool:
2025-01-15 10:54:52 +00:00
return self.getter('dimfpl')[0] == 1
2022-04-05 20:05:55 +01:00
@dimfpl.setter
def dimfpl(self, val: bool):
2025-01-15 10:54:52 +00:00
self.setter('dimfpl', 1 if val else 0)
2022-04-05 20:05:55 +01:00
def _make_links_mixin(kind):
"""Creates a links mixin"""
return type(
2025-01-15 10:54:52 +00:00
f'Links{kind}',
2022-04-05 20:05:55 +01:00
(),
{
2025-01-15 10:54:52 +00:00
'link_eq': bool_prop('linkcfg/eq'),
'link_dyn': bool_prop('linkcfg/dyn'),
'link_fader_mute': bool_prop('linkcfg/fdrmute'),
2022-04-05 20:05:55 +01:00
**{
2025-01-15 10:54:52 +00:00
f'chlink{i}_{i+1}': bool_prop(f'chlink/{i}-{i+1}')
2022-04-05 20:05:55 +01:00
for i in range(1, kind.num_strip, 2)
},
**{
2025-01-15 10:54:52 +00:00
f'buslink{i}_{i+1}': bool_prop(f'buslink/{i}-{i+1}')
2022-04-05 20:05:55 +01:00
for i in range(1, kind.num_bus, 2)
},
},
)
_make_links_mixins = {kind.id_: _make_links_mixin(kind) for kind in kinds.all}