diff options
author | Maxim Devaev <[email protected]> | 2024-10-21 17:46:59 +0300 |
---|---|---|
committer | Maxim Devaev <[email protected]> | 2024-10-21 17:46:59 +0300 |
commit | cda32a083faf3e7326cfe317336e473c905c6dfd (patch) | |
tree | 19445e4098d4603f3b2cd296504a648110712af1 /kvmd/plugins/msd | |
parent | b67a2325842a6f407d3935f8445d50cb8bf307f2 (diff) |
new events model
Diffstat (limited to 'kvmd/plugins/msd')
-rw-r--r-- | kvmd/plugins/msd/__init__.py | 3 | ||||
-rw-r--r-- | kvmd/plugins/msd/disabled.py | 8 | ||||
-rw-r--r-- | kvmd/plugins/msd/otg/__init__.py | 17 |
3 files changed, 21 insertions, 7 deletions
diff --git a/kvmd/plugins/msd/__init__.py b/kvmd/plugins/msd/__init__.py index e0210921..11cd8be5 100644 --- a/kvmd/plugins/msd/__init__.py +++ b/kvmd/plugins/msd/__init__.py @@ -117,6 +117,9 @@ class BaseMsd(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]: if self is not None: # XXX: Vulture and pylint hack raise NotImplementedError() diff --git a/kvmd/plugins/msd/disabled.py b/kvmd/plugins/msd/disabled.py index ad49875e..b9f14f6e 100644 --- a/kvmd/plugins/msd/disabled.py +++ b/kvmd/plugins/msd/disabled.py @@ -40,6 +40,9 @@ class MsdDisabledError(MsdOperationError): # ===== class Plugin(BaseMsd): + def __init__(self) -> None: + self.__notifier = aiotools.AioNotifier() + async def get_state(self) -> dict: return { "enabled": False, @@ -49,10 +52,13 @@ class Plugin(BaseMsd): "drive": None, } + 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() async def reset(self) -> None: raise MsdDisabledError() diff --git a/kvmd/plugins/msd/otg/__init__.py b/kvmd/plugins/msd/otg/__init__.py index b652cdd0..0bb9f489 100644 --- a/kvmd/plugins/msd/otg/__init__.py +++ b/kvmd/plugins/msd/otg/__init__.py @@ -24,6 +24,7 @@ import asyncio import contextlib import dataclasses import functools +import copy import time from typing import AsyncGenerator @@ -195,14 +196,18 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes "drive": vd, } + 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.__watch_inotify() |