mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2025-12-12 17:20:30 +08:00
async gpio plugins
This commit is contained in:
parent
6cc161427a
commit
6f60118320
@ -91,10 +91,10 @@ class _GpioInput:
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
def get_state(self) -> Dict:
|
async def get_state(self) -> Dict:
|
||||||
(online, state) = (True, False)
|
(online, state) = (True, False)
|
||||||
try:
|
try:
|
||||||
state = (self.__driver.read(self.__pin) ^ self.__inverted)
|
state = (await self.__driver.read(self.__pin) ^ self.__inverted)
|
||||||
except GpioDriverOfflineError:
|
except GpioDriverOfflineError:
|
||||||
online = False
|
online = False
|
||||||
return {
|
return {
|
||||||
@ -153,12 +153,12 @@ class _GpioOutput: # pylint: disable=too-many-instance-attributes
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
def get_state(self) -> Dict:
|
async def get_state(self) -> Dict:
|
||||||
busy = self.__region.is_busy()
|
busy = self.__region.is_busy()
|
||||||
(online, state) = (True, False)
|
(online, state) = (True, False)
|
||||||
if not busy:
|
if not busy:
|
||||||
try:
|
try:
|
||||||
state = self.__read()
|
state = await self.__read()
|
||||||
except GpioDriverOfflineError:
|
except GpioDriverOfflineError:
|
||||||
online = False
|
online = False
|
||||||
return {
|
return {
|
||||||
@ -201,27 +201,27 @@ class _GpioOutput: # pylint: disable=too-many-instance-attributes
|
|||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic
|
||||||
async def __inner_switch(self, state: bool) -> None:
|
async def __inner_switch(self, state: bool) -> None:
|
||||||
self.__write(state)
|
await self.__write(state)
|
||||||
get_logger(0).info("Ensured switch %s to state=%d", self, state)
|
get_logger(0).info("Ensured switch %s to state=%d", self, state)
|
||||||
await asyncio.sleep(self.__busy_delay)
|
await asyncio.sleep(self.__busy_delay)
|
||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic
|
||||||
async def __inner_pulse(self, delay: float) -> None:
|
async def __inner_pulse(self, delay: float) -> None:
|
||||||
try:
|
try:
|
||||||
self.__write(True)
|
await self.__write(True)
|
||||||
await asyncio.sleep(delay)
|
await asyncio.sleep(delay)
|
||||||
finally:
|
finally:
|
||||||
self.__write(False)
|
await self.__write(False)
|
||||||
await asyncio.sleep(self.__busy_delay)
|
await asyncio.sleep(self.__busy_delay)
|
||||||
get_logger(0).info("Pulsed %s with delay=%.2f", self, delay)
|
get_logger(0).info("Pulsed %s with delay=%.2f", self, delay)
|
||||||
|
|
||||||
# =====
|
# =====
|
||||||
|
|
||||||
def __read(self) -> bool:
|
async def __read(self) -> bool:
|
||||||
return (self.__driver.read(self.__pin) ^ self.__inverted)
|
return (await self.__driver.read(self.__pin) ^ self.__inverted)
|
||||||
|
|
||||||
def __write(self, state: bool) -> None:
|
async def __write(self, state: bool) -> None:
|
||||||
self.__driver.write(self.__pin, (state ^ self.__inverted))
|
await self.__driver.write(self.__pin, (state ^ self.__inverted))
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return f"Output({self.__channel}, driver={self.__driver}, pin={self.__pin})"
|
return f"Output({self.__channel}, driver={self.__driver}, pin={self.__pin})"
|
||||||
@ -267,8 +267,8 @@ class UserGpio:
|
|||||||
|
|
||||||
async def get_state(self) -> Dict:
|
async def get_state(self) -> Dict:
|
||||||
return {
|
return {
|
||||||
"inputs": {channel: gin.get_state() for (channel, gin) in self.__inputs.items()},
|
"inputs": {channel: await gin.get_state() for (channel, gin) in self.__inputs.items()},
|
||||||
"outputs": {channel: gout.get_state() for (channel, gout) in self.__outputs.items()},
|
"outputs": {channel: await gout.get_state() for (channel, gout) in self.__outputs.items()},
|
||||||
}
|
}
|
||||||
|
|
||||||
async def poll_state(self) -> AsyncGenerator[Dict, None]:
|
async def poll_state(self) -> AsyncGenerator[Dict, None]:
|
||||||
|
|||||||
@ -89,10 +89,10 @@ class BaseUserGpioDriver(BasePlugin):
|
|||||||
def cleanup(self) -> None:
|
def cleanup(self) -> None:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def read(self, pin: int) -> bool:
|
async def read(self, pin: int) -> bool:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def write(self, pin: int, state: bool) -> None:
|
async def write(self, pin: int, state: bool) -> None:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -108,12 +108,12 @@ class Plugin(BaseUserGpioDriver): # pylint: disable=too-many-instance-attribute
|
|||||||
if self.__proc.exitcode is not None:
|
if self.__proc.exitcode is not None:
|
||||||
self.__proc.join()
|
self.__proc.join()
|
||||||
|
|
||||||
def read(self, pin: int) -> bool:
|
async def read(self, pin: int) -> bool:
|
||||||
if not self.__is_online():
|
if not self.__is_online():
|
||||||
raise GpioDriverOfflineError(self)
|
raise GpioDriverOfflineError(self)
|
||||||
return (self.__channel == pin)
|
return (self.__channel == pin)
|
||||||
|
|
||||||
def write(self, pin: int, state: bool) -> None:
|
async def write(self, pin: int, state: bool) -> None:
|
||||||
if not self.__is_online():
|
if not self.__is_online():
|
||||||
raise GpioDriverOfflineError(self)
|
raise GpioDriverOfflineError(self)
|
||||||
if state and (0 <= pin <= 3):
|
if state and (0 <= pin <= 3):
|
||||||
|
|||||||
@ -95,13 +95,13 @@ class Plugin(BaseUserGpioDriver):
|
|||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def read(self, pin: int) -> bool:
|
async def read(self, pin: int) -> bool:
|
||||||
assert self.__reader
|
assert self.__reader
|
||||||
if pin in self.__input_pins:
|
if pin in self.__input_pins:
|
||||||
return self.__reader.get(pin)
|
return self.__reader.get(pin)
|
||||||
return bool(self.__output_lines[pin].get_value())
|
return bool(self.__output_lines[pin].get_value())
|
||||||
|
|
||||||
def write(self, pin: int, state: bool) -> None:
|
async def write(self, pin: int, state: bool) -> None:
|
||||||
self.__output_lines[pin].set_value(int(state))
|
self.__output_lines[pin].set_value(int(state))
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
|
|||||||
@ -113,13 +113,13 @@ class Plugin(BaseUserGpioDriver):
|
|||||||
self.__close_device()
|
self.__close_device()
|
||||||
self.__stop = True
|
self.__stop = True
|
||||||
|
|
||||||
def read(self, pin: int) -> bool:
|
async def read(self, pin: int) -> bool:
|
||||||
try:
|
try:
|
||||||
return self.__inner_read(pin)
|
return self.__inner_read(pin)
|
||||||
except Exception:
|
except Exception:
|
||||||
raise GpioDriverOfflineError(self)
|
raise GpioDriverOfflineError(self)
|
||||||
|
|
||||||
def write(self, pin: int, state: bool) -> None:
|
async def write(self, pin: int, state: bool) -> None:
|
||||||
try:
|
try:
|
||||||
return self.__inner_write(pin, state)
|
return self.__inner_write(pin, state)
|
||||||
except Exception:
|
except Exception:
|
||||||
|
|||||||
@ -100,11 +100,11 @@ class Plugin(BaseUserGpioDriver):
|
|||||||
def cleanup(self) -> None:
|
def cleanup(self) -> None:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def read(self, pin: int) -> bool:
|
async def read(self, pin: int) -> bool:
|
||||||
_ = pin
|
_ = pin
|
||||||
return os.path.islink(self.__get_driver_path(self.__udc))
|
return os.path.islink(self.__get_driver_path(self.__udc))
|
||||||
|
|
||||||
def write(self, pin: int, state: bool) -> None:
|
async def write(self, pin: int, state: bool) -> None:
|
||||||
_ = pin
|
_ = pin
|
||||||
with open(self.__get_driver_path("bind" if state else "unbind"), "w") as ctl_file:
|
with open(self.__get_driver_path("bind" if state else "unbind"), "w") as ctl_file:
|
||||||
ctl_file.write(f"{self.__udc}\n")
|
ctl_file.write(f"{self.__udc}\n")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user