MQTT Subscriber Configuration
The subscriber can receive messages from the broker. A payload processing scripts transforms the payloads to HumanOS® internal calls, such as command calls, setting data or properties.
Note: The payload script file (PayloadScriptFile) is mandatory. It is used to transform the payload from other clients into internal calls and sending a corresponding reply.
The following table contains the specific properties of the MQTTClient Subscriber configuration. It extends the general DataLogger subscriber configuration documented in Common DataLogger Configuration.
Parameter | Description | Data Type |
---|---|---|
Address | IP Address or host name of the message broker | System.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 Publisher | System.String |
Username | [OPT] Username | System.String |
Password | [OPT] Password of the user. | System.String |
Topic | Topic name | System.String |
QualityOfService | Quality of Service (QOS) | System.Int32 |
CleanSession | [OPT] MQTT Clean Session Flag | System.Boolean |
ReplyTopic | Reply topic address | System.String |
ReplyQualityOfService | Quality of Service for reply channel | System.String |
TransportLayerSecurity | [OPT] Security settings for encrypted connection. Possible values are: "None", "Default", "Ssl2", "Ssl3", "Tls", "Tls11", "Tls12" | 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 certificate | System.String |
Subscriber Scripts
The Subscriber scripts are used to parse incoming payloads and to execute functions of the HumanOS® Kernel, such as calling commands, setting data values, ect.
The script object must extend the TAbstractDataConsumerScriptObject<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 payload is received by the subscription, this method is called.postProcess()
: After processPayload, this method is called to do some work after successfully processing the payload.
using HumanOS.Kernel;
using HumanOS.Kernel.DataModel;
using HumanOS.Kernel.PeSeL.Script;
using HumanOS.Kernel.Utils;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HumanOS.PeSeL.MQTTClient.Test.HumanOS.PeSeL.MQTTClient
{
/// <summary>
/// Implements a simple script to write a temperature limit into node space
/// </summary>
public class TDataConsumerScript : TAbstractDataConsumerScriptObject<byte[]>
{
///<see cref="TAbstractDataConsumerScriptObject{T}"/>
public override void initialize(IKernelAccess Kernel, TPayloadProcessingContext Context)
{
}
///<see cref="TAbstractDataConsumerScriptObject{T}"/>
public override void postProcess(IKernelAccess Kernel, TPayloadProcessingContext Context)
{
}
///<see cref="TAbstractDataConsumerScriptObject{T}"/>
public override void processPayload(IKernelAccess Kernel, TPayloadProcessingContext Context, byte[] DataReceived, out byte[] oDataToSend)
{
oDataToSend = null;
JObject Obj = JObject.Parse(Encoding.UTF8.GetString(DataReceived));
string strName = (string)Obj["name"];
if (strName == "TemperatureLimit")
{
INode TempNode = Kernel.NodeSpace.queryNode(n => n.Name == "Temperature");
TempNode.addProperty<double>("Limit", TValueConverter.convert<double>(Obj["value"]));
oDataToSend = Encoding.UTF8.GetBytes("Limit Set");
}
}
}
}
Example
{
"Id": "80E66CB3-9A67-41D0-8F1C-99F95F0ADB8D",
"Name": "DataNodeSubscriber",
"PayloadScriptFile": "TDataConsumerScript.cs",
"SamplingRate": 0.1,
"Address": "10.196.24.12",
"Username": "",
"Password": "",
"RetainMessages": true,
"QualityOfService": 2,
"Topic": "/home/humanos1/in",
"ReplyTopic": "/home/humanos1/out",
"ReplyQualityOfService": 2
}