|
56 | 56 | LOCALFILE = "local.txt"
|
57 | 57 | # pylint: enable=line-too-long
|
58 | 58 |
|
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 |
66 | 67 |
|
67 | 68 | CONTENT_TEXT = const(1)
|
68 | 69 | CONTENT_JSON = const(2)
|
@@ -171,10 +172,7 @@ def url_encode(url):
|
171 | 172 | """
|
172 | 173 | A function to perform minimal URL encoding
|
173 | 174 | """
|
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") |
178 | 176 |
|
179 | 177 | def get_strftime(self, time_format, location=None):
|
180 | 178 | """
|
@@ -206,13 +204,16 @@ def get_strftime(self, time_format, location=None):
|
206 | 204 | api_url += "&fmt=" + self.url_encode(time_format)
|
207 | 205 |
|
208 | 206 | try:
|
| 207 | + self.neo_status(STATUS_FETCHING) |
209 | 208 | response = self._wifi.requests.get(api_url, timeout=10)
|
| 209 | + self.neo_status(STATUS_DATA_RECEIVED) |
210 | 210 | if response.status_code != 200:
|
211 | 211 | print(response)
|
212 | 212 | error_message = (
|
213 | 213 | "Error connecting to Adafruit IO. The response was: "
|
214 | 214 | + response.text
|
215 | 215 | )
|
| 216 | + self.neo_status(STATUS_HTTP_ERROR) |
216 | 217 | raise RuntimeError(error_message)
|
217 | 218 | if self._debug:
|
218 | 219 | print("Time request: ", api_url)
|
@@ -285,7 +286,7 @@ def wget(self, url, filename, *, chunk_size=12000, headers=None):
|
285 | 286 | print("Content-Length: {}".format(int(headers["content-length"])))
|
286 | 287 | if "date" in headers:
|
287 | 288 | 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 |
289 | 290 | raise HttpError(
|
290 | 291 | "Code {}: {}".format(
|
291 | 292 | response.status_code, response.reason.decode("utf-8")
|
@@ -375,6 +376,7 @@ def connect(self, max_attempts=10):
|
375 | 376 | try:
|
376 | 377 | self._wifi.connect(secret_entry["ssid"], secret_entry["password"])
|
377 | 378 | self.requests = self._wifi.requests
|
| 379 | + self._wifi.neo_status(STATUS_CONNECTED) |
378 | 380 | break
|
379 | 381 | except (RuntimeError, ConnectionError) as error:
|
380 | 382 | if max_attempts is not None and attempt >= max_attempts:
|
|
0 commit comments