diff options
author | Devaev Maxim <[email protected]> | 2019-09-25 03:15:20 +0300 |
---|---|---|
committer | Devaev Maxim <[email protected]> | 2019-09-25 03:15:20 +0300 |
commit | 5c4e8f7962fe89ee0dcdea9ba859f7045fa2d6d8 (patch) | |
tree | 785b2454f56480464d18c71c2221b262bb92eabb /kvmd/plugins | |
parent | 5d437c58e342bcb9847e419e46c751b3ed70e747 (diff) |
extended msd api for future otg
Diffstat (limited to 'kvmd/plugins')
-rw-r--r-- | kvmd/plugins/msd/__init__.py | 16 | ||||
-rw-r--r-- | kvmd/plugins/msd/disabled.py | 12 | ||||
-rw-r--r-- | kvmd/plugins/msd/relay.py | 66 |
3 files changed, 57 insertions, 37 deletions
diff --git a/kvmd/plugins/msd/__init__.py b/kvmd/plugins/msd/__init__.py index d5aa3064..bff60232 100644 --- a/kvmd/plugins/msd/__init__.py +++ b/kvmd/plugins/msd/__init__.py @@ -64,6 +64,11 @@ class MsdIsBusyError(MsdOperationError): super().__init__("Performing another MSD operation, please try again later") +class MsdMultiNotSupported(MsdOperationError): + def __init__(self) -> None: + super().__init__("This MSD does not support storing multiple images") + + # ===== class BaseMsd(BasePlugin): def get_state(self) -> Dict: @@ -73,22 +78,27 @@ class BaseMsd(BasePlugin): yield {} raise NotImplementedError + async def reset(self) -> None: + raise NotImplementedError + async def cleanup(self) -> None: pass + # ===== + async def connect(self) -> Dict: raise NotImplementedError async def disconnect(self) -> Dict: raise NotImplementedError - async def reset(self) -> None: + async def select(self, name: str) -> Dict: raise NotImplementedError - async def __aenter__(self) -> "BaseMsd": + async def remove(self, name: str) -> Dict: raise NotImplementedError - def get_chunk_size(self) -> int: + async def __aenter__(self) -> "BaseMsd": raise NotImplementedError async def write_image_info(self, name: str, complete: bool) -> None: diff --git a/kvmd/plugins/msd/disabled.py b/kvmd/plugins/msd/disabled.py index 56ed10db..5eb3083b 100644 --- a/kvmd/plugins/msd/disabled.py +++ b/kvmd/plugins/msd/disabled.py @@ -42,6 +42,7 @@ class Plugin(BaseMsd): def get_state(self) -> Dict: return { "enabled": False, + "multi": False, "online": False, "busy": False, "uploading": False, @@ -56,19 +57,24 @@ class Plugin(BaseMsd): yield self.get_state() await asyncio.sleep(60) + async def reset(self) -> None: + raise MsdDisabledError() + + # ===== + async def connect(self) -> Dict: raise MsdDisabledError() async def disconnect(self) -> Dict: raise MsdDisabledError() - async def reset(self) -> None: + async def select(self, name: str) -> Dict: raise MsdDisabledError() - async def __aenter__(self) -> BaseMsd: + async def remove(self, name: str) -> Dict: raise MsdDisabledError() - def get_chunk_size(self) -> int: + async def __aenter__(self) -> BaseMsd: raise MsdDisabledError() async def write_image_info(self, name: str, complete: bool) -> None: diff --git a/kvmd/plugins/msd/relay.py b/kvmd/plugins/msd/relay.py index 39818e95..c6b755fb 100644 --- a/kvmd/plugins/msd/relay.py +++ b/kvmd/plugins/msd/relay.py @@ -48,7 +48,6 @@ from ... import gpio from ...yamlconf import Option -from ...validators.basic import valid_number from ...validators.basic import valid_int_f1 from ...validators.basic import valid_float_f01 @@ -62,6 +61,7 @@ from . import MsdAlreadyConnectedError from . import MsdAlreadyDisconnectedError from . import MsdConnectedError from . import MsdIsBusyError +from . import MsdMultiNotSupported from . import BaseMsd @@ -170,7 +170,6 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes init_delay: float, init_retries: int, reset_delay: float, - chunk_size: int, ) -> None: self.__target_pin = gpio.set_output(target_pin) @@ -180,7 +179,6 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes self.__init_delay = init_delay self.__init_retries = init_retries self.__reset_delay = reset_delay - self.__chunk_size = chunk_size self.__region = aioregion.AioExclusiveRegion(MsdIsBusyError) @@ -209,8 +207,6 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes "init_delay": Option(1.0, type=valid_float_f01), "init_retries": Option(5, type=valid_int_f1), "reset_delay": Option(1.0, type=valid_float_f01), - - "chunk_size": Option(65536, type=(lambda arg: valid_number(arg, min=1024))), } def get_state(self) -> Dict: @@ -225,6 +221,7 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes current = dataclasses.asdict(self._device_info.image) return { "enabled": True, + "multi": False, "online": bool(self._device_info), "busy": self.__region.is_busy(), "uploading": bool(self.__device_file), @@ -239,11 +236,38 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes yield (await self.__state_queue.get()) @aiotools.atomic + async def reset(self) -> None: + with aiotools.unregion_only_on_exception(self.__region): + await self.__inner_reset() + + @aiotools.tasked + @aiotools.muted("Can't reset MSD or operation was not completed") + async def __inner_reset(self) -> None: + try: + gpio.write(self.__reset_pin, True) + await asyncio.sleep(self.__reset_delay) + gpio.write(self.__reset_pin, False) + + gpio.write(self.__target_pin, False) + self.__on_kvm = True + + await self.__load_device_info() + get_logger(0).info("MSD reset has been successful") + finally: + try: + gpio.write(self.__reset_pin, False) + finally: + self.__region.exit() + await self.__state_queue.put(self.get_state()) + + @aiotools.atomic async def cleanup(self) -> None: await self.__close_device_file() gpio.write(self.__target_pin, False) gpio.write(self.__reset_pin, False) + # ===== + @_msd_working @aiotools.atomic async def connect(self) -> Dict: @@ -292,30 +316,13 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes if notify: await self.__state_queue.put(state or self.get_state()) - @aiotools.atomic - async def reset(self) -> None: - with aiotools.unregion_only_on_exception(self.__region): - await self.__inner_reset() - - @aiotools.tasked - @aiotools.muted("Can't reset MSD or operation was not completed") - async def __inner_reset(self) -> None: - try: - gpio.write(self.__reset_pin, True) - await asyncio.sleep(self.__reset_delay) - gpio.write(self.__reset_pin, False) - - gpio.write(self.__target_pin, False) - self.__on_kvm = True + @_msd_working + async def select(self, name: str) -> Dict: + raise MsdMultiNotSupported() - await self.__load_device_info() - get_logger(0).info("MSD reset has been successful") - finally: - try: - gpio.write(self.__reset_pin, False) - finally: - self.__region.exit() - await self.__state_queue.put(self.get_state()) + @_msd_working + async def remove(self, name: str) -> Dict: + raise MsdMultiNotSupported() @_msd_working @aiotools.atomic @@ -334,9 +341,6 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes finally: await self.__state_queue.put(self.get_state()) - def get_chunk_size(self) -> int: - return self.__chunk_size - @aiotools.atomic async def write_image_info(self, name: str, complete: bool) -> None: assert self.__device_file |