randomsync / robotframework-mqttlibrary

MQTT Keyword Library for Robot Framework
Apache License 2.0
25 stars 30 forks source link

Subscribe is empty #24

Open willisfeng opened 3 years ago

willisfeng commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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

ralienpp commented 3 years ago

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

Did this do the trick?