extended msd api for future otg

This commit is contained in:
Devaev Maxim
2019-09-25 03:15:20 +03:00
parent 5d437c58e3
commit 5c4e8f7962
7 changed files with 118 additions and 44 deletions

View File

@@ -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,24 +78,29 @@ 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 remove(self, name: str) -> Dict:
raise NotImplementedError
async def __aenter__(self) -> "BaseMsd":
raise NotImplementedError
def get_chunk_size(self) -> int:
raise NotImplementedError
async def write_image_info(self, name: str, complete: bool) -> None:
raise NotImplementedError

View File

@@ -42,6 +42,7 @@ class Plugin(BaseMsd):
def get_state(self) -> Dict:
return {
"enabled": False,
"multi": False,
"online": False,
"busy": False,
"uploading": False,
@@ -56,21 +57,26 @@ 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 remove(self, name: str) -> Dict:
raise MsdDisabledError()
async def __aenter__(self) -> BaseMsd:
raise MsdDisabledError()
def get_chunk_size(self) -> int:
raise MsdDisabledError()
async def write_image_info(self, name: str, complete: bool) -> None:
raise MsdDisabledError()

View File

@@ -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),
@@ -238,12 +235,39 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
while True:
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()
@_msd_working
async def select(self, name: str) -> Dict:
raise MsdMultiNotSupported()
@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())
@_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