Skip to content

Fix status colors when getting time #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions adafruit_portalbase/network.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@
LOCALFILE = "local.txt"
# pylint: enable=line-too-long

STATUS_NO_CONNECTION = (100, 0, 0)
STATUS_CONNECTING = (0, 0, 100)
STATUS_FETCHING = (200, 100, 0)
STATUS_DOWNLOADING = (0, 100, 100)
STATUS_CONNECTED = (0, 100, 0)
STATUS_DATA_RECEIVED = (0, 0, 100)
STATUS_OFF = (0, 0, 0)
STATUS_NO_CONNECTION = (100, 0, 0) # Red
STATUS_CONNECTING = (0, 0, 100) # Blue
STATUS_FETCHING = (150, 100, 0) # Orange
STATUS_DOWNLOADING = (0, 100, 100) # Cyan
STATUS_CONNECTED = (0, 0, 100) # Blue
STATUS_DATA_RECEIVED = (0, 100, 0) # Green
STATUS_HTTP_ERROR = (100, 0, 0) # Red
STATUS_OFF = (0, 0, 0) # Off

CONTENT_TEXT = const(1)
CONTENT_JSON = const(2)
Expand Down Expand Up @@ -171,10 +172,7 @@ def url_encode(url):
"""
A function to perform minimal URL encoding
"""
url = url.replace(" ", "+")
url = url.replace("%", "%25")
url = url.replace(":", "%3A")
return url
return url.replace(" ", "+").replace("%", "%25").replace(":", "%3A")

def get_strftime(self, time_format, location=None):
"""
Expand Down Expand Up @@ -206,13 +204,16 @@ def get_strftime(self, time_format, location=None):
api_url += "&fmt=" + self.url_encode(time_format)

try:
self.neo_status(STATUS_FETCHING)
response = self._wifi.requests.get(api_url, timeout=10)
self.neo_status(STATUS_DATA_RECEIVED)
if response.status_code != 200:
print(response)
error_message = (
"Error connecting to Adafruit IO. The response was: "
+ response.text
)
self.neo_status(STATUS_HTTP_ERROR)
raise RuntimeError(error_message)
if self._debug:
print("Time request: ", api_url)
Expand Down Expand Up @@ -285,7 +286,7 @@ def wget(self, url, filename, *, chunk_size=12000, headers=None):
print("Content-Length: {}".format(int(headers["content-length"])))
if "date" in headers:
print("Date: {}".format(headers["date"]))
self.neo_status((100, 0, 0)) # red = http error
self.neo_status(STATUS_HTTP_ERROR) # red = http error
raise HttpError(
"Code {}: {}".format(
response.status_code, response.reason.decode("utf-8")
Expand Down Expand Up @@ -375,6 +376,7 @@ def connect(self, max_attempts=10):
try:
self._wifi.connect(secret_entry["ssid"], secret_entry["password"])
self.requests = self._wifi.requests
self._wifi.neo_status(STATUS_CONNECTED)
break
except (RuntimeError, ConnectionError) as error:
if max_attempts is not None and attempt >= max_attempts:
Expand Down