Skip to content

Remove forced dep on ntp #20

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 3 commits into from
Feb 19, 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
25 changes: 17 additions & 8 deletions adafruit_gc_iot_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

import adafruit_logging as logging
from adafruit_jwt import JWT
import adafruit_ntp as NTP

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_GC_IOT_Core.git"
Expand Down Expand Up @@ -61,14 +60,15 @@ def __init__(self, mqtt_client):
)
# Verify that the MiniMQTT client was setup correctly.
try:
self.user = self._client.user
# I have no idea where _client.user comes from, but ._username exists when .user doesn't
self.user = self._client._username
except Exception as err:
raise TypeError(
"Google Cloud Core IoT MQTT API requires a username."
) from err
# Validate provided JWT before connecting
try:
JWT.validate(self._client.password)
JWT.validate(self._client._password) # Again, .password isn't valid here..
except Exception as err:
raise TypeError("Invalid JWT provided.") from err
# If client has KeepAlive =0 or if KeepAlive > 20min,
Expand Down Expand Up @@ -296,10 +296,10 @@ class Cloud_Core:

"""

def __init__(self, esp, secrets, log=False):
def __init__(self, esp=None, secrets=None, log=False):
self._esp = esp
# Validate Secrets
if hasattr(secrets, "keys"):
if secrets and hasattr(secrets, "keys"):
self._secrets = secrets
else:
raise AttributeError(
Expand Down Expand Up @@ -343,15 +343,24 @@ def generate_jwt(self, ttl=43200, algo="RS256"):
"""
if self.logger:
self.logger.debug("Generating JWT...")
ntp = NTP.NTP(self._esp)
ntp.set_time()

if self._esp is not None:
# Not all boards have ESP access easily (eg: featherS2). If we pass in a False or None in init, lets
# assume that we've handled setting the RTC outside of here
import adafruit_ntp as NTP
ntp = NTP.NTP(self._esp)
ntp.set_time()
else:
if self.logger:
self.logger.info(f"No self._esp instance found, assuming RTC has been previously set")

claims = {
# The time that the token was issued at
"iat": time.time(),
# The time the token expires.
"exp": time.time() + ttl,
# The audience field should always be set to the GCP project id.
"aud": self._proj_id,
"aud": self._proj_id
}
jwt = JWT.generate(claims, self._private_key, algo)
return jwt