Skip to content

Commit

Permalink
Updated MQTT client init
Browse files Browse the repository at this point in the history
  • Loading branch information
lewis-chambers committed Jun 14, 2024
1 parent faba278 commit c1ed82f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ dependencies = [
"boto3",
"autosemver",
"config",
"click",
"docutils<0.17",
"awscli",
"awscrt",
"awsiotsdk",
"oracledb",
"backoff",
]
Expand Down
40 changes: 23 additions & 17 deletions src/iotswarm/messaging/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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")

0 comments on commit c1ed82f

Please sign in to comment.