Created
May 9, 2019 11:12
-
-
Save TheSkorm/59a15edaea7de2d93f2c7c141cd3c9f3 to your computer and use it in GitHub Desktop.
Sondehub Gateway
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3.7 | |
| import sys | |
| sys.path.append('./packages') | |
| import boto3 | |
| import requests | |
| import json | |
| import base64 | |
| from natsort import natsorted | |
| from aws_xray_sdk.core import xray_recorder | |
| from aws_xray_sdk.core import patch_all | |
| xray_recorder.configure() | |
| patch_all() | |
| #TODO make an env variable | |
| BANNED_CALLSIGNS = ["banned"] | |
| MINIMUM_VERSION = "autorx-1.0.2.1" | |
| def valid_version(version): #TODO | |
| versions_sorted = natsorted([version, MINIMUM_VERSION]) | |
| if version == MINIMUM_VERSION: | |
| return True | |
| if versions_sorted[1] == MINIMUM_VERSION: | |
| return False | |
| return True | |
| def valid_call(callsign): | |
| if callsign in BANNED_CALLSIGNS: | |
| return False | |
| return True | |
| def iot_post(data): | |
| try: | |
| iot_data = boto3.client('iot-data') | |
| result = iot_data.publish( | |
| topic="sondes", | |
| payload=json.dumps(data), | |
| qos=0 | |
| ) | |
| except Exception as e: | |
| print(e) | |
| def habhub_post(path, body): | |
| response = None | |
| try: | |
| response = requests.post( | |
| url="http://habitat.habhub.org" + path, | |
| json=body | |
| ) | |
| except Exception as e: | |
| print(e) | |
| return response | |
| def decode_body(body): | |
| data = json.loads(body) | |
| raw = str(base64.b64decode(data['data']['_raw'])) | |
| (serial, frame, time, lat, lng, alt, vel_h, temp, humidity, comment) = raw.split(",",9) | |
| (comment, checksum) = comment.strip().split("*") | |
| for receiver in data["receivers"]: | |
| decoded_data = { | |
| "serial": serial, | |
| "frame": frame, | |
| "time": time, | |
| "location": { | |
| "lat": lat, | |
| "lon": lng}, | |
| "alt": alt, | |
| "vel_h": vel_h, | |
| "temp": temp, | |
| "humidity": humidity, | |
| "comment": comment, | |
| "checksum": checksum, | |
| "type": data["type"], | |
| "receiver": receiver, | |
| "time_created": data["receivers"][receiver]["time_created"], | |
| "time_uploaded": data["receivers"][receiver]["time_uploaded"] | |
| } | |
| return decoded_data | |
| def handler(event, context): | |
| print(event) | |
| data = decode_body(event['body']) | |
| json_body = json.loads(event['body']) | |
| if not valid_call(data["receiver"]): | |
| return {"statusCode": 426,"body": "YOU ARE BANNED - CONTACT mwheeler or darkside on #highaltitude on Freenode","isBase64Encoded": False} | |
| if not valid_version(event["headers"]["User-Agent"]) and "_DFM" in data["serial"]: | |
| return {"statusCode": 426,"body": "Black listed version. This version of AutoRX is known to cause issues, please update to a later version. Your data has not been uploaded.","isBase64Encoded": False} | |
| habhub_response = habhub_post(event['path'], json_body) | |
| iot_data = json_body | |
| iot_data['version']=event["headers"]["User-Agent"] | |
| iot_data['decoded']=data | |
| iot_post(iot_data) | |
| if type(habhub_response) == requests.models.Response: | |
| print({"habhub-seconds": habhub_response.elapsed.total_seconds()}) | |
| print(habhub_response.status_code) | |
| print(habhub_response.text) | |
| return { "statusCode": habhub_response.status_code , "body": habhub_response.text, "isBase64Encoded": False } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment