2024-01-30 01:29:25 +01:00
|
|
|
from paho.mqtt.client import Client
|
|
|
|
|
from owrx.reporting.reporter import Reporter
|
|
|
|
|
from owrx.config import Config
|
2024-01-30 23:02:28 +01:00
|
|
|
from owrx.property import PropertyDeleted
|
2024-01-30 01:29:25 +01:00
|
|
|
import json
|
|
|
|
|
|
2024-01-30 23:02:28 +01:00
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2024-01-30 01:29:25 +01:00
|
|
|
|
|
|
|
|
class MqttReporter(Reporter):
|
2024-01-30 23:02:28 +01:00
|
|
|
DEFAULT_TOPIC = "openwebrx/decodes"
|
|
|
|
|
|
2024-01-30 01:29:25 +01:00
|
|
|
def __init__(self):
|
|
|
|
|
pm = Config.get()
|
|
|
|
|
self.client = Client()
|
2024-01-30 23:02:28 +01:00
|
|
|
self.topic = self.DEFAULT_TOPIC
|
|
|
|
|
self.subscriptions = [
|
|
|
|
|
pm.wireProperty("mqtt_host", self._setHost),
|
|
|
|
|
pm.wireProperty("mqtt_topic", self._setTopic),
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
def _setHost(self, host):
|
|
|
|
|
logger.debug("setting host to %s", host)
|
|
|
|
|
self.client.disconnect()
|
|
|
|
|
parts = host.split(":")
|
|
|
|
|
host = parts[0]
|
|
|
|
|
port = int(parts[1]) if len(parts) > 1 else 1883
|
|
|
|
|
self.client.connect(host=host, port=port)
|
|
|
|
|
|
|
|
|
|
def _setTopic(self, topic):
|
|
|
|
|
if topic is PropertyDeleted:
|
|
|
|
|
self.topic = self.DEFAULT_TOPIC
|
|
|
|
|
else:
|
|
|
|
|
self.topic = topic
|
2024-01-30 01:29:25 +01:00
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
|
self.client.disconnect()
|
2024-01-30 23:02:28 +01:00
|
|
|
while self.subscriptions:
|
|
|
|
|
self.subscriptions.pop().cancel()
|
2024-01-30 01:29:25 +01:00
|
|
|
|
|
|
|
|
def spot(self, spot):
|
2024-01-30 23:02:28 +01:00
|
|
|
self.client.publish(self.topic, payload=json.dumps(spot))
|