1
ewconfig/scripts/pskreporter-to-mqtt
2024-11-21 15:48:57 -05:00

59 lines
1.9 KiB
Python
Executable File

#! /usr/bin/env python3
import argparse
import sys
import logging
import json
import paho.mqtt.client as mqtt # pip install paho-mqtt
from datetime import datetime
logger = logging.getLogger(__name__)
def main() -> int:
# Handle program arguments
ap = argparse.ArgumentParser(prog='pskreporter-to-mqtt', description='Reflects pskreporter spots to an MQTT broker')
ap.add_argument("mqtt_broker", help="MQTT broker to connect to")
ap.add_argument('-v', '--verbose', help='Enable verbose logging', action='store_true')
args = ap.parse_args()
# Configure logging
logging.basicConfig(
level=logging.DEBUG if args.verbose else logging.INFO,
format='%(levelname)s: %(message)s',
)
# Connect to the MQTT broker
mqtt_client = mqtt.Client()
mqtt_client.connect(args.mqtt_broker)
# Connect to the pskreporter MQTT broker
pskreporter_client = mqtt.Client()
pskreporter_client.connect("mqtt.pskreporter.info")
# Subscribe to the pskreporter MQTT broker
pskreporter_client.subscribe("pskr/filter/v2/+/+/+/#")
# Handle incoming pskreporter spots
def on_message(client, userdata, message):
payload = json.loads(message.payload)
logger.debug(f"Received spot: {payload}")
# Sanitize the payload
sanitized = {
"frequency_khz": float(payload["f"]) / 1000,
"mode": payload["md"],
"db": int(payload["rp"]),
"timestamp": float(payload["t"]),
"spotter": payload["rc"],
"spotter_grid": payload["rl"],
"spotted": payload["sc"],
"spotted_grid": payload["sl"],
}
mqtt_client.publish(f"radio/spots/pskreporter/{sanitized['mode']}/{sanitized['spotted']}", json.dumps(sanitized))
pskreporter_client.on_message = on_message
pskreporter_client.loop_forever()
return 0
if __name__ == "__main__":
sys.exit(main())