Skip to content

Commit c1ed82f

Browse files
Updated MQTT client init
1 parent faba278 commit c1ed82f

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ dependencies = [
88
"boto3",
99
"autosemver",
1010
"config",
11+
"click",
1112
"docutils<0.17",
1213
"awscli",
1314
"awscrt",
15+
"awsiotsdk",
1416
"oracledb",
1517
"backoff",
1618
]

src/iotswarm/messaging/aws.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import awscrt
44
from awscrt import mqtt
5+
from awsiot import mqtt_connection_builder
56
import awscrt.io
67
import json
78
from awscrt.exceptions import AwsCrtError
@@ -95,26 +96,18 @@ def __init__(
9596
socket_options.keep_alive_interval_secs = 0
9697
socket_options.keep_alive_max_probes = 0
9798

98-
client_bootstrap = awscrt.io.ClientBootstrap.get_or_create_static_default()
99-
100-
tls_ctx = awscrt.io.ClientTlsContext(tls_ctx_options)
101-
mqtt_client = awscrt.mqtt.Client(client_bootstrap, tls_ctx)
102-
103-
self.connection = awscrt.mqtt.Connection(
104-
client=mqtt_client,
99+
self.connection = mqtt_connection_builder.mtls_from_path(
100+
endpoint=endpoint,
101+
port=port,
102+
cert_filepath=cert_path,
103+
pri_key_filepath=key_path,
104+
ca_filepath=ca_cert_path,
105105
on_connection_interrupted=self._on_connection_interrupted,
106106
on_connection_resumed=self._on_connection_resumed,
107107
client_id=client_id,
108-
host_name=endpoint,
109-
port=port,
110-
clean_session=clean_session,
111-
reconnect_min_timeout_secs=5,
112-
reconnect_max_timeout_secs=60,
113-
keep_alive_secs=keep_alive_secs,
114-
ping_timeout_ms=3000,
115-
protocol_operation_timeout_ms=0,
116-
socket_options=socket_options,
117-
use_websockets=False,
108+
proxy_options=None,
109+
clean_session=False,
110+
keep_alive_secs=30,
118111
on_connection_success=self._on_connection_success,
119112
on_connection_failure=self._on_connection_failure,
120113
on_connection_closed=self._on_connection_closed,
@@ -215,3 +208,16 @@ def send_message(
215208
use_logger.info(f'Sent {sys.getsizeof(payload)} bytes to "{topic}"')
216209

217210
# self._disconnect()
211+
212+
213+
if __name__ == "__main__":
214+
215+
conn = IotCoreMQTTConnection(
216+
endpoint="a10mem0twl4qxt-ats.iot.eu-west-2.amazonaws.com",
217+
cert_path="C:/Users/lewcha/OneDrive - UKCEH/Documents/FDRI/projects/iot-device-simulator/src/iotswarm/__assets__/.certs/cosmos_soilmet-certificate.pem.crt",
218+
key_path="C:/Users/lewcha/OneDrive - UKCEH/Documents/FDRI/projects/iot-device-simulator/src/iotswarm/__assets__/.certs/cosmos_soilmet-private.pem.key",
219+
ca_cert_path="C:/Users/lewcha/OneDrive - UKCEH/Documents/FDRI/projects/iot-device-simulator/src/iotswarm/__assets__/.certs/AmazonRootCA1.pem",
220+
client_id="cosmos_soilmet",
221+
)
222+
223+
conn.send_message("hello there", "test/topic")

0 commit comments

Comments
 (0)