Skip to content

Commit 0a772cf

Browse files
committed
remove infinite loop, clean up serial output
Using Justins suggested changes
1 parent 5b86eb1 commit 0a772cf

File tree

1 file changed

+44
-62
lines changed

1 file changed

+44
-62
lines changed

examples/wifi/expanded/requests_wifi_api_queuetimes.py

Lines changed: 44 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -40,67 +40,49 @@ def time_calc(input_time):
4040

4141

4242
qtimes_json = {}
43-
while True:
44-
now = time.monotonic()
45-
# Connect to Wi-Fi
46-
print("\n===============================")
47-
print("Connecting to WiFi...")
48-
while not wifi.radio.ipv4_address:
49-
try:
50-
wifi.radio.connect(ssid, password)
51-
except ConnectionError as e:
52-
print("❌ Connection Error:", e)
53-
print("Retrying in 10 seconds")
54-
print("✅ WiFi!")
5543

44+
# Connect to Wi-Fi
45+
print("\n===============================")
46+
print("Connecting to WiFi...")
47+
while not wifi.radio.ipv4_address:
5648
try:
57-
print(" | Attempting to GET Queue-Times JSON!")
58-
try:
59-
qtimes_response = requests.get(url=QTIMES_SOURCE)
60-
qtimes_json = qtimes_response.json()
61-
except ConnectionError as e:
62-
print("Connection Error:", e)
63-
print("Retrying in 10 seconds")
64-
print(" | ✅ Queue-Times JSON!")
65-
66-
DEBUG_QTIMES = False
67-
if DEBUG_QTIMES:
68-
print("Full API GET URL: ", QTIMES_SOURCE)
69-
print(qtimes_json)
70-
qtimes_response.close()
71-
print("✂️ Disconnected from Queue-Times API")
72-
73-
print("\nFinished!")
74-
print(f"Board Uptime: {time_calc(time.monotonic())}")
75-
print(f"Next Update: {time_calc(SLEEP_TIME)}")
76-
print("===============================")
77-
except (ValueError, RuntimeError) as e:
78-
print("Failed to get data, retrying\n", e)
79-
time.sleep(60)
80-
break
81-
82-
# Loop infinitely until its time to re-poll
83-
if time.monotonic() - now <= SLEEP_TIME:
84-
for land in qtimes_json["lands"]:
85-
qtimes_lands = str(land["name"])
86-
print(f" | | Lands: {qtimes_lands}")
87-
time.sleep(1)
88-
89-
# Loop through each ride in the land
90-
for ride in land["rides"]:
91-
qtimes_rides = str(ride["name"])
92-
qtimes_queuetime = str(ride["wait_time"])
93-
qtimes_isopen = str(ride["is_open"])
94-
95-
print(f" | | Ride: {qtimes_rides}")
96-
print(f" | | Queue Time: {qtimes_queuetime} Minutes")
97-
if qtimes_isopen == "False":
98-
print(" | | Status: Closed")
99-
elif qtimes_isopen == "True":
100-
print(" | | Status: Open")
101-
else:
102-
print(" | | Status: Unknown")
103-
104-
time.sleep(1) # delay between list items
105-
else: # When its time to poll, break to top of while True loop.
106-
break
49+
wifi.radio.connect(ssid, password)
50+
except ConnectionError as e:
51+
print("❌ Connection Error:", e)
52+
print("Retrying in 10 seconds")
53+
print("✅ WiFi!")
54+
55+
try:
56+
with requests.get(url=QTIMES_SOURCE) as qtimes_response:
57+
qtimes_json = qtimes_response.json()
58+
except ConnectionError as e:
59+
print("Connection Error:", e)
60+
print(" | ✅ Queue-Times JSON\n")
61+
DEBUG_QTIMES = False
62+
if DEBUG_QTIMES:
63+
print("Full API GET URL: ", QTIMES_SOURCE)
64+
print(qtimes_json)
65+
qtimes_response.close()
66+
67+
# Poll Once and end script
68+
for land in qtimes_json["lands"]:
69+
qtimes_lands = str(land["name"])
70+
print(f" | Land: {qtimes_lands}")
71+
time.sleep(1)
72+
73+
# Loop through each ride in the land
74+
for ride in land["rides"]:
75+
qtimes_rides = str(ride["name"])
76+
qtimes_queuetime = str(ride["wait_time"])
77+
qtimes_isopen = str(ride["is_open"])
78+
79+
print(f" | | Ride: {qtimes_rides}")
80+
print(f" | | Queue Time: {qtimes_queuetime} Minutes")
81+
if qtimes_isopen == "False":
82+
print(" | | Status: Closed\n")
83+
elif qtimes_isopen == "True":
84+
print(" | | Status: Open\n")
85+
else:
86+
print(" | | Status: Unknown\n")
87+
88+
time.sleep(1) # delay between list items

0 commit comments

Comments
 (0)