Skip to content

Commit 26cbd08

Browse files
authored
Merge pull request #23 from tannewt/use_real_ntp
Stop using NTP library and get time directly
2 parents af2628c + b3f7273 commit 26cbd08

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

adafruit_gc_iot_core.py

+14-13
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import adafruit_logging as logging
3232
from adafruit_jwt import JWT
33+
import rtc
3334

3435
__version__ = "0.0.0-auto.0"
3536
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_GC_IOT_Core.git"
@@ -353,19 +354,19 @@ def generate_jwt(self, ttl=43200, algo="RS256"):
353354
self.logger.debug("Generating JWT...")
354355

355356
if self._esp is not None:
356-
# Not all boards have ESP access easily (eg: featherS2).
357-
# If we pass in a False or None in init, lets
358-
# assume that we've handled setting the RTC outside of here
359-
# pylint: disable=import-outside-toplevel
360-
import adafruit_ntp as NTP
361-
362-
ntp = NTP.NTP(self._esp)
363-
ntp.set_time()
364-
else:
365-
if self.logger:
366-
self.logger.info(
367-
"No self._esp instance found, assuming RTC has been previously set"
368-
)
357+
# get_time will raise ValueError if the time isn't available yet so loop until
358+
# it works.
359+
now_utc = None
360+
while now_utc is None:
361+
try:
362+
now_utc = time.localtime(self._esp.get_time()[0])
363+
except ValueError:
364+
pass
365+
rtc.RTC().datetime = now_utc
366+
elif self.logger:
367+
self.logger.info(
368+
"No self._esp instance found, assuming RTC has been previously set"
369+
)
369370

370371
claims = {
371372
# The time that the token was issued at

docs/conf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
# Uncomment the below if you use native CircuitPython modules such as
2525
# digitalio, micropython and busio. List the modules you use. Without it, the
2626
# autodoc module docs will fail to generate with a warning.
27-
autodoc_mock_imports = ["adafruit_logging", "adafruit_jwt", "adafruit_ntp", "rtc"]
27+
autodoc_mock_imports = ["adafruit_logging", "adafruit_jwt", "rtc"]
2828

2929
intersphinx_mapping = {
3030
"python": ("https://docs.python.org/3", None),

0 commit comments

Comments
 (0)