Coder Social home page Coder Social logo

Comments (2)

Lenka42 avatar Lenka42 commented on July 23, 2024 1

@jeffreykira For now there are two possibilities:

  1. you can match received message topic with regexp, like this:
pattern = r'LIGHT/(?P<device_id>\d+)/TURNON'
match = re.fullmatch(pattern, topic)
if match is not None:
    device_id = match.group('device_id')
    # do the turn on logic here
  1. or if you use broker supporting MQTT 5.0 version, you can use subscription identifiers for this.
    TOPIC_LIST = [
        {'topic': 'TV/TURNON'., 'qos': 1, 'method': turn_on_tv, 'subscription_identifier': 1},
        {'topic': 'LIGHT/+/TURNON', 'qos': 1, 'method': turn_on_light, 'subscription_identifier': 2},
    ]

    for t in TOPIC_LIST:
        # pass it as kwarg to subscribe method
        client.subscribe(t['topic'], t['qos'], subscription_identifier=t['subscription_identifier'])

on_message handler will receive messgae with property subscription_identifier, which is list:

def on_message(client, topic, payload, qos, properties):
    if 2 in properties['subscription_identifier']:
        # do the turnon logic here

from gmqtt.

jeffreykira avatar jeffreykira commented on July 23, 2024 1

@Lenka42 Thank you very much for your reply, I will use the second one.

from gmqtt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.