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

allow to set user data in the init method #136

Merged
merged 1 commit into from
Jan 11, 2023
Merged

Conversation

vladak
Copy link
Contributor

@vladak vladak commented Dec 23, 2022

This change allows to set the opaque user data in the init method. Works like this:

"""
MQTT utility functions
"""
import ssl

import adafruit_logging as logging
import adafruit_minimqtt.adafruit_minimqtt as MQTT

# pylint: disable=unused-argument, redefined-outer-name, invalid-name
def connect(mqtt_client, userdata, flags, rc):
    """
    This function will be called when the mqtt_client is connected
    successfully to the broker.
    """
    logger = userdata

    logger.info("Connected to MQTT Broker!")
    logger.debug(f"Flags: {flags}\n RC: {rc}")


# pylint: disable=unused-argument, invalid-name
def disconnect(mqtt_client, userdata, rc):
    """
    This method is called when the mqtt_client disconnects from the broker.
    """
    logger = userdata

    logger.info("Disconnected from MQTT Broker!")


def publish(mqtt_client, userdata, topic, pid):
    """
    This method is called when the mqtt_client publishes data to a feed.
    """
    logger = userdata

    logger.info(f"Published to {topic} with PID {pid}")


def mqtt_client_setup(pool, broker, port):
    """
    Set up a MiniMQTT Client
    """

    logger = logging.getLogger("mqtt")
    logger.setLevel(logging.DEBUG)

    mqtt_client = MQTT.MQTT(
        broker=broker,
        port=port,
        socket_pool=pool,
        ssl_context=ssl.create_default_context(),
        user_data=logger,
    )

    mqtt_client.on_connect = connect
    mqtt_client.on_disconnect = disconnect
    mqtt_client.on_publish = publish

    return mqtt_client


def main():
    import socket
    mqtt_client = mqtt_client_setup(broker="localhost", port=1883, pool=socket)
    mqtt_client.connect()
    mqtt_client.publish("foo/bar", "msg")


if __name__ == "__main__":
    main()

Tested with CPython against custom MQTT broker implementation, produces the following output:

601394.401: INFO - Connected to MQTT Broker!
601394.401: DEBUG - Flags: 0
 RC: 0
601394.401: INFO - Published to foo/bar with PID 0

Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

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

Makes sense, and looks good to me.

It's interesting this was partially implemented already, but seemingly no way to actually set the data, and none of the examples made use of it.

I tested it successfully on a Feather ESP32-S2 TFT with a modified simpletest that sets an object as userdata and accesses it from each of the callback functions.

@FoamyGuy FoamyGuy merged commit 10d7045 into adafruit:main Jan 11, 2023
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Jan 11, 2023
Updating https://github.com/adafruit/Adafruit_CircuitPython_DisplayIO_SH1106 to 1.2.10 from 1.2.9:
  > Merge pull request adafruit/Adafruit_CircuitPython_DisplayIO_SH1106#11 from greatest-gatsby/main
  > Add .venv to .gitignore
  > Update .pylintrc for v2.15.5
  > Fix release CI files
  > Update pylint to 2.15.5
  > Updated pylint version to 2.13.0
  > Switching to composite actions

Updating https://github.com/adafruit/Adafruit_CircuitPython_Display_Text to 2.23.0 from 2.22.13:
  > Merge pull request adafruit/Adafruit_CircuitPython_Display_Text#180 from FoamyGuy/fix_left_glpyh_logic

Updating https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT to 7.1.0 from 7.0.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#136 from vladak/user_data
  > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#139 from vladak/tls_port
  > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#141 from vladak/enable_logger_nits

Updating https://github.com/adafruit/Adafruit_CircuitPython_OneWire to 2.0.4 from 2.0.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_OneWire#28 from tekktrik/dev/fix-annotations
  > Add .venv to .gitignore
  > Update .pylintrc for v2.15.5
  > Fix release CI files
  > Update pylint to 2.15.5
  > Updated pylint version to 2.13.0
  > Switching to composite actions

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Updated download stats for the libraries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants