Skip to content

Commit 11ead55

Browse files
authored
Merge pull request #84 from makermelissa/main
Fix status colors when getting time
2 parents a757cc0 + 49798b7 commit 11ead55

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

adafruit_portalbase/network.py

100644100755
Lines changed: 14 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
"""
@@ -206,13 +204,16 @@ def get_strftime(self, time_format, location=None):
206204
api_url += "&fmt=" + self.url_encode(time_format)
207205

208206
try:
207+
self.neo_status(STATUS_FETCHING)
209208
response = self._wifi.requests.get(api_url, timeout=10)
209+
self.neo_status(STATUS_DATA_RECEIVED)
210210
if response.status_code != 200:
211211
print(response)
212212
error_message = (
213213
"Error connecting to Adafruit IO. The response was: "
214214
+ response.text
215215
)
216+
self.neo_status(STATUS_HTTP_ERROR)
216217
raise RuntimeError(error_message)
217218
if self._debug:
218219
print("Time request: ", api_url)
@@ -285,7 +286,7 @@ def wget(self, url, filename, *, chunk_size=12000, headers=None):
285286
print("Content-Length: {}".format(int(headers["content-length"])))
286287
if "date" in headers:
287288
print("Date: {}".format(headers["date"]))
288-
self.neo_status((100, 0, 0)) # red = http error
289+
self.neo_status(STATUS_HTTP_ERROR) # red = http error
289290
raise HttpError(
290291
"Code {}: {}".format(
291292
response.status_code, response.reason.decode("utf-8")
@@ -375,6 +376,7 @@ def connect(self, max_attempts=10):
375376
try:
376377
self._wifi.connect(secret_entry["ssid"], secret_entry["password"])
377378
self.requests = self._wifi.requests
379+
self._wifi.neo_status(STATUS_CONNECTED)
378380
break
379381
except (RuntimeError, ConnectionError) as error:
380382
if max_attempts is not None and attempt >= max_attempts:

0 commit comments

Comments
 (0)