mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2026-01-29 09:01:54 +08:00
ugpio plugins
This commit is contained in:
@@ -62,8 +62,8 @@ class Plugin(BaseAtx): # pylint: disable=too-many-instance-attributes
|
||||
|
||||
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)
|
||||
self.__power_switch_pin = gpio.set_output(power_switch_pin, False)
|
||||
self.__reset_switch_pin = gpio.set_output(reset_switch_pin, False)
|
||||
|
||||
self.__power_led_inverted = power_led_inverted
|
||||
self.__hdd_led_inverted = hdd_led_inverted
|
||||
@@ -75,7 +75,7 @@ class Plugin(BaseAtx): # pylint: disable=too-many-instance-attributes
|
||||
self.__region = aiotools.AioExclusiveRegion(AtxIsBusyError, self.__state_notifier)
|
||||
|
||||
self.__reader = gpio.BatchReader(
|
||||
pins=[self.__power_led_pin, self.__hdd_led_pin],
|
||||
pins=set([self.__power_led_pin, self.__hdd_led_pin]),
|
||||
interval=state_poll,
|
||||
notifier=self.__state_notifier,
|
||||
)
|
||||
|
||||
@@ -32,8 +32,8 @@ from .. import get_plugin_class
|
||||
|
||||
# =====
|
||||
class BaseHid(BasePlugin):
|
||||
def start(self) -> None:
|
||||
pass
|
||||
def sysprep(self) -> None:
|
||||
raise NotImplementedError
|
||||
|
||||
async def get_state(self) -> Dict:
|
||||
raise NotImplementedError
|
||||
|
||||
@@ -74,7 +74,7 @@ class Plugin(BaseHid):
|
||||
"noop": Option(False, type=valid_bool),
|
||||
}
|
||||
|
||||
def start(self) -> None:
|
||||
def sysprep(self) -> None:
|
||||
self.__keyboard_proc.start()
|
||||
self.__mouse_proc.start()
|
||||
|
||||
|
||||
@@ -175,7 +175,7 @@ class Plugin(BaseHid, multiprocessing.Process): # pylint: disable=too-many-inst
|
||||
|
||||
multiprocessing.Process.__init__(self, daemon=True)
|
||||
|
||||
self.__reset_pin = gpio.set_output(reset_pin)
|
||||
self.__reset_pin = gpio.set_output(reset_pin, False)
|
||||
self.__reset_delay = reset_delay
|
||||
|
||||
self.__device_path = device_path
|
||||
@@ -217,9 +217,9 @@ class Plugin(BaseHid, multiprocessing.Process): # pylint: disable=too-many-inst
|
||||
"noop": Option(False, type=valid_bool),
|
||||
}
|
||||
|
||||
def start(self) -> None:
|
||||
def sysprep(self) -> None:
|
||||
get_logger(0).info("Starting HID daemon ...")
|
||||
multiprocessing.Process.start(self)
|
||||
self.start()
|
||||
|
||||
async def get_state(self) -> Dict:
|
||||
state = await self.__state_flags.get()
|
||||
|
||||
@@ -165,8 +165,8 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
||||
reset_delay: float,
|
||||
) -> None:
|
||||
|
||||
self.__target_pin = gpio.set_output(target_pin)
|
||||
self.__reset_pin = gpio.set_output(reset_pin)
|
||||
self.__target_pin = gpio.set_output(target_pin, False)
|
||||
self.__reset_pin = gpio.set_output(reset_pin, False)
|
||||
|
||||
self.__device_path = device_path
|
||||
self.__init_delay = init_delay
|
||||
|
||||
77
kvmd/plugins/ugpio/__init__.py
Normal file
77
kvmd/plugins/ugpio/__init__.py
Normal file
@@ -0,0 +1,77 @@
|
||||
# ========================================================================== #
|
||||
# #
|
||||
# KVMD - The main Pi-KVM daemon. #
|
||||
# #
|
||||
# Copyright (C) 2018 Maxim Devaev <mdevaev@gmail.com> #
|
||||
# #
|
||||
# This program is free software: you can redistribute it and/or modify #
|
||||
# it under the terms of the GNU General Public License as published by #
|
||||
# the Free Software Foundation, either version 3 of the License, or #
|
||||
# (at your option) any later version. #
|
||||
# #
|
||||
# This program is distributed in the hope that it will be useful, #
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
|
||||
# GNU General Public License for more details. #
|
||||
# #
|
||||
# You should have received a copy of the GNU General Public License #
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
|
||||
# #
|
||||
# ========================================================================== #
|
||||
|
||||
|
||||
from typing import Type
|
||||
from typing import Optional
|
||||
|
||||
from ...errors import OperationError
|
||||
|
||||
from ... import aiotools
|
||||
|
||||
from .. import BasePlugin
|
||||
from .. import get_plugin_class
|
||||
|
||||
|
||||
# =====
|
||||
class GpioError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class GpioOperationError(OperationError, GpioError):
|
||||
pass
|
||||
|
||||
|
||||
class GpioDriverOfflineError(GpioOperationError):
|
||||
def __init__(self, driver: "BaseUserGpioDriver") -> None:
|
||||
super().__init__(f"GPIO driver {driver.get_instance_name()!r} is offline")
|
||||
|
||||
|
||||
# =====
|
||||
class BaseUserGpioDriver(BasePlugin):
|
||||
def get_instance_name(self) -> str:
|
||||
raise NotImplementedError
|
||||
|
||||
def register_input(self, pin: int) -> None:
|
||||
raise NotImplementedError
|
||||
|
||||
def register_output(self, pin: int, initial: Optional[bool]) -> None:
|
||||
raise NotImplementedError
|
||||
|
||||
def prepare(self, notifier: aiotools.AioNotifier) -> None:
|
||||
raise NotImplementedError
|
||||
|
||||
async def run(self) -> None:
|
||||
raise NotImplementedError
|
||||
|
||||
def cleanup(self) -> None:
|
||||
pass
|
||||
|
||||
def read(self, pin: int) -> bool:
|
||||
raise NotImplementedError
|
||||
|
||||
def write(self, pin: int, state: bool) -> None:
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
# =====
|
||||
def get_ugpio_driver_class(name: str) -> Type[BaseUserGpioDriver]:
|
||||
return get_plugin_class("ugpio", name) # type: ignore
|
||||
86
kvmd/plugins/ugpio/gpio.py
Normal file
86
kvmd/plugins/ugpio/gpio.py
Normal file
@@ -0,0 +1,86 @@
|
||||
# ========================================================================== #
|
||||
# #
|
||||
# KVMD - The main Pi-KVM daemon. #
|
||||
# #
|
||||
# Copyright (C) 2018 Maxim Devaev <mdevaev@gmail.com> #
|
||||
# #
|
||||
# This program is free software: you can redistribute it and/or modify #
|
||||
# it under the terms of the GNU General Public License as published by #
|
||||
# the Free Software Foundation, either version 3 of the License, or #
|
||||
# (at your option) any later version. #
|
||||
# #
|
||||
# This program is distributed in the hope that it will be useful, #
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
|
||||
# GNU General Public License for more details. #
|
||||
# #
|
||||
# You should have received a copy of the GNU General Public License #
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
|
||||
# #
|
||||
# ========================================================================== #
|
||||
|
||||
|
||||
from typing import Dict
|
||||
from typing import Set
|
||||
from typing import Optional
|
||||
|
||||
from ... import aiotools
|
||||
from ... import gpio
|
||||
|
||||
from ...yamlconf import Option
|
||||
|
||||
from ...validators.basic import valid_float_f01
|
||||
|
||||
from . import BaseUserGpioDriver
|
||||
|
||||
|
||||
# =====
|
||||
class Plugin(BaseUserGpioDriver):
|
||||
def __init__(self, state_poll: float) -> None: # pylint: disable=super-init-not-called
|
||||
self.__state_poll = state_poll
|
||||
|
||||
self.__input_pins: Set[int] = set()
|
||||
self.__output_pins: Dict[int, Optional[bool]] = {}
|
||||
|
||||
self.__reader: Optional[gpio.BatchReader] = None
|
||||
|
||||
@classmethod
|
||||
def get_plugin_options(cls) -> Dict:
|
||||
return {
|
||||
"state_poll": Option(0.1, type=valid_float_f01),
|
||||
}
|
||||
|
||||
def get_instance_name(self) -> str:
|
||||
return "gpio"
|
||||
|
||||
def register_input(self, pin: int) -> None:
|
||||
self.__input_pins.add(pin)
|
||||
|
||||
def register_output(self, pin: int, initial: Optional[bool]) -> None:
|
||||
self.__output_pins[pin] = initial
|
||||
|
||||
def prepare(self, notifier: aiotools.AioNotifier) -> None:
|
||||
assert self.__reader is None
|
||||
self.__reader = gpio.BatchReader(
|
||||
pins=set([
|
||||
*map(gpio.set_input, self.__input_pins),
|
||||
*[
|
||||
gpio.set_output(pin, initial)
|
||||
for (pin, initial) in self.__output_pins.items()
|
||||
],
|
||||
]),
|
||||
interval=self.__state_poll,
|
||||
notifier=notifier,
|
||||
)
|
||||
|
||||
async def run(self) -> None:
|
||||
assert self.__reader
|
||||
await self.__reader.poll()
|
||||
|
||||
def read(self, pin: int) -> bool:
|
||||
assert self.__reader
|
||||
return self.__reader.get(pin)
|
||||
|
||||
def write(self, pin: int, state: bool) -> None:
|
||||
assert self.__reader
|
||||
gpio.write(pin, state)
|
||||
Reference in New Issue
Block a user