mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2026-01-29 00:51:53 +08:00
optional msd and atx
This commit is contained in:
@@ -103,7 +103,7 @@ def _as_pin(pin: int) -> int:
|
||||
|
||||
|
||||
def _as_optional_pin(pin: int) -> int:
|
||||
if not isinstance(pin, int) or pin == 0:
|
||||
if not isinstance(pin, int) or pin < -1:
|
||||
raise ValueError("Invalid optional pin number")
|
||||
return pin
|
||||
|
||||
@@ -168,11 +168,13 @@ def _get_config_scheme() -> Dict:
|
||||
},
|
||||
|
||||
"atx": {
|
||||
"power_led_pin": Option(0, type=_as_pin),
|
||||
"hdd_led_pin": Option(0, type=_as_pin),
|
||||
"enabled": Option(True),
|
||||
|
||||
"power_led_pin": Option(-1, type=_as_optional_pin),
|
||||
"hdd_led_pin": Option(-1, type=_as_optional_pin),
|
||||
"power_switch_pin": Option(-1, type=_as_optional_pin),
|
||||
"reset_switch_pin": Option(-1, type=_as_optional_pin),
|
||||
|
||||
"power_switch_pin": Option(0, type=_as_pin),
|
||||
"reset_switch_pin": Option(0, type=_as_pin),
|
||||
"click_delay": Option(0.1),
|
||||
"long_click_delay": Option(5.5),
|
||||
|
||||
@@ -180,9 +182,12 @@ def _get_config_scheme() -> Dict:
|
||||
},
|
||||
|
||||
"msd": {
|
||||
"target_pin": Option(0, type=_as_pin),
|
||||
"reset_pin": Option(0, type=_as_pin),
|
||||
"device": Option("", type=_as_path, rename="device_path"),
|
||||
"enabled": Option(True),
|
||||
|
||||
"target_pin": Option(-1, type=_as_optional_pin),
|
||||
"reset_pin": Option(-1, type=_as_optional_pin),
|
||||
|
||||
"device": Option("", type=_as_optional_path, rename="device_path"),
|
||||
"init_delay": Option(2.0),
|
||||
"reset_delay": Option(1.0),
|
||||
"write_meta": Option(True),
|
||||
@@ -190,8 +195,8 @@ def _get_config_scheme() -> Dict:
|
||||
},
|
||||
|
||||
"streamer": {
|
||||
"cap_pin": Option(-1, type=_as_optional_pin),
|
||||
"conv_pin": Option(-1, type=_as_optional_pin),
|
||||
"cap_pin": Option(0, type=_as_optional_pin),
|
||||
"conv_pin": Option(0, type=_as_optional_pin),
|
||||
|
||||
"sync_delay": Option(1.0),
|
||||
"init_delay": Option(1.0),
|
||||
|
||||
@@ -23,7 +23,9 @@
|
||||
import asyncio
|
||||
|
||||
from typing import Dict
|
||||
from typing import Callable
|
||||
from typing import AsyncGenerator
|
||||
from typing import Any
|
||||
|
||||
from ...logging import get_logger
|
||||
|
||||
@@ -32,13 +34,36 @@ from ... import gpio
|
||||
|
||||
|
||||
# =====
|
||||
class AtxIsBusy(aioregion.RegionIsBusyError):
|
||||
class AtxError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class AtxOperationError(AtxError):
|
||||
pass
|
||||
|
||||
|
||||
class AtxDisabledError(AtxOperationError):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("ATX is disabled")
|
||||
|
||||
|
||||
class AtxIsBusyError(AtxOperationError, aioregion.RegionIsBusyError):
|
||||
pass
|
||||
|
||||
|
||||
def _atx_working(method: Callable) -> Callable:
|
||||
async def wrap(self: "Atx", *args: Any, **kwargs: Any) -> Any:
|
||||
if not self._enabled: # pylint: disable=protected-access
|
||||
raise AtxDisabledError()
|
||||
return (await method(self, *args, **kwargs))
|
||||
return wrap
|
||||
|
||||
|
||||
class Atx: # pylint: disable=too-many-instance-attributes
|
||||
def __init__(
|
||||
self,
|
||||
enabled: bool,
|
||||
|
||||
power_led_pin: int,
|
||||
hdd_led_pin: int,
|
||||
|
||||
@@ -50,31 +75,43 @@ class Atx: # pylint: disable=too-many-instance-attributes
|
||||
state_poll: float,
|
||||
) -> None:
|
||||
|
||||
self.__power_led_pin = gpio.set_input(power_led_pin)
|
||||
self.__hdd_led_pin = gpio.set_input(hdd_led_pin)
|
||||
self._enabled = enabled
|
||||
|
||||
if self._enabled:
|
||||
self.__power_led_pin = gpio.set_input(power_led_pin)
|
||||
self.__hdd_led_pin = gpio.set_input(hdd_led_pin)
|
||||
self.__power_switch_pin = gpio.set_output(power_switch_pin)
|
||||
self.__reset_switch_pin = gpio.set_output(reset_switch_pin)
|
||||
else:
|
||||
self.__power_led_pin = -1
|
||||
self.__hdd_led_pin = -1
|
||||
self.__power_switch_pin = -1
|
||||
self.__reset_switch_pin = -1
|
||||
|
||||
self.__power_switch_pin = gpio.set_output(power_switch_pin)
|
||||
self.__reset_switch_pin = gpio.set_output(reset_switch_pin)
|
||||
self.__click_delay = click_delay
|
||||
self.__long_click_delay = long_click_delay
|
||||
|
||||
self.__state_poll = state_poll
|
||||
|
||||
self.__region = aioregion.AioExclusiveRegion(AtxIsBusy)
|
||||
self.__region = aioregion.AioExclusiveRegion(AtxIsBusyError)
|
||||
|
||||
def get_state(self) -> Dict:
|
||||
return {
|
||||
"enabled": self._enabled,
|
||||
"busy": self.__region.is_busy(),
|
||||
"leds": {
|
||||
"power": (not gpio.read(self.__power_led_pin)),
|
||||
"hdd": (not gpio.read(self.__hdd_led_pin)),
|
||||
"power": ((not gpio.read(self.__power_led_pin)) if self._enabled else False),
|
||||
"hdd": ((not gpio.read(self.__hdd_led_pin)) if self._enabled else False),
|
||||
},
|
||||
}
|
||||
|
||||
async def poll_state(self) -> AsyncGenerator[Dict, None]:
|
||||
while True:
|
||||
yield self.get_state()
|
||||
await asyncio.sleep(self.__state_poll)
|
||||
if self._enabled:
|
||||
yield self.get_state()
|
||||
await asyncio.sleep(self.__state_poll)
|
||||
else:
|
||||
await asyncio.sleep(60)
|
||||
|
||||
async def click_power(self) -> None:
|
||||
get_logger().info("Clicking power ...")
|
||||
@@ -88,6 +125,7 @@ class Atx: # pylint: disable=too-many-instance-attributes
|
||||
get_logger().info("Clicking reset")
|
||||
await self.__click(self.__reset_switch_pin, self.__click_delay)
|
||||
|
||||
@_atx_working
|
||||
async def __click(self, pin: int, delay: float) -> None:
|
||||
self.__region.enter()
|
||||
asyncio.ensure_future(self.__inner_click(pin, delay))
|
||||
|
||||
@@ -54,9 +54,14 @@ class MsdOperationError(MsdError):
|
||||
pass
|
||||
|
||||
|
||||
class MsdIsNotOperationalError(MsdOperationError):
|
||||
class MsdDisabledError(MsdOperationError):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Missing path for mass-storage device")
|
||||
super().__init__("Mass-storage device is disabled")
|
||||
|
||||
|
||||
class MsdOfflineError(MsdOperationError):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Mass-storage device is not found")
|
||||
|
||||
|
||||
class MsdAlreadyConnectedToPcError(MsdOperationError):
|
||||
@@ -69,7 +74,7 @@ class MsdAlreadyConnectedToKvmError(MsdOperationError):
|
||||
super().__init__("Mass-storage is already connected to KVM")
|
||||
|
||||
|
||||
class MsdIsNotConnectedToKvmError(MsdOperationError):
|
||||
class MsdNotConnectedToKvmError(MsdOperationError):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Mass-storage is not connected to KVM")
|
||||
|
||||
@@ -177,10 +182,12 @@ def _explore_device(device_path: str) -> Optional[_MassStorageDeviceInfo]:
|
||||
)
|
||||
|
||||
|
||||
def _msd_operated(method: Callable) -> Callable:
|
||||
def _msd_working(method: Callable) -> Callable:
|
||||
async def wrap(self: "MassStorageDevice", *args: Any, **kwargs: Any) -> Any:
|
||||
if not self._enabled: # pylint: disable=protected-access
|
||||
raise MsdDisabledError()
|
||||
if not self._device_path: # pylint: disable=protected-access
|
||||
MsdIsNotOperationalError()
|
||||
raise MsdOfflineError()
|
||||
return (await method(self, *args, **kwargs))
|
||||
return wrap
|
||||
|
||||
@@ -189,6 +196,8 @@ def _msd_operated(method: Callable) -> Callable:
|
||||
class MassStorageDevice: # pylint: disable=too-many-instance-attributes
|
||||
def __init__(
|
||||
self,
|
||||
enabled: bool,
|
||||
|
||||
target_pin: int,
|
||||
reset_pin: int,
|
||||
|
||||
@@ -201,8 +210,15 @@ class MassStorageDevice: # pylint: disable=too-many-instance-attributes
|
||||
loop: asyncio.AbstractEventLoop,
|
||||
) -> None:
|
||||
|
||||
self.__target_pin = gpio.set_output(target_pin)
|
||||
self.__reset_pin = gpio.set_output(reset_pin)
|
||||
self._enabled = enabled
|
||||
|
||||
if self._enabled:
|
||||
self.__target_pin = gpio.set_output(target_pin)
|
||||
self.__reset_pin = gpio.set_output(reset_pin)
|
||||
assert bool(device_path)
|
||||
else:
|
||||
self.__target_pin = -1
|
||||
self.__reset_pin = -1
|
||||
|
||||
self._device_path = device_path
|
||||
self.__init_delay = init_delay
|
||||
@@ -221,7 +237,7 @@ class MassStorageDevice: # pylint: disable=too-many-instance-attributes
|
||||
self.__state_queue: asyncio.queues.Queue = asyncio.Queue()
|
||||
|
||||
logger = get_logger(0)
|
||||
if self._device_path:
|
||||
if self._enabled:
|
||||
logger.info("Using %r as mass-storage device", self._device_path)
|
||||
try:
|
||||
logger.info("Enabled image metadata writing")
|
||||
@@ -231,10 +247,10 @@ class MassStorageDevice: # pylint: disable=too-many-instance-attributes
|
||||
log = logger.error
|
||||
else:
|
||||
log = logger.exception
|
||||
log("Mass-storage device is not operational: %s", err)
|
||||
log("Mass-storage device is offline: %s", err)
|
||||
self._device_path = ""
|
||||
else:
|
||||
logger.warning("Mass-storage device is not operational")
|
||||
logger.info("Mass-storage device is disabled")
|
||||
|
||||
def get_state(self) -> Dict:
|
||||
info = (self.__saved_device_info._asdict() if self.__saved_device_info else None)
|
||||
@@ -243,11 +259,12 @@ class MassStorageDevice: # pylint: disable=too-many-instance-attributes
|
||||
info["image"] = (info["image"]._asdict() if info["image"] else None)
|
||||
|
||||
connected_to: Optional[str] = None
|
||||
if self._device_path:
|
||||
if self._enabled and self._device_path:
|
||||
connected_to = ("kvm" if self.__device_info else "server")
|
||||
|
||||
return {
|
||||
"in_operate": bool(self._device_path),
|
||||
"enabled": self._enabled,
|
||||
"online": (self._enabled and bool(self._device_path)),
|
||||
"connected_to": connected_to,
|
||||
"busy": bool(self.__device_file),
|
||||
"written": self.__written,
|
||||
@@ -256,14 +273,18 @@ class MassStorageDevice: # pylint: disable=too-many-instance-attributes
|
||||
|
||||
async def poll_state(self) -> AsyncGenerator[Dict, None]:
|
||||
while True:
|
||||
yield (await self.__state_queue.get())
|
||||
if self._enabled:
|
||||
yield (await self.__state_queue.get())
|
||||
else:
|
||||
await asyncio.sleep(60)
|
||||
|
||||
async def cleanup(self) -> None:
|
||||
await self.__close_device_file()
|
||||
gpio.write(self.__target_pin, False)
|
||||
gpio.write(self.__reset_pin, False)
|
||||
if self._enabled:
|
||||
await self.__close_device_file()
|
||||
gpio.write(self.__target_pin, False)
|
||||
gpio.write(self.__reset_pin, False)
|
||||
|
||||
@_msd_operated
|
||||
@_msd_working
|
||||
async def connect_to_kvm(self, no_delay: bool=False) -> Dict:
|
||||
with self.__region:
|
||||
if self.__device_info:
|
||||
@@ -277,7 +298,7 @@ class MassStorageDevice: # pylint: disable=too-many-instance-attributes
|
||||
get_logger().info("Mass-storage device switched to KVM: %s", self.__device_info)
|
||||
return state
|
||||
|
||||
@_msd_operated
|
||||
@_msd_working
|
||||
async def connect_to_pc(self) -> Dict:
|
||||
with self.__region:
|
||||
if not self.__device_info:
|
||||
@@ -289,7 +310,7 @@ class MassStorageDevice: # pylint: disable=too-many-instance-attributes
|
||||
get_logger().info("Mass-storage device switched to Server")
|
||||
return state
|
||||
|
||||
@_msd_operated
|
||||
@_msd_working
|
||||
async def reset(self) -> None:
|
||||
with self.__region:
|
||||
get_logger().info("Mass-storage device reset")
|
||||
@@ -298,12 +319,12 @@ class MassStorageDevice: # pylint: disable=too-many-instance-attributes
|
||||
gpio.write(self.__reset_pin, False)
|
||||
await self.__state_queue.put(self.get_state())
|
||||
|
||||
@_msd_operated
|
||||
@_msd_working
|
||||
async def __aenter__(self) -> "MassStorageDevice":
|
||||
self.__region.enter()
|
||||
try:
|
||||
if not self.__device_info:
|
||||
raise MsdIsNotConnectedToKvmError()
|
||||
raise MsdNotConnectedToKvmError()
|
||||
self.__device_file = await aiofiles.open(self.__device_info.path, mode="w+b", buffering=0)
|
||||
self.__written = 0
|
||||
return self
|
||||
|
||||
@@ -51,6 +51,7 @@ from .auth import AuthManager
|
||||
from .info import InfoManager
|
||||
from .logreader import LogReader
|
||||
from .hid import Hid
|
||||
from .atx import AtxOperationError
|
||||
from .atx import Atx
|
||||
from .msd import MsdOperationError
|
||||
from .msd import MassStorageDevice
|
||||
@@ -148,7 +149,7 @@ def _exposed(http_method: str, path: str, auth_required: bool=True) -> Callable:
|
||||
|
||||
except RegionIsBusyError as err:
|
||||
return _json_exception(err, 409)
|
||||
except (BadRequestError, MsdOperationError) as err:
|
||||
except (BadRequestError, AtxOperationError, MsdOperationError) as err:
|
||||
return _json_exception(err, 400)
|
||||
except UnauthorizedError as err:
|
||||
return _json_exception(err, 401)
|
||||
|
||||
@@ -63,8 +63,8 @@ class Streamer: # pylint: disable=too-many-instance-attributes
|
||||
loop: asyncio.AbstractEventLoop,
|
||||
) -> None:
|
||||
|
||||
self.__cap_pin = (gpio.set_output(cap_pin) if cap_pin > 0 else cap_pin)
|
||||
self.__conv_pin = (gpio.set_output(conv_pin) if conv_pin > 0 else conv_pin)
|
||||
self.__cap_pin = (gpio.set_output(cap_pin) if cap_pin > 0 else 0)
|
||||
self.__conv_pin = (gpio.set_output(conv_pin) if conv_pin > 0 else 0)
|
||||
|
||||
self.__sync_delay = sync_delay
|
||||
self.__init_delay = init_delay
|
||||
|
||||
Reference in New Issue
Block a user