|
30 | 30 |
|
31 | 31 | import adafruit_logging as logging |
32 | 32 | from adafruit_jwt import JWT |
33 | | -import adafruit_ntp as NTP |
34 | 33 |
|
35 | 34 | __version__ = "0.0.0-auto.0" |
36 | 35 | __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_GC_IOT_Core.git" |
@@ -61,14 +60,15 @@ def __init__(self, mqtt_client): |
61 | 60 | ) |
62 | 61 | # Verify that the MiniMQTT client was setup correctly. |
63 | 62 | try: |
64 | | - self.user = self._client.user |
| 63 | + # I have no idea where _client.user comes from, but ._username exists when .user doesn't |
| 64 | + self.user = self._client._username |
65 | 65 | except Exception as err: |
66 | 66 | raise TypeError( |
67 | 67 | "Google Cloud Core IoT MQTT API requires a username." |
68 | 68 | ) from err |
69 | 69 | # Validate provided JWT before connecting |
70 | 70 | try: |
71 | | - JWT.validate(self._client.password) |
| 71 | + JWT.validate(self._client._password) # Again, .password isn't valid here.. |
72 | 72 | except Exception as err: |
73 | 73 | raise TypeError("Invalid JWT provided.") from err |
74 | 74 | # If client has KeepAlive =0 or if KeepAlive > 20min, |
@@ -296,10 +296,10 @@ class Cloud_Core: |
296 | 296 |
|
297 | 297 | """ |
298 | 298 |
|
299 | | - def __init__(self, esp, secrets, log=False): |
| 299 | + def __init__(self, esp=None, secrets=None, log=False): |
300 | 300 | self._esp = esp |
301 | 301 | # Validate Secrets |
302 | | - if hasattr(secrets, "keys"): |
| 302 | + if secrets and hasattr(secrets, "keys"): |
303 | 303 | self._secrets = secrets |
304 | 304 | else: |
305 | 305 | raise AttributeError( |
@@ -343,15 +343,24 @@ def generate_jwt(self, ttl=43200, algo="RS256"): |
343 | 343 | """ |
344 | 344 | if self.logger: |
345 | 345 | self.logger.debug("Generating JWT...") |
346 | | - ntp = NTP.NTP(self._esp) |
347 | | - ntp.set_time() |
| 346 | + |
| 347 | + if self._esp is not None: |
| 348 | + # Not all boards have ESP access easily (eg: featherS2). If we pass in a False or None in init, lets |
| 349 | + # assume that we've handled setting the RTC outside of here |
| 350 | + import adafruit_ntp as NTP |
| 351 | + ntp = NTP.NTP(self._esp) |
| 352 | + ntp.set_time() |
| 353 | + else: |
| 354 | + if self.logger: |
| 355 | + self.logger.info(f"No self._esp instance found, assuming RTC has been previously set") |
| 356 | + |
348 | 357 | claims = { |
349 | 358 | # The time that the token was issued at |
350 | 359 | "iat": time.time(), |
351 | 360 | # The time the token expires. |
352 | 361 | "exp": time.time() + ttl, |
353 | 362 | # The audience field should always be set to the GCP project id. |
354 | | - "aud": self._proj_id, |
| 363 | + "aud": self._proj_id |
355 | 364 | } |
356 | 365 | jwt = JWT.generate(claims, self._private_key, algo) |
357 | 366 | return jwt |
0 commit comments