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

Subscribe is empty #24

Open
willisfeng opened this issue Nov 24, 2020 · 3 comments
Open

Subscribe is empty #24

willisfeng opened this issue Nov 24, 2020 · 3 comments

Comments

@willisfeng
Copy link

Publish
Connect 172..0.0.1
Publish home/garden/fountain qos=1 message=test message
[Teardown] Disconnect

Subscribe
Connect 172..0.0.1
${messages}= Subscribe home/garden/fountain qos=1 timeout=3 limit=1
[Teardown] Disconnect
------------------------------------###

20201124 14:52:41.539 : INFO : Connecting to 172..0.0.1 at port 1883
20201124 14:52:41.547 : INFO : Subscribing to topic: home/garden/fountain
20201124 14:52:44.550 : INFO : ${messages} = []

@willisfeng
Copy link
Author

def subscribe(self, topic, qos, timeout=1, limit=1):
""" Subscribe to a topic and return a list of message payloads received
within the specified time.

    `topic` topic to subscribe to

    `qos` quality of service for the subscription

    `timeout` duration of subscription. Specify 0 to enable background looping (async)

    `limit` the max number of payloads that will be returned. Specify 0
        for no limit

    Examples:

    Subscribe and get a list of all messages received within 5 seconds
    | ${messages}= | Subscribe | test/test | qos=1 | timeout=5 | limit=0 |

    Subscribe and get 1st message received within 60 seconds
    | @{messages}= | Subscribe | test/test | qos=1 | timeout=60 | limit=1 |
    | Length should be | ${messages} | 1 |

    """
    seconds = convert_time(timeout)
    self._messages[topic] = []
    limit = int(limit)
    self._subscribed = False

    logger.info('Subscribing to topic: %s' % topic)
    self._mqttc.on_subscribe = self._on_subscribe
    self._mqttc.subscribe(str(topic), int(qos))

    self._mqttc.on_message = self._on_message_list

    if seconds == 0:
        logger.info('Starting background loop')
        self._background_mqttc = self._mqttc
        self._background_mqttc.loop_start()
        return self._messages[topic]

    timer_start = time.time()
    while time.time() < timer_start + seconds:
        if limit == 0 or len(self._messages[topic]) < limit:
            self._mqttc.loop()
        else:
            # workaround for client to ack the publish. Otherwise,
            # it seems that if client disconnects quickly, broker
            # will not get the ack and publish the message again on
            # next connect.
            time.sleep(1)
            break
    return self._messages[topic]

@willisfeng
Copy link
Author

@janvanoverwalle @randomsync @vogoltsov
Brokers run in containers
docker run -d -p 1883:1883 eclipse-mosquitto

@ralienpp
Copy link

Perhaps the problem is here: Connect 172..0.0.1:

  • most likely you mean 127.0.0.1
  • and note that there must be a single dot between each element of the IP address

Did this do the trick?

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

No branches or pull requests

2 participants