redesigned of rigctld driver

This commit is contained in:
dj2ls 2023-01-04 08:33:25 +01:00
parent 5d8b8a4bde
commit 7ba1dbadb2

View file

@ -21,9 +21,11 @@ class radio:
def __init__(self, hostname="localhost", port=4532, poll_rate=5, timeout=5): def __init__(self, hostname="localhost", port=4532, poll_rate=5, timeout=5):
"""Open a connection to rigctld, and test it for validity""" """Open a connection to rigctld, and test it for validity"""
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.ptt_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.data_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.connected = False self.ptt_connected = False
self.data_connected = False
self.hostname = hostname self.hostname = hostname
self.port = port self.port = port
self.connection_attempts = 5 self.connection_attempts = 5
@ -33,7 +35,6 @@ class radio:
self.frequency = '' self.frequency = ''
self.mode = '' self.mode = ''
def open_rig( def open_rig(
self, self,
devicename, devicename,
@ -67,8 +68,20 @@ class radio:
self.hostname = rigctld_ip self.hostname = rigctld_ip
self.port = int(rigctld_port) self.port = int(rigctld_port)
if self.connect(): #_ptt_connect = self.ptt_connect()
self.log.debug("Rigctl initialized") #_data_connect = self.data_connect()
ptt_thread = threading.Thread(target=self.ptt_connect, args=[], daemon=True)
ptt_thread.start()
data_thread = threading.Thread(target=self.data_connect, args=[], daemon=True)
data_thread.start()
# wait some time
threading.Event().wait(0.5)
if self.ptt_connection and self.data_connection:
self.log.debug("Rigctl DATA/PTT initialized")
return True return True
self.log.error( self.log.error(
@ -76,33 +89,59 @@ class radio:
) )
return False return False
def connect(self): def ptt_connect(self):
"""Connect to rigctld instance""" """Connect to rigctld instance"""
if not self.connected: while True:
try:
self.connection = socket.create_connection((self.hostname, self.port)) if not self.ptt_connected:
self.connected = True try:
self.log.info( self.ptt_connection = socket.create_connection((self.hostname, self.port))
"[RIGCTLD] Connected to rigctld!", ip=self.hostname, port=self.port self.ptt_connected = True
) self.log.info(
return True "[RIGCTLD] Connected PTT instance to rigctld!", ip=self.hostname, port=self.port
except Exception as err: )
# ConnectionRefusedError: [Errno 111] Connection refused except Exception as err:
self.close_rig() # ConnectionRefusedError: [Errno 111] Connection refused
self.log.warning( self.close_rig()
"[RIGCTLD] Reconnect...", self.log.warning(
ip=self.hostname, "[RIGCTLD] PTT Reconnect...",
port=self.port, ip=self.hostname,
e=err, port=self.port,
) e=err,
return False )
threading.Event().wait(0.5)
def data_connect(self):
"""Connect to rigctld instance"""
while True:
if not self.data_connected:
try:
self.data_connection = socket.create_connection((self.hostname, self.port))
self.data_connected = True
self.log.info(
"[RIGCTLD] Connected DATA instance to rigctld!", ip=self.hostname, port=self.port
)
except Exception as err:
# ConnectionRefusedError: [Errno 111] Connection refused
self.close_rig()
self.log.warning(
"[RIGCTLD] DATA Reconnect...",
ip=self.hostname,
port=self.port,
e=err,
)
threading.Event().wait(0.5)
def close_rig(self): def close_rig(self):
""" """ """ """
self.sock.close() self.ptt_sock.close()
self.connected = False self.data_sock.close()
self.ptt_connected = False
self.data_connected = False
def send_command(self, command, expect_answer) -> bytes:
def send_ptt_command(self, command, expect_answer) -> bytes:
"""Send a command to the connected rotctld instance, """Send a command to the connected rotctld instance,
and return the return value. and return the return value.
@ -110,9 +149,9 @@ class radio:
command: command:
""" """
if self.connected: if self.ptt_connected:
try: try:
self.connection.sendall(command + b"\n") self.ptt_connection.sendall(command + b"\n")
except Exception: except Exception:
self.log.warning( self.log.warning(
"[RIGCTLD] Command not executed!", "[RIGCTLD] Command not executed!",
@ -120,12 +159,33 @@ class radio:
ip=self.hostname, ip=self.hostname,
port=self.port, port=self.port,
) )
self.connected = False self.ptt_connected = False
return b""
def send_data_command(self, command, expect_answer) -> bytes:
"""Send a command to the connected rotctld instance,
and return the return value.
Args:
command:
"""
if self.data_connected:
try:
self.data_connection.sendall(command + b"\n")
except Exception:
self.log.warning(
"[RIGCTLD] Command not executed!",
command=command,
ip=self.hostname,
port=self.port,
)
self.data_connected = False
try: try:
# recv seems to be blocking so in case of ptt we dont need the response # recv seems to be blocking so in case of ptt we don't need the response
# maybe this speeds things up and avoids blocking states # maybe this speeds things up and avoids blocking states
return self.connection.recv(16) if expect_answer else True return self.data_connection.recv(64) if expect_answer else True
except Exception: except Exception:
self.log.warning( self.log.warning(
"[RIGCTLD] No command response!", "[RIGCTLD] No command response!",
@ -133,23 +193,18 @@ class radio:
ip=self.hostname, ip=self.hostname,
port=self.port, port=self.port,
) )
self.connected = False self.data_connected = False
else:
# reconnecting....
threading.Event().wait(0.5)
self.connect()
return b"" return b""
def get_status(self): def get_status(self):
""" """ """ """
return "connected" if self.connected else "unknown/disconnected" return "connected" if self.data_connected and self.ptt_connected else "unknown/disconnected"
def get_mode(self): def get_mode(self):
""" """ """ """
try: try:
data = self.send_command(b"m", True) data = self.send_data_command(b"m", True)
print(data)
data = data.split(b"\n") data = data.split(b"\n")
data = data[0].decode("utf-8") data = data[0].decode("utf-8")
if 'RPRT' not in data: if 'RPRT' not in data:
@ -165,7 +220,7 @@ class radio:
def get_bandwidth(self): def get_bandwidth(self):
""" """ """ """
try: try:
data = self.send_command(b"m", True) data = self.send_data_command(b"m", True)
data = data.split(b"\n") data = data.split(b"\n")
data = data[1].decode("utf-8") data = data[1].decode("utf-8")
@ -179,7 +234,7 @@ class radio:
def get_frequency(self): def get_frequency(self):
""" """ """ """
try: try:
data = self.send_command(b"f", True) data = self.send_data_command(b"f", True)
data = data.decode("utf-8") data = data.decode("utf-8")
if 'RPRT' not in data and data not in [0, '0', '']: if 'RPRT' not in data and data not in [0, '0', '']:
with contextlib.suppress(ValueError): with contextlib.suppress(ValueError):
@ -209,9 +264,9 @@ class radio:
""" """
try: try:
if state: if state:
self.send_command(b"T 1", False) self.send_ptt_command(b"T 1", False)
else: else:
self.send_command(b"T 0", False) self.send_ptt_command(b"T 0", False)
return state return state
except Exception: except Exception:
return False return False