refactoring

This commit is contained in:
Devaev Maxim 2020-12-21 01:59:49 +03:00
parent 2cb210c975
commit 42475809fc
6 changed files with 43 additions and 65 deletions

View File

@ -60,14 +60,9 @@ class MsdApi:
await self.__msd.set_params(**params) # type: ignore await self.__msd.set_params(**params) # type: ignore
return make_json_response() return make_json_response()
@exposed_http("POST", "/msd/connect") @exposed_http("POST", "/msd/set_connected")
async def __connect_handler(self, _: Request) -> Response: async def __set_connected_handler(self, request: Request) -> Response:
await self.__msd.connect() await self.__msd.set_connected(valid_bool(request.query.get("connected")))
return make_json_response()
@exposed_http("POST", "/msd/disconnect")
async def __disconnect_handler(self, _: Request) -> Response:
await self.__msd.disconnect()
return make_json_response() return make_json_response()
@exposed_http("POST", "/msd/write") @exposed_http("POST", "/msd/write")

View File

@ -109,10 +109,7 @@ class BaseMsd(BasePlugin):
async def set_params(self, name: Optional[str]=None, cdrom: Optional[bool]=None) -> None: async def set_params(self, name: Optional[str]=None, cdrom: Optional[bool]=None) -> None:
raise NotImplementedError() raise NotImplementedError()
async def connect(self) -> None: async def set_connected(self, connected: bool) -> None:
raise NotImplementedError()
async def disconnect(self) -> None:
raise NotImplementedError() raise NotImplementedError()
@contextlib.asynccontextmanager @contextlib.asynccontextmanager

View File

@ -66,10 +66,7 @@ class Plugin(BaseMsd):
async def set_params(self, name: Optional[str]=None, cdrom: Optional[bool]=None) -> None: async def set_params(self, name: Optional[str]=None, cdrom: Optional[bool]=None) -> None:
raise MsdDisabledError() raise MsdDisabledError()
async def connect(self) -> None: async def set_connected(self, connected: bool) -> None:
raise MsdDisabledError()
async def disconnect(self) -> None:
raise MsdDisabledError() raise MsdDisabledError()
@contextlib.asynccontextmanager @contextlib.asynccontextmanager

View File

@ -255,10 +255,10 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
self.__state.vd.cdrom = cdrom self.__state.vd.cdrom = cdrom
@aiotools.atomic @aiotools.atomic
async def connect(self) -> None: async def set_connected(self, connected: bool) -> None:
async with self.__state.busy(): async with self.__state.busy():
assert self.__state.vd assert self.__state.vd
if connected:
if self.__state.vd.connected or self.__drive.get_image_path(): if self.__state.vd.connected or self.__drive.get_image_path():
raise MsdConnectedError() raise MsdConnectedError()
if self.__state.vd.image is None: if self.__state.vd.image is None:
@ -272,19 +272,15 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
await self.__unlock_drive() await self.__unlock_drive()
self.__drive.set_cdrom_flag(self.__state.vd.cdrom) self.__drive.set_cdrom_flag(self.__state.vd.cdrom)
self.__drive.set_image_path(self.__state.vd.image.path) self.__drive.set_image_path(self.__state.vd.image.path)
self.__state.vd.connected = True
@aiotools.atomic
async def disconnect(self) -> None:
async with self.__state.busy():
assert self.__state.vd
else:
if not (self.__state.vd.connected or self.__drive.get_image_path()): if not (self.__state.vd.connected or self.__drive.get_image_path()):
raise MsdDisconnectedError() raise MsdDisconnectedError()
await self.__unlock_drive() await self.__unlock_drive()
self.__drive.set_image_path("") self.__drive.set_image_path("")
self.__state.vd.connected = False
self.__state.vd.connected = connected
@contextlib.asynccontextmanager @contextlib.asynccontextmanager
async def write_image(self, name: str) -> AsyncGenerator[None, None]: async def write_image(self, name: str) -> AsyncGenerator[None, None]:

View File

@ -176,23 +176,17 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
raise MsdCdromNotSupported() raise MsdCdromNotSupported()
@aiotools.atomic @aiotools.atomic
async def connect(self) -> None: async def set_connected(self, connected: bool) -> None:
async with self.__working(): async with self.__working():
async with self.__region: async with self.__region:
if connected:
if self.__connected: if self.__connected:
raise MsdConnectedError() raise MsdConnectedError()
self.__gpio.switch_to_server() self.__gpio.switch_to_server()
self.__connected = True
get_logger(0).info("MSD switched to Server") get_logger(0).info("MSD switched to Server")
else:
@aiotools.atomic
async def disconnect(self) -> None:
async with self.__working():
async with self.__region:
if not self.__connected: if not self.__connected:
raise MsdDisconnectedError() raise MsdDisconnectedError()
self.__gpio.switch_to_local() self.__gpio.switch_to_local()
try: try:
await self.__load_device_info() await self.__load_device_info()
@ -200,8 +194,8 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
if self.__connected: if self.__connected:
self.__gpio.switch_to_server() self.__gpio.switch_to_server()
raise raise
self.__connected = False
get_logger(0).info("MSD switched to KVM: %s", self.__device_info) get_logger(0).info("MSD switched to KVM: %s", self.__device_info)
self.__connected = connected
@contextlib.asynccontextmanager @contextlib.asynccontextmanager
async def write_image(self, name: str) -> AsyncGenerator[None, None]: async def write_image(self, name: str) -> AsyncGenerator[None, None]:

View File

@ -138,9 +138,8 @@ export function Msd() {
tools.progressSetValue($("msd-uploading-progress"), "Aborted", 0); tools.progressSetValue($("msd-uploading-progress"), "Aborted", 0);
}; };
var __clickConnectButton = function(connect) { var __clickConnectButton = function(connected) {
let action = (connect ? "connect" : "disconnect"); let http = tools.makeRequest("POST", `/api/msd/set_connected?connected=${connected}`, function() {
let http = tools.makeRequest("POST", `/api/msd/${action}`, function() {
if (http.readyState === 4) { if (http.readyState === 4) {
if (http.status !== 200) { if (http.status !== 200) {
wm.error("Switch error:<br>", http.responseText); wm.error("Switch error:<br>", http.responseText);
@ -149,7 +148,7 @@ export function Msd() {
__applyState(); __applyState();
}); });
__applyState(); __applyState();
wm.switchEnabled($(`msd-${action}-button`), false); wm.switchEnabled($(`msd-${connected ? "connect" : "disconnect"}-button`), false);
}; };
var __selectNewImageFile = function() { var __selectNewImageFile = function() {