FreeDATA/tnc/rig.py

279 lines
8.9 KiB
Python
Raw Normal View History

2021-12-25 16:05:38 +00:00
import atexit
2022-01-12 07:01:43 +00:00
import os
import re
import subprocess
import sys
import structlog
mainlog = structlog.get_logger(__file__)
2021-12-25 16:05:38 +00:00
2022-01-15 16:06:02 +00:00
# set global hamlib version
hamlib_version = 0
2022-01-11 21:16:14 +00:00
# append local search path
# check if we are running in a pyinstaller environment
if hasattr(sys, "_MEIPASS"):
sys.path.append(getattr(sys, "_MEIPASS"))
else:
sys.path.append(os.path.abspath("."))
2022-05-09 00:41:49 +00:00
# try importing hamlib
2021-12-25 16:05:38 +00:00
try:
# get python version
python_version = f"{str(sys.version_info[0])}.{str(sys.version_info[1])}"
2021-12-25 16:05:38 +00:00
# installation path for Ubuntu 20.04 LTS python modules
# sys.path.append(f"/usr/local/lib/python{python_version}/site-packages")
2022-05-09 00:41:49 +00:00
2021-12-25 16:05:38 +00:00
# installation path for Ubuntu 20.10 +
sys.path.append("/usr/local/lib/")
2022-05-09 00:41:49 +00:00
2021-12-27 16:00:00 +00:00
# installation path for Suse
sys.path.append(f"/usr/local/lib64/python{python_version}/site-packages")
2022-01-11 13:57:11 +00:00
# everything else... not nice, but an attempt to see how it's running within app bundle
# this is not needed as python will be shipped with app bundle
sys.path.append("/usr/local/lib/python3.6/site-packages")
sys.path.append("/usr/local/lib/python3.7/site-packages")
sys.path.append("/usr/local/lib/python3.8/site-packages")
sys.path.append("/usr/local/lib/python3.9/site-packages")
sys.path.append("/usr/local/lib/python3.10/site-packages")
2022-05-09 00:41:49 +00:00
sys.path.append("lib/hamlib/linux/python3.8/site-packages")
2021-12-25 16:05:38 +00:00
import Hamlib
2022-05-09 00:41:49 +00:00
2021-12-25 16:05:38 +00:00
# https://stackoverflow.com/a/4703409
2022-05-09 00:41:49 +00:00
hamlib_version = re.findall(r"[-+]?\d*\.?\d+|\d+", Hamlib.cvar.hamlib_version)
2021-12-25 16:05:38 +00:00
hamlib_version = float(hamlib_version[0])
2022-05-09 00:41:49 +00:00
2021-12-25 16:05:38 +00:00
min_hamlib_version = 4.1
if hamlib_version > min_hamlib_version:
mainlog.info("[RIG] Hamlib found", version=hamlib_version)
2021-12-25 16:05:38 +00:00
else:
mainlog.warning(
"[RIG] Hamlib outdated", found=hamlib_version, recommend=min_hamlib_version
)
except Exception as err:
mainlog.warning("[RIG] Python Hamlib binding not found", error=err)
2021-12-28 16:05:48 +00:00
try:
mainlog.warning("[RIG] Trying to open rigctl")
rigctl = subprocess.Popen(
"rigctl -V",
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
text=True,
)
2021-12-28 16:05:48 +00:00
hamlib_version = rigctl.stdout.readline()
hamlib_version = hamlib_version.split(" ")
2022-01-15 16:06:02 +00:00
if hamlib_version[1] == "Hamlib":
mainlog.warning(
"[RIG] Rigctl found! Please try using this", version=hamlib_version[2]
)
2022-01-15 16:06:02 +00:00
sys.exit()
2021-12-28 16:05:48 +00:00
else:
raise Exception
except Exception as err1:
mainlog.critical("[RIG] HAMLIB NOT INSTALLED", error=err1)
2022-01-15 16:06:02 +00:00
hamlib_version = 0
sys.exit()
2022-05-09 00:41:49 +00:00
2021-12-25 16:05:38 +00:00
class radio:
""" """
log = structlog.get_logger(__name__)
2021-12-25 16:05:38 +00:00
def __init__(self):
self.devicename = ""
self.devicenumber = ""
self.deviceport = ""
self.serialspeed = ""
self.hamlib_ptt_type = ""
self.my_rig = ""
self.pttport = ""
self.data_bits = ""
self.stop_bits = ""
self.handshake = ""
def open_rig(
self,
devicename,
deviceport,
hamlib_ptt_type,
serialspeed,
pttport,
data_bits,
stop_bits,
handshake,
rigctld_port,
rigctld_ip,
):
"""
Args:
2022-05-09 00:41:49 +00:00
devicename:
deviceport:
hamlib_ptt_type:
serialspeed:
pttport:
data_bits:
stop_bits:
handshake:
rigctld_port:
rigctld_ip:
"""
2021-12-25 16:05:38 +00:00
self.devicename = devicename
2021-12-28 16:05:48 +00:00
self.deviceport = str(deviceport)
# we need to ensure this is a str, otherwise set_conf functions are crashing
self.serialspeed = str(serialspeed)
2021-12-28 16:05:48 +00:00
self.hamlib_ptt_type = str(hamlib_ptt_type)
self.pttport = str(pttport)
self.data_bits = str(data_bits)
self.stop_bits = str(stop_bits)
self.handshake = str(handshake)
2022-05-09 00:41:49 +00:00
2021-12-25 16:05:38 +00:00
# try to init hamlib
try:
Hamlib.rig_set_debug(Hamlib.RIG_DEBUG_NONE)
# get devicenumber by looking for deviceobject in Hamlib module
try:
self.devicenumber = int(getattr(Hamlib, self.devicename))
except Exception:
self.log.error("[RIG] Hamlib: rig not supported...")
2021-12-25 16:05:38 +00:00
self.devicenumber = 0
2022-05-09 00:41:49 +00:00
2021-12-25 16:05:38 +00:00
self.my_rig = Hamlib.Rig(self.devicenumber)
self.my_rig.set_conf("rig_pathname", self.deviceport)
self.my_rig.set_conf("retry", "5")
self.my_rig.set_conf("serial_speed", self.serialspeed)
self.my_rig.set_conf("serial_handshake", self.handshake)
self.my_rig.set_conf("stop_bits", self.stop_bits)
self.my_rig.set_conf("data_bits", self.data_bits)
self.my_rig.set_conf("ptt_pathname", self.pttport)
2021-12-28 16:05:48 +00:00
if self.hamlib_ptt_type == "RIG":
2021-12-25 16:05:38 +00:00
self.hamlib_ptt_type = Hamlib.RIG_PTT_RIG
self.my_rig.set_conf("ptt_type", "RIG")
2021-12-25 16:05:38 +00:00
elif self.hamlib_ptt_type == "USB":
self.hamlib_ptt_type = Hamlib.RIG_PORT_USB
self.my_rig.set_conf("ptt_type", "USB")
elif self.hamlib_ptt_type == "DTR-H":
2021-12-25 16:05:38 +00:00
self.hamlib_ptt_type = Hamlib.RIG_PTT_SERIAL_DTR
self.my_rig.set_conf("dtr_state", "HIGH")
self.my_rig.set_conf("ptt_type", "DTR")
elif self.hamlib_ptt_type == "DTR-L":
2021-12-25 16:05:38 +00:00
self.hamlib_ptt_type = Hamlib.RIG_PTT_SERIAL_DTR
self.my_rig.set_conf("dtr_state", "LOW")
self.my_rig.set_conf("ptt_type", "DTR")
elif self.hamlib_ptt_type == "RTS":
2021-12-25 16:05:38 +00:00
self.hamlib_ptt_type = Hamlib.RIG_PTT_SERIAL_RTS
self.my_rig.set_conf("dtr_state", "OFF")
self.my_rig.set_conf("ptt_type", "RTS")
elif self.hamlib_ptt_type == "PARALLEL":
2021-12-25 16:05:38 +00:00
self.hamlib_ptt_type = Hamlib.RIG_PTT_PARALLEL
elif self.hamlib_ptt_type == "MICDATA":
2021-12-25 16:05:38 +00:00
self.hamlib_ptt_type = Hamlib.RIG_PTT_RIG_MICDATA
elif self.hamlib_ptt_type == "CM108":
2021-12-25 16:05:38 +00:00
self.hamlib_ptt_type = Hamlib.RIG_PTT_CM108
elif self.hamlib_ptt_type == "RIG_PTT_NONE":
2021-12-25 16:05:38 +00:00
self.hamlib_ptt_type = Hamlib.RIG_PTT_NONE
2022-05-09 00:41:49 +00:00
else: # self.hamlib_ptt_type == "RIG_PTT_NONE":
2022-04-11 09:03:54 +00:00
self.hamlib_ptt_type = Hamlib.RIG_PTT_NONE
self.log.info(
"[RIG] Opening...",
device=self.devicenumber,
path=self.my_rig.get_conf("rig_pathname"),
serial_speed=self.my_rig.get_conf("serial_speed"),
serial_handshake=self.my_rig.get_conf("serial_handshake"),
stop_bits=self.my_rig.get_conf("stop_bits"),
data_bits=self.my_rig.get_conf("data_bits"),
ptt_pathname=self.my_rig.get_conf("ptt_pathname"),
)
2021-12-25 16:05:38 +00:00
self.my_rig.open()
atexit.register(self.my_rig.close)
2022-05-09 00:41:49 +00:00
2021-12-26 08:55:20 +00:00
try:
# lets determine the error message when opening rig
error = str(Hamlib.rigerror(self.my_rig.error_status)).splitlines()
error = error[1].split("err=")
2021-12-26 08:55:20 +00:00
error = error[1]
2022-05-09 00:41:49 +00:00
if error == "Permission denied":
self.log.error("[RIG] Hamlib has no permissions", e=error)
help_url = "https://github.com/DJ2LS/FreeDATA/wiki/UBUNTU-Manual-installation#1-permissions"
self.log.error("[RIG] HELP:", check=help_url)
except Exception:
self.log.info("[RIG] Hamlib device opened", status="SUCCESS")
# set ptt to false if ptt is stuck for some reason
self.set_ptt(False)
2021-12-25 16:05:38 +00:00
# set rig mode to USB
# temporarly outcommented because of possible problems.
# self.my_rig.set_mode(Hamlib.RIG_MODE_USB)
# self.my_rig.set_mode(Hamlib.RIG_MODE_PKTUSB)
2021-12-25 16:05:38 +00:00
return True
except Exception as err2:
self.log.error(
"[RIG] Hamlib - can't open rig", error=err2, e=sys.exc_info()[0]
)
2021-12-25 16:05:38 +00:00
return False
2022-05-09 00:41:49 +00:00
def get_frequency(self):
""" """
return int(self.my_rig.get_freq())
2022-05-09 00:41:49 +00:00
def get_mode(self):
""" """
(hamlib_mode, bandwidth) = self.my_rig.get_mode()
return Hamlib.rig_strrmode(hamlib_mode)
2022-05-09 00:41:49 +00:00
def get_bandwidth(self):
""" """
(hamlib_mode, bandwidth) = self.my_rig.get_mode()
return bandwidth
2021-12-28 16:05:48 +00:00
# not needed yet beacuse of some possible problems
# def set_mode(self, mode):
2021-12-28 16:05:48 +00:00
# return 0
2022-05-09 00:41:49 +00:00
def get_ptt(self):
""" """
return self.my_rig.get_ptt()
2022-05-09 00:41:49 +00:00
2021-12-25 16:05:38 +00:00
def set_ptt(self, state):
"""
Args:
2022-05-09 00:41:49 +00:00
state:
Returns:
"""
2021-12-25 16:05:38 +00:00
if state:
self.my_rig.set_ptt(Hamlib.RIG_VFO_CURR, 1)
2021-12-25 16:05:38 +00:00
else:
self.my_rig.set_ptt(Hamlib.RIG_VFO_CURR, 0)
2021-12-25 16:05:38 +00:00
return state
2022-05-09 00:41:49 +00:00
2021-12-26 08:55:20 +00:00
def close_rig(self):
""" """
2021-12-26 08:55:20 +00:00
self.my_rig.close()