From deb4b4bc19ddc142a070a2d23f7fb99e77de4bf6 Mon Sep 17 00:00:00 2001 From: acmacunlay Date: Sat, 12 Oct 2024 21:39:37 +0800 Subject: [PATCH] WIP: Implement worker data listening mechanism p2 --- src/pywaveshare/boards/sim868/protocols.py | 10 ++-- src/pywaveshare/boards/sim868/worker.py | 56 ++++++++++++++-------- 2 files changed, 43 insertions(+), 23 deletions(-) diff --git a/src/pywaveshare/boards/sim868/protocols.py b/src/pywaveshare/boards/sim868/protocols.py index 65e6dc1..6227062 100644 --- a/src/pywaveshare/boards/sim868/protocols.py +++ b/src/pywaveshare/boards/sim868/protocols.py @@ -2,14 +2,16 @@ import re import typing +from . import config -class SupportedStandard(abc.ABC): + +class SupportedProtocol(abc.ABC): NAME: typing.Optional[str] = None RESPONSE_PATTERN = r"" -class SMS(SupportedStandard): +class SMS(SupportedProtocol): NAME = "SMS" RESPONSE_PATTERN = ( @@ -23,8 +25,8 @@ class SMS(SupportedStandard): ) -class SupportedProtocolFactory: - pass +def get_enabled_protocols(config: config.Config) -> typing.List[SupportedProtocol]: + return [] if __name__ == "__main__": diff --git a/src/pywaveshare/boards/sim868/worker.py b/src/pywaveshare/boards/sim868/worker.py index 4cba2b5..111499b 100644 --- a/src/pywaveshare/boards/sim868/worker.py +++ b/src/pywaveshare/boards/sim868/worker.py @@ -8,37 +8,40 @@ class WorkerState(enum.Enum): - START = 0x0000 - LISTEN_TO_CLIENT = 0x0001 - LISTEN_TO_SERIAL = 0x0002 + STARTUP = 0x00 + RESTART = 0x01 + SHUTDOWN = 0x02 + LISTEN_TO_SOURCES = 0x03 + LISTEN_TO_CLIENT = 0x04 + LISTEN_TO_SERIAL = 0x05 class Worker(threading.Thread): logger = config.get_logger("worker") def __init__(self, config: config.Config) -> None: - super().__init__() + super().__init__(daemon=True) self.config = config - self.protocols = protocols.SupportedProtocolFactory() + self.protocols = protocols.get_enabled_protocols(self.config) + self.comm_port = serial.Serial(self.config.serial_port, self.config.baud_rate) def run(self) -> None: if not itc.IS_WORKER_RUNNING.is_set(): raise exceptions.WaveshareException("Event 'IS_WORKER_RUNNING' is not set.") - # TODO: initialize serial object and SIM868 - self.comm_port = serial.Serial(self.config.serial_port, self.config.baud_rate) - self.comm_port.open() - WORKER_STATE_MAPPING: typing.Dict[ - WorkerState, typing.Callable[..., typing.Any] + WorkerState, typing.Callable[..., typing.Union[WorkerState, None]] ] = { - WorkerState.START: self.on_start, + WorkerState.STARTUP: self.on_startup, + WorkerState.RESTART: self.on_restart, + WorkerState.SHUTDOWN: self.on_shutdown, + WorkerState.LISTEN_TO_SOURCES: self.on_listen_to_sources, WorkerState.LISTEN_TO_CLIENT: self.on_listen_to_client, WorkerState.LISTEN_TO_SERIAL: self.on_listen_to_serial, } - curr_state: typing.Optional[WorkerState] = WorkerState.START + curr_state: typing.Optional[WorkerState] = WorkerState.STARTUP next_state: typing.Optional[WorkerState] = None while itc.IS_WORKER_RUNNING.is_set(): @@ -48,24 +51,39 @@ def run(self) -> None: next_state = WORKER_STATE_MAPPING[curr_state]() curr_state = next_state - def on_start(self) -> WorkerState: + def on_startup(self) -> WorkerState: + self.comm_port.open() # TODO: initialize all enabled protocols + return WorkerState.LISTEN_TO_SOURCES + def on_restart(self) -> WorkerState: + pass + + def on_shutdown(self) -> None: + pass + + def on_listen_to_sources(self) -> WorkerState: with itc.ACQUIRE_CLIENT_TX_DATA: - if itc.CLIENT_TX_Q.qsize() > 0: + if self.is_client_data_available(): self.logger.debug("Client TX data available!") return WorkerState.LISTEN_TO_CLIENT with itc.ACQUIRE_SERIAL_RX_DATA: - if itc.SERIAL_RX_Q.qsize() > 0: + if self.is_serial_data_available(): self.logger.debug("Serial RX data available!") return WorkerState.LISTEN_TO_SERIAL - self.logger.debug("No data available :(") - return WorkerState.START + self.logger.debug("No data available :( Rechecking sources...") + return WorkerState.LISTEN_TO_SOURCES + + def is_client_data_available(self) -> bool: + return itc.CLIENT_TX_Q.qsize() > 0 def on_listen_to_client(self) -> WorkerState: - return WorkerState.START + return WorkerState.LISTEN_TO_SOURCES + + def is_serial_data_available(self) -> bool: + return itc.SERIAL_RX_Q.qsize() > 0 def on_listen_to_serial(self) -> WorkerState: - return WorkerState.START + return WorkerState.LISTEN_TO_SOURCES