FreeDATA/modem/command.py

66 lines
2 KiB
Python
Raw Normal View History

2023-11-23 15:59:53 +00:00
from data_frame_factory import DataFrameFactory
2023-11-25 17:21:41 +00:00
import queue
from codec2 import FREEDV_MODE
2023-12-04 10:54:03 +00:00
import structlog
2023-12-05 14:40:04 +00:00
from state_manager import StateManager
from arq_data_type_handler import ARQDataTypeHandler
2023-11-23 15:59:53 +00:00
class TxCommand():
2024-03-16 14:52:14 +00:00
def __init__(self, config: dict, state_manager: StateManager, event_manager, apiParams:dict = {}, socket_command_handler=None):
2023-11-25 17:21:41 +00:00
self.config = config
2024-02-02 18:37:02 +00:00
self.logger = structlog.get_logger(type(self).__name__)
self.state_manager = state_manager
self.event_manager = event_manager
2023-11-25 17:21:41 +00:00
self.set_params_from_api(apiParams)
self.frame_factory = DataFrameFactory(config)
2024-02-02 18:37:02 +00:00
self.arq_data_type_handler = ARQDataTypeHandler(event_manager, state_manager)
2024-03-16 14:52:14 +00:00
self.socket_command_handler = socket_command_handler
2024-02-02 18:37:02 +00:00
def log(self, message, isWarning = False):
msg = f"[{type(self).__name__}]: {message}"
logger = self.logger.warn if isWarning else self.logger.info
logger(msg)
2023-11-23 15:59:53 +00:00
2023-11-25 17:21:41 +00:00
def set_params_from_api(self, apiParams):
2023-11-23 15:59:53 +00:00
pass
2023-11-25 17:21:41 +00:00
def get_name(self):
2023-11-24 09:46:51 +00:00
return type(self).__name__
def emit_event(self, event_queue):
2023-11-23 15:59:53 +00:00
pass
2023-11-25 17:21:41 +00:00
def log_message(self):
2023-11-28 19:34:33 +00:00
return f"Running {self.get_name()}"
2023-11-25 17:21:41 +00:00
def build_frame(self):
pass
2023-11-23 15:59:53 +00:00
def get_tx_mode(self):
2023-12-16 09:28:30 +00:00
return FREEDV_MODE.signalling
def make_modem_queue_item(self, mode, repeat, repeat_delay, frame):
2023-11-28 19:34:33 +00:00
return {
'mode': mode,
'repeat': repeat,
'repeat_delay': repeat_delay,
2023-11-28 19:34:33 +00:00
'frame': frame,
}
def transmit(self, modem):
frame = self.build_frame()
modem.transmit(self.get_tx_mode(), 1, 0, frame)
2023-11-23 15:59:53 +00:00
def run(self, event_queue: queue.Queue, modem):
self.emit_event(event_queue)
2023-11-26 12:11:45 +00:00
self.logger.info(self.log_message())
self.transmit(modem)
2023-12-16 11:54:16 +00:00
def test(self, event_queue: queue.Queue):
self.emit_event(event_queue)
self.logger.info(self.log_message())
frame = self.build_frame()
return frame