From 4f199744bd79faa0ea2b88d8ae88c3a408622bab Mon Sep 17 00:00:00 2001 From: dj2ls Date: Tue, 20 Sep 2022 11:34:28 +0200 Subject: [PATCH] first config file attempt --- tnc/config.py | 97 +++++++++++++++++++++++++++++++++++++++ tnc/daemon.py | 9 +++- tnc/main.py | 124 ++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 194 insertions(+), 36 deletions(-) create mode 100644 tnc/config.py diff --git a/tnc/config.py b/tnc/config.py new file mode 100644 index 00000000..937fc759 --- /dev/null +++ b/tnc/config.py @@ -0,0 +1,97 @@ +import configparser +import structlog + +class CONFIG: + """ + CONFIG class for handling with config files + + """ + + def __init__(self): + # set up logger + self.log = structlog.get_logger("CONFIG") + + # init configparser + self.config = configparser.ConfigParser(inline_comment_prefixes="#", allow_no_value=True) + self.config_name = "config.ini" + + self.log.info("[CFG] logfile init", file=self.config_name) + + # check if log file exists + self.config_exists() + + def config_exists(self): + """ + check if config file exists + """ + try: + return bool(self.config.read(self.config_name, None)) + except Exception as configerror: + self.log.error("[CFG] logfile init error", e=configerror) + return False + + def write_config(self, section: str, key: str, value): + """ + write values to config + """ + + def write_entire_config(self, data): + """ + write entire config + """ + #self.config['DEFAULT'] = {'':''} + + self.config['NETWORK'] = {'#Network settings': None, + 'TNCPORT': 0 + } + + self.config['STATION'] = {'#Station settings': None, + 'mycall' : data[1], + 'mygrid' : data[2] + } + + self.config['AUDIO'] = {'#Audio settings': None, + 'rx': data[3], + 'tx': data[4], + 'txaudiolevel': data[22] + + } + self.config['RADIO'] = {'#Radio settings': None, + 'radiocontrol': data[13], + 'devicename': data[5], + 'deviceport': data[6], + 'serialspeed': data[7], + 'pttprotocol': data[8], + 'pttport': data[9], + 'data_bits': data[10], + 'stop_bits': data[11], + 'handshake': data[12], + 'rigctld_ip': data[14], + 'rigctld_port': data[15] + } + self.config['TNC'] = {'#TNC settings': None, + 'scatter': data[16], + 'fft': data[17], + 'narrowband': data[18], + 'fmin': data[19], + 'fmax': data[20], + 'qrv': data[23], + 'rxbuffersize': data[24] + } + try: + with open(self.config_name, 'w') as configfile: + self.config.write(configfile) + except Exception as conferror: + pass + + + def read_config(self): + """ + read config file + """ + if self.config_exists(): + #print(self.config.read(self.config_name)) + #print(self.config.sections()) + + return self.config + diff --git a/tnc/daemon.py b/tnc/daemon.py index dc638b47..614dbbba 100755 --- a/tnc/daemon.py +++ b/tnc/daemon.py @@ -21,7 +21,6 @@ import subprocess import sys import threading import time - import audio import crcengine import log_handler @@ -30,6 +29,8 @@ import sock import static import structlog import ujson as json +import config + # signal handler for closing application @@ -242,6 +243,9 @@ class DAEMON: options.append("--rx-buffer-size") options.append(data[24]) + # safe data to config file + config.write_entire_config(data) + # Try running tnc from binary, else run from source # This helps running the tnc in a developer environment try: @@ -392,6 +396,9 @@ if __name__ == "__main__": except Exception as err: mainlog.error("[DMN] logger init error", exception=err) + # init config + config = config.CONFIG() + try: mainlog.info("[DMN] Starting TCP/IP socket", port=static.DAEMONPORT) # https://stackoverflow.com/a/16641793 diff --git a/tnc/main.py b/tnc/main.py index 64328580..9eac1dea 100755 --- a/tnc/main.py +++ b/tnc/main.py @@ -15,7 +15,7 @@ import socketserver import sys import threading import time - +import config import data_handler import helpers import log_handler @@ -48,8 +48,19 @@ if __name__ == "__main__": multiprocessing.freeze_support() # --------------------------------------------GET PARAMETER INPUTS PARSER = argparse.ArgumentParser(description="FreeDATA TNC") + PARSER.add_argument( - "--mycall", dest="mycall", default="AA0AA", help="My callsign", type=str + "--use-config", + dest="configfile", + action="store_true", + help="Use the default config file config.ini", + ) + PARSER.add_argument( + "--mycall", + dest="mycall", + default="AA0AA", + help="My callsign", + type=str ) PARSER.add_argument( "--ssid", @@ -60,7 +71,11 @@ if __name__ == "__main__": type=str, ) PARSER.add_argument( - "--mygrid", dest="mygrid", default="JN12AA", help="My gridsquare", type=str + "--mygrid", + dest="mygrid", + default="JN12AA", + help="My gridsquare", + type=str ) PARSER.add_argument( "--rx", @@ -231,42 +246,81 @@ if __name__ == "__main__": help="Set the maximum size of rx buffer.", type=int, ) + ARGS = PARSER.parse_args() + if ARGS.configfile: + # init config + config = config.CONFIG().read_config() - # additional step for being sure our callsign is correctly - # in case we are not getting a station ssid - # then we are forcing a station ssid = 0 - mycallsign = bytes(ARGS.mycall.upper(), "utf-8") - mycallsign = helpers.callsign_to_bytes(mycallsign) - static.MYCALLSIGN = helpers.bytes_to_callsign(mycallsign) - static.MYCALLSIGN_CRC = helpers.get_crc_24(static.MYCALLSIGN) + # additional step for being sure our callsign is correctly + # in case we are not getting a station ssid + # then we are forcing a station ssid = 0 + mycallsign = bytes(config['STATION']['mycall'], "utf-8") + mycallsign = helpers.callsign_to_bytes(mycallsign) + static.MYCALLSIGN = helpers.bytes_to_callsign(mycallsign) + static.MYCALLSIGN_CRC = helpers.get_crc_24(static.MYCALLSIGN) - static.SSID_LIST = ARGS.ssid_list + static.SSID_LIST = [] #### + static.MYGRID = bytes(config['STATION']['mygrid'], "utf-8") + static.AUDIO_INPUT_DEVICE = int(config['AUDIO']['rx']) + static.AUDIO_OUTPUT_DEVICE = int(config['AUDIO']['tx']) + static.PORT = 0 #### + static.HAMLIB_DEVICE_NAME = config['RADIO']['devicename'] + static.HAMLIB_DEVICE_PORT = config['RADIO']['deviceport'] + static.HAMLIB_PTT_TYPE = config['RADIO']['pttprotocol'] + static.HAMLIB_PTT_PORT = config['RADIO']['pttport'] + static.HAMLIB_SERIAL_SPEED = str(config['RADIO']['serialspeed']) + static.HAMLIB_DATA_BITS = str(config['RADIO']['data_bits']) + static.HAMLIB_STOP_BITS = str(config['RADIO']['stop_bits']) + static.HAMLIB_HANDSHAKE = config['RADIO']['handshake'] + static.HAMLIB_RADIOCONTROL = config['RADIO']['radiocontrol'] + static.HAMLIB_RIGCTLD_IP = config['RADIO']['rigctld_ip'] + static.HAMLIB_RIGCTLD_PORT = str(config['RADIO']['rigctld_port']) + static.ENABLE_SCATTER = config['TNC']['scatter'] + static.ENABLE_FFT = config['TNC']['fft'] + static.ENABLE_FSK = False + static.LOW_BANDWIDTH_MODE = config['TNC']['narrowband'] + static.TUNING_RANGE_FMIN = float(config['TNC']['fmin']) + static.TUNING_RANGE_FMAX = float(config['TNC']['fmax']) + static.TX_AUDIO_LEVEL = config['AUDIO']['txaudiolevel'] + static.RESPOND_TO_CQ = config['TNC']['qrv'] + static.RX_BUFFER_SIZE = config['TNC']['rxbuffersize'] - static.MYGRID = bytes(ARGS.mygrid, "utf-8") - static.AUDIO_INPUT_DEVICE = ARGS.audio_input_device - static.AUDIO_OUTPUT_DEVICE = ARGS.audio_output_device - static.PORT = ARGS.socket_port - static.HAMLIB_DEVICE_NAME = ARGS.hamlib_device_name - static.HAMLIB_DEVICE_PORT = ARGS.hamlib_device_port - static.HAMLIB_PTT_TYPE = ARGS.hamlib_ptt_type - static.HAMLIB_PTT_PORT = ARGS.hamlib_ptt_port - static.HAMLIB_SERIAL_SPEED = str(ARGS.hamlib_serialspeed) - static.HAMLIB_DATA_BITS = str(ARGS.hamlib_data_bits) - static.HAMLIB_STOP_BITS = str(ARGS.hamlib_stop_bits) - static.HAMLIB_HANDSHAKE = ARGS.hamlib_handshake - static.HAMLIB_RADIOCONTROL = ARGS.hamlib_radiocontrol - static.HAMLIB_RIGCTLD_IP = ARGS.rigctld_ip - static.HAMLIB_RIGCTLD_PORT = str(ARGS.rigctld_port) - static.ENABLE_SCATTER = ARGS.send_scatter - static.ENABLE_FFT = ARGS.send_fft - static.ENABLE_FSK = ARGS.enable_fsk - static.LOW_BANDWIDTH_MODE = ARGS.low_bandwidth_mode - static.TUNING_RANGE_FMIN = ARGS.tuning_range_fmin - static.TUNING_RANGE_FMAX = ARGS.tuning_range_fmax - static.TX_AUDIO_LEVEL = ARGS.tx_audio_level - static.RESPOND_TO_CQ = ARGS.enable_respond_to_cq - static.RX_BUFFER_SIZE = ARGS.rx_buffer_size + + else: + # additional step for being sure our callsign is correctly + # in case we are not getting a station ssid + # then we are forcing a station ssid = 0 + mycallsign = bytes(ARGS.mycall.upper(), "utf-8") + mycallsign = helpers.callsign_to_bytes(mycallsign) + static.MYCALLSIGN = helpers.bytes_to_callsign(mycallsign) + static.MYCALLSIGN_CRC = helpers.get_crc_24(static.MYCALLSIGN) + + static.SSID_LIST = ARGS.ssid_list + static.MYGRID = bytes(ARGS.mygrid, "utf-8") + static.AUDIO_INPUT_DEVICE = ARGS.audio_input_device + static.AUDIO_OUTPUT_DEVICE = ARGS.audio_output_device + static.PORT = ARGS.socket_port + static.HAMLIB_DEVICE_NAME = ARGS.hamlib_device_name + static.HAMLIB_DEVICE_PORT = ARGS.hamlib_device_port + static.HAMLIB_PTT_TYPE = ARGS.hamlib_ptt_type + static.HAMLIB_PTT_PORT = ARGS.hamlib_ptt_port + static.HAMLIB_SERIAL_SPEED = str(ARGS.hamlib_serialspeed) + static.HAMLIB_DATA_BITS = str(ARGS.hamlib_data_bits) + static.HAMLIB_STOP_BITS = str(ARGS.hamlib_stop_bits) + static.HAMLIB_HANDSHAKE = ARGS.hamlib_handshake + static.HAMLIB_RADIOCONTROL = ARGS.hamlib_radiocontrol + static.HAMLIB_RIGCTLD_IP = ARGS.rigctld_ip + static.HAMLIB_RIGCTLD_PORT = str(ARGS.rigctld_port) + static.ENABLE_SCATTER = ARGS.send_scatter + static.ENABLE_FFT = ARGS.send_fft + static.ENABLE_FSK = ARGS.enable_fsk + static.LOW_BANDWIDTH_MODE = ARGS.low_bandwidth_mode + static.TUNING_RANGE_FMIN = ARGS.tuning_range_fmin + static.TUNING_RANGE_FMAX = ARGS.tuning_range_fmax + static.TX_AUDIO_LEVEL = ARGS.tx_audio_level + static.RESPOND_TO_CQ = ARGS.enable_respond_to_cq + static.RX_BUFFER_SIZE = ARGS.rx_buffer_size # we need to wait until we got all parameters from argparse first before we can load the other modules import sock