From 7533a3bae8ce05d853e501952ddd1dfe041aaa98 Mon Sep 17 00:00:00 2001 From: Mark Jessop Date: Sun, 26 Jul 2020 16:27:11 +0930 Subject: [PATCH] Add OziMux output option --- horusdemodlib/__init__.py | 2 +- horusdemodlib/horusudp.py | 43 ++++++++++++++++++++++++++++++++++++++- pyproject.toml | 2 +- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/horusdemodlib/__init__.py b/horusdemodlib/__init__.py index 86205cb..08f934f 100755 --- a/horusdemodlib/__init__.py +++ b/horusdemodlib/__init__.py @@ -1 +1 @@ -__version__ = "0.1.17" +__version__ = "0.1.18" diff --git a/horusdemodlib/horusudp.py b/horusdemodlib/horusudp.py index b57168e..be8fde2 100644 --- a/horusdemodlib/horusudp.py +++ b/horusdemodlib/horusudp.py @@ -11,7 +11,8 @@ import socket def send_payload_summary(telemetry, port=55672, comment="HorusDemodLib"): - """ Send a payload summary message into the network via UDP broadcast. + """ + Send a payload summary message into the network via UDP broadcast. Args: telemetry (dict): Telemetry dictionary to send. @@ -71,6 +72,44 @@ def send_payload_summary(telemetry, port=55672, comment="HorusDemodLib"): logging.error("Horus UDP - Error sending Payload Summary: %s" % str(e)) +def send_ozimux_message(telemetry, port=55683): + """ + Send an OziMux-compatible message via UDP broadcast, of the form: + + TELEMETRY,HH:MM:SS,lat,lon,alt\n + + """ + try: + _sentence = f"TELEMETRY,{telemetry['time']},{telemetry['latitude']:.5f},{telemetry['longitude']:.5f},{telemetry['altitude']}\n" + except Exception as e: + logging.error(f"OziMux Message - Could not convert telemetry - {str(e)}") + return + + try: + _ozisock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) + + # Set up socket for broadcast, and allow re-use of the address + _ozisock.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST,1) + _ozisock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + # SO_REUSEPORT doesn't work on all platforms, so catch the exception if it fails + try: + _ozisock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) + except: + pass + # Send! + try: + _ozisock.sendto(_sentence.encode('ascii'),('',port)) + except socket.error as e: + logging.warning("OziMux Message - Send to broadcast address failed, sending to localhost instead.") + _ozisock.sendto(_sentence.encode('ascii'),('127.0.0.1',port)) + + _ozisock.close() + logging.debug(f"Sent Telemetry to OziMux ({port}): {sentence.strip()}") + return _sentence + except Exception as e: + logging.error(f"Failed to send OziMux packet: {str(e)}") + + if __name__ == "__main__": # Test script for the above functions from horusdemodlib.decoder import parse_ukhas_string @@ -89,3 +128,5 @@ if __name__ == "__main__": print(_decoded) send_payload_summary(_decoded) + + print(send_ozimux_message(_decoded)) diff --git a/pyproject.toml b/pyproject.toml index 295fe19..ac1810a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "horusdemodlib" -version = "0.1.17" +version = "0.1.18" description = "Project Horus HAB Telemetry Demodulators" authors = ["Mark Jessop"] license = "LGPL-2.1-or-later"