Comments and cleanup.

This commit is contained in:
Marat Fayzullin 2024-10-01 11:14:16 -04:00
parent 89c84b8f76
commit eadf6024e7
1 changed files with 20 additions and 14 deletions

View File

@ -12,6 +12,7 @@ import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class RigControl(): class RigControl():
# Mapping from rig names to Rigctl rig types
RIGS = { RIGS = {
# "Hamlib Dummy" : 1, # "Hamlib Dummy" : 1,
"Hamlib" : 2, "Hamlib" : 2,
@ -299,6 +300,7 @@ class RigControl():
"Yaesu VR-5000" : 1026, "Yaesu VR-5000" : 1026,
} }
# Mapping from OpenWebRX modulations to Rigctl modulations
MODES = { MODES = {
"nfm" : "FM", "wfm" : "WFM", "nfm" : "FM", "wfm" : "WFM",
"am" : "AM", "sam" : "SAM", "am" : "AM", "sam" : "SAM",
@ -340,19 +342,22 @@ class RigControl():
if mod != self.mod and self.rigModulation(mod): if mod != self.mod and self.rigModulation(mod):
self.mod = mod self.mod = mod
# Press or release rig's PTT (i.e. transmit)
def rigTX(self, active: bool) -> bool: def rigTX(self, active: bool) -> bool:
return self.rigCommand("T {0}".format(1 if active else 0)) return self.rigCommand("T {0}".format(1 if active else 0))
# Set rig's frequency
def rigFrequency(self, freq: int) -> bool: def rigFrequency(self, freq: int) -> bool:
return self.rigCommand("F {0}".format(freq)) return self.rigCommand("F {0}".format(freq))
# Set rig's modulation
def rigModulation(self, mod: str) -> bool: def rigModulation(self, mod: str) -> bool:
if mod in self.MODES: if mod in self.MODES:
return self.rigCommand("M {0} 0".format(self.MODES[mod])) return self.rigCommand("M {0} 0".format(self.MODES[mod]))
else: else:
return False return False
# Start the main thread # Start Rigctl and associated thread
def rigStart(self): def rigStart(self):
# Do not start twice # Do not start twice
if self.rigctl is not None: if self.rigctl is not None:
@ -383,19 +388,21 @@ class RigControl():
logger.debug("Started RigControl as '{0}'.".format(" ".join(cmd))) logger.debug("Started RigControl as '{0}'.".format(" ".join(cmd)))
return True return True
# Stop the main thread # Stop Rigctl and associated thread
def rigStop(self): def rigStop(self):
# Do not stop twice # Do not stop twice
if self.rigctl is None: if self.rigctl is None:
return return
# Try terminating RigCtl normally, kill if failed to terminate # If Rigctl still running...
logger.info("Stopping RigControl executable...") if self.rigctl.poll() is None:
try: # Try terminating Rigctl normally, kill if failed
self.rigctl.terminate() logger.info("Stopping RigControl executable...")
self.rigctl.wait(3) try:
except TimeoutExpired: self.rigctl.terminate()
self.rigctl.kill() self.rigctl.wait(3)
# The thread should have exited, since stdout/stderr closed except TimeoutExpired:
self.rigctl.kill()
# The thread should have exited, since Rigctl exited
logger.info("Waiting for RigControl thread...") logger.info("Waiting for RigControl thread...")
self.thread.join() self.thread.join()
logger.info("Stopped RigControl.") logger.info("Stopped RigControl.")
@ -418,19 +425,18 @@ class RigControl():
# Failed to send command # Failed to send command
return False return False
# This is the actual thread function # This thread function reads from Rigctl process' stdout/stderr
def _rigThread(self): def _rigThread(self):
# While RigControl is running... # While process is running...
while self.rigctl.poll() is None: while self.rigctl.poll() is None:
try: try:
# Wait for output from the process # Wait for output from the process
readable, _, _ = select.select([self.rigctl.stdout, self.rigctl.stderr], [], []) readable, _, _ = select.select([self.rigctl.stdout, self.rigctl.stderr], [], [])
for pipe in readable: for pipe in readable:
rsp = pipe.read().strip() rsp = pipe.read().strip()
#if len(rsp) > 0:
logger.debug("STD{0}: {1}".format("ERR" if pipe==self.rigctl.stderr else "OUT", rsp)) logger.debug("STD{0}: {1}".format("ERR" if pipe==self.rigctl.stderr else "OUT", rsp))
except Exception as e: except Exception as e:
logger.debug("Failed receiving from RigControl: {1}.".format(str(e))) logger.debug("Failed receiving from RigControl: {1}.".format(str(e)))
# RigControl stopped # Process stopped
logger.debug("RigControl process quit ({0}).".format(self.rigctl.poll())) logger.debug("RigControl process quit ({0}).".format(self.rigctl.poll()))