2021-09-02 17:41:01 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import socket
|
|
|
|
import logging
|
2021-09-02 18:16:46 +00:00
|
|
|
import static
|
2021-09-02 17:41:01 +00:00
|
|
|
|
|
|
|
# rigctl - https://github.com/darksidelemm/rotctld-web-gui/blob/master/rotatorgui.py#L35
|
|
|
|
# https://github.com/xssfox/freedv-tnc/blob/master/freedvtnc/rigctl.py
|
|
|
|
|
|
|
|
|
|
|
|
class Rigctld():
|
|
|
|
""" rotctld (hamlib) communication class """
|
|
|
|
# Note: This is a massive hack.
|
|
|
|
|
|
|
|
def __init__(self, hostname="localhost", port=4532, poll_rate=5, timeout=5):
|
|
|
|
""" Open a connection to rotctld, and test it for validity """
|
|
|
|
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
self.sock.settimeout(timeout)
|
|
|
|
|
|
|
|
self.hostname = hostname
|
|
|
|
self.port = port
|
|
|
|
self.connect()
|
|
|
|
logging.debug(f"Rigctl intialized")
|
|
|
|
|
|
|
|
def get_model(self):
|
|
|
|
""" Get the rotator model from rotctld """
|
|
|
|
model = self.send_command(b'_')
|
|
|
|
return model
|
|
|
|
|
|
|
|
def connect(self):
|
|
|
|
""" Connect to rotctld instance """
|
|
|
|
self.sock.connect((self.hostname,self.port))
|
|
|
|
model = self.get_model()
|
|
|
|
if model == None:
|
|
|
|
# Timeout!
|
|
|
|
self.close()
|
|
|
|
raise Exception("Timeout!")
|
|
|
|
else:
|
|
|
|
return model
|
|
|
|
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self.sock.close()
|
|
|
|
|
|
|
|
|
|
|
|
def send_command(self, command):
|
|
|
|
""" Send a command to the connected rotctld instance,
|
|
|
|
and return the return value.
|
|
|
|
"""
|
|
|
|
self.sock.sendall(command+b'\n')
|
|
|
|
try:
|
|
|
|
return self.sock.recv(1024)
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def ptt_enable(self):
|
|
|
|
logging.debug(f"PTT enabled")
|
|
|
|
self.send_command(b"T 1")
|
|
|
|
|
|
|
|
def ptt_disable(self):
|
|
|
|
logging.debug(f"PTT disabled")
|
|
|
|
self.send_command(b"T 0")
|
|
|
|
|
|
|
|
def get_frequency(self):
|
|
|
|
data = self.send_command(b"f")
|
|
|
|
if data is not None:
|
|
|
|
data = data.split(b'\n')
|
|
|
|
try:
|
2021-09-02 18:16:46 +00:00
|
|
|
freq = int(data[0])/1000
|
2021-09-02 17:41:01 +00:00
|
|
|
except:
|
2021-09-02 18:16:46 +00:00
|
|
|
freq = static.HAMLIB_FREQUENCY
|
2021-09-02 17:41:01 +00:00
|
|
|
print(data)
|
|
|
|
return freq
|
|
|
|
|
|
|
|
def get_mode(self):
|
|
|
|
data = self.send_command(b"m")
|
|
|
|
if data is not None:
|
|
|
|
data = data.split(b'\n')
|
|
|
|
try:
|
|
|
|
mode = str(data[0], "utf-8")
|
|
|
|
bandwith = int(data[1])
|
|
|
|
except:
|
|
|
|
print(data)
|
2021-09-02 18:16:46 +00:00
|
|
|
mode = static.HAMLIB_MODE
|
|
|
|
bandwith = static.HAMLIB_BANDWITH
|
2021-09-02 17:41:01 +00:00
|
|
|
return [mode, bandwith]
|
|
|
|
|