Skip to content

Commit 358bf78

Browse files
committed
Travis fixes
1 parent 23f80ff commit 358bf78

File tree

1 file changed

+48
-50
lines changed

1 file changed

+48
-50
lines changed

PyPortal_Smart_Switch/code.py

Lines changed: 48 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,49 @@
8585
time_font = bitmap_font.load_font("/fonts/RobotoMono-72.bdf")
8686
time_font.load_glyphs(b'0123456789:')
8787

88-
#spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
89-
#_esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
88+
def get_local_timestamp(self, location=None):
89+
# pylint: disable=line-too-long
90+
"""Fetch and "set" the local time of this microcontroller to the local time at the location, using an internet time API.
91+
:param str location: Your city and country, e.g. ``"New York, US"``.
92+
"""
93+
# pylint: enable=line-too-long
94+
api_url = None
95+
try:
96+
aio_username = secrets['aio_username']
97+
aio_key = secrets['aio_key']
98+
except KeyError:
99+
raise KeyError("\n\nOur time service requires a login/password to rate-limit. Please register for a free adafruit.io account and place the user/key in your secrets file under 'aio_username' and 'aio_key'")# pylint: disable=line-too-long
100+
101+
location = secrets.get('timezone', location)
102+
if location:
103+
print("Getting time for timezone", location)
104+
api_url = (TIME_SERVICE + "&tz=%s") % (aio_username, aio_key, location)
105+
else: # we'll try to figure it out from the IP address
106+
print("Getting time from IP address")
107+
api_url = TIME_SERVICE % (aio_username, aio_key)
108+
api_url += TIME_SERVICE_TIMESTAMP
109+
try:
110+
print("api_url:",api_url)
111+
response = requests.get(api_url)
112+
times = response.text.split(' ')
113+
seconds = int(times[0])
114+
tzoffset = times[1]
115+
tzhours = int(tzoffset[0:3])
116+
tzminutes = int(tzoffset[3:5])
117+
tzseconds = tzhours * 60 * 60
118+
if tzseconds < 0:
119+
tzseconds -= tzminutes * 60
120+
else:
121+
tzseconds += tzminutes * 60
122+
print(seconds + tzseconds, tzoffset, tzhours, tzminutes)
123+
except KeyError:
124+
raise KeyError("Was unable to lookup the time, try setting secrets['timezone'] according to http://worldtimeapi.org/timezones") # pylint: disable=line-too-long
125+
126+
# now clean up
127+
response.close()
128+
response = None
129+
gc.collect()
130+
return int(seconds + tzseconds)
90131

91132
def create_text_areas(configs):
92133
"""Given a list of area specifications, create and return text areas."""
@@ -135,8 +176,8 @@ def status(self):
135176
# See https://apidock.com/ruby/DateTime/strftime for full options
136177
TIME_SERVICE_TIMESTAMP = '&fmt=%25s+%25z'
137178

138-
class Clock(object, my_pyportal):
139-
def __init__(self):
179+
class Clock(object):
180+
def __init__(self, my_pyportal):
140181
self.low_light = False
141182
self.update_time = None
142183
self.snapshot_time = None
@@ -165,7 +206,7 @@ def tick(self, now):
165206
# Update the time
166207
print("update the time")
167208
self.update_time = int(now)
168-
self.snapshot_time = self.get_local_timestamp(secrets['timezone'])
209+
self.snapshot_time = get_local_timestamp(secrets['timezone'])
169210
self.current_time = time.localtime(self.snapshot_time)
170211
else:
171212
self.current_time = time.localtime(int(now) - self.update_time + self.snapshot_time)
@@ -180,54 +221,11 @@ def tick(self, now):
180221
if self.current_time.tm_hour >= 12:
181222
ampm_string = "PM"
182223
self.text_areas[1].text = ampm_string
183-
self.text_areas[2].text = months[int(self.current_time.tm_mon - 1)] + " " + str(self.current_time.tm_mday)
224+
self.text_areas[2].text = (months[int(self.current_time.tm_mon - 1)] +
225+
" " + str(self.current_time.tm_mday))
184226
board.DISPLAY.refresh_soon()
185227
board.DISPLAY.wait_for_frame()
186228

187-
def get_local_timestamp(self, location=None):
188-
# pylint: disable=line-too-long
189-
"""Fetch and "set" the local time of this microcontroller to the local time at the location, using an internet time API.
190-
:param str location: Your city and country, e.g. ``"New York, US"``.
191-
"""
192-
# pylint: enable=line-too-long
193-
api_url = None
194-
try:
195-
aio_username = secrets['aio_username']
196-
aio_key = secrets['aio_key']
197-
except KeyError:
198-
raise KeyError("\n\nOur time service requires a login/password to rate-limit. Please register for a free adafruit.io account and place the user/key in your secrets file under 'aio_username' and 'aio_key'")# pylint: disable=line-too-long
199-
200-
location = secrets.get('timezone', location)
201-
if location:
202-
print("Getting time for timezone", location)
203-
api_url = (TIME_SERVICE + "&tz=%s") % (aio_username, aio_key, location)
204-
else: # we'll try to figure it out from the IP address
205-
print("Getting time from IP address")
206-
api_url = TIME_SERVICE % (aio_username, aio_key)
207-
api_url += TIME_SERVICE_TIMESTAMP
208-
try:
209-
print("api_url:",api_url)
210-
response = requests.get(api_url)
211-
times = response.text.split(' ')
212-
seconds = int(times[0])
213-
tzoffset = times[1]
214-
tzhours = int(tzoffset[0:3])
215-
tzminutes = int(tzoffset[3:5])
216-
tzseconds = tzhours * 60 * 60
217-
if tzseconds < 0:
218-
tzseconds -= tzminutes * 60
219-
else:
220-
tzseconds += tzminutes * 60
221-
print(seconds + tzseconds, tzoffset, tzhours, tzminutes)
222-
except KeyError:
223-
raise KeyError("Was unable to lookup the time, try setting secrets['timezone'] according to http://worldtimeapi.org/timezones") # pylint: disable=line-too-long
224-
225-
# now clean up
226-
response.close()
227-
response = None
228-
gc.collect()
229-
return int(seconds + tzseconds)
230-
231229
# Define callback methods which are called when events occur
232230
# pylint: disable=unused-argument, redefined-outer-name
233231
def connected(client, userdata, flags, rc):

0 commit comments

Comments
 (0)