Skip to content

Commit

Permalink
Hardcoded to MQTT QoS level 2 #1393
Browse files Browse the repository at this point in the history
  • Loading branch information
dennissiemensma committed May 30, 2021
1 parent 6dc7c02 commit ad3efe3
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 13 deletions.
8 changes: 7 additions & 1 deletion docs/reference/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ Current version
:doc:`How to update</how-to/upgrading/upgrade>` *(minor updates only)*


v4.17.0 - 2021-xx-xx
v4.16.3 - 2021-05-30
--------------------

.. note::

The MQTT QoS level is no longer configurable. Level 2 is now always used, since this seems to work fine for any users that had issues recently.

- ``Changed`` Hardcoded to MQTT QoS level 2 [`#1393 <https://github.com/dsmrreader/dsmr-reader/issues/1393>`_]


v4.16.2 - 2021-05-12
--------------------
Expand Down
2 changes: 1 addition & 1 deletion dsmr_mqtt/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MQTTBrokerSettingsAdmin(SingletonModelAdmin):
),
(
_('Misc'), {
'fields': ['username', 'password', 'qos'],
'fields': ['username', 'password'],
}
)
)
Expand Down
1 change: 1 addition & 0 deletions dsmr_mqtt/models/settings/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class MQTTBrokerSettings(ModelUpdateMixin, SingletonModel):
verbose_name=_('Client ID'),
help_text=_('The client ID used to identify DSMR-reader sending the MQTT messages.')
)
# @todo @deprecated No longer in use. Drop in later release safely.
qos = models.IntegerField(
default=QOS_2,
choices=QOS_CHOICES,
Expand Down
18 changes: 11 additions & 7 deletions dsmr_mqtt/services/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ def initialize_client():
broker_settings.update(enabled=False)
raise RuntimeError('No hostname found in settings')

logger.debug('MQTT: Initializing MQTT client for "%s:%d"', broker_settings.hostname, broker_settings.port)
logger.debug(
'MQTT: Initializing MQTT client for "%s:%d" (QoS level %d)',
broker_settings.hostname,
broker_settings.port,
settings.DSMRREADER_MQTT_QOS_LEVEL
)
mqtt_client = paho.Client(client_id=broker_settings.client_id)
mqtt_client.on_connect = on_connect
mqtt_client.on_disconnect = on_disconnect
Expand Down Expand Up @@ -60,26 +65,25 @@ def initialize_client():

def run(mqtt_client):
""" Reads any messages from the queue and publishing them to the MQTT broker. """
broker_settings = MQTTBrokerSettings.get_solo()

# Keep batches small, only send the latest X messages. The rest will be trimmed (in case of delay).
message_queue = queue.Message.objects.all().order_by('-pk')[0:settings.DSMRREADER_MQTT_MAX_MESSAGES_IN_QUEUE]

if not message_queue:
return

logger.info('MQTT: Processing %d message(s) using QoS level %d', len(message_queue), broker_settings.qos)
logger.debug('MQTT: Processing %d message(s)', len(message_queue))

for current in message_queue:
logger.debug('MQTT: Publishing queued message (#%s) for %s: %s', current.pk, current.topic, current.payload)
message_info = mqtt_client.publish(
topic=current.topic,
payload=current.payload,
qos=broker_settings.qos,
qos=settings.DSMRREADER_MQTT_QOS_LEVEL,
retain=True
)
# Make sure to call this, since message_info.is_published() will ALWAYS be True when using QoS level 0!
loop_result = mqtt_client.loop(settings.DSMRREADER_CLIENT_TIMEOUT)

loop_result = mqtt_client.loop(0.1)

# Detect any networking errors early.
if loop_result != paho.MQTT_ERR_SUCCESS:
Expand All @@ -91,7 +95,7 @@ def run(mqtt_client):
# Networking errors should terminate this loop as well, along with a request for restart.
while not message_info.is_published():
logger.debug('MQTT: Waiting for message (#%s) to be marked published by broker', current.pk)
loop_result = mqtt_client.loop(settings.DSMRREADER_CLIENT_TIMEOUT)
loop_result = mqtt_client.loop(0.5)

# Prevents infinite loop on connection errors.
if loop_result != paho.MQTT_ERR_SUCCESS:
Expand Down
2 changes: 1 addition & 1 deletion dsmrreader/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.utils.version import get_version

VERSION = (4, 17, 0, 'final', 0)
VERSION = (4, 16, 3, 'final', 0)

__version__ = get_version(VERSION)
4 changes: 2 additions & 2 deletions dsmrreader/config/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@
'disable_existing_loggers': False,
'formatters': {
'simple': {
'format': '{asctime} | {levelname:8} | {message}',
'format': '{asctime} {levelname:8} | {message}',
'style': '{',
},
'verbose': {
'format': '{asctime} | {levelname:8} {module:12} {funcName:30} {lineno:4} | {message}',
'format': '{asctime} {levelname:8} {module:12} {funcName:30} {lineno:4} | {message}',
'style': '{',
},
},
Expand Down
5 changes: 4 additions & 1 deletion dsmrreader/config/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@
DSMRREADER_DROPBOX_SYNC_INTERVAL = 1 # Only check for changes once per hour.
DSMRREADER_DROPBOX_ERROR_INTERVAL = 12 # Skip new files for 12 hours when insufficient space in Dropbox account.

DSMRREADER_CLIENT_TIMEOUT = 10
DSMRREADER_CLIENT_TIMEOUT = 20

# See #1391 #1387 #1393 and https://www.hivemq.com/blog/mqtt-essentials-part-6-mqtt-quality-of-service-levels/
DSMRREADER_MQTT_QOS_LEVEL = 2

# Max telegrams to compact in a single run.
DSMRREADER_COMPACT_MAX = 1024
Expand Down

0 comments on commit ad3efe3

Please sign in to comment.