Skip to content

Commit

Permalink
WIP: Implement worker data listening mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
acmacunlay committed Oct 10, 2024
1 parent 1210f72 commit dd21ec7
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
- name: Run common security issue checker
run: |
bandit -c pyproject.toml -r .
- name: Run unit tests + code coverage
run: |
coverage run -m pytest .
13 changes: 5 additions & 8 deletions src/pywaveshare/boards/sim868/itc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@

IS_WORKER_RUNNING = threading.Event()

SERIAL_LOCK = threading.Lock()
C2W_Q_LOCK = threading.Lock()
"""
If acquired, then the acquirer can to safely work on `CLIENT_TO_WORKER_Q` queue.
"""
ACQUIRE_CLIENT_TX_DATA = threading.Lock()
ACQUIRE_SERIAL_RX_DATA = threading.Lock()

CLIENT_TO_WORKER_Q: queue.Queue[bytes] = queue.Queue()
WORKER_TO_SERIAL_Q: queue.Queue[bytes] = queue.Queue()
SERIAL_TO_WORKER_Q: queue.Queue[bytes] = queue.Queue()
CLIENT_TX_Q: queue.Queue[bytes] = queue.Queue()
SERIAL_RX_Q: queue.Queue[bytes] = queue.Queue()
SERIAL_TX_Q: queue.Queue[bytes] = queue.Queue()
5 changes: 5 additions & 0 deletions src/pywaveshare/boards/sim868/protocols.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import abc
import re
import typing


Expand Down Expand Up @@ -29,3 +30,7 @@ class SupportedProtocolFactory:
if __name__ == "__main__":
protocol = SMS()
print(protocol.RESPONSE_PATTERN)
DATA = "OK\r\n"
match = re.fullmatch(r"OK\r\n", DATA)
print(match)
print(match.string if match is not None else None)
58 changes: 56 additions & 2 deletions src/pywaveshare/boards/sim868/worker.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import enum
import threading
import typing

from . import config, itc, protocols
import serial

from . import config, exceptions, itc, protocols


class WorkerState(enum.Enum):
START = 0x0000
LISTEN_TO_CLIENT = 0x0001
LISTEN_TO_SERIAL = 0x0002


class Worker(threading.Thread):
Expand All @@ -13,5 +23,49 @@ def __init__(self, config: config.Config) -> None:
self.protocols = protocols.SupportedProtocolFactory()

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.START: self.on_start,
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
next_state: typing.Optional[WorkerState] = None

while itc.IS_WORKER_RUNNING.is_set():
pass
self.logger.debug(f"Current State: {curr_state}")
if curr_state is None:
break
next_state = WORKER_STATE_MAPPING[curr_state]()
curr_state = next_state

def on_start(self) -> WorkerState:
# TODO: initialize all enabled protocols

with itc.ACQUIRE_CLIENT_TX_DATA:
if itc.CLIENT_TX_Q.qsize() > 0:
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:
self.logger.debug("Serial RX data available!")
return WorkerState.LISTEN_TO_SERIAL

self.logger.debug("No data available :(")
return WorkerState.START

def on_listen_to_client(self) -> WorkerState:
return WorkerState.START

def on_listen_to_serial(self) -> WorkerState:
return WorkerState.START

0 comments on commit dd21ec7

Please sign in to comment.