MqttMeasurementWriter

Description

The MqttMeasurementWriter object is a special MqttPublication object which publishes a set of Measurement objects in a certain topic structure or data format. The Object.objectId property of each measurement is used as the corresponding key in the measurements JSON/CBOR array (in MqttMeasurementWriter.JsonMap or MqttMeasurementWriter.CborMap mode) or to construct the topic name (in MqttMeasurementWriter.TopicTree mode).

This object was introduced in InCore 2.5.

› Inherits:MqttPublication

Properties

bufferDatabase

This property holds the database to which the measurements are written temporarily when MeasurementBufferDatabase.buffering is set to true and the MQTT client is not connected to a broker.

› Type:MeasurementBufferDatabase
› Signal:bufferDatabaseChanged()
› Attributes:Readonly

container

This property holds a map with keys/values in which to embed the JSON/CBOR data in the measurementsFieldName field. This allows publish additional metadata such as the device name or location. If empty, the string representation of the JSON/CBOR data is published directly.

› Type:Map
› Signal:containerChanged()
› Attributes:Writable

fields

This property holds a combination of MqttMeasurementWriter.Field flags specifying which properties of each Measurement to publish.

› Type:Fields
› Default:enumitem{MqttMeasurementWriter::Field::}
› Signal:fieldsChanged()
› Attributes:Writable

grouping

This property holds whether an additional hierarchy level for measurement groups should be used. When enabled, the Object.objectId associated with the objects is added to topicBaseName or inserted in the JSON/CBOR map.

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

measurementsFieldName

This property holds the name of the field in the container map in which to embed the JSON/CBOR-encoded measurements.

› Type:String
› Default:measurements
› Signal:measurementsFieldNameChanged()
› Attributes:Writable

mode

This property holds the mode specifying how the measurements are published.

› Type:MqttMeasurementWriter.Mode
› Default:MqttMeasurementWriter.JsonMap
› Signal:modeChanged()
› Attributes:Writable

topicBaseName

This property holds a string which to prepend to the topic name of all publications.

› Type:String
› Signal:topicBaseNameChanged()
› Attributes:Writable

Enumerations

Fields

This enumeration describes flags for fields which the writer can publish.

Name Value Description
MqttMeasurementWriter.Value 1 publish the DataObject.data property.
MqttMeasurementWriter.Timestamp 2 publish the DataObject.timestamp property.
MqttMeasurementWriter.Name 4 publish the DataObject.name property.
MqttMeasurementWriter.Description 8 publish the DataObject.description property.
MqttMeasurementWriter.Unit 16 publish the Measurement.unit property.
MqttMeasurementWriter.SiPrefix 32 publish the Measurement.siPrefix property.
MqttMeasurementWriter.Decimals 64 publish the Measurement.decimals property.
MqttMeasurementWriter.Range 128 publish the MeasurementView.range property.

Mode

This enumeration describes all supported modes in which the writer can operate.

Name Value Description
MqttMeasurementWriter.TopicTree 0 publish measurements and their properties in subtopics, e.g. topicBaseName/myMeasurement/value (with grouping set to false) or topicBaseName/myGroup/myMeasurement/value (with grouping set to true).
MqttMeasurementWriter.JsonMap 1 publish measurements and their properties as a JSON map.
MqttMeasurementWriter.CborMap 2 publish measurements and their properties as a CBOR map.
MqttMeasurementWriter.JsonArray 3 publish measurements and their properties as a JSON array.
MqttMeasurementWriter.CborArray 4 publish measurements and their properties as a CBOR array.

Example

import InCore.Foundation 2.5
import InCore.Mqtt 2.5

Application {

    ObjectArray {
        id: measurements
        MeasurementGroup {
            objectId: "weather"
            Measurement {
                objectId: "temperature"
                name: "Temperature"
                data: 25
                dataType: Measurement.Float
                unit: Measurement.DegreeCelsius
                property var t: Timer { onTriggered: parent.data += Math.random() - 0.5 }
            }
            Measurement {
                objectId: "humidity"
                name: "Relative humidity"
                data: 65
                dataType: Measurement.SignedInteger
                unit: "%"
                property var t: Timer { onTriggered: parent.data += Math.random() - 0.5 }
            }
        }
    }

    MqttClient {
        clientId: "MqttMeasurementWriterExample"
        hostname: "localhost"

        MqttMeasurementWriter {
            qos: 1
            retain: true
            topicBaseName: "weather"
            grouping: true
            submitMode: MqttMeasurementWriter.SubmitOnCompleteDataset
            mode: MqttMeasurementWriter.JsonMap
            fields: MqttMeasurementWriter.Timestamp | MqttMeasurementWriter.Value |
                    MqttMeasurementWriter.Name | MqttMeasurementWriter.Unit
            Gather on objects {
                source: measurements
                typeFilter: Measurement {}
            }
        }
    }
}