From 705809caf7d274ffc1b9a9397387fd06a9b6fdbe Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 12 Mar 2019 11:46:34 -0400 Subject: [PATCH 1/4] old time, change for clock --- adafruit_io/adafruit_io.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/adafruit_io/adafruit_io.py b/adafruit_io/adafruit_io.py index 1fd3db4..39e6990 100755 --- a/adafruit_io/adafruit_io.py +++ b/adafruit_io/adafruit_io.py @@ -39,6 +39,7 @@ https://github.com/adafruit/Adafruit_CircuitPython_ESP32SPI https://github.com/adafruit/Adafruit_CircuitPython_ESP_ATcontrol """ +from time import struct_time from adafruit_io.adafruit_io_errors import AdafruitIO_RequestError, AdafruitIO_ThrottleError __version__ = "0.0.0-auto.0" @@ -268,10 +269,19 @@ def receive_random_data(self, generator_id): path = self._compose_path("integrations/words/{0}".format(generator_id)) return self._get(path) - def receive_time(self, time_type): - """ - Returns the current time from the Adafruit IO Server - :param string time_type: Type of time to be returned: millis, seconds, or ISO-8601 - """ - path = 'https://io.adafruit.com/api/v2/time/{0}'.format(time_type) - return self._get(path, return_text=True) + def receive_time(self): + """ + Returns a struct_time from the Adafruit IO Server based on the device's IP address. + https://circuitpython.readthedocs.io/en/latest/shared-bindings/time/__init__.html#time.struct_time + """ + path = self._compose_path('/integrations/time/clock.json{0}'.format(strftime_format)) + time_response = self._get(path, return_text=True) + times = time_response.split(' ') + the_date = times[0] + the_time = times[1] + year_day = int(times[2]) + week_day = int(times[3]) + year, month, mday = [int(x) for x in the_date.split('-')] + the_time = the_time.split('.')[0] + hours, minutes, seconds = [int(x) for x in the_time.split(':')] + return struct_time((year, month, mday, hours, minutes, seconds, week_day, year_day, None)) From b835fafc8f6a4afa05791cd17f2a828dabcb2530 Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 12 Mar 2019 12:10:36 -0400 Subject: [PATCH 2/4] adding the clock.json response instead, minimize code on the device --- adafruit_io/adafruit_io.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/adafruit_io/adafruit_io.py b/adafruit_io/adafruit_io.py index 39e6990..d774d76 100755 --- a/adafruit_io/adafruit_io.py +++ b/adafruit_io/adafruit_io.py @@ -274,14 +274,9 @@ def receive_time(self): Returns a struct_time from the Adafruit IO Server based on the device's IP address. https://circuitpython.readthedocs.io/en/latest/shared-bindings/time/__init__.html#time.struct_time """ - path = self._compose_path('/integrations/time/clock.json{0}'.format(strftime_format)) - time_response = self._get(path, return_text=True) - times = time_response.split(' ') - the_date = times[0] - the_time = times[1] - year_day = int(times[2]) - week_day = int(times[3]) - year, month, mday = [int(x) for x in the_date.split('-')] - the_time = the_time.split('.')[0] - hours, minutes, seconds = [int(x) for x in the_time.split(':')] - return struct_time((year, month, mday, hours, minutes, seconds, week_day, year_day, None)) + path = self._compose_path('integrations/time/clock.json') + time = self._get(path) + print(time) + print(time['wday']) + return struct_time((time['year'], time['mon'], time['mday'], time['hour'], + time['min'], time['sec'], time['wday'], time['yday'], time['isdst'])) From a906b59771805a19e93b692cfb85abd742ab1d16 Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 12 Mar 2019 12:26:06 -0400 Subject: [PATCH 3/4] add time struct endpoint instead of clock --- adafruit_io/adafruit_io.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/adafruit_io/adafruit_io.py b/adafruit_io/adafruit_io.py index d774d76..003d4c5 100755 --- a/adafruit_io/adafruit_io.py +++ b/adafruit_io/adafruit_io.py @@ -274,9 +274,7 @@ def receive_time(self): Returns a struct_time from the Adafruit IO Server based on the device's IP address. https://circuitpython.readthedocs.io/en/latest/shared-bindings/time/__init__.html#time.struct_time """ - path = self._compose_path('integrations/time/clock.json') + path = self._compose_path('integrations/time/struct.json') time = self._get(path) - print(time) - print(time['wday']) return struct_time((time['year'], time['mon'], time['mday'], time['hour'], time['min'], time['sec'], time['wday'], time['yday'], time['isdst'])) From 88cb0117973d0221d883d12fef5e160506db7c60 Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 12 Mar 2019 12:33:40 -0400 Subject: [PATCH 4/4] remove text kwarg from GET since we're only expecting JSON responses now --- adafruit_io/adafruit_io.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/adafruit_io/adafruit_io.py b/adafruit_io/adafruit_io.py index 003d4c5..0b77e58 100755 --- a/adafruit_io/adafruit_io.py +++ b/adafruit_io/adafruit_io.py @@ -120,18 +120,15 @@ def _post(self, path, payload): self._handle_error(response) return response.json() - def _get(self, path, return_text=False): + def _get(self, path): """ GET data from Adafruit IO :param str path: Formatted Adafruit IO URL from _compose_path - :param bool return_text: Returns text instead of json """ response = self.wifi.get( path, headers=self._create_headers(self._aio_headers[1])) self._handle_error(response) - if return_text: - return response.text return response.json() def _delete(self, path):