Please note that you need use Inxton connector and compiler in order to use the extension functions.
Create an MQTT client according to your needs. See following example with a public broker.
private static IMqttClient CreateClientAndConnect()
{
int MqttPort = 1883;
var MqttFactory = new MqttFactory();
var Broker = MqttFactory.CreateMqttServer();
var mqttServerOptions = MqttFactory
.CreateServerOptionsBuilder()
.WithDefaultEndpointPort(MqttPort)
.Build();
Broker.StartAsync(mqttServerOptions);
var c = MqttFactory.CreateMqttClient();
var mqttClientOptions = MqttFactory.CreateClientOptionsBuilder().WithTcpServer("broker.emqx.io")
.Build();
c.UseApplicationMessageReceivedHandler(new TcoAppMqttHandler());
c.ConnectAsync(mqttClientOptions, CancellationToken.None).Wait();
return c;
}The most important line here is
c.UseApplicationMessageReceivedHandler(new TcoAppMqttHandler());Without this line you won't be able to use extensions bellow.
Access the C# Twin of a PLC variable and use the PublishChanges extension from TcOpen.Inxton.Mqtt namespace.
Entry.MAIN_TECHNOLOGY._technology
._startCycleCount
.PublishChanges(client, "fun_with_TcOpen_Hammer");You can specify a condition before publishing the value. this code will publish a new value only if the difference is greater than 100.
Entry.MAIN_TECHNOLOGY._technology
._startCycleCount
.PublishChanges(client, "fun_with_TcOpen_Hammer",
publishCondition: (lastPublished, toPublish) => toPublish - lastPublished >= 100);var handle = Entry.MAIN_TECHNOLOGY._technology._mirrorStartCycle.Subscribe(client, "fun_with_TcOpen_Hammer");
handle.StopPublishing();If I'm streaming the variable _startCycleCount I can subscribe with variable _mirrorStartCycle of the same type to mirror the value via MQTT.
Entry.MAIN_TECHNOLOGY._technology
._mirrorStartCycle
.Subscribe(client, "fun_with_TcOpen_Hammer");Similar to primitives.
Entry.MAIN_TECHNOLOGY._technology._ST001
._components
.PublishChanges(client, "fun_with_TcOpen_HammerX");Complex objects are published every 500ms by default. To change the sample rate use the parameter sampleRate
Entry.MAIN_TECHNOLOGY._technology._ST001
._components
.PublishChanges(client, "fun_with_TcOpen_HammerX", TimeSpan.FromSeconds(1));You can also write a condition which will determine wheter to publish or not based on the last and latest value.
Entry.MAIN_TECHNOLOGY._technology._ST001
._components
.PublishChanges(client, "fun_with_TcOpen_Hammer", publishCondition: ComponentsCondition); PublishCondition delegate can be defined as a method. If you're publishing a certain PLC twin, the type of objects inside the will always be a prefixed with "Plain".
private bool ComponentsCondition(object LastPublished, object ToPublish)
{
var lastPublihed = (PlainST001_ComponentsHandler)LastPublished;
var toPublihed = (PlainST001_ComponentsHandler)ToPublish;
return lastPublihed._drive._power != toPublihed._drive._power;
}When you subscribe to complex type, you need to specify its plain countertype.
If you want to observe changes of type ST001_ComponentsHandler you need to subscribe to PlainST001_ComponentsHandler.
Entry.MAIN_TECHNOLOGY._technology._ST001
._mqttMirrorComponents
.Subscribe<PlainST001_ComponentsHandler>(client, "fun_with_TcOpen_HammerX");