2022-01-18 18:38:05 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# class taken from darsidelemm
|
|
|
|
# rigctl - https://github.com/darksidelemm/rotctld-web-gui/blob/master/rotatorgui.py#L35
|
|
|
|
#
|
|
|
|
# modified and adjusted to FreeDATA needs by DJ2LS
|
|
|
|
|
2022-05-11 22:10:59 +00:00
|
|
|
import socket
|
|
|
|
import time
|
|
|
|
|
|
|
|
import structlog
|
|
|
|
|
2022-01-18 18:38:05 +00:00
|
|
|
# set global hamlib version
|
|
|
|
hamlib_version = 0
|
|
|
|
|
2022-05-09 01:27:24 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
class radio:
|
2022-05-11 22:10:59 +00:00
|
|
|
"""rigctld (hamlib) communication class"""
|
2022-05-26 01:23:30 +00:00
|
|
|
|
2022-05-09 00:41:49 +00:00
|
|
|
# Note: This is a massive hack.
|
2022-01-18 18:38:05 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
log = structlog.get_logger(__name__)
|
|
|
|
|
2022-01-18 18:38:05 +00:00
|
|
|
def __init__(self, hostname="localhost", port=4532, poll_rate=5, timeout=5):
|
2022-05-28 15:52:05 +00:00
|
|
|
"""Open a connection to rigctld, and test it for validity"""
|
2022-01-18 18:38:05 +00:00
|
|
|
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
2022-05-26 01:23:30 +00:00
|
|
|
# self.sock.settimeout(timeout)
|
2022-03-06 16:23:04 +00:00
|
|
|
|
2022-01-23 07:38:02 +00:00
|
|
|
self.connected = False
|
2022-01-18 18:38:05 +00:00
|
|
|
self.hostname = hostname
|
|
|
|
self.port = port
|
2022-01-24 21:01:01 +00:00
|
|
|
self.connection_attempts = 5
|
2022-01-18 18:38:05 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
def open_rig(
|
|
|
|
self,
|
|
|
|
devicename,
|
|
|
|
deviceport,
|
|
|
|
hamlib_ptt_type,
|
|
|
|
serialspeed,
|
|
|
|
pttport,
|
|
|
|
data_bits,
|
|
|
|
stop_bits,
|
|
|
|
handshake,
|
|
|
|
rigctld_ip,
|
|
|
|
rigctld_port,
|
|
|
|
):
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
Args:
|
2022-05-09 00:41:49 +00:00
|
|
|
devicename:
|
|
|
|
deviceport:
|
|
|
|
hamlib_ptt_type:
|
|
|
|
serialspeed:
|
|
|
|
pttport:
|
|
|
|
data_bits:
|
|
|
|
stop_bits:
|
|
|
|
handshake:
|
|
|
|
rigctld_ip:
|
|
|
|
rigctld_port:
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2022-01-24 21:01:01 +00:00
|
|
|
self.hostname = rigctld_ip
|
2022-01-24 22:29:34 +00:00
|
|
|
self.port = int(rigctld_port)
|
2022-05-26 01:23:30 +00:00
|
|
|
|
2022-01-24 21:01:01 +00:00
|
|
|
if self.connect():
|
2022-05-26 01:23:30 +00:00
|
|
|
self.log.debug("Rigctl initialized")
|
2022-01-24 21:01:01 +00:00
|
|
|
return True
|
2022-05-11 22:10:59 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
self.log.error(
|
|
|
|
"[RIGCTLD] Can't connect to rigctld!", ip=self.hostname, port=self.port
|
|
|
|
)
|
2022-05-11 22:10:59 +00:00
|
|
|
return False
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-18 18:38:05 +00:00
|
|
|
def connect(self):
|
2022-03-06 16:23:04 +00:00
|
|
|
"""Connect to rigctld instance"""
|
|
|
|
if not self.connected:
|
2022-01-24 21:01:01 +00:00
|
|
|
try:
|
2022-05-26 01:23:30 +00:00
|
|
|
self.connection = socket.create_connection((self.hostname, self.port))
|
2022-01-24 21:01:01 +00:00
|
|
|
self.connected = True
|
2022-05-26 01:23:30 +00:00
|
|
|
self.log.info(
|
|
|
|
"[RIGCTLD] Connected to rigctld!", ip=self.hostname, port=self.port
|
|
|
|
)
|
2022-01-24 21:01:01 +00:00
|
|
|
return True
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception as err:
|
2022-01-24 21:01:01 +00:00
|
|
|
# ConnectionRefusedError: [Errno 111] Connection refused
|
2022-03-06 16:23:04 +00:00
|
|
|
self.close_rig()
|
2022-05-26 01:23:30 +00:00
|
|
|
self.log.warning(
|
|
|
|
"[RIGCTLD] Connection to rigctld refused! Reconnect...",
|
|
|
|
ip=self.hostname,
|
|
|
|
port=self.port,
|
|
|
|
e=err,
|
|
|
|
)
|
2022-03-06 16:23:04 +00:00
|
|
|
return False
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-18 18:38:05 +00:00
|
|
|
def close_rig(self):
|
2022-03-04 15:50:32 +00:00
|
|
|
""" """
|
2022-01-18 18:38:05 +00:00
|
|
|
self.sock.close()
|
2022-01-24 21:01:01 +00:00
|
|
|
self.connected = False
|
2022-01-18 18:38:05 +00:00
|
|
|
|
2022-05-25 22:27:33 +00:00
|
|
|
def send_command(self, command) -> bytes:
|
2022-03-04 15:50:32 +00:00
|
|
|
"""Send a command to the connected rotctld instance,
|
2022-01-18 18:38:05 +00:00
|
|
|
and return the return value.
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
Args:
|
2022-05-09 00:41:49 +00:00
|
|
|
command:
|
2022-03-04 15:50:32 +00:00
|
|
|
|
2022-01-18 18:38:05 +00:00
|
|
|
"""
|
2022-01-23 07:38:02 +00:00
|
|
|
if self.connected:
|
2022-01-24 21:01:01 +00:00
|
|
|
try:
|
2022-05-26 01:23:30 +00:00
|
|
|
self.connection.sendall(command + b"\n")
|
|
|
|
except Exception:
|
|
|
|
self.log.warning(
|
|
|
|
"[RIGCTLD] Command not executed!",
|
|
|
|
command=command,
|
|
|
|
ip=self.hostname,
|
|
|
|
port=self.port,
|
|
|
|
)
|
2022-01-24 21:01:01 +00:00
|
|
|
self.connected = False
|
2022-01-18 18:38:05 +00:00
|
|
|
|
2022-01-24 21:01:01 +00:00
|
|
|
try:
|
2022-03-06 16:23:04 +00:00
|
|
|
return self.connection.recv(1024)
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception:
|
|
|
|
self.log.warning(
|
|
|
|
"[RIGCTLD] No command response!",
|
|
|
|
command=command,
|
|
|
|
ip=self.hostname,
|
|
|
|
port=self.port,
|
|
|
|
)
|
2022-01-24 21:01:01 +00:00
|
|
|
self.connected = False
|
|
|
|
else:
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-03-06 16:23:04 +00:00
|
|
|
# reconnecting....
|
|
|
|
time.sleep(0.5)
|
2022-01-24 21:01:01 +00:00
|
|
|
self.connect()
|
2022-03-06 16:23:04 +00:00
|
|
|
|
2022-05-25 22:27:33 +00:00
|
|
|
return b""
|
|
|
|
|
2022-01-18 18:38:05 +00:00
|
|
|
def get_mode(self):
|
2022-03-04 15:50:32 +00:00
|
|
|
""" """
|
2022-01-24 21:01:01 +00:00
|
|
|
try:
|
|
|
|
data = self.send_command(b"m")
|
2022-05-26 01:23:30 +00:00
|
|
|
data = data.split(b"\n")
|
2022-01-24 21:01:01 +00:00
|
|
|
mode = data[0]
|
2022-05-09 00:41:49 +00:00
|
|
|
return mode.decode("utf-8")
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception:
|
2022-05-11 22:10:59 +00:00
|
|
|
return 0
|
2022-05-09 01:27:24 +00:00
|
|
|
|
2022-05-28 12:08:33 +00:00
|
|
|
def get_bandwidth(self):
|
2022-03-04 15:50:32 +00:00
|
|
|
""" """
|
2022-01-24 21:01:01 +00:00
|
|
|
try:
|
|
|
|
data = self.send_command(b"m")
|
2022-05-26 01:23:30 +00:00
|
|
|
data = data.split(b"\n")
|
2022-05-28 12:08:33 +00:00
|
|
|
bandwidth = data[1]
|
|
|
|
return bandwidth.decode("utf-8")
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception:
|
2022-01-24 21:01:01 +00:00
|
|
|
return 0
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-18 18:38:05 +00:00
|
|
|
def get_frequency(self):
|
2022-03-04 15:50:32 +00:00
|
|
|
""" """
|
2022-01-24 21:01:01 +00:00
|
|
|
try:
|
|
|
|
frequency = self.send_command(b"f")
|
|
|
|
return frequency.decode("utf-8")
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception:
|
2022-01-24 21:01:01 +00:00
|
|
|
return 0
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-18 18:38:05 +00:00
|
|
|
def get_ptt(self):
|
2022-03-04 15:50:32 +00:00
|
|
|
""" """
|
2022-01-24 21:01:01 +00:00
|
|
|
try:
|
|
|
|
return self.send_command(b"t")
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception:
|
2022-01-24 21:01:01 +00:00
|
|
|
return False
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-24 21:01:01 +00:00
|
|
|
def set_ptt(self, state):
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
Args:
|
2022-05-09 00:41:49 +00:00
|
|
|
state:
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2022-01-24 21:01:01 +00:00
|
|
|
try:
|
|
|
|
if state:
|
2022-05-26 01:23:30 +00:00
|
|
|
self.send_command(b"T 1")
|
2022-01-24 21:01:01 +00:00
|
|
|
else:
|
2022-05-26 01:23:30 +00:00
|
|
|
self.send_command(b"T 0")
|
2022-05-09 00:41:49 +00:00
|
|
|
return state
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception:
|
2022-01-24 21:01:01 +00:00
|
|
|
return False
|