Skip to content

Update MQTTKeywords.py suites to repair a messages abandoning deficiency #23

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 18 additions & 4 deletions src/MQTTLibrary/MQTTKeywords.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def __init__(self, loop_timeout=LOOP_TIMEOUT):
self._username = None
self._password = None
#self._mqttc = mqtt.Client()

self.msg_tmp = {}

def set_username_and_password(self, username, password=None):
self._username = username
self._password = password
Expand Down Expand Up @@ -149,7 +150,10 @@ def subscribe(self, topic, qos, timeout=1, limit=1):

"""
seconds = convert_time(timeout)
self._messages[topic] = []
try:
self._messages[topic] = [i for i in self._messages[topic] if i not in self.msg_tmp[topic]]
except KeyError:
self._messages[topic] = []
limit = int(limit)
self._subscribed = False

Expand All @@ -163,6 +167,7 @@ def subscribe(self, topic, qos, timeout=1, limit=1):
logger.info('Starting background loop')
self._background_mqttc = self._mqttc
self._background_mqttc.loop_start()
self.msg_tmp = self._messages
return self._messages[topic]

timer_start = time.time()
Expand All @@ -176,6 +181,7 @@ def subscribe(self, topic, qos, timeout=1, limit=1):
# next connect.
time.sleep(1)
break
self.msg_tmp = self._messages
return self._messages[topic]

def listen(self, topic, timeout=1, limit=1):
Expand Down Expand Up @@ -215,7 +221,11 @@ def listen(self, topic, timeout=1, limit=1):
# If enough messages have already been gathered, return them
if limit != 0 and len(self._messages[topic]) >= limit:
messages = self._messages[topic][:] # Copy the list's contents
self._messages[topic] = []
try:
self._messages[topic] = [i for i in self._messages[topic] if i not in self.msg_tmp[topic]]
except KeyError:
self._messages[topic] = []
self.msg_tmp = self._messages
return messages[-limit:]

seconds = convert_time(timeout)
Expand All @@ -241,7 +251,11 @@ def listen(self, topic, timeout=1, limit=1):
break

messages = self._messages[topic][:] # Copy the list's contents
self._messages[topic] = []
try:
self._messages[topic] = [i for i in self._messages[topic] if i not in self.msg_tmp[topic]]
except KeyError:
self._messages[topic] = []
self.msg_tmp = self._messages
return messages[-limit:] if limit != 0 else messages

def subscribe_and_validate(self, topic, qos, payload, timeout=1):
Expand Down