Extract queues from data_handler and modem.

Eliminates a cyclical import identified by pylint.
This commit is contained in:
Paul Kronenwetter 2022-07-03 13:41:06 -04:00
parent 9b7056a774
commit 7f649f98df
4 changed files with 24 additions and 20 deletions

View file

@ -8,7 +8,6 @@ Created on Sun Dec 27 20:43:40 2020
# pylint: disable=import-outside-toplevel, attribute-defined-outside-init # pylint: disable=import-outside-toplevel, attribute-defined-outside-init
import base64 import base64
import queue
import sys import sys
import threading import threading
import time import time
@ -26,13 +25,11 @@ import structlog
import ujson as json import ujson as json
from codec2 import FREEDV_MODE from codec2 import FREEDV_MODE
from exceptions import NoCallsign from exceptions import NoCallsign
from queues import DATA_QUEUE_RECEIVED, DATA_QUEUE_TRANSMIT
from static import FRAME_TYPE as FR_TYPE from static import FRAME_TYPE as FR_TYPE
TESTMODE = False TESTMODE = False
DATA_QUEUE_TRANSMIT = queue.Queue()
DATA_QUEUE_RECEIVED = queue.Queue()
class DATA: class DATA:
"""Terminal Node Controller for FreeDATA""" """Terminal Node Controller for FreeDATA"""

View file

@ -11,28 +11,24 @@ Created on Wed Dec 23 07:04:24 2020
import atexit import atexit
import ctypes import ctypes
import os import os
import queue
import sys import sys
import threading import threading
import time import time
from collections import deque from collections import deque
import codec2 import codec2
import data_handler
import numpy as np import numpy as np
import sock import sock
import sounddevice as sd import sounddevice as sd
import static import static
import structlog import structlog
import ujson as json import ujson as json
from queues import DATA_QUEUE_RECEIVED, MODEM_RECEIVED_QUEUE, MODEM_TRANSMIT_QUEUE
TESTMODE = False TESTMODE = False
RXCHANNEL = "" RXCHANNEL = ""
TXCHANNEL = "" TXCHANNEL = ""
# Initialize FIFO queue to store received frames
MODEM_RECEIVED_QUEUE = queue.Queue()
MODEM_TRANSMIT_QUEUE = queue.Queue()
static.TRANSMITTING = False static.TRANSMITTING = False
# Receive only specific modes to reduce CPU load # Receive only specific modes to reduce CPU load
@ -690,7 +686,7 @@ class RF:
# data[0] = bytes_out # data[0] = bytes_out
# data[1] = freedv session # data[1] = freedv session
# data[2] = bytes_per_frame # data[2] = bytes_per_frame
data_handler.DATA_QUEUE_RECEIVED.put([data[0], data[1], data[2]]) DATA_QUEUE_RECEIVED.put([data[0], data[1], data[2]])
self.modem_received_queue.task_done() self.modem_received_queue.task_done()
def get_frequency_offset(self, freedv: ctypes.c_void_p) -> float: def get_frequency_offset(self, freedv: ctypes.c_void_p) -> float:

11
tnc/queues.py Normal file
View file

@ -0,0 +1,11 @@
"""
Hold queues used by more than one module to eliminate cyclic imports.
"""
import queue
DATA_QUEUE_TRANSMIT = queue.Queue()
DATA_QUEUE_RECEIVED = queue.Queue()
# Initialize FIFO queue to store received frames
MODEM_RECEIVED_QUEUE = queue.Queue()
MODEM_TRANSMIT_QUEUE = queue.Queue()

View file

@ -25,12 +25,12 @@ import sys
import threading import threading
import time import time
import data_handler
import helpers import helpers
import static import static
import structlog import structlog
import ujson as json import ujson as json
from exceptions import NoCallsign from exceptions import NoCallsign
from queues import DATA_QUEUE_TRANSMIT
SOCKET_QUEUE = queue.Queue() SOCKET_QUEUE = queue.Queue()
DAEMON_QUEUE = queue.Queue() DAEMON_QUEUE = queue.Queue()
@ -227,7 +227,7 @@ def process_tnc_commands(data):
and received_json["command"] == "send_test_frame" and received_json["command"] == "send_test_frame"
): ):
try: try:
data_handler.DATA_QUEUE_TRANSMIT.put(["SEND_TEST_FRAME"]) DATA_QUEUE_TRANSMIT.put(["SEND_TEST_FRAME"])
command_response("send_test_frame", True) command_response("send_test_frame", True)
except Exception as err: except Exception as err:
command_response("send_test_frame", False) command_response("send_test_frame", False)
@ -240,7 +240,7 @@ def process_tnc_commands(data):
# CQ CQ CQ ----------------------------------------------------- # CQ CQ CQ -----------------------------------------------------
if received_json["command"] == "cqcqcq": if received_json["command"] == "cqcqcq":
try: try:
data_handler.DATA_QUEUE_TRANSMIT.put(["CQ"]) DATA_QUEUE_TRANSMIT.put(["CQ"])
command_response("cqcqcq", True) command_response("cqcqcq", True)
except Exception as err: except Exception as err:
@ -254,7 +254,7 @@ def process_tnc_commands(data):
try: try:
static.BEACON_STATE = True static.BEACON_STATE = True
interval = int(received_json["parameter"]) interval = int(received_json["parameter"])
data_handler.DATA_QUEUE_TRANSMIT.put(["BEACON", interval, True]) DATA_QUEUE_TRANSMIT.put(["BEACON", interval, True])
command_response("start_beacon", True) command_response("start_beacon", True)
except Exception as err: except Exception as err:
command_response("start_beacon", False) command_response("start_beacon", False)
@ -269,7 +269,7 @@ def process_tnc_commands(data):
try: try:
log.warning("[SCK] Stopping beacon!") log.warning("[SCK] Stopping beacon!")
static.BEACON_STATE = False static.BEACON_STATE = False
data_handler.DATA_QUEUE_TRANSMIT.put(["BEACON", None, False]) DATA_QUEUE_TRANSMIT.put(["BEACON", None, False])
command_response("stop_beacon", True) command_response("stop_beacon", True)
except Exception as err: except Exception as err:
command_response("stop_beacon", False) command_response("stop_beacon", False)
@ -293,7 +293,7 @@ def process_tnc_commands(data):
dxcallsign = helpers.callsign_to_bytes(dxcallsign) dxcallsign = helpers.callsign_to_bytes(dxcallsign)
dxcallsign = helpers.bytes_to_callsign(dxcallsign) dxcallsign = helpers.bytes_to_callsign(dxcallsign)
data_handler.DATA_QUEUE_TRANSMIT.put(["PING", dxcallsign]) DATA_QUEUE_TRANSMIT.put(["PING", dxcallsign])
command_response("ping", True) command_response("ping", True)
except NoCallsign: except NoCallsign:
command_response("ping", False) command_response("ping", False)
@ -320,7 +320,7 @@ def process_tnc_commands(data):
static.DXCALLSIGN = dxcallsign static.DXCALLSIGN = dxcallsign
static.DXCALLSIGN_CRC = helpers.get_crc_24(static.DXCALLSIGN) static.DXCALLSIGN_CRC = helpers.get_crc_24(static.DXCALLSIGN)
data_handler.DATA_QUEUE_TRANSMIT.put(["CONNECT", dxcallsign]) DATA_QUEUE_TRANSMIT.put(["CONNECT", dxcallsign])
command_response("connect", True) command_response("connect", True)
except Exception as err: except Exception as err:
command_response("connect", False) command_response("connect", False)
@ -334,7 +334,7 @@ def process_tnc_commands(data):
if received_json["type"] == "arq" and received_json["command"] == "disconnect": if received_json["type"] == "arq" and received_json["command"] == "disconnect":
# send ping frame and wait for ACK # send ping frame and wait for ACK
try: try:
data_handler.DATA_QUEUE_TRANSMIT.put(["DISCONNECT"]) DATA_QUEUE_TRANSMIT.put(["DISCONNECT"])
command_response("disconnect", True) command_response("disconnect", True)
except Exception as err: except Exception as err:
command_response("disconnect", False) command_response("disconnect", False)
@ -383,7 +383,7 @@ def process_tnc_commands(data):
binarydata = base64.b64decode(base64data) binarydata = base64.b64decode(base64data)
data_handler.DATA_QUEUE_TRANSMIT.put( DATA_QUEUE_TRANSMIT.put(
["ARQ_RAW", binarydata, mode, n_frames, arq_uuid, mycallsign] ["ARQ_RAW", binarydata, mode, n_frames, arq_uuid, mycallsign]
) )
@ -402,7 +402,7 @@ def process_tnc_commands(data):
): ):
try: try:
if static.TNC_STATE == "BUSY" or static.ARQ_STATE: if static.TNC_STATE == "BUSY" or static.ARQ_STATE:
data_handler.DATA_QUEUE_TRANSMIT.put(["STOP"]) DATA_QUEUE_TRANSMIT.put(["STOP"])
log.warning("[SCK] Stopping transmission!") log.warning("[SCK] Stopping transmission!")
static.TNC_STATE = "IDLE" static.TNC_STATE = "IDLE"
static.ARQ_STATE = False static.ARQ_STATE = False