Skip to content

Commit 7e79913

Browse files
authored
Merge pull request #175 from DJDevon3/DJDevon3-RocketLaunchLive
minor updates for RocketLaunchLive API
2 parents 833c4ad + 750317a commit 7e79913

File tree

1 file changed

+38
-28
lines changed

1 file changed

+38
-28
lines changed

examples/wifi/expanded/requests_wifi_api_rocketlaunch_live.py

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# SPDX-FileCopyrightText: 2024 DJDevon3
22
# SPDX-License-Identifier: MIT
3-
# Coded for Circuit Python 8.2.x
3+
# Coded for Circuit Python 9.0
44
"""RocketLaunch.Live API Example"""
55

66
import os
@@ -13,31 +13,22 @@
1313

1414
# Time between API refreshes
1515
# 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour
16-
sleep_time = 43200
16+
SLEEP_TIME = 43200
1717

1818
# Get WiFi details, ensure these are setup in settings.toml
19-
ssid = os.getenv("WIFI_SSID")
20-
password = os.getenv("WIFI_PASSWORD")
19+
ssid = os.getenv("CIRCUITPY_WIFI_SSID")
20+
password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
2121

2222

23-
# Converts seconds in minutes/hours/days
2423
def time_calc(input_time):
24+
"""Converts seconds to minutes/hours/days"""
2525
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
26+
return f"{input_time:.0f} seconds"
27+
if input_time < 3600:
28+
return f"{input_time / 60:.0f} minutes"
29+
if input_time < 86400:
30+
return f"{input_time / 60 / 60:.0f} hours"
31+
return f"{input_time / 60 / 60 / 24:.1f} days"
4132

4233

4334
# Publicly available data no header required
@@ -81,7 +72,7 @@ def time_calc(input_time):
8172
# JSON Endpoints
8273
RLFN = str(rocketlaunch_json["result"][0]["name"])
8374
RLWO = str(rocketlaunch_json["result"][0]["win_open"])
84-
TMINUS = str(rocketlaunch_json["result"][0]["t0"])
75+
TZERO = str(rocketlaunch_json["result"][0]["t0"])
8576
RLWC = str(rocketlaunch_json["result"][0]["win_close"])
8677
RLP = str(rocketlaunch_json["result"][0]["provider"]["name"])
8778
RLVN = str(rocketlaunch_json["result"][0]["vehicle"]["name"])
@@ -100,10 +91,26 @@ def time_calc(input_time):
10091
print(f" | | Provider: {RLP}")
10192
if RLVN != "None":
10293
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}")
94+
95+
# Launch time can sometimes be Window Open to Close, T-Zero, or weird combination.
96+
# Should obviously be standardized but they're not input that way.
97+
# Have to account for every combination of 3 conditions.
98+
# T-Zero Launch Time Conditionals
99+
if RLWO == "None" and TZERO != "None" and RLWC != "None":
100+
print(f" | | Window: {TZERO} | {RLWC}")
101+
elif RLWO != "None" and TZERO != "None" and RLWC == "None":
102+
print(f" | | Window: {RLWO} | {TZERO}")
103+
elif RLWO != "None" and TZERO == "None" and RLWC != "None":
104+
print(f" | | Window: {RLWO} | {RLWC}")
105+
elif RLWO != "None" and TZERO != "None" and RLWC != "None":
106+
print(f" | | Window: {RLWO} | {TZERO} | {RLWC}")
107+
elif RLWO == "None" and TZERO != "None" and RLWC == "None":
108+
print(f" | | Window: {TZERO}")
109+
elif RLWO != "None" and TZERO == "None" and RLWC == "None":
110+
print(f" | | Window: {RLWO}")
111+
elif RLWO == "None" and TZERO == "None" and RLWC != "None":
112+
print(f" | | Window: {RLWC}")
113+
107114
if RLLS != "None":
108115
print(f" | | Site: {RLLS}")
109116
if RLPN != "None":
@@ -113,13 +120,16 @@ def time_calc(input_time):
113120
if RLM != "None":
114121
print(f" | | Mission: {RLM}")
115122

123+
rocketlaunch_response.close()
124+
print("✂️ Disconnected from RocketLaunch.Live API")
125+
116126
print("\nFinished!")
117-
print("Board Uptime: ", time.monotonic())
118-
print("Next Update in: ", time_calc(sleep_time))
127+
print(f"Board Uptime: {time_calc(time.monotonic())}")
128+
print(f"Next Update: {time_calc(SLEEP_TIME)}")
119129
print("===============================")
120130

121131
except (ValueError, RuntimeError) as e:
122132
print("Failed to get data, retrying\n", e)
123133
time.sleep(60)
124134
break
125-
time.sleep(sleep_time)
135+
time.sleep(SLEEP_TIME)

0 commit comments

Comments
 (0)