From c7c3810c6f9d00d327b0f95c46806f49e9941e7a Mon Sep 17 00:00:00 2001 From: Mark Jessop Date: Sat, 11 Jul 2020 17:40:09 +0930 Subject: [PATCH] Move horus UDP packet generator to this repo --- horusdemodlib/__init__.py | 2 +- horusdemodlib/horusudp.py | 91 +++++++++++++++++++++++++++++++++++++++ pyproject.toml | 2 +- 3 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 horusdemodlib/horusudp.py diff --git a/horusdemodlib/__init__.py b/horusdemodlib/__init__.py index c11f861..569b121 100755 --- a/horusdemodlib/__init__.py +++ b/horusdemodlib/__init__.py @@ -1 +1 @@ -__version__ = "0.1.9" +__version__ = "0.1.10" diff --git a/horusdemodlib/horusudp.py b/horusdemodlib/horusudp.py new file mode 100644 index 0000000..b57168e --- /dev/null +++ b/horusdemodlib/horusudp.py @@ -0,0 +1,91 @@ +#!/usr/bin/env python +# +# Horus Telemetry GUI - Horus UDP +# +# Mark Jessop +# +import datetime +import json +import logging +import socket + + +def send_payload_summary(telemetry, port=55672, comment="HorusDemodLib"): + """ Send a payload summary message into the network via UDP broadcast. + + Args: + telemetry (dict): Telemetry dictionary to send. + port (int): UDP port to send to. + + """ + + try: + # Do a few checks before sending. + if telemetry["latitude"] == 0.0 and telemetry["longitude"] == 0.0: + logging.error("Horus UDP - Zero Latitude/Longitude, not sending.") + return + + packet = { + "type": "PAYLOAD_SUMMARY", + "callsign": telemetry["callsign"], + "latitude": telemetry["latitude"], + "longitude": telemetry["longitude"], + "altitude": telemetry["altitude"], + "speed": -1, + "heading": -1, + "time": telemetry["time"], + "comment": comment, + "temp": -1, + "sats": -1, + "batt_voltage": -1, + } + + if 'snr' in telemetry: + packet['snr'] = telemetry['snr'] + + # Set up our UDP socket + _s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + _s.settimeout(1) + # Set up socket for broadcast, and allow re-use of the address + _s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) + _s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + # Under OSX we also need to set SO_REUSEPORT to 1 + try: + _s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) + except: + pass + + try: + _s.sendto(json.dumps(packet).encode("ascii"), ("", port)) + # Catch any socket errors, that may occur when attempting to send to a broadcast address + # when there is no network connected. In this case, re-try and send to localhost instead. + except socket.error as e: + logging.debug( + "Horus UDP - Send to broadcast address failed, sending to localhost instead." + ) + _s.sendto(json.dumps(packet).encode("ascii"), ("127.0.0.1", port)) + + _s.close() + + except Exception as e: + logging.error("Horus UDP - Error sending Payload Summary: %s" % str(e)) + + +if __name__ == "__main__": + # Test script for the above functions + from horusdemodlib.decoder import parse_ukhas_string + from horusdemodlib.checksums import ukhas_crc + # Setup Logging + logging.basicConfig( + format="%(asctime)s %(levelname)s: %(message)s", level=logging.INFO + ) + + sentence = "$$TESTING,1,01:02:03,-34.0,138.0,1000" + crc = ukhas_crc(sentence[2:].encode("ascii")) + sentence = sentence + "*" + crc + print("Sentence: " + sentence) + + _decoded = parse_ukhas_string(sentence) + print(_decoded) + + send_payload_summary(_decoded) diff --git a/pyproject.toml b/pyproject.toml index 75147e3..5847505 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "horusdemodlib" -version = "0.1.9" +version = "0.1.10" description = "Project Horus HAB Telemetry Demodulators" authors = ["Mark Jessop"] license = "LGPL-2.1-or-later"