Added wait plugin
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
from .abstract_plugin import AbstractCommand, AbstractCommandCompiler, AbstractPlugin
|
||||
from .sleep_plugin import SleepPlugin
|
||||
from .sync_plugin import SyncPlugin
|
||||
from .sync_plugin import SyncPlugin
|
||||
from .wait_plugin import WaitPlugin
|
||||
68
single_ursim_control/plugins/wait_plugin.py
Normal file
68
single_ursim_control/plugins/wait_plugin.py
Normal file
@@ -0,0 +1,68 @@
|
||||
import threading
|
||||
from typing import Dict
|
||||
|
||||
from .abstract_plugin import AbstractCommand, AbstractPlugin, AbstractCommandCompiler
|
||||
import logging
|
||||
from threading import Event
|
||||
|
||||
|
||||
#
|
||||
# I have to admit, the abort() solution is not the most robust thing I've ever designed
|
||||
# But for this simple scenario, it's good enough
|
||||
#
|
||||
|
||||
class WaitCommand(AbstractCommand):
|
||||
|
||||
def __init__(self, logger: logging.Logger, shared_event: threading.Event):
|
||||
self._shared_event = shared_event
|
||||
self._logger = logger
|
||||
self._aborted = False
|
||||
|
||||
def execute(self):
|
||||
self._logger.debug(f"Waiting for user interaction...")
|
||||
self._aborted = False # Have to reset, because of looping
|
||||
self._shared_event.clear()
|
||||
self._shared_event.wait()
|
||||
|
||||
if self._aborted:
|
||||
self._logger.warning("Waiting for interaction aborted externally!")
|
||||
else:
|
||||
self._logger.debug(f"User interaction received!")
|
||||
|
||||
def abort(self):
|
||||
self._aborted = True
|
||||
self._shared_event.set() # <- force the event.wait to return
|
||||
|
||||
def describe(self) -> dict:
|
||||
return {
|
||||
"command": "wait"
|
||||
}
|
||||
|
||||
|
||||
class WaitCompiler(AbstractCommandCompiler):
|
||||
|
||||
def __init__(self, logger: logging.Logger, shared_event: threading.Event):
|
||||
self._logger = logger
|
||||
self._shared_event = shared_event
|
||||
|
||||
def compile(self) -> AbstractCommand:
|
||||
return WaitCommand(self._logger, self._shared_event)
|
||||
|
||||
|
||||
class WaitPlugin(AbstractPlugin):
|
||||
plugin_name = "wait"
|
||||
|
||||
def __init__(self):
|
||||
self._logger = logging.getLogger("plugin").getChild("wait")
|
||||
self._shared_event = threading.Event()
|
||||
|
||||
def load_compilers(self) -> Dict[str, AbstractCommandCompiler]:
|
||||
return {
|
||||
"wait": WaitCompiler(self._logger, self._shared_event)
|
||||
}
|
||||
|
||||
def cont(self): # <- magic
|
||||
self._shared_event.set()
|
||||
|
||||
def close(self):
|
||||
pass
|
||||
Reference in New Issue
Block a user