diff options
author | Devaev Maxim <[email protected]> | 2019-04-27 22:09:58 +0300 |
---|---|---|
committer | Devaev Maxim <[email protected]> | 2019-04-27 22:09:58 +0300 |
commit | 380b1d15e3c8fcb7cc324cebf209bfe38cbb4011 (patch) | |
tree | fe09dc5deffef71099df0acc9f983ebeaa1c492f /kvmd | |
parent | d4c597e945de5e80b6f98bc64d4faa66ceddf70b (diff) |
highlevel atx api
Diffstat (limited to 'kvmd')
-rw-r--r-- | kvmd/apps/kvmd/atx.py | 38 | ||||
-rw-r--r-- | kvmd/apps/kvmd/server.py | 12 | ||||
-rw-r--r-- | kvmd/validators/kvm.py | 4 |
3 files changed, 53 insertions, 1 deletions
diff --git a/kvmd/apps/kvmd/atx.py b/kvmd/apps/kvmd/atx.py index a255611d..72269c23 100644 --- a/kvmd/apps/kvmd/atx.py +++ b/kvmd/apps/kvmd/atx.py @@ -125,19 +125,55 @@ class Atx: # pylint: disable=too-many-instance-attributes else: await asyncio.sleep(60) + # ===== + + @_atx_working + async def power_on(self) -> bool: + if not self.get_state()["leds"]["power"]: + await self.click_power() + return True + return False + + @_atx_working + async def power_off(self) -> bool: + if self.get_state()["leds"]["power"]: + await self.click_power_long() + return True + return False + + @_atx_working + async def power_off_soft(self) -> bool: + if self.get_state()["leds"]["power"]: + await self.click_power() + return True + return False + + @_atx_working + async def power_reset(self) -> bool: + if self.get_state()["leds"]["power"]: + await self.click_reset() + return True + return False + + # ===== + + @_atx_working async def click_power(self) -> None: get_logger().info("Clicking power ...") await self.__click(self.__power_switch_pin, self.__click_delay) + @_atx_working async def click_power_long(self) -> None: get_logger().info("Clicking power (long press) ...") await self.__click(self.__power_switch_pin, self.__long_click_delay) + @_atx_working async def click_reset(self) -> None: 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)) diff --git a/kvmd/apps/kvmd/server.py b/kvmd/apps/kvmd/server.py index c8e686db..5afddad1 100644 --- a/kvmd/apps/kvmd/server.py +++ b/kvmd/apps/kvmd/server.py @@ -52,6 +52,7 @@ from ...validators.auth import valid_user from ...validators.auth import valid_passwd from ...validators.auth import valid_auth_token +from ...validators.kvm import valid_atx_power_action from ...validators.kvm import valid_atx_button from ...validators.kvm import valid_kvm_target from ...validators.kvm import valid_log_seek @@ -430,6 +431,17 @@ class Server: # pylint: disable=too-many-instance-attributes async def __atx_state_handler(self, _: aiohttp.web.Request) -> aiohttp.web.Response: return _json(self.__atx.get_state()) + @_exposed("POST", "/atx/power") + async def __atx_power_handler(self, request: aiohttp.web.Request) -> aiohttp.web.Response: + action = valid_atx_power_action(request.query.get("action")) + done = await ({ + "on": self.__atx.power_on, + "off": self.__atx.power_off, + "off_soft": self.__atx.power_off_soft, + "reset": self.__atx.power_reset, + }[action])() + return _json({"action": action, "done": done}) + @_exposed("POST", "/atx/click") async def __atx_click_handler(self, request: aiohttp.web.Request) -> aiohttp.web.Response: button = valid_atx_button(request.query.get("button")) diff --git a/kvmd/validators/kvm.py b/kvmd/validators/kvm.py index 40f5ced1..09bd7c2b 100644 --- a/kvmd/validators/kvm.py +++ b/kvmd/validators/kvm.py @@ -30,6 +30,10 @@ from .basic import valid_number # ===== +def valid_atx_power_action(arg: Any) -> str: + return check_string_in_list(arg, "ATX power action", ["on", "off", "off_soft", "reset"]) + + def valid_atx_button(arg: Any) -> str: return check_string_in_list(arg, "ATX button", ["power", "power_long", "reset"]) |