Skip to content

Commit

Permalink
WIP: Implement worker data listening mechanism p2
Browse files Browse the repository at this point in the history
  • Loading branch information
acmacunlay committed Oct 12, 2024
1 parent 7eb5afb commit deb4b4b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 23 deletions.
10 changes: 6 additions & 4 deletions src/pywaveshare/boards/sim868/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand All @@ -23,8 +25,8 @@ class SMS(SupportedStandard):
)


class SupportedProtocolFactory:
pass
def get_enabled_protocols(config: config.Config) -> typing.List[SupportedProtocol]:
return []


if __name__ == "__main__":
Expand Down
56 changes: 37 additions & 19 deletions src/pywaveshare/boards/sim868/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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

0 comments on commit deb4b4b

Please sign in to comment.