openwebrx-clone/owrx/source/connector.py

75 lines
2.3 KiB
Python
Raw Normal View History

from . import SdrSource
from owrx.socket import getAvailablePort
import socket
2019-12-27 23:26:45 +00:00
from owrx.command import CommandMapper, Flag, Option
import logging
logger = logging.getLogger(__name__)
class ConnectorSource(SdrSource):
def __init__(self, id, props):
self.controlSocket = None
self.controlPort = getAvailablePort()
2019-12-31 18:14:05 +00:00
super().__init__(id, props)
def getCommandMapper(self):
return super().getCommandMapper().setMappings(
2019-12-28 00:24:07 +00:00
{
"samp_rate": Option("-s"),
"tuner_freq": Option("-f"),
"port": Option("-p"),
"controlPort": Option("-c"),
"device": Option("-d"),
"iqswap": Flag("-i"),
"rtltcp_compat": Flag("-r"),
2019-12-28 22:05:59 +00:00
"ppm": Option("-P"),
2019-12-28 00:24:07 +00:00
"rf_gain": Option("-g"),
}
)
def getEventNames(self):
2019-12-27 23:26:45 +00:00
return super().getEventNames() + [
"device",
"iqswap",
"rtltcp_compat",
]
def sendControlMessage(self, prop, value):
logger.debug("sending property change over control socket: {0} changed to {1}".format(prop, value))
self.controlSocket.sendall("{prop}:{value}\n".format(prop=prop, value=value).encode())
2019-12-27 23:26:45 +00:00
def onPropertyChange(self, prop, value):
if self.monitor is None:
return
if (
2019-12-28 00:24:07 +00:00
(prop == "center_freq" or prop == "lfo_offset")
2020-03-24 21:52:17 +00:00
and "lfo_offset" in self.sdrProps
and self.sdrProps["lfo_offset"] is not None
2019-12-27 23:26:45 +00:00
):
2020-03-24 21:52:17 +00:00
freq = self.sdrProps["center_freq"] + self.sdrProps["lfo_offset"]
2019-12-27 23:26:45 +00:00
self.sendControlMessage("center_freq", freq)
else:
self.sendControlMessage(prop, value)
def postStart(self):
logger.debug("opening control socket...")
self.controlSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.controlSocket.connect(("localhost", self.controlPort))
def stop(self):
super().stop()
if self.controlSocket:
self.controlSocket.close()
self.controlSocket = None
2019-12-27 23:26:45 +00:00
def getControlPort(self):
return self.controlPort
2019-12-27 23:26:45 +00:00
def getCommandValues(self):
values = super().getCommandValues()
values["port"] = self.getPort()
values["controlPort"] = self.getControlPort()
return values