Skip to content

ucloud: Set default server/port based on auth type. #17

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 1 commit into from
Sep 27, 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
31 changes: 25 additions & 6 deletions arduino_iot_cloud/ucloud.py
Original file line number Diff line number Diff line change
@@ -42,6 +42,12 @@
class InvalidStateError(Exception):
pass

# Server/port for basic auth.
_DEFAULT_UP_SERVER = ("mqtts-up.iot.oniudra.cc", 8884)

# Default server/port for key/cert auth.
_DEFAULT_SA_SERVER = ("mqtts-sa.iot.oniudra.cc", 8883)


def timestamp():
return int(time.time())
@@ -171,8 +177,8 @@ def __init__(
username=None,
password=None,
ssl_params={},
server="mqtts-sa.iot.oniudra.cc",
port=8883,
server=None,
port=None,
keepalive=10
):
self.tasks = {}
@@ -183,15 +189,28 @@ def __init__(
self.last_ping = timestamp()
self.device_topic = b"/a/d/" + device_id + b"/e/i"
self.senmlpack = SenmlPack("", self.senml_generic_callback)

# MicroPython does not support secure elements yet, and key/cert
# must be loaded from DER files and passed as binary blobs.
if "keyfile" in ssl_params and "der" in ssl_params["keyfile"]:
# MicroPython does not support secure elements yet, and key/cert
# must be loaded from DER files and passed as binary blobs.
with open(ssl_params.pop("keyfile"), "rb") as f:
ssl_params["key"] = f.read()
with open(ssl_params.pop("certfile"), "rb") as f:
ssl_params["cert"] = f.read()
self.mqtt = MQTTClient(device_id, server, port, ssl_params, username, password, keepalive, self.mqtt_callback)
# Note: the following internal objects are initialized by the cloud.

# If no server/port were passed in args, set the default server/port
# based on authentication type.
if server is None:
server = _DEFAULT_SA_SERVER[0] if password is None else _DEFAULT_UP_SERVER[0]
if port is None:
port = _DEFAULT_SA_SERVER[1] if password is None else _DEFAULT_UP_SERVER[1]

# Create MQTT client.
self.mqtt = MQTTClient(
device_id, server, port, ssl_params, username, password, keepalive, self.mqtt_callback
)

# Add internal objects initialized by the cloud.
for name in ["thing_id", "tz_offset", "tz_dst_until"]:
self.register(name, value=None)

5 changes: 1 addition & 4 deletions examples/micropython.py
Original file line number Diff line number Diff line change
@@ -36,10 +36,7 @@ def on_clight_changed(client, clight):
async def main():
# Create a client to connect to the Arduino IoT cloud. For MicroPython, the key and cert files must be stored
# in DER format on the filesystem. Alternatively, a username and a password can be used for authentication:
# client = AIOTClient(
# device_id=b"DEVICE_ID",
# username=b"DEVICE_ID", password=b"SECRET_KEY", server="mqtts-up.iot.oniudra.cc", port=8884
# )
# client = AIOTClient(device_id=b"DEVICE_ID", username=b"DEVICE_ID", password=b"SECRET_KEY")
client = AIOTClient(device_id=DEVICE_ID, ssl_params={"keyfile": KEY_PATH, "certfile": CERT_PATH})

# Register cloud objects. Note these objects must be created first in the dashboard.