2021-09-07 12:45:52 +00:00
|
|
|
from csdr.module import AutoStartModule
|
2021-09-06 13:05:33 +00:00
|
|
|
from pycsdr.types import Format
|
2021-09-15 13:04:12 +00:00
|
|
|
from pycsdr.modules import Writer, TcpSource
|
2021-09-06 13:05:33 +00:00
|
|
|
from subprocess import Popen, PIPE
|
2021-09-15 13:37:09 +00:00
|
|
|
from owrx.aprs.direwolf import DirewolfConfig, DirewolfConfigSubscriber
|
2021-09-06 13:05:33 +00:00
|
|
|
from owrx.config.core import CoreConfig
|
|
|
|
import threading
|
|
|
|
import time
|
2021-09-15 13:04:12 +00:00
|
|
|
import os
|
2021-09-06 13:05:33 +00:00
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2021-09-15 13:37:09 +00:00
|
|
|
class DirewolfModule(AutoStartModule, DirewolfConfigSubscriber):
|
2021-09-06 13:05:33 +00:00
|
|
|
def __init__(self, service: bool = False):
|
|
|
|
self.process = None
|
|
|
|
self.tcpSource = None
|
|
|
|
self.service = service
|
2021-09-15 13:37:09 +00:00
|
|
|
self.direwolfConfigPath = "{tmp_dir}/openwebrx_direwolf_{myid}.conf".format(
|
|
|
|
tmp_dir=CoreConfig().get_temporary_directory(), myid=id(self)
|
|
|
|
)
|
|
|
|
self.direwolfConfig = None
|
2021-09-06 13:05:33 +00:00
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
def setWriter(self, writer: Writer) -> None:
|
|
|
|
super().setWriter(writer)
|
|
|
|
if self.tcpSource is not None:
|
|
|
|
self.tcpSource.setWriter(writer)
|
|
|
|
|
|
|
|
def getInputFormat(self) -> Format:
|
|
|
|
return Format.SHORT
|
|
|
|
|
|
|
|
def getOutputFormat(self) -> Format:
|
|
|
|
return Format.CHAR
|
|
|
|
|
|
|
|
def start(self):
|
2021-09-15 13:37:09 +00:00
|
|
|
self.direwolfConfig = DirewolfConfig()
|
|
|
|
self.direwolfConfig.wire(self)
|
2021-09-15 13:04:12 +00:00
|
|
|
file = open(self.direwolfConfigPath, "w")
|
2021-09-15 13:37:09 +00:00
|
|
|
file.write(self.direwolfConfig.getConfig(self.service))
|
2021-09-06 13:05:33 +00:00
|
|
|
file.close()
|
|
|
|
|
|
|
|
# direwolf -c {direwolf_config} -r {audio_rate} -t 0 -q d -q h 1>&2
|
|
|
|
self.process = Popen(
|
2021-09-15 13:04:12 +00:00
|
|
|
["direwolf", "-c", self.direwolfConfigPath, "-r", "48000", "-t", "0", "-q", "d", "-q", "h"],
|
2021-09-06 13:05:33 +00:00
|
|
|
start_new_session=True,
|
|
|
|
stdin=PIPE,
|
|
|
|
)
|
|
|
|
|
2021-10-26 14:40:38 +00:00
|
|
|
# resume in case the reader has been stop()ed before
|
|
|
|
self.reader.resume()
|
2021-09-06 13:05:33 +00:00
|
|
|
threading.Thread(target=self.pump(self.reader.read, self.process.stdin.write)).start()
|
|
|
|
|
|
|
|
delay = 0.5
|
|
|
|
retries = 0
|
|
|
|
while True:
|
|
|
|
try:
|
2021-09-15 13:37:09 +00:00
|
|
|
self.tcpSource = TcpSource(self.direwolfConfig.getPort(), Format.CHAR)
|
2021-09-06 13:05:33 +00:00
|
|
|
if self.writer:
|
|
|
|
self.tcpSource.setWriter(self.writer)
|
|
|
|
break
|
|
|
|
except ConnectionError:
|
|
|
|
if retries > 20:
|
|
|
|
logger.error("maximum number of connection attempts reached. did direwolf start up correctly?")
|
|
|
|
raise
|
|
|
|
retries += 1
|
|
|
|
time.sleep(delay)
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
if self.process is not None:
|
|
|
|
self.process.terminate()
|
|
|
|
self.process.wait()
|
|
|
|
self.process = None
|
2021-09-15 13:04:12 +00:00
|
|
|
os.unlink(self.direwolfConfigPath)
|
2021-09-15 13:37:09 +00:00
|
|
|
self.direwolfConfig.unwire(self)
|
|
|
|
self.direwolfConfig = None
|
2021-09-06 13:05:33 +00:00
|
|
|
self.reader.stop()
|
2021-09-15 13:37:09 +00:00
|
|
|
|
|
|
|
def onConfigChanged(self):
|
|
|
|
self.stop()
|
|
|
|
self.start()
|