Merge pull request #254 from DJ2LS/ls-config-file

This commit is contained in:
DJ2LS 2022-10-04 08:01:11 +02:00 committed by GitHub
commit 8c6652aad2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 197 additions and 36 deletions

95
tnc/config.py Normal file
View file

@ -0,0 +1,95 @@
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['NETWORK'] = {'#Network settings': None,
'TNCPORT': data[50]
}
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:
self.log.error("[CFG] reading logfile", e=conferror)
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

View file

@ -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
@ -127,6 +128,9 @@ class DAEMON:
while True:
try:
data = self.daemon_queue.get()
# increase length of list for storing additional
# parameters starting at entry 64
data = data[:64] + [None] * (64 - len(data))
# data[1] mycall
# data[2] mygrid
@ -162,6 +166,8 @@ class DAEMON:
options.append("--port")
options.append(str(static.DAEMONPORT - 1))
# create an additional list entry for parameters not covered by gui
data[50] = int(static.DAEMONPORT - 1)
options.append("--mycall")
options.append(data[1])
@ -242,6 +248,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 +401,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

View file

@ -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 = int(config['NETWORK']['tncport'])
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