Skip to main content
Version: 2.7

MQTT Publisher Configuration

The publisher is used to report datasets (values or alarms) to the broker. To check the possibilities of publisher configuration, please check the Common DataLogger Configuration manual.

The following table contains the specific properties of the MQTTClient Publisher configuration. It extends the general DataLogger publisher configuration documented in Common DataLogger Configuration.

ParameterDescriptionData Type
AddressIP Address or host name of the message brokerSystem.String
ServerPort[OPT] Port of the server. Default ports must not be specified (1883 without TLS & 8883 with TLS)System.Int32
ClientId[OPT] Mqtt client id; default is Id of the PublisherSystem.String
Username[OPT] UsernameSystem.String
Password[OPT] Password of the user.System.String
TopicTopic nameSystem.String
QualityOfServiceQuality of Service (QOS)System.Int32
CleanSession[OPT] MQTT Clean Session FlagSystem.Boolean
RetainMessagesFlag if messages are retained in the brokerSystem.Boolean
TransportLayerSecurity[OPT] Security settings for encrypted connection. Possible values are: "None", "Default", "Ssl2", "Ssl3", "Tls", "Tls11", "Tls12", "Tls13"System.String
IgnoreServerCertificateErrors[OPT] Flag to ignore certificate errors. Ignores chain errors and name mismatches of server certificate. Default value is false.System.Boolean
ClientCaCertificateFile[OPT] Path to client chain certificate for certificate authentication. Requires a ".crt" file. (Is only used for authentication, not for encrypted connection)System.String
ClientCertificateFile[OPT] Path to client certificate for certificate authentication. Requires a ".pfx" file (Is only used for authentication, not for encrypted connection)System.String
ClientCertificatePassword[OPT] Password for the client certificateSystem.String

Publisher Scripts

The publisher script transforms a dataset into a byte-array.

The script object must extend the TAbstractDataLoggerScriptObject<byte[]> class. Following method must be overwritten:

  • initialize(): initializes the context of the script. This method is called only once at the beginning.
  • processPayload(): Each time a dataset is generated, this method is called to create a payload.
  • postProcess(): After processPayload, this method is called to do some work after successfully generating the payload.
using CyberTech.Diagnostics;
using HumanOS.Kernel;
using HumanOS.Kernel.PeSeL.DataLogger;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using HumanOS.Kernel.DataModel;
using HumanOS.Kernel.Utils;
using CyberTech;
using HumanOS.Kernel.PeSeL.Script;
using Newtonsoft.Json.Linq;

namespace HumanOS.PeSeL.MQTTClient
{
/// <summary>
/// Script defining the payload
/// </summary>
public class TLoggerPayload : TAbstractDataLoggerScriptObject<byte[]>
{
///<see cref="TAbstractDataLoggerScriptObject{T}"/>
public override void initialize(IKernelAccess Kernel, TPayloadProcessingContext Context)
{
TLogger.writeInfo("Payload script initialized");
}

///<see cref="TAbstractDataLoggerScriptObject{T}"/>
public override void postProcess(IKernelAccess Kernel, TPayloadProcessingContext Context)
{
}

///<see cref="TAbstractDataLoggerScriptObject{T}"/>
public override byte[][] processPayload(IKernelAccess Kernel, TPayloadProcessingContext Context, List<TDataSet> lstDatasets)
{
List<byte[]> lstRetval = new List<byte[]>();
JObject Content = new JObject();
m_iCounter++;
Content.Add(new JProperty("messageId", m_iCounter));
Content.Add(new JProperty("timeStamp", DateTime.UtcNow.ToString("o")));

foreach (TDataSet DataSet in lstDatasets)
{
Content.Add(new JProperty(DataSet.getFieldValue<string>("DataName"), DataSet.getFieldValue<string>("Value")));
}
lstRetval.Add(Encoding.UTF8.GetBytes(Content.ToString()));

//Kernel.Logger.writeDebug(Content.ToString());
return lstRetval.ToArray();
}

private int m_iCounter = 0;
}
}

Context

The TPayloadProcessingContext Context contains information which can be read and changed when processing the payload.

  • QualityOfService : Quality of service
  • Topic : Topic to which the payload is sent
  • RetainMessages : Flag if the messages are retained on the broker.

These context variables are directly used to send the payload to the broker. However, these variables are not secure for reading the default values from the original configuration, since they are changed by the processing script.

To read the default or original settings, use

  • DefaultQualityOfService
  • DefaultTopic
  • DefaultRetainMessages

Example

{
"Id": "B8DFA2BC-13B2-47D6-A9B0-AEA1257B33A6",
"Name": "DataNodePublisher",
"PayloadScriptFile": "Payload.cs",
"SamplingRate": 5,
"SamplerType": "OnlyLastValue",
"Address": "10.196.24.12",
"Username": "",
"Password": "",
"RetainMessages": true,
"Topic": "/home/humanos1/out",
"QualityOfService": 2,
"DataSets": [
{
"Name": "Data",
"Type": "DataNode",
"NodeFilter": "node.hasProperty<string>(\"EnableMqtt\", \"1\")",
"Fields": [
{
"Name": "TimeStamp",
"DataType": "System.DateTime",
"Query": "item.TimeStamp"
},
{
"Name": "Value",
"DataType": "System.Double",
"Query": "item.Value"
},
{
"Name": "State",
"DataType": "System.Int32",
"Query": "item.DataState"
}
]
}
],
"ServiceRule": {
"BindingRuleId": "{8A80927A-B1F8-4505-B4BA-FB2DFED765E4}",
"UnbindingRuleId": "{1058566D-0787-48C3-95A5-235E9A55CCF3}",
"Type": "All"
}
}