msd: fixed notifications

This commit is contained in:
Devaev Maxim 2019-06-11 11:00:38 +03:00
parent b71977d29a
commit 8306b0e9a6

View File

@ -65,7 +65,7 @@ class MsdOfflineError(MsdOperationError):
super().__init__("Mass-storage device is not found") super().__init__("Mass-storage device is not found")
class MsdAlreadyConnectedToPcError(MsdOperationError): class MsdAlreadyConnectedToServerError(MsdOperationError):
def __init__(self) -> None: def __init__(self) -> None:
super().__init__("Mass-storage is already connected to Server") super().__init__("Mass-storage is already connected to Server")
@ -278,9 +278,13 @@ class MassStorageDevice: # pylint: disable=too-many-instance-attributes
@_msd_working @_msd_working
@aiotools.atomic @aiotools.atomic
async def connect_to_kvm(self) -> Dict: async def connect_to_kvm(self) -> Dict:
notify = False
state: Dict = {}
try:
with self.__region: with self.__region:
if self.__on_kvm: if self.__on_kvm:
raise MsdAlreadyConnectedToKvmError() raise MsdAlreadyConnectedToKvmError()
notify = True
gpio.write(self.__target_pin, False) gpio.write(self.__target_pin, False)
try: try:
@ -292,20 +296,32 @@ class MassStorageDevice: # pylint: disable=too-many-instance-attributes
self.__on_kvm = True self.__on_kvm = True
get_logger().info("Mass-storage device switched to KVM: %s", self._device_info) get_logger().info("Mass-storage device switched to KVM: %s", self._device_info)
return (await self.__queue_current_state()) state = self.get_state()
return state
finally:
if notify:
await self.__state_queue.put(state or self.get_state())
@_msd_working @_msd_working
@aiotools.atomic @aiotools.atomic
async def connect_to_pc(self) -> Dict: async def connect_to_pc(self) -> Dict:
notify = False
state: Dict = {}
try:
with self.__region: with self.__region:
if not self.__on_kvm: if not self.__on_kvm:
raise MsdAlreadyConnectedToPcError() raise MsdAlreadyConnectedToServerError()
notify = True
gpio.write(self.__target_pin, True) gpio.write(self.__target_pin, True)
self.__on_kvm = False self.__on_kvm = False
get_logger().info("Mass-storage device switched to Server") get_logger().info("Mass-storage device switched to Server")
return (await self.__queue_current_state()) state = self.get_state()
return state
finally:
if notify:
await self.__state_queue.put(state or self.get_state())
@aiotools.tasked @aiotools.tasked
@aiotools.atomic @aiotools.atomic
@ -315,9 +331,7 @@ class MassStorageDevice: # pylint: disable=too-many-instance-attributes
with self.__region: with self.__region:
if not self._enabled: if not self._enabled:
raise MsdDisabledError() raise MsdDisabledError()
notify = True notify = True
get_logger(0).info("Mass-storage device reset")
gpio.write(self.__reset_pin, True) gpio.write(self.__reset_pin, True)
await asyncio.sleep(self.__reset_delay) await asyncio.sleep(self.__reset_delay)
@ -327,9 +341,10 @@ class MassStorageDevice: # pylint: disable=too-many-instance-attributes
gpio.write(self.__reset_pin, False) gpio.write(self.__reset_pin, False)
await self.__load_device_info() await self.__load_device_info()
get_logger(0).info("Mass-storage device reset has been successful")
finally: finally:
if notify: if notify:
await self.__queue_current_state() await self.__state_queue.put(self.get_state())
@_msd_working @_msd_working
@aiotools.atomic @aiotools.atomic
@ -346,7 +361,7 @@ class MassStorageDevice: # pylint: disable=too-many-instance-attributes
self.__region.exit() self.__region.exit()
raise raise
finally: finally:
await self.__queue_current_state() await self.__state_queue.put(self.get_state())
@aiotools.atomic @aiotools.atomic
async def write_image_info(self, name: str, complete: bool) -> None: async def write_image_info(self, name: str, complete: bool) -> None:
@ -378,12 +393,7 @@ class MassStorageDevice: # pylint: disable=too-many-instance-attributes
await self.__load_device_info() await self.__load_device_info()
finally: finally:
self.__region.exit() self.__region.exit()
await self.__queue_current_state() await self.__state_queue.put(self.get_state())
async def __queue_current_state(self) -> Dict:
state = self.get_state()
await self.__state_queue.put(state)
return state
async def __write_to_device_file(self, data: bytes) -> None: async def __write_to_device_file(self, data: bytes) -> None:
assert self.__device_file assert self.__device_file