Skip to content

Commit deb4b4b

Browse files
committed
WIP: Implement worker data listening mechanism p2
1 parent 7eb5afb commit deb4b4b

File tree

2 files changed

+43
-23
lines changed

2 files changed

+43
-23
lines changed

src/pywaveshare/boards/sim868/protocols.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
import re
33
import typing
44

5+
from . import config
56

6-
class SupportedStandard(abc.ABC):
7+
8+
class SupportedProtocol(abc.ABC):
79
NAME: typing.Optional[str] = None
810

911
RESPONSE_PATTERN = r""
1012

1113

12-
class SMS(SupportedStandard):
14+
class SMS(SupportedProtocol):
1315
NAME = "SMS"
1416

1517
RESPONSE_PATTERN = (
@@ -23,8 +25,8 @@ class SMS(SupportedStandard):
2325
)
2426

2527

26-
class SupportedProtocolFactory:
27-
pass
28+
def get_enabled_protocols(config: config.Config) -> typing.List[SupportedProtocol]:
29+
return []
2830

2931

3032
if __name__ == "__main__":

src/pywaveshare/boards/sim868/worker.py

+37-19
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,40 @@
88

99

1010
class WorkerState(enum.Enum):
11-
START = 0x0000
12-
LISTEN_TO_CLIENT = 0x0001
13-
LISTEN_TO_SERIAL = 0x0002
11+
STARTUP = 0x00
12+
RESTART = 0x01
13+
SHUTDOWN = 0x02
14+
LISTEN_TO_SOURCES = 0x03
15+
LISTEN_TO_CLIENT = 0x04
16+
LISTEN_TO_SERIAL = 0x05
1417

1518

1619
class Worker(threading.Thread):
1720
logger = config.get_logger("worker")
1821

1922
def __init__(self, config: config.Config) -> None:
20-
super().__init__()
23+
super().__init__(daemon=True)
2124

2225
self.config = config
23-
self.protocols = protocols.SupportedProtocolFactory()
26+
self.protocols = protocols.get_enabled_protocols(self.config)
27+
self.comm_port = serial.Serial(self.config.serial_port, self.config.baud_rate)
2428

2529
def run(self) -> None:
2630
if not itc.IS_WORKER_RUNNING.is_set():
2731
raise exceptions.WaveshareException("Event 'IS_WORKER_RUNNING' is not set.")
2832

29-
# TODO: initialize serial object and SIM868
30-
self.comm_port = serial.Serial(self.config.serial_port, self.config.baud_rate)
31-
self.comm_port.open()
32-
3333
WORKER_STATE_MAPPING: typing.Dict[
34-
WorkerState, typing.Callable[..., typing.Any]
34+
WorkerState, typing.Callable[..., typing.Union[WorkerState, None]]
3535
] = {
36-
WorkerState.START: self.on_start,
36+
WorkerState.STARTUP: self.on_startup,
37+
WorkerState.RESTART: self.on_restart,
38+
WorkerState.SHUTDOWN: self.on_shutdown,
39+
WorkerState.LISTEN_TO_SOURCES: self.on_listen_to_sources,
3740
WorkerState.LISTEN_TO_CLIENT: self.on_listen_to_client,
3841
WorkerState.LISTEN_TO_SERIAL: self.on_listen_to_serial,
3942
}
4043

41-
curr_state: typing.Optional[WorkerState] = WorkerState.START
44+
curr_state: typing.Optional[WorkerState] = WorkerState.STARTUP
4245
next_state: typing.Optional[WorkerState] = None
4346

4447
while itc.IS_WORKER_RUNNING.is_set():
@@ -48,24 +51,39 @@ def run(self) -> None:
4851
next_state = WORKER_STATE_MAPPING[curr_state]()
4952
curr_state = next_state
5053

51-
def on_start(self) -> WorkerState:
54+
def on_startup(self) -> WorkerState:
55+
self.comm_port.open()
5256
# TODO: initialize all enabled protocols
57+
return WorkerState.LISTEN_TO_SOURCES
5358

59+
def on_restart(self) -> WorkerState:
60+
pass
61+
62+
def on_shutdown(self) -> None:
63+
pass
64+
65+
def on_listen_to_sources(self) -> WorkerState:
5466
with itc.ACQUIRE_CLIENT_TX_DATA:
55-
if itc.CLIENT_TX_Q.qsize() > 0:
67+
if self.is_client_data_available():
5668
self.logger.debug("Client TX data available!")
5769
return WorkerState.LISTEN_TO_CLIENT
5870

5971
with itc.ACQUIRE_SERIAL_RX_DATA:
60-
if itc.SERIAL_RX_Q.qsize() > 0:
72+
if self.is_serial_data_available():
6173
self.logger.debug("Serial RX data available!")
6274
return WorkerState.LISTEN_TO_SERIAL
6375

64-
self.logger.debug("No data available :(")
65-
return WorkerState.START
76+
self.logger.debug("No data available :( Rechecking sources...")
77+
return WorkerState.LISTEN_TO_SOURCES
78+
79+
def is_client_data_available(self) -> bool:
80+
return itc.CLIENT_TX_Q.qsize() > 0
6681

6782
def on_listen_to_client(self) -> WorkerState:
68-
return WorkerState.START
83+
return WorkerState.LISTEN_TO_SOURCES
84+
85+
def is_serial_data_available(self) -> bool:
86+
return itc.SERIAL_RX_Q.qsize() > 0
6987

7088
def on_listen_to_serial(self) -> WorkerState:
71-
return WorkerState.START
89+
return WorkerState.LISTEN_TO_SOURCES

0 commit comments

Comments
 (0)