MQTT Control Driver Configuration
Plugin Configuration
The plugin configuration file is named settings.json
located in $(install directory)\Config\HumanOS.UHAL.MqttControl\
. It contains all global settings of the plugin.
See Generic Plugin Configuration for more details.
Device Information File
The device information file is used to configure the access to the MQTT devices. It contains:
Id
: unique device id (GUID), which MUST match the device license id.DriverId
:11034AA5-13C7-4D05-83E0-361BC816E779
Address
: Connection address- Data Access
See Device Information Model for more details.
Connection Address
The connections are established on TCP Port defined by the device address setting, so make sure it is open. The Address for MQTT Control must be provided like following:
Name | Description | Example |
---|---|---|
Address | IP address and Port | address=10.256.35.32;port=1883 |
This information is contained by the device configuration.
Additional Device and Connection Properties
The following connection options can be set via the device properties.
Name | Description | DataType |
---|---|---|
MqttProtocol | The protocol version of MQTT, possible values are
| System.String |
ReconnectInterval | When the connection to broker cannot established, the task processor tries to reconnect with this interval to the broker. Time unit in milliseconds | System.Int32 |
KeepAliveInterval | MQTT can be configured by design with a keep alive interval. This property specifies the KeepAlive interval. Time unit in milliseconds | System.Int32 |
ConnectionTimeout | When the MQTT plugin is connected to the broker, this timeout specifies how long to wait for an answer from the broker until the connection will be aborted. Time unit in milliseconds | System.Int32 |
Username | MQTT connections can be protected by a username and a password. This property specifies the username | System.String |
Password | This property specifies the password | System.String |
ClientId | Each MQTT client should have an unique id. This property specifies the MQTT client id. Default is the device Id | System.String |
CleanSession | With this property, the MQTT client can tell the broker not to store session information or the client does not need to get messages that it misses offline | System.Boolean |
QualityOfService | The quality of the messages. Following values are possible:
| System.Int32 |
RetainMessages | With this property enabled, the Plugin will send all Data as retain messages | System.Boolean |
SslProtocol | For secure connections, the SSL-Protocol can be specified. Possible values are:
| System.String |
Device Example
{
"Name": "MQTT Control 1",
"Id": "50EFD36B-95D4-450F-A1FB-16969CE70DB9",
"DriverId": "E78FB177-AEDC-4A3A-A237-B9835738FD68",
"Address": "http://localhost:1883",
"Properties": [
{
"Name": "ClientId",
"Value": "bb6ffeb6"
},
{
"Name": "Username",
"Value": "user"
},
{
"Name": "Password",
"Value": "password"
},
{
"Name": "CleanSession",
"Value": true
},
{
"Name": "KeepAliveInterval",
"Value": 30000
},
{
"Name": "ReconnectInterval",
"Value": 10000
},
{
"Name": "ConnectionTimeout",
"Value": 10000
}
]
}
Custom Task Processor
With the custom task processor, some device specific code can be implemented. To work with custom task processors, the task processor(s) in the device file must be configured.
Following example shows the additional task processor
{
// Task processor to do custom connection details
"TaskProcessors": [
{
"Id": "E63EA07D-1938-47BD-9C23-3D4AED34A97C",
"Name": "CustomTaskProcessor",
"ScriptFile": "TaskProcessors\\TCustomProcessor.cs"
}
]
}
The main class of the task processor script must be derived from TAbstractMqttProcessingScriptObject
.
The method doProcess()
is called every time the task processor is executed and the device is connected.
onConnectedToDevice()
is called every time the UHAL Plugin is connected to the broker.
onDisconnectFromDevice()
is called every time the UHAL Plugin is disconnected from the broker.
Following example shows the script
using CyberTech.Diagnostics;
using HumanOS.Kernel;
using HumanOS.Kernel.Processing;
using HumanOS.Kernel.UHAL.InfoModel;
using HumanOS.UHAL.MqttControl.Script;
using MQTTnet.Client;
namespace HumanOS.IoT.Designer.Library.Scripts
{
/// <summary>
/// Example for MQTT processing
/// </summary>
public class Blank_UHALMqttProcessingScriptObject : TAbstractMqttProcessingScriptObject
{
///<see cref="TAbstractMqttProcessingScriptObject"/>
public override void doProcess(IKernelAccess Kernel, TDeviceSchemaInfo DeviceInfo, ILogger Logger, IMqttClient nClient)
{
}
///<see cref="TAbstractMqttProcessingScriptObject"/>
public override EProcessingState onConnectedToDevice(IKernelAccess Kernel, TDeviceSchemaInfo DeviceInfo, ILogger Logger, IMqttClient nClient)
{
return base.onConnectedToDevice(Kernel, DeviceInfo, Logger, nClient);
}
///<see cref="TAbstractMqttProcessingScriptObject"/>
public override void onDisconnectFromDevice(IKernelAccess Kernel, TDeviceSchemaInfo DeviceInfo, ILogger Logger, IMqttClient nClient)
{
base.onDisconnectFromDevice(Kernel, DeviceInfo, Logger, nClient);
}
}
}