From 43aa4696337a21bbea6348bd269adbfca7cb2bd1 Mon Sep 17 00:00:00 2001 From: dj2ls Date: Mon, 24 Jan 2022 22:01:01 +0100 Subject: [PATCH] hotfix for rigctld fixing wrong ip and improved error handling so rigctld is hopefully not crashing for the early testers out there --- tnc/data_handler.py | 2 + tnc/rigctld.py | 128 ++++++++++++++++++++++++++------------------ tnc/sock.py | 4 +- 3 files changed, 81 insertions(+), 53 deletions(-) diff --git a/tnc/data_handler.py b/tnc/data_handler.py index c0692a5d..d17eddaf 100644 --- a/tnc/data_handler.py +++ b/tnc/data_handler.py @@ -74,7 +74,9 @@ class DATA(): if data[0] == 'CQ': # [0] CQ self.transmit_cq() + elif data[0] == 'STOP': + # [0] STOP self.stop_transmission() elif data[0] == 'PING': diff --git a/tnc/rigctld.py b/tnc/rigctld.py index 64764314..fb1e6e1e 100644 --- a/tnc/rigctld.py +++ b/tnc/rigctld.py @@ -3,6 +3,7 @@ import socket import structlog import log_handler import logging +import time # class taken from darsidelemm # rigctl - https://github.com/darksidelemm/rotctld-web-gui/blob/master/rotatorgui.py#L35 # @@ -22,35 +23,38 @@ class radio(): self.connected = False self.hostname = hostname self.port = port - + self.connection_attempts = 5 def open_rig(self, devicename, deviceport, hamlib_ptt_type, serialspeed, pttport, data_bits, stop_bits, handshake, rigctld_ip, rigctld_port): - self.connect() - logging.debug(f"Rigctl intialized") - return True - + self.hostname = rigctld_ip + self.ip = rigctld_port + + + if self.connect(): + logging.debug(f"Rigctl intialized") + return True + else: + structlog.get_logger("structlog").error("[RIGCTLD] Can't connect to rigctld!", ip=self.hostname, port=self.port) + return False + def connect(self): """ Connect to rotctld instance """ - try: - self.sock.connect((self.hostname,self.port)) - self.connected = True - structlog.get_logger("structlog").info("[RIGCTLD] Connected to rigctld!", ip=self.hostname, port=self.port) - except: - # ConnectionRefusedError: [Errno 111] Connection refused - structlog.get_logger("structlog").critical("[RIGCTLD] Could not connect to rigctld!", ip=self.hostname, port=self.port) + for a in range(0,self.connection_attempts): + try: + self.sock.connect((self.hostname,self.port)) + self.connected = True + structlog.get_logger("structlog").info("[RIGCTLD] Connected to rigctld!", attempt=a+1, ip=self.hostname, port=self.port) + return True + except: + # ConnectionRefusedError: [Errno 111] Connection refused + self.connected = False + structlog.get_logger("structlog").warning("[RIGCTLD] Re-Trying to establish a connection to rigctld!", attempt=a+1, ip=self.hostname, port=self.port) + time.sleep(1) + - ptt = self.get_ptt() - if ptt == None: - # Timeout! - self.close() - raise Exception("Timeout!") - - else: - return ptt - - def close_rig(self): self.sock.close() + self.connected = False def send_command(self, command): @@ -58,35 +62,57 @@ class radio(): and return the return value. """ if self.connected: - self.sock.sendall(command+b'\n') - try: - return self.sock.recv(1024) - except: - return None + try: + self.sock.sendall(command+b'\n') + except: + structlog.get_logger("structlog").warning("[RIGCTLD] Command not executed!", command=command, ip=self.hostname, port=self.port) + self.connected = False - def get_mode(self): - data = self.send_command(b"m") - data = data.split(b'\n') - mode = data[0] - return mode.decode("utf-8") - - def get_bandwith(self): - data = self.send_command(b"m") - data = data.split(b'\n') - bandwith = data[1] - return bandwith.decode("utf-8") - - def get_frequency(self): - frequency = self.send_command(b"f") - return frequency.decode("utf-8") - - def get_ptt(self): - return self.send_command(b"t") - - def set_ptt(self, state): - if state: - self.send_command(b"T 1") + try: + return self.sock.recv(1024) + except: + structlog.get_logger("structlog").warning("[RIGCTLD] No command response!", command=command, ip=self.hostname, port=self.port) + self.connected = False else: - self.send_command(b"T 0") - return state + structlog.get_logger("structlog").error("[RIGCTLD] No connection to rigctl!", ip=self.hostname, port=self.port) + self.connect() + def get_mode(self): + try: + data = self.send_command(b"m") + data = data.split(b'\n') + mode = data[0] + return mode.decode("utf-8") + except: + 0 + def get_bandwith(self): + try: + data = self.send_command(b"m") + data = data.split(b'\n') + bandwith = data[1] + return bandwith.decode("utf-8") + except: + return 0 + + def get_frequency(self): + try: + frequency = self.send_command(b"f") + return frequency.decode("utf-8") + except: + return 0 + + def get_ptt(self): + try: + return self.send_command(b"t") + except: + return False + + def set_ptt(self, state): + try: + if state: + self.send_command(b"T 1") + else: + self.send_command(b"T 0") + return state + except: + return False diff --git a/tnc/sock.py b/tnc/sock.py index 17d32a92..99a3206b 100644 --- a/tnc/sock.py +++ b/tnc/sock.py @@ -94,6 +94,7 @@ class ThreadedTCPRequestHandler(socketserver.StreamRequestHandler): if data.startswith(b'{"type"') and data.endswith(b'}\n'): data = data[:-1] # remove b'\n' + print(data) if self.server.server_address[1] == static.PORT: process_tnc_commands(data) else: @@ -145,7 +146,7 @@ def process_tnc_commands(data): if received_json["command"] == "STOP_BEACON": static.BEACON_STATE = False structlog.get_logger("structlog").warning("[TNC] Stopping beacon!") - data_handler.DATA_QUEUE_TRANSMIT.put(['BEACON', interval, False]) + data_handler.DATA_QUEUE_TRANSMIT.put(['BEACON', None, False]) # PING ---------------------------------------------------------- @@ -404,7 +405,6 @@ def send_daemon_state(): python_version = str(sys.version_info[0]) + "." + str(sys.version_info[1]) - output = { 'COMMAND': 'DAEMON_STATE', 'DAEMON_STATE': [],