Skip to content
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

Update interface to match MQTT client #27

Merged
merged 6 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
13 changes: 4 additions & 9 deletions adafruit_aws_iot.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,13 @@ def _on_unsubscribe_mqtt(
self.on_unsubscribe(self, user_data, topic, pid)

# MiniMQTT Network Control Flow
def loop(self) -> None:
def loop(self, timeout: float = 0) -> None:
"""Starts a synchronous message loop which maintains connection with AWS IoT.
Must be called within the keep_alive timeout specified to init.
This method does not handle network connection/disconnection.

:param float timeout: client return after this timeout, in seconds.

Example of "pumping" an AWS IoT message loop:
..code-block::python

Expand All @@ -235,14 +237,7 @@ def loop(self) -> None:

"""
if self.connected_to_aws:
self.client.loop()

def loop_forever(self) -> None:
"""Begins a blocking, asynchronous message loop.
This method handles network connection/disconnection.
"""
if self.connected_to_aws:
self.client.loop_forever()
self.client.loop(timeout)

@staticmethod
def validate_topic(topic: str) -> None:
Expand Down
2 changes: 1 addition & 1 deletion examples/aws_iot_native_networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,6 @@ def message(client, topic, msg):
# NOTE: NO code below this loop will execute
# NOTE: Network reconnection is NOT handled within this loop
while True:
aws_iot.loop()
aws_iot.loop(10)
brentru marked this conversation as resolved.
Show resolved Hide resolved

time.sleep(1)
5 changes: 3 additions & 2 deletions examples/aws_iot_shadows.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ def message(client, topic, msg):
client = MQTT.MQTT(
broker=secrets["broker"],
client_id=secrets["client_id"],
is_ssl=True,
socket_pool=pool,
ssl_context=ssl_context,
)
Comment on lines 141 to 147
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jersu11 can you make sure all 3 look the same way? The native one also as port in it (which really isn't needed). The closer these are, the easier for users just to swap it out for what they need

client = MQTT.MQTT(
    broker=secrets["broker"],
    client_id=secrets["client_id"],
    is_ssl=True,
    socket_pool=pool,
    ssl_context=ssl_context,
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call out. I've updated the native example to match the MQTT client settings of the other examples.

Expand All @@ -162,14 +163,14 @@ def message(client, topic, msg):
# Pump the message loop forever, all events
# are handled in their callback handlers
# while True:
# aws_iot.loop()
# aws_iot.loop(10)

# Start a blocking message loop...
# NOTE: NO code below this loop will execute
# NOTE: Network reconnection is handled within this loop
while True:
try:
aws_iot.loop()
aws_iot.loop(10)
except (ValueError, RuntimeError) as e:
print("Failed to get data, retrying\n", e)
wifi.reset()
Expand Down
5 changes: 3 additions & 2 deletions examples/aws_iot_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ def message(client, topic, msg):
client = MQTT.MQTT(
broker=secrets["broker"],
client_id=secrets["client_id"],
is_ssl=True,
socket_pool=pool,
ssl_context=ssl_context,
)
Expand All @@ -159,14 +160,14 @@ def message(client, topic, msg):
# Pump the message loop forever, all events
# are handled in their callback handlers
# while True:
# aws_iot.loop()
# aws_iot.loop(10)

# Start a blocking message loop...
# NOTE: NO code below this loop will execute
# NOTE: Network reconnection is handled within this loop
while True:
try:
aws_iot.loop()
aws_iot.loop(10)
except (ValueError, RuntimeError) as e:
print("Failed to get data, retrying\n", e)
wifi.reset()
Expand Down
Loading