Skip to content

Commit 2042811

Browse files
committed
Fix status colors when getting time
1 parent a757cc0 commit 2042811

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

adafruit_portalbase/network.py

100644100755
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,14 @@
5656
LOCALFILE = "local.txt"
5757
# pylint: enable=line-too-long
5858

59-
STATUS_NO_CONNECTION = (100, 0, 0)
60-
STATUS_CONNECTING = (0, 0, 100)
61-
STATUS_FETCHING = (200, 100, 0)
62-
STATUS_DOWNLOADING = (0, 100, 100)
63-
STATUS_CONNECTED = (0, 100, 0)
64-
STATUS_DATA_RECEIVED = (0, 0, 100)
65-
STATUS_OFF = (0, 0, 0)
59+
STATUS_NO_CONNECTION = (100, 0, 0) # Red
60+
STATUS_CONNECTING = (0, 0, 100) # Blue
61+
STATUS_FETCHING = (150, 100, 0) # Orange
62+
STATUS_DOWNLOADING = (0, 100, 100) # Cyan
63+
STATUS_CONNECTED = (0, 0, 100) # Blue
64+
STATUS_DATA_RECEIVED = (0, 100, 0) # Green
65+
STATUS_HTTP_ERROR = (100, 0, 0) # Red
66+
STATUS_OFF = (0, 0, 0) # Off
6667

6768
CONTENT_TEXT = const(1)
6869
CONTENT_JSON = const(2)
@@ -171,10 +172,7 @@ def url_encode(url):
171172
"""
172173
A function to perform minimal URL encoding
173174
"""
174-
url = url.replace(" ", "+")
175-
url = url.replace("%", "%25")
176-
url = url.replace(":", "%3A")
177-
return url
175+
return url.replace(" ", "+").replace("%", "%25").replace(":", "%3A")
178176

179177
def get_strftime(self, time_format, location=None):
180178
"""
@@ -187,6 +185,7 @@ def get_strftime(self, time_format, location=None):
187185
self.connect()
188186
api_url = None
189187
reply = None
188+
print("Get time")
190189
try:
191190
aio_username = self._secrets["aio_username"]
192191
aio_key = self._secrets["aio_key"]
@@ -206,13 +205,16 @@ def get_strftime(self, time_format, location=None):
206205
api_url += "&fmt=" + self.url_encode(time_format)
207206

208207
try:
208+
self.neo_status(STATUS_FETCHING)
209209
response = self._wifi.requests.get(api_url, timeout=10)
210+
self.neo_status(STATUS_DATA_RECEIVED)
210211
if response.status_code != 200:
211212
print(response)
212213
error_message = (
213214
"Error connecting to Adafruit IO. The response was: "
214215
+ response.text
215216
)
217+
self.neo_status(STATUS_HTTP_ERROR)
216218
raise RuntimeError(error_message)
217219
if self._debug:
218220
print("Time request: ", api_url)
@@ -285,7 +287,7 @@ def wget(self, url, filename, *, chunk_size=12000, headers=None):
285287
print("Content-Length: {}".format(int(headers["content-length"])))
286288
if "date" in headers:
287289
print("Date: {}".format(headers["date"]))
288-
self.neo_status((100, 0, 0)) # red = http error
290+
self.neo_status(STATUS_HTTP_ERROR) # red = http error
289291
raise HttpError(
290292
"Code {}: {}".format(
291293
response.status_code, response.reason.decode("utf-8")
@@ -375,6 +377,7 @@ def connect(self, max_attempts=10):
375377
try:
376378
self._wifi.connect(secret_entry["ssid"], secret_entry["password"])
377379
self.requests = self._wifi.requests
380+
self._wifi.neo_status(STATUS_CONNECTED)
378381
break
379382
except (RuntimeError, ConnectionError) as error:
380383
if max_attempts is not None and attempt >= max_attempts:

0 commit comments

Comments
 (0)