FreeDATA/modem/data_handler_broadcasts.py

195 lines
5.8 KiB
Python
Raw Normal View History

2023-11-19 13:56:29 +00:00
import time
from modem_frametypes import FRAME_TYPE as FR_TYPE
2023-11-19 16:09:47 +00:00
from codec2 import FREEDV_MODE
2023-11-19 13:56:29 +00:00
from queues import MODEM_TRANSMIT_QUEUE
import helpers
2023-11-19 16:09:47 +00:00
from random import randrange
import uuid
import structlog
2023-11-20 08:00:07 +00:00
import event_manager
2023-11-26 14:23:20 +00:00
import command_qrv
2023-11-19 16:09:47 +00:00
2023-11-23 09:14:11 +00:00
from data_handler import DATA
2023-11-19 16:09:47 +00:00
TESTMODE = False
2023-11-23 09:14:11 +00:00
class BROADCAST(DATA):
2023-11-19 13:56:29 +00:00
2023-11-19 16:09:47 +00:00
def __init__(self, config, event_queue, states):
2023-11-23 09:14:11 +00:00
super().__init__(config, event_queue, states)
2023-11-19 16:09:47 +00:00
self.log = structlog.get_logger("DHBC")
self.states = states
self.event_queue = event_queue
self.config = config
2023-11-20 08:00:07 +00:00
self.event_manager = event_manager.EventManager([event_queue])
# length of signalling frame
self.length_sig0_frame = 14
self.modem_frequency_offset = 0
# load config
self.mycallsign = config['STATION']['mycall']
self.myssid = config['STATION']['myssid']
self.mycallsign += "-" + str(self.myssid)
encoded_call = helpers.callsign_to_bytes(self.mycallsign)
self.mycallsign_bytes = helpers.bytes_to_callsign(encoded_call)
self.mygrid = config['STATION']['mygrid']
self.enable_fsk = config['MODEM']['enable_fsk']
self.respond_to_cq = config['MODEM']['respond_to_cq']
self.respond_to_call = True
self.duration_datac13 = 2.0
self.duration_sig1_frame = self.duration_datac13
def received_cq(self, frame_data, snr) -> None:
2023-11-19 13:56:29 +00:00
"""
Called when we receive a CQ frame
Args:
data_in:bytes:
Returns:
Nothing
"""
# here we add the received station to the heard stations buffer
dxcallsign = frame_data['origin']
2023-11-19 13:56:29 +00:00
self.log.debug("[Modem] received_cq:", dxcallsign=dxcallsign)
self.dxgrid = frame_data['gridsquare']
2023-11-19 13:56:29 +00:00
2023-11-20 08:00:07 +00:00
self.event_manager.send_custom_event(
2023-11-20 06:39:54 +00:00
freedata="modem-message",
cq="received",
mycallsign=self.mycallsign,
dxcallsign=dxcallsign,
dxgrid=self.dxgrid,
2023-11-20 06:39:54 +00:00
)
2023-11-19 13:56:29 +00:00
self.log.info(
"[Modem] CQ RCVD ["
+ dxcallsign
2023-11-19 13:56:29 +00:00
+ "]["
+ self.dxgrid
2023-11-19 13:56:29 +00:00
+ "] ",
snr=snr,
)
helpers.add_to_heard_stations(
dxcallsign,
self.dxgrid,
"CQ CQ CQ",
snr,
self.modem_frequency_offset,
self.states.radio_frequency,
self.states.heard_stations
)
# Sleep a random amount of time before responding to make it more likely to be
# heard when many stations respond. Each DATAC0 frame is 0.44 sec (440ms) in
# duration, plus overhead. Set the wait interval to be random between 0 and
# self.duration_sig1_frame * 4 == 4 slots
# in self.duration_sig1_frame increments.
2023-11-26 14:23:20 +00:00
if self.respond_to_cq and self.respond_to_call:
params = {'snr': snr, 'dxcall': dxcallsign}
cmd = command_qrv.QRVCommand(self.config, self.log, params)
cmd.run(self.event_queue, MODEM_TRANSMIT_QUEUE)
2023-11-19 13:56:29 +00:00
def received_qrv(self, data_in: bytes, snr) -> None:
"""
Called when we receive a QRV frame
Args:
data_in:bytes:
"""
# here we add the received station to the heard stations buffer
dxcallsign = helpers.bytes_to_callsign(bytes(data_in[1:7]))
self.dxgrid = bytes(helpers.decode_grid(data_in[7:11]), "UTF-8")
dxsnr = helpers.snr_from_bytes(data_in[11:12])
combined_snr = f"{snr}/{dxsnr}"
2023-11-20 08:00:07 +00:00
self.event_manager.send_custom_event(
2023-11-20 06:39:54 +00:00
freedata="modem-message",
qrv="received",
dxcallsign=str(dxcallsign, "UTF-8"),
dxgrid=str(self.dxgrid, "UTF-8"),
snr=str(snr),
dxsnr=str(dxsnr)
)
2023-11-19 13:56:29 +00:00
self.log.info(
"[Modem] QRV RCVD ["
+ str(dxcallsign, "UTF-8")
+ "]["
+ str(self.dxgrid, "UTF-8")
+ "] ",
snr=snr,
dxsnr=dxsnr
)
helpers.add_to_heard_stations(
dxcallsign,
self.dxgrid,
"QRV",
combined_snr,
self.modem_frequency_offset,
self.states.radio_frequency,
self.states.heard_stations
)
def received_is_writing(self, data_in: bytes, snr) -> None:
"""
Called when we receive a IS WRITING frame
Args:
data_in:bytes:
"""
# here we add the received station to the heard stations buffer
dxcallsign = helpers.bytes_to_callsign(bytes(data_in[1:7]))
2023-11-20 08:00:07 +00:00
self.event_manager.send_custom_event(
2023-11-19 13:56:29 +00:00
freedata="modem-message",
fec="is_writing",
dxcallsign=str(dxcallsign, "UTF-8")
)
self.log.info(
"[Modem] IS_WRITING RCVD ["
+ str(dxcallsign, "UTF-8")
+ "] ",
)
2023-11-26 14:23:20 +00:00
# ----------- BROADCASTS
2023-11-29 17:51:30 +00:00
def received_beacon(self, frame_data, snr) -> None:
2023-11-19 16:09:47 +00:00
"""
Called if we received a beacon
Args:
data_in:bytes:
"""
# here we add the received station to the heard stations buffer
2023-11-29 17:51:30 +00:00
beacon_callsign = frame_data['origin']
self.dxgrid = frame_data['gridsquare']
2023-11-27 22:08:00 +00:00
2023-11-20 08:00:07 +00:00
self.event_manager.send_custom_event(
2023-11-19 16:09:47 +00:00
freedata="modem-message",
beacon="received",
uuid=str(uuid.uuid4()),
timestamp=int(time.time()),
2023-11-29 17:51:30 +00:00
dxcallsign=beacon_callsign,
dxgrid=self.dxgrid,
2023-11-19 16:09:47 +00:00
snr=str(snr),
)
self.log.info(
2023-11-29 17:51:30 +00:00
f"[Modem] BEACON RCVD [{beacon_callsign}][{self.dxgrid}]",
2023-11-19 16:09:47 +00:00
snr=snr,
)
helpers.add_to_heard_stations(
beacon_callsign,
self.dxgrid,
"BEACON",
snr,
self.modem_frequency_offset,
self.states.radio_frequency,
self.states.heard_stations
2023-11-20 08:00:07 +00:00
)