From c1ed82f55d260ee00cdbfa7aaaa8bc3abf605d9f Mon Sep 17 00:00:00 2001 From: Lewis Chambers Date: Fri, 14 Jun 2024 16:24:59 +0100 Subject: [PATCH] Updated MQTT client init --- pyproject.toml | 2 ++ src/iotswarm/messaging/aws.py | 40 ++++++++++++++++++++--------------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 6662040..3a23095 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,9 +8,11 @@ dependencies = [ "boto3", "autosemver", "config", + "click", "docutils<0.17", "awscli", "awscrt", + "awsiotsdk", "oracledb", "backoff", ] diff --git a/src/iotswarm/messaging/aws.py b/src/iotswarm/messaging/aws.py index 8205964..d0f114e 100644 --- a/src/iotswarm/messaging/aws.py +++ b/src/iotswarm/messaging/aws.py @@ -2,6 +2,7 @@ import awscrt from awscrt import mqtt +from awsiot import mqtt_connection_builder import awscrt.io import json from awscrt.exceptions import AwsCrtError @@ -95,26 +96,18 @@ def __init__( socket_options.keep_alive_interval_secs = 0 socket_options.keep_alive_max_probes = 0 - client_bootstrap = awscrt.io.ClientBootstrap.get_or_create_static_default() - - tls_ctx = awscrt.io.ClientTlsContext(tls_ctx_options) - mqtt_client = awscrt.mqtt.Client(client_bootstrap, tls_ctx) - - self.connection = awscrt.mqtt.Connection( - client=mqtt_client, + self.connection = mqtt_connection_builder.mtls_from_path( + endpoint=endpoint, + port=port, + cert_filepath=cert_path, + pri_key_filepath=key_path, + ca_filepath=ca_cert_path, on_connection_interrupted=self._on_connection_interrupted, on_connection_resumed=self._on_connection_resumed, client_id=client_id, - host_name=endpoint, - port=port, - clean_session=clean_session, - reconnect_min_timeout_secs=5, - reconnect_max_timeout_secs=60, - keep_alive_secs=keep_alive_secs, - ping_timeout_ms=3000, - protocol_operation_timeout_ms=0, - socket_options=socket_options, - use_websockets=False, + proxy_options=None, + clean_session=False, + keep_alive_secs=30, on_connection_success=self._on_connection_success, on_connection_failure=self._on_connection_failure, on_connection_closed=self._on_connection_closed, @@ -215,3 +208,16 @@ def send_message( use_logger.info(f'Sent {sys.getsizeof(payload)} bytes to "{topic}"') # self._disconnect() + + +if __name__ == "__main__": + + conn = IotCoreMQTTConnection( + endpoint="a10mem0twl4qxt-ats.iot.eu-west-2.amazonaws.com", + cert_path="C:/Users/lewcha/OneDrive - UKCEH/Documents/FDRI/projects/iot-device-simulator/src/iotswarm/__assets__/.certs/cosmos_soilmet-certificate.pem.crt", + key_path="C:/Users/lewcha/OneDrive - UKCEH/Documents/FDRI/projects/iot-device-simulator/src/iotswarm/__assets__/.certs/cosmos_soilmet-private.pem.key", + ca_cert_path="C:/Users/lewcha/OneDrive - UKCEH/Documents/FDRI/projects/iot-device-simulator/src/iotswarm/__assets__/.certs/AmazonRootCA1.pem", + client_id="cosmos_soilmet", + ) + + conn.send_message("hello there", "test/topic")