1
1
# SPDX-FileCopyrightText: 2024 DJDevon3
2
2
# SPDX-License-Identifier: MIT
3
- # Coded for Circuit Python 8.2.x
3
+ # Coded for Circuit Python 9.0
4
4
"""RocketLaunch.Live API Example"""
5
5
6
6
import os
13
13
14
14
# Time between API refreshes
15
15
# 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour
16
- sleep_time = 43200
16
+ SLEEP_TIME = 43200
17
17
18
18
# 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 " )
21
21
22
22
23
- # Converts seconds in minutes/hours/days
24
23
def time_calc (input_time ):
24
+ """Converts seconds to minutes/hours/days"""
25
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
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"
41
32
42
33
43
34
# Publicly available data no header required
@@ -81,7 +72,7 @@ def time_calc(input_time):
81
72
# JSON Endpoints
82
73
RLFN = str (rocketlaunch_json ["result" ][0 ]["name" ])
83
74
RLWO = str (rocketlaunch_json ["result" ][0 ]["win_open" ])
84
- TMINUS = str (rocketlaunch_json ["result" ][0 ]["t0" ])
75
+ TZERO = str (rocketlaunch_json ["result" ][0 ]["t0" ])
85
76
RLWC = str (rocketlaunch_json ["result" ][0 ]["win_close" ])
86
77
RLP = str (rocketlaunch_json ["result" ][0 ]["provider" ]["name" ])
87
78
RLVN = str (rocketlaunch_json ["result" ][0 ]["vehicle" ]["name" ])
@@ -100,10 +91,26 @@ def time_calc(input_time):
100
91
print (f" | | Provider: { RLP } " )
101
92
if RLVN != "None" :
102
93
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
+
107
114
if RLLS != "None" :
108
115
print (f" | | Site: { RLLS } " )
109
116
if RLPN != "None" :
@@ -113,13 +120,16 @@ def time_calc(input_time):
113
120
if RLM != "None" :
114
121
print (f" | | Mission: { RLM } " )
115
122
123
+ rocketlaunch_response .close ()
124
+ print ("✂️ Disconnected from RocketLaunch.Live API" )
125
+
116
126
print ("\n Finished!" )
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 ) } " )
119
129
print ("===============================" )
120
130
121
131
except (ValueError , RuntimeError ) as e :
122
132
print ("Failed to get data, retrying\n " , e )
123
133
time .sleep (60 )
124
134
break
125
- time .sleep (sleep_time )
135
+ time .sleep (SLEEP_TIME )
0 commit comments