Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Latest commit

 

History

History

README.md

TcoMQTT

Please note that you need use Inxton connector and compiler in order to use the extension functions.

Creating the MQTT Client

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.

How to publish primitives

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");

Publishing with a conditon

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);

How to stop publishing

var handle = Entry.MAIN_TECHNOLOGY._technology._mirrorStartCycle.Subscribe(client, "fun_with_TcOpen_Hammer");
handle.StopPublishing();

Subscribe to a value

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");

Publishing and observign complex types

Publish

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;
}

Subscribe

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");