mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2026-01-29 00:51:53 +08:00
refactoring
This commit is contained in:
@@ -113,13 +113,13 @@ class Plugin(BaseUserGpioDriver): # pylint: disable=too-many-instance-attribute
|
||||
while True:
|
||||
session = self.__ensure_http_session()
|
||||
try:
|
||||
async with session.get(f"{self.__url}/strg.cfg") as response:
|
||||
htclient.raise_not_200(response)
|
||||
parts = (await response.text()).split(";")
|
||||
async with session.get(f"{self.__url}/strg.cfg") as resp:
|
||||
htclient.raise_not_200(resp)
|
||||
parts = (await resp.text()).split(";")
|
||||
for pin in self.__state:
|
||||
self.__state[pin] = (parts[1 + int(pin) * 5] == "1")
|
||||
except Exception as err:
|
||||
get_logger().error("Failed ANELPWR bulk GET request: %s", tools.efmt(err))
|
||||
except Exception as ex:
|
||||
get_logger().error("Failed ANELPWR bulk GET request: %s", tools.efmt(ex))
|
||||
self.__state = dict.fromkeys(self.__state, None)
|
||||
if self.__state != prev_state:
|
||||
self._notifier.notify()
|
||||
@@ -143,10 +143,10 @@ class Plugin(BaseUserGpioDriver): # pylint: disable=too-many-instance-attribute
|
||||
url=f"{self.__url}/ctrl.htm",
|
||||
data=f"F{pin}={int(state)}",
|
||||
headers={"Content-Type": "text/plain"},
|
||||
) as response:
|
||||
htclient.raise_not_200(response)
|
||||
except Exception as err:
|
||||
get_logger().error("Failed ANELPWR POST request to pin %s: %s", pin, tools.efmt(err))
|
||||
) as resp:
|
||||
htclient.raise_not_200(resp)
|
||||
except Exception as ex:
|
||||
get_logger().error("Failed ANELPWR POST request to pin %s: %s", pin, tools.efmt(ex))
|
||||
raise GpioDriverOfflineError(self)
|
||||
self.__update_notifier.notify()
|
||||
|
||||
|
||||
@@ -78,9 +78,9 @@ class Plugin(BaseUserGpioDriver): # pylint: disable=too-many-instance-attribute
|
||||
proc = await aioproc.log_process(self.__cmd, logger=get_logger(0), prefix=str(self))
|
||||
if proc.returncode != 0:
|
||||
raise RuntimeError(f"Custom command error: retcode={proc.returncode}")
|
||||
except Exception as err:
|
||||
except Exception as ex:
|
||||
get_logger(0).error("Can't run custom command [ %s ]: %s",
|
||||
tools.cmdfmt(self.__cmd), tools.efmt(err))
|
||||
tools.cmdfmt(self.__cmd), tools.efmt(ex))
|
||||
raise GpioDriverOfflineError(self)
|
||||
|
||||
def __str__(self) -> str:
|
||||
|
||||
@@ -71,9 +71,9 @@ class Plugin(BaseUserGpioDriver): # pylint: disable=too-many-instance-attribute
|
||||
try:
|
||||
proc = await aioproc.log_process(self.__cmd, logger=get_logger(0), prefix=str(self))
|
||||
return (proc.returncode == 0)
|
||||
except Exception as err:
|
||||
except Exception as ex:
|
||||
get_logger(0).error("Can't run custom command [ %s ]: %s",
|
||||
tools.cmdfmt(self.__cmd), tools.efmt(err))
|
||||
tools.cmdfmt(self.__cmd), tools.efmt(ex))
|
||||
raise GpioDriverOfflineError(self)
|
||||
|
||||
async def write(self, pin: str, state: bool) -> None:
|
||||
|
||||
@@ -150,9 +150,9 @@ class Plugin(BaseUserGpioDriver): # pylint: disable=too-many-instance-attribute
|
||||
assert channel is not None
|
||||
self.__send_channel(tty, channel)
|
||||
|
||||
except Exception as err:
|
||||
except Exception as ex:
|
||||
self.__channel_queue.put_nowait(None)
|
||||
if isinstance(err, serial.SerialException) and err.errno == errno.ENOENT: # pylint: disable=no-member
|
||||
if isinstance(ex, serial.SerialException) and ex.errno == errno.ENOENT: # pylint: disable=no-member
|
||||
logger.error("Missing %s serial device: %s", self, self.__device_path)
|
||||
else:
|
||||
logger.exception("Unexpected %s error", self)
|
||||
|
||||
@@ -150,9 +150,9 @@ class Plugin(BaseUserGpioDriver): # pylint: disable=too-many-instance-attribute
|
||||
assert channel is not None
|
||||
self.__send_channel(tty, channel)
|
||||
|
||||
except Exception as err:
|
||||
except Exception as ex:
|
||||
self.__channel_queue.put_nowait(None)
|
||||
if isinstance(err, serial.SerialException) and err.errno == errno.ENOENT: # pylint: disable=no-member
|
||||
if isinstance(ex, serial.SerialException) and ex.errno == errno.ENOENT: # pylint: disable=no-member
|
||||
logger.error("Missing %s serial device: %s", self, self.__device_path)
|
||||
else:
|
||||
logger.exception("Unexpected %s error", self)
|
||||
|
||||
@@ -54,7 +54,7 @@ class Plugin(BaseUserGpioDriver):
|
||||
self.__output_pins: dict[int, (bool | None)] = {}
|
||||
|
||||
self.__reader: (aiogp.AioReader | None) = None
|
||||
self.__outputs_request: (gpiod.LineRequest | None) = None
|
||||
self.__outputs_req: (gpiod.LineRequest | None) = None
|
||||
|
||||
@classmethod
|
||||
def get_plugin_options(cls) -> dict:
|
||||
@@ -74,7 +74,7 @@ class Plugin(BaseUserGpioDriver):
|
||||
|
||||
def prepare(self) -> None:
|
||||
assert self.__reader is None
|
||||
assert self.__outputs_request is None
|
||||
assert self.__outputs_req is None
|
||||
self.__reader = aiogp.AioReader(
|
||||
path=self.__device_path,
|
||||
consumer="kvmd::gpio::inputs",
|
||||
@@ -82,7 +82,7 @@ class Plugin(BaseUserGpioDriver):
|
||||
notifier=self._notifier,
|
||||
)
|
||||
if self.__output_pins:
|
||||
self.__outputs_request = gpiod.request_lines(
|
||||
self.__outputs_req = gpiod.request_lines(
|
||||
self.__device_path,
|
||||
consumer="kvmd::gpiod::outputs",
|
||||
config={
|
||||
@@ -99,9 +99,9 @@ class Plugin(BaseUserGpioDriver):
|
||||
await self.__reader.poll()
|
||||
|
||||
async def cleanup(self) -> None:
|
||||
if self.__outputs_request:
|
||||
if self.__outputs_req:
|
||||
try:
|
||||
self.__outputs_request.release()
|
||||
self.__outputs_req.release()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@@ -110,15 +110,15 @@ class Plugin(BaseUserGpioDriver):
|
||||
pin_int = int(pin)
|
||||
if pin_int in self.__input_pins:
|
||||
return self.__reader.get(pin_int)
|
||||
assert self.__outputs_request
|
||||
assert self.__outputs_req
|
||||
assert pin_int in self.__output_pins
|
||||
return bool(self.__outputs_request.get_value(pin_int).value)
|
||||
return bool(self.__outputs_req.get_value(pin_int).value)
|
||||
|
||||
async def write(self, pin: str, state: bool) -> None:
|
||||
assert self.__outputs_request
|
||||
assert self.__outputs_req
|
||||
pin_int = int(pin)
|
||||
assert pin_int in self.__output_pins
|
||||
self.__outputs_request.set_value(pin_int, gpiod.line.Value(state))
|
||||
self.__outputs_req.set_value(pin_int, gpiod.line.Value(state))
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"GPIO({self._instance_name})"
|
||||
|
||||
@@ -93,9 +93,9 @@ class Plugin(BaseUserGpioDriver):
|
||||
try:
|
||||
with self.__ensure_device("probing"):
|
||||
pass
|
||||
except Exception as err:
|
||||
except Exception as ex:
|
||||
logger.error("Can't probe %s on %s: %s",
|
||||
self, self.__device_path, tools.efmt(err))
|
||||
self, self.__device_path, tools.efmt(ex))
|
||||
self.__reset_pins()
|
||||
|
||||
async def run(self) -> None:
|
||||
@@ -137,9 +137,9 @@ class Plugin(BaseUserGpioDriver):
|
||||
pin, state, self, self.__device_path)
|
||||
try:
|
||||
self.__inner_write(pin, state)
|
||||
except Exception as err:
|
||||
except Exception as ex:
|
||||
logger.error("Can't reset pin=%d of %s on %s: %s",
|
||||
pin, self, self.__device_path, tools.efmt(err))
|
||||
pin, self, self.__device_path, tools.efmt(ex))
|
||||
|
||||
def __inner_read(self, pin: int) -> bool:
|
||||
assert 0 <= pin <= 7
|
||||
@@ -168,9 +168,9 @@ class Plugin(BaseUserGpioDriver):
|
||||
get_logger(0).info("Opened %s on %s while %s", self, self.__device_path, context)
|
||||
try:
|
||||
yield self.__device
|
||||
except Exception as err:
|
||||
except Exception as ex:
|
||||
get_logger(0).error("Error occured on %s on %s while %s: %s",
|
||||
self, self.__device_path, context, tools.efmt(err))
|
||||
self, self.__device_path, context, tools.efmt(ex))
|
||||
self.__close_device()
|
||||
raise
|
||||
|
||||
|
||||
@@ -111,13 +111,13 @@ class Plugin(BaseUserGpioDriver): # pylint: disable=too-many-instance-attribute
|
||||
while True:
|
||||
session = self.__ensure_http_session()
|
||||
try:
|
||||
async with session.get(f"{self.__url}/api/{self.__token}/lights") as response:
|
||||
results = await response.json()
|
||||
async with session.get(f"{self.__url}/api/{self.__token}/lights") as resp:
|
||||
results = await resp.json()
|
||||
for pin in self.__state:
|
||||
if pin in results:
|
||||
self.__state[pin] = bool(results[pin]["state"]["on"])
|
||||
except Exception as err:
|
||||
get_logger().error("Failed Hue bulk GET request: %s", tools.efmt(err))
|
||||
except Exception as ex:
|
||||
get_logger().error("Failed Hue bulk GET request: %s", tools.efmt(ex))
|
||||
self.__state = dict.fromkeys(self.__state, None)
|
||||
if self.__state != prev_state:
|
||||
self._notifier.notify()
|
||||
@@ -140,10 +140,10 @@ class Plugin(BaseUserGpioDriver): # pylint: disable=too-many-instance-attribute
|
||||
async with session.put(
|
||||
url=f"{self.__url}/api/{self.__token}/lights/{pin}/state",
|
||||
json={"on": state},
|
||||
) as response:
|
||||
htclient.raise_not_200(response)
|
||||
except Exception as err:
|
||||
get_logger().error("Failed Hue PUT request to pin %s: %s", pin, tools.efmt(err))
|
||||
) as resp:
|
||||
htclient.raise_not_200(resp)
|
||||
except Exception as ex:
|
||||
get_logger().error("Failed Hue PUT request to pin %s: %s", pin, tools.efmt(ex))
|
||||
raise GpioDriverOfflineError(self)
|
||||
self.__update_notifier.notify()
|
||||
|
||||
|
||||
@@ -153,9 +153,9 @@ class Plugin(BaseUserGpioDriver): # pylint: disable=too-many-instance-attribute
|
||||
proc = await aioproc.log_process(**self.__make_ipmitool_kwargs(action), logger=get_logger(0), prefix=str(self))
|
||||
if proc.returncode != 0:
|
||||
raise RuntimeError(f"Ipmitool error: retcode={proc.returncode}")
|
||||
except Exception as err:
|
||||
except Exception as ex:
|
||||
get_logger(0).error("Can't send IPMI power-%s request to %s:%d: %s",
|
||||
action, self.__host, self.__port, tools.efmt(err))
|
||||
action, self.__host, self.__port, tools.efmt(ex))
|
||||
raise GpioDriverOfflineError(self)
|
||||
|
||||
# =====
|
||||
@@ -171,9 +171,9 @@ class Plugin(BaseUserGpioDriver): # pylint: disable=too-many-instance-attribute
|
||||
self.__online = True
|
||||
return
|
||||
raise RuntimeError(f"Invalid ipmitool response: {text}")
|
||||
except Exception as err:
|
||||
except Exception as ex:
|
||||
get_logger(0).error("Can't fetch IPMI power status from %s:%d: %s",
|
||||
self.__host, self.__port, tools.efmt(err))
|
||||
self.__host, self.__port, tools.efmt(ex))
|
||||
self.__power = False
|
||||
self.__online = False
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ class Plugin(BaseUserGpioDriver):
|
||||
self.__device_path = device_path
|
||||
|
||||
self.__tasks: dict[int, (asyncio.Task | None)] = {}
|
||||
self.__line_request: (gpiod.LineRequest | None) = None
|
||||
self.__line_req: (gpiod.LineRequest | None) = None
|
||||
|
||||
@classmethod
|
||||
def get_plugin_options(cls) -> dict:
|
||||
@@ -74,7 +74,7 @@ class Plugin(BaseUserGpioDriver):
|
||||
self.__tasks[int(pin)] = None
|
||||
|
||||
def prepare(self) -> None:
|
||||
self.__line_request = gpiod.request_lines(
|
||||
self.__line_req = gpiod.request_lines(
|
||||
self.__device_path,
|
||||
consumer="kvmd::locator",
|
||||
config={
|
||||
@@ -94,9 +94,9 @@ class Plugin(BaseUserGpioDriver):
|
||||
for task in tasks:
|
||||
task.cancel()
|
||||
await asyncio.gather(*tasks, return_exceptions=True)
|
||||
if self.__line_request:
|
||||
if self.__line_req:
|
||||
try:
|
||||
self.__line_request.release()
|
||||
self.__line_req.release()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@@ -115,17 +115,17 @@ class Plugin(BaseUserGpioDriver):
|
||||
|
||||
async def __blink(self, pin: int) -> None:
|
||||
assert pin in self.__tasks
|
||||
assert self.__line_request
|
||||
assert self.__line_req
|
||||
try:
|
||||
state = True
|
||||
while True:
|
||||
self.__line_request.set_value(pin, gpiod.line.Value(state))
|
||||
self.__line_req.set_value(pin, gpiod.line.Value(state))
|
||||
state = (not state)
|
||||
await asyncio.sleep(0.1)
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
finally:
|
||||
self.__line_request.set_value(pin, gpiod.line.Value(False))
|
||||
self.__line_req.set_value(pin, gpiod.line.Value(False))
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"Locator({self._instance_name})"
|
||||
|
||||
@@ -91,9 +91,9 @@ class Plugin(BaseUserGpioDriver):
|
||||
try:
|
||||
with self.__ensure_device("probing"):
|
||||
pass
|
||||
except Exception as err:
|
||||
except Exception as ex:
|
||||
logger.error("Can't probe %s on %s: %s",
|
||||
self, self.__device_path, tools.efmt(err))
|
||||
self, self.__device_path, tools.efmt(ex))
|
||||
self.__reset_pins()
|
||||
|
||||
async def cleanup(self) -> None:
|
||||
@@ -119,9 +119,9 @@ class Plugin(BaseUserGpioDriver):
|
||||
pin, state, self, self.__device_path)
|
||||
try:
|
||||
self.__inner_write(pin, state)
|
||||
except Exception as err:
|
||||
except Exception as ex:
|
||||
logger.error("Can't reset pin=%d of %s on %s: %s",
|
||||
pin, self, self.__device_path, tools.efmt(err))
|
||||
pin, self, self.__device_path, tools.efmt(ex))
|
||||
|
||||
def __inner_write(self, pin: int, state: bool) -> None:
|
||||
assert 0 <= pin <= 7
|
||||
@@ -144,9 +144,9 @@ class Plugin(BaseUserGpioDriver):
|
||||
get_logger(0).info("Opened %s on %s while %s", self, self.__device_path, context)
|
||||
try:
|
||||
yield self.__device
|
||||
except Exception as err:
|
||||
except Exception as ex:
|
||||
get_logger(0).error("Error occured on %s on %s while %s: %s",
|
||||
self, self.__device_path, context, tools.efmt(err))
|
||||
self, self.__device_path, context, tools.efmt(ex))
|
||||
self.__close_device()
|
||||
raise
|
||||
|
||||
|
||||
@@ -153,9 +153,9 @@ class Plugin(BaseUserGpioDriver): # pylint: disable=too-many-instance-attribute
|
||||
assert channel is not None
|
||||
self.__send_channel(tty, channel)
|
||||
|
||||
except Exception as err:
|
||||
except Exception as ex:
|
||||
self.__channel_queue.put_nowait(None)
|
||||
if isinstance(err, serial.SerialException) and err.errno == errno.ENOENT: # pylint: disable=no-member
|
||||
if isinstance(ex, serial.SerialException) and ex.errno == errno.ENOENT: # pylint: disable=no-member
|
||||
logger.error("Missing %s serial device: %s", self, self.__device_path)
|
||||
else:
|
||||
logger.exception("Unexpected %s error", self)
|
||||
|
||||
@@ -94,18 +94,18 @@ class Plugin(BaseUserGpioDriver):
|
||||
pwm.period_ns = self.__period
|
||||
pwm.duty_cycle_ns = self.__get_duty_cycle(bool(initial))
|
||||
pwm.enable()
|
||||
except Exception as err:
|
||||
except Exception as ex:
|
||||
logger.error("Can't get PWM chip %d channel %d: %s",
|
||||
self.__chip, pin, tools.efmt(err))
|
||||
self.__chip, pin, tools.efmt(ex))
|
||||
|
||||
async def cleanup(self) -> None:
|
||||
for (pin, pwm) in self.__pwms.items():
|
||||
try:
|
||||
pwm.disable()
|
||||
pwm.close()
|
||||
except Exception as err:
|
||||
except Exception as ex:
|
||||
get_logger(0).error("Can't cleanup PWM chip %d channel %d: %s",
|
||||
self.__chip, pin, tools.efmt(err))
|
||||
self.__chip, pin, tools.efmt(ex))
|
||||
|
||||
async def read(self, pin: str) -> bool:
|
||||
try:
|
||||
|
||||
@@ -146,9 +146,9 @@ class Plugin(BaseUserGpioDriver): # pylint: disable=too-many-instance-attribute
|
||||
asyncio.ensure_future(self.__reader.readexactly(6)),
|
||||
timeout=self.__timeout,
|
||||
))[4]
|
||||
except Exception as err:
|
||||
except Exception as ex:
|
||||
get_logger(0).error("Can't send command to TESmart KVM [%s]:%d: %s",
|
||||
self.__host, self.__port, tools.efmt(err))
|
||||
self.__host, self.__port, tools.efmt(ex))
|
||||
await self.__close_device()
|
||||
self.__active = -1
|
||||
raise GpioDriverOfflineError(self)
|
||||
@@ -168,9 +168,9 @@ class Plugin(BaseUserGpioDriver): # pylint: disable=too-many-instance-attribute
|
||||
asyncio.ensure_future(asyncio.open_connection(self.__host, self.__port)),
|
||||
timeout=self.__timeout,
|
||||
)
|
||||
except Exception as err:
|
||||
except Exception as ex:
|
||||
get_logger(0).error("Can't connect to TESmart KVM [%s]:%d: %s",
|
||||
self.__host, self.__port, tools.efmt(err))
|
||||
self.__host, self.__port, tools.efmt(ex))
|
||||
raise GpioDriverOfflineError(self)
|
||||
|
||||
async def __ensure_device_serial(self) -> None:
|
||||
@@ -179,9 +179,9 @@ class Plugin(BaseUserGpioDriver): # pylint: disable=too-many-instance-attribute
|
||||
serial_asyncio.open_serial_connection(url=self.__device_path, baudrate=self.__speed),
|
||||
timeout=self.__timeout,
|
||||
)
|
||||
except Exception as err:
|
||||
except Exception as ex:
|
||||
get_logger(0).error("Can't connect to TESmart KVM [%s]:%d: %s",
|
||||
self.__device_path, self.__speed, tools.efmt(err))
|
||||
self.__device_path, self.__speed, tools.efmt(ex))
|
||||
raise GpioDriverOfflineError(self)
|
||||
|
||||
async def __close_device(self) -> None:
|
||||
|
||||
@@ -157,9 +157,9 @@ class Plugin(BaseUserGpioDriver): # pylint: disable=too-many-instance-attribute
|
||||
if self.__protocol == 2:
|
||||
self.__channel_queue.put_nowait(channel)
|
||||
|
||||
except Exception as err:
|
||||
except Exception as ex:
|
||||
self.__channel_queue.put_nowait(None)
|
||||
if isinstance(err, serial.SerialException) and err.errno == errno.ENOENT: # pylint: disable=no-member
|
||||
if isinstance(ex, serial.SerialException) and ex.errno == errno.ENOENT: # pylint: disable=no-member
|
||||
logger.error("Missing %s serial device: %s", self, self.__device_path)
|
||||
else:
|
||||
logger.exception("Unexpected %s error", self)
|
||||
|
||||
Reference in New Issue
Block a user