MqttPublication

Description

The MqttPublication object aggregates DataObject objects as topics to be published through the MQTT broker. Both retain and qos (Quality of Service) properties can be set to be used for all topics.

The parent object must be of type MqttClient.

The inherited DataObjectWriter.submitMode property is initialized to DataObjectWriter.SubmitOnAnyChange which will automatically publish any topic whenever it changes. The submit mode can be changed to e.g. only publish all topics when all of them have been updated (DataObjectWriter.SubmitOnCompleteDataset) or periodically every DataObjectWriter.submitInterval milliseconds (DataObjectWriter.SubmitPeriodically). Manual submission can be implemented through the DataObjectWriter.SubmitManually submit mode and calling publish().

› Inherits:DataObjectWriter
› Inherited by:MqttMeasurementWriter, MqttObjectPublication

Properties

autoPublish

This property holds whether to automatically publish all topics whenever the MQTT client established a connection to the MQTT broker.

› Type:Boolean
› Default:true
› Signal:autoPublishChanged()
› Attributes:Writable

error

This property holds the most recently occurred error or MqttPublication.NoError if no error occurred. If the same error occurs multiple times this property does not change. Use the errorOccurred() signal to detect multiple occurrences of the same error.

› Type:Error
› Signal:errorChanged()
› Attributes:Readonly

errorString

This property holds the current human readable error string corresponding to the current value in the error property. It may include additional information such as failure reasons or locations.

› Type:String
› Signal:errorStringChanged()
› Attributes:Readonly

qos

This property holds the Quality of Service to set for the published topics. The QoS level defines how hard the client will try to ensure that a message is received. MQTT defines three QoS levels:

  • 0: The client will deliver the message once, with no confirmation. This level could be used, for example, with ambient sensor data where it does not matter if an individual reading is lost as the next one will be published soon after.
  • 1: The client will deliver the message at least once, with confirmation required.
  • 2: The client will deliver the message exactly once by using a four step handshake. This level could be used, for example, with billing systems where duplicate or lost messages could lead to incorrect charges being applied.
› Type:SignedInteger
› Default:0
› Signal:qosChanged()
› Attributes:Writable

retain

This property holds whether to retain messages for new subscribers. See mosquitto.org for more information on retained messages.

› Type:Boolean
› Default:false
› Signal:retainChanged()
› Attributes:Writable

Methods

publish()

This method publishes all topics manually. This is usually not required as a topic is published automatically whenever its DataObject.data property is changed and the DataObjectWriter.submitMode property is set to its default value DataObjectWriter.SubmitOnAnyChange.

Signals

errorOccurred()

This signal is emitted whenever an error has occurred, regardless of whether the error property has changed or not. In contrast to the change notification signal of the error property this signal is also emitted several times if a certain error occurs several times in succession.

Enumerations

Error

This enumeration describes all errors which can occur in MqttPublication objects. The most recently occurred error is stored in the error property.

Name Value Description
MqttPublication.NoError 0 No error occurred or was detected.
MqttPublication.InvalidClient 1 Parent object is not an MqttClient.
MqttPublication.EmptyTopicBasename 2 Topic base name not set.
MqttPublication.MissingObjectsIds 3 Some data objects do not have an object ID.

Example

import InCore.Foundation 2.5
import InCore.Mqtt 2.5

Application {
    System {
        id: system
        Polling on deviceTemperature { interval: 5000 }
    }

    DateTime {
        id: dateTime
        dateFormat: DateTime.FormatISO
    }

    Counter {
        id: counter
        interval: 1000
    }

    MqttClient {
        clientId: "MqttPublicationExample"
        hostname: "localhost"

        MqttPublication {
            qos: 1
            retain: false

            // publish current device temperature which is polled and updated every 5 seconds
            MqttTopic {
                name: "incore/temperature"
                data: system.deviceTemperature
            }

            MqttTopic {
                name: "incore/foo/counter"
                data: counter.value
            }

            // publish current date string on every change (i.e. every second)
            MqttTopic {
                name: "incore/bar/date"
                dataType: MqttTopic.DateTime
                data: dateTime.string
            }

            // publish array as comma separated string list
            MqttTopic {
                name: "incore/array"
                dataType: MqttTopic.StringList
                data: [ 1, 2, 3 ]
            }
        }
    }
}