new events model

This commit is contained in:
Maxim Devaev
2024-10-21 17:46:59 +03:00
parent b67a232584
commit cda32a083f
30 changed files with 335 additions and 165 deletions

View File

@@ -48,6 +48,9 @@ class BaseAtx(BasePlugin):
async def get_state(self) -> dict:
raise NotImplementedError
async def trigger_state(self) -> None:
raise NotImplementedError
async def poll_state(self) -> AsyncGenerator[dict, None]:
yield {}
raise NotImplementedError

View File

@@ -36,6 +36,9 @@ class AtxDisabledError(AtxOperationError):
# =====
class Plugin(BaseAtx):
def __init__(self) -> None:
self.__notifier = aiotools.AioNotifier()
async def get_state(self) -> dict:
return {
"enabled": False,
@@ -46,10 +49,13 @@ class Plugin(BaseAtx):
},
}
async def trigger_state(self) -> None:
self.__notifier.notify()
async def poll_state(self) -> AsyncGenerator[dict, None]:
while True:
await self.__notifier.wait()
yield (await self.get_state())
await aiotools.wait_infinite()
# =====

View File

@@ -21,6 +21,7 @@
import asyncio
import copy
from typing import AsyncGenerator
@@ -130,14 +131,18 @@ class Plugin(BaseAtx): # pylint: disable=too-many-instance-attributes
},
}
async def trigger_state(self) -> None:
self.__notifier.notify(1)
async def poll_state(self) -> AsyncGenerator[dict, None]:
prev_state: dict = {}
prev: dict = {}
while True:
state = await self.get_state()
if state != prev_state:
yield state
prev_state = state
await self.__notifier.wait()
if (await self.__notifier.wait()) > 0:
prev = {}
new = await self.get_state()
if new != prev:
prev = copy.deepcopy(new)
yield new
async def systask(self) -> None:
await self.__reader.poll()