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

feat: add support for binary messages #97

Merged
merged 1 commit into from
Jan 11, 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
8 changes: 6 additions & 2 deletions adafruit_minimqtt/adafruit_minimqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class MQTT:
:param int keep_alive: KeepAlive interval between the broker and the MiniMQTT client.
:param socket socket_pool: A pool of socket resources available for the given radio.
:param ssl_context: SSL context for long-lived SSL connections.
:param bool use_binary_mode: Messages are passed as bytearray instead of string to callbacks.

"""

Expand All @@ -141,12 +142,14 @@ def __init__(
keep_alive=60,
socket_pool=None,
ssl_context=None,
use_binary_mode=False,
):

self._socket_pool = socket_pool
self._ssl_context = ssl_context
self._sock = None
self._backwards_compatible_sock = False
self._use_binary_mode = use_binary_mode

self.keep_alive = keep_alive
self._user_data = None
Expand Down Expand Up @@ -839,8 +842,9 @@ def _wait_for_msg(self, timeout=0.1):
pid = pid[0] << 0x08 | pid[1]
sz -= 0x02
# read message contents
msg = self._sock_exact_recv(sz)
self._handle_on_message(self, topic, str(msg, "utf-8"))
raw_msg = self._sock_exact_recv(sz)
brentru marked this conversation as resolved.
Show resolved Hide resolved
msg = raw_msg if self._use_binary_mode else str(raw_msg, "utf-8")
self._handle_on_message(self, topic, msg)
if res[0] & 0x06 == 0x02:
pkt = bytearray(b"\x40\x02\0\0")
struct.pack_into("!H", pkt, 2, pid)
Expand Down