|
| 1 | +# SPDX-FileCopyrightText: 2024 DJDevon3 |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +# Coded for Circuit Python 8.2.x |
| 4 | +"""RocketLaunch.Live API Example""" |
| 5 | + |
| 6 | +import os |
| 7 | +import time |
| 8 | + |
| 9 | +import adafruit_connection_manager |
| 10 | +import wifi |
| 11 | + |
| 12 | +import adafruit_requests |
| 13 | + |
| 14 | +# Time between API refreshes |
| 15 | +# 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour |
| 16 | +sleep_time = 43200 |
| 17 | + |
| 18 | +# Get WiFi details, ensure these are setup in settings.toml |
| 19 | +ssid = os.getenv("WIFI_SSID") |
| 20 | +password = os.getenv("WIFI_PASSWORD") |
| 21 | + |
| 22 | + |
| 23 | +# Converts seconds in minutes/hours/days |
| 24 | +def time_calc(input_time): |
| 25 | + if input_time < 60: |
| 26 | + sleep_int = input_time |
| 27 | + time_output = f"{sleep_int:.0f} seconds" |
| 28 | + elif 60 <= input_time < 3600: |
| 29 | + sleep_int = input_time / 60 |
| 30 | + time_output = f"{sleep_int:.0f} minutes" |
| 31 | + elif 3600 <= input_time < 86400: |
| 32 | + sleep_int = input_time / 60 / 60 |
| 33 | + time_output = f"{sleep_int:.0f} hours" |
| 34 | + elif 86400 <= input_time < 432000: |
| 35 | + sleep_int = input_time / 60 / 60 / 24 |
| 36 | + time_output = f"{sleep_int:.1f} days" |
| 37 | + else: # if > 5 days convert float to int & display whole days |
| 38 | + sleep_int = input_time / 60 / 60 / 24 |
| 39 | + time_output = f"{sleep_int:.0f} days" |
| 40 | + return time_output |
| 41 | + |
| 42 | + |
| 43 | +# Publicly available data no header required |
| 44 | +# The number at the end is the amount of launches (max 5 free api) |
| 45 | +ROCKETLAUNCH_SOURCE = "https://fdo.rocketlaunch.live/json/launches/next/1" |
| 46 | + |
| 47 | +# Initalize Wifi, Socket Pool, Request Session |
| 48 | +pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) |
| 49 | +ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) |
| 50 | +requests = adafruit_requests.Session(pool, ssl_context) |
| 51 | + |
| 52 | +while True: |
| 53 | + # Connect to Wi-Fi |
| 54 | + print("\n===============================") |
| 55 | + print("Connecting to WiFi...") |
| 56 | + while not wifi.radio.ipv4_address: |
| 57 | + try: |
| 58 | + wifi.radio.connect(ssid, password) |
| 59 | + except ConnectionError as e: |
| 60 | + print("❌ Connection Error:", e) |
| 61 | + print("Retrying in 10 seconds") |
| 62 | + print("✅ Wifi!") |
| 63 | + try: |
| 64 | + # Print Request to Serial |
| 65 | + print(" | Attempting to GET RocketLaunch.Live JSON!") |
| 66 | + time.sleep(2) |
| 67 | + debug_rocketlaunch_full_response = False |
| 68 | + |
| 69 | + try: |
| 70 | + rocketlaunch_response = requests.get(url=ROCKETLAUNCH_SOURCE) |
| 71 | + rocketlaunch_json = rocketlaunch_response.json() |
| 72 | + except ConnectionError as e: |
| 73 | + print("Connection Error:", e) |
| 74 | + print("Retrying in 10 seconds") |
| 75 | + print(" | ✅ RocketLaunch.Live JSON!") |
| 76 | + |
| 77 | + if debug_rocketlaunch_full_response: |
| 78 | + print("Full API GET URL: ", ROCKETLAUNCH_SOURCE) |
| 79 | + print(rocketlaunch_json) |
| 80 | + |
| 81 | + # JSON Endpoints |
| 82 | + RLFN = str(rocketlaunch_json["result"][0]["name"]) |
| 83 | + RLWO = str(rocketlaunch_json["result"][0]["win_open"]) |
| 84 | + TMINUS = str(rocketlaunch_json["result"][0]["t0"]) |
| 85 | + RLWC = str(rocketlaunch_json["result"][0]["win_close"]) |
| 86 | + RLP = str(rocketlaunch_json["result"][0]["provider"]["name"]) |
| 87 | + RLVN = str(rocketlaunch_json["result"][0]["vehicle"]["name"]) |
| 88 | + RLPN = str(rocketlaunch_json["result"][0]["pad"]["name"]) |
| 89 | + RLLS = str(rocketlaunch_json["result"][0]["pad"]["location"]["name"]) |
| 90 | + RLLD = str(rocketlaunch_json["result"][0]["launch_description"]) |
| 91 | + RLM = str(rocketlaunch_json["result"][0]["mission_description"]) |
| 92 | + RLDATE = str(rocketlaunch_json["result"][0]["date_str"]) |
| 93 | + |
| 94 | + # Print to serial & display label if endpoint not "None" |
| 95 | + if RLDATE != "None": |
| 96 | + print(f" | | Date: {RLDATE}") |
| 97 | + if RLFN != "None": |
| 98 | + print(f" | | Flight: {RLFN}") |
| 99 | + if RLP != "None": |
| 100 | + print(f" | | Provider: {RLP}") |
| 101 | + if RLVN != "None": |
| 102 | + print(f" | | Vehicle: {RLVN}") |
| 103 | + if RLWO != "None": |
| 104 | + print(f" | | Window: {RLWO} to {RLWC}") |
| 105 | + elif TMINUS != "None": |
| 106 | + print(f" | | Window: {TMINUS} to {RLWC}") |
| 107 | + if RLLS != "None": |
| 108 | + print(f" | | Site: {RLLS}") |
| 109 | + if RLPN != "None": |
| 110 | + print(f" | | Pad: {RLPN}") |
| 111 | + if RLLD != "None": |
| 112 | + print(f" | | Description: {RLLD}") |
| 113 | + if RLM != "None": |
| 114 | + print(f" | | Mission: {RLM}") |
| 115 | + |
| 116 | + print("\nFinished!") |
| 117 | + print("Board Uptime: ", time.monotonic()) |
| 118 | + print("Next Update in: ", time_calc(sleep_time)) |
| 119 | + print("===============================") |
| 120 | + |
| 121 | + except (ValueError, RuntimeError) as e: |
| 122 | + print("Failed to get data, retrying\n", e) |
| 123 | + time.sleep(60) |
| 124 | + break |
| 125 | + time.sleep(sleep_time) |
0 commit comments