FreeDATA/modem/frame_handler.py

129 lines
3.7 KiB
Python
Raw Normal View History

import helpers
from event_manager import EventManager
from state_manager import StateManager
from queue import Queue
import structlog
2023-11-29 23:15:16 +00:00
import time, uuid
from codec2 import FREEDV_MODE
class FrameHandler():
2023-11-29 23:15:16 +00:00
def __init__(self, name: str, config, states: StateManager, event_manager: EventManager,
tx_frame_queue: Queue,
arq_sessions: list) -> None:
self.name = name
2023-11-29 23:15:16 +00:00
self.config = config
self.states = states
self.event_manager = event_manager
2023-11-29 23:15:16 +00:00
self.tx_frame_queue = tx_frame_queue
self.arq_sessions = arq_sessions
self.logger = structlog.get_logger("Frame Handler")
2023-11-29 23:15:16 +00:00
self.details = {
'frame' : None,
'snr' : 0,
'freq_offset': 0,
'freedv_inst': None,
'bytes_per_frame': 0
}
2023-12-03 09:40:31 +00:00
def add_to_activity_list(self):
frame = self.details['frame']
activity = {
"direction": "received",
"snr": self.details['snr'],
2023-12-03 09:41:20 +00:00
"offset": self.details['freq_offset'],
2023-12-03 09:40:31 +00:00
"activity_type": frame["frame_type"]
}
if "origin" in frame:
activity["origin"] = frame["origin"]
if "destination" in frame:
activity["destination"] = frame["destination"]
if "gridsquare" in frame:
activity["gridsquare"] = frame["gridsquare"]
if "session_id" in frame:
activity["session_id"] = frame["session_id"]
self.states.add_activity(activity)
def add_to_heard_stations(self):
2023-11-29 23:15:16 +00:00
frame = self.details['frame']
if 'origin' not in frame:
return
2023-11-29 23:15:16 +00:00
dxgrid = frame['gridsquare'] if 'gridsquare' in frame else "------"
helpers.add_to_heard_stations(
frame['origin'],
dxgrid,
self.name,
self.details['snr'],
self.details['freq_offset'],
self.states.radio_frequency,
self.states.heard_stations,
)
2023-11-29 23:15:16 +00:00
def make_event(self):
event = {
"freedata": "modem-message",
2023-11-30 07:44:18 +00:00
"received": self.details['frame']['frame_type'],
2023-11-29 23:15:16 +00:00
"uuid": str(uuid.uuid4()),
"timestamp": int(time.time()),
"mycallsign": self.config['STATION']['mycall'],
"snr": str(self.details['snr']),
}
if 'origin' in self.details['frame']:
event['dxcallsign'] = self.details['frame']['origin']
return event
2023-11-29 23:15:16 +00:00
def emit_event(self):
event_data = self.make_event()
self.event_manager.broadcast(event_data)
def make_modem_queue_item(self, mode, repeat, repeat_delay, frame):
return {
'mode': self.get_tx_mode(),
2023-11-29 23:15:16 +00:00
'repeat': repeat,
'repeat_delay': repeat_delay,
'frame': frame,
}
2023-11-29 23:15:16 +00:00
def get_tx_mode(self):
return (
FREEDV_MODE.fsk_ldpc_0.value
if self.config['MODEM']['enable_fsk']
else FREEDV_MODE.sig0.value
)
def transmit(self, frame):
tx_queue_item = self.make_modem_queue_item(self.get_tx_mode(), 1, 0, frame)
self.tx_frame_queue.put(tx_queue_item)
def follow_protocol(self):
pass
2023-11-29 23:15:16 +00:00
def log(self):
self.logger.info(f"[Frame Handler] Handling frame {self.details['frame']}")
pass
def handle(self, frame, snr, freq_offset, freedv_inst, bytes_per_frame):
2023-11-29 23:15:16 +00:00
self.details['frame'] = frame
self.details['snr'] = snr
self.details['freq_offset'] = freq_offset
self.details['freedv_inst'] = freedv_inst
self.details['bytes_per_frame'] = bytes_per_frame
self.log()
self.add_to_heard_stations()
2023-12-03 09:40:31 +00:00
self.add_to_activity_list()
2023-11-29 23:15:16 +00:00
self.emit_event()
self.follow_protocol()