refactoring

This commit is contained in:
Devaev Maxim
2020-09-14 21:16:02 +03:00
parent ddb60e5a73
commit 50d0612f82

View File

@@ -24,6 +24,7 @@ import os
import asyncio import asyncio
import threading import threading
from typing import Tuple
from typing import Dict from typing import Dict
from typing import Optional from typing import Optional
@@ -48,7 +49,7 @@ async def pulse(line: gpiod.Line, delay: float, final: float) -> None:
await asyncio.sleep(final) await asyncio.sleep(final)
class AioPinsReader(threading.Thread): class AioPinsReader: # pylint: disable=too-many-instance-attributes
def __init__( def __init__(
self, self,
path: str, path: str,
@@ -57,8 +58,6 @@ class AioPinsReader(threading.Thread):
notifier: aiotools.AioNotifier, notifier: aiotools.AioNotifier,
) -> None: ) -> None:
super().__init__(daemon=True)
self.__path = path self.__path = path
self.__consumer = consumer self.__consumer = consumer
self.__pins = pins self.__pins = pins
@@ -69,6 +68,8 @@ class AioPinsReader(threading.Thread):
self.__stop_event = threading.Event() self.__stop_event = threading.Event()
self.__loop: Optional[asyncio.AbstractEventLoop] = None self.__loop: Optional[asyncio.AbstractEventLoop] = None
self.__thread = threading.Thread(target=self.__run, daemon=True)
def get(self, pin: int) -> bool: def get(self, pin: int) -> bool:
return (self.__state[pin] ^ self.__pins[pin]) return (self.__state[pin] ^ self.__pins[pin])
@@ -78,15 +79,14 @@ class AioPinsReader(threading.Thread):
else: else:
assert self.__loop is None assert self.__loop is None
self.__loop = asyncio.get_running_loop() self.__loop = asyncio.get_running_loop()
self.start() self.__thread.start()
try: try:
await aiotools.run_async(self.join) await aiotools.run_async(self.__thread.join)
finally: finally:
self.__stop_event.set() self.__stop_event.set()
await aiotools.run_async(self.join) await aiotools.run_async(self.__thread.join)
def run(self) -> None: def __run(self) -> None:
assert self.__loop
with gpiod.Chip(self.__path) as chip: with gpiod.Chip(self.__path) as chip:
pins = sorted(self.__pins) pins = sorted(self.__pins)
lines = chip.get_lines(pins) lines = chip.get_lines(pins)
@@ -97,7 +97,7 @@ class AioPinsReader(threading.Thread):
pin: bool(value) pin: bool(value)
for (pin, value) in zip(pins, lines.get_values()) for (pin, value) in zip(pins, lines.get_values())
} }
self.__loop.call_soon_threadsafe(self.__notifier.notify_sync) self.__notify()
while not self.__stop_event.is_set(): while not self.__stop_event.is_set():
ev_lines = lines.event_wait(1) ev_lines = lines.event_wait(1)
@@ -105,12 +105,18 @@ class AioPinsReader(threading.Thread):
for ev_line in ev_lines: for ev_line in ev_lines:
events = ev_line.event_read_multiply() events = ev_line.event_read_multiply()
if events: if events:
event = events[-1] (pin, value) = self.__parse_event(events[-1])
if event.type == gpiod.LineEvent.RISING_EDGE: self.__state[pin] = value
value = True self.__notify()
elif event.type == gpiod.LineEvent.FALLING_EDGE:
value = False def __parse_event(self, event: gpiod.LineEvent) -> Tuple[int, bool]:
else: pin = event.source.offset()
raise RuntimeError(f"Invalid event {event} type: {event.type}") if event.type == gpiod.LineEvent.RISING_EDGE:
self.__state[event.source.offset()] = value return (pin, True)
self.__loop.call_soon_threadsafe(self.__notifier.notify_sync) elif event.type == gpiod.LineEvent.FALLING_EDGE:
return (pin, False)
raise RuntimeError(f"Invalid event {event} type: {event.type}")
def __notify(self) -> None:
assert self.__loop
self.__loop.call_soon_threadsafe(self.__notifier.notify_sync)