Connectivity Software User's Guide and Reference
SparkplugDataSourceConnectionMode Enumeration
Example Example 



OpcLabs.EasySparkplug Assembly > OpcLabs.EasySparkplug Namespace : SparkplugDataSourceConnectionMode Enumeration
Defines when and how the Sparkplug producer (edge node or device) connects and disconnects its data source.
Syntax
'Declaration
 
<DisplayName2Attribute("Sparkplug Data Source Connection Mode")>
Public Enum SparkplugDataSourceConnectionMode 
   Inherits System.Enum
   Implements System.IComparable, System.IConvertible, System.IFormattable 
'Usage
 
Dim instance As SparkplugDataSourceConnectionMode
[DisplayName2("Sparkplug Data Source Connection Mode")]
public enum SparkplugDataSourceConnectionMode : System.Enum, System.IComparable, System.IConvertible, System.IFormattable  
[DisplayName2("Sparkplug Data Source Connection Mode")]
public enum class SparkplugDataSourceConnectionMode : public System.Enum, System.IComparable, System.IConvertible, System.IFormattable  
Members
MemberValueDescription
Manual5Connect and disconnect the data source manually, using method calls on the Sparkplug producer (edge node or device).

When this mode is set on the Sparkplug edge node or device, the calls to initiate manual connection and disconnection are made from your code at your discretion.

See also: DataSourceConnectionMode

See also: ISparkplugProducer.PerformConnectDataSource

See also: ISparkplugProducer.PerformDisconnectDataSource````

The value of this enum member is 5.

WhenApplicationOnline3Connect the data source when the Primary Host Application is online, and disconnect it when the Primary Host Application is offline.

In this mode, the data source connection is controlled by the value of the ApplicationOnline of the edge node or device.

When the edge node is not set to use the Primary Host Application (IEasySparkplugEdgeNode.PrimaryHostId is empty), the data source will not be connected or disconnected automatically, because the Primary Host Application will never come online. In this case, this mode is equivalent to Manual.

When the edge node is set to use the Primary Host Application (IEasySparkplugEdgeNode.PrimaryHostId is not empty), the data source will be connected automatically when the Primary Host Application status is online, and the data source will be disconnected when the Primary Host Application status is offline.

See also: DataSourceConnectionMode

The value of this enum member is 3.

WhenComponentStarted0Connect the data source when the Sparkplug component is started, and disconnect it is stopping.

The "Sparkplug component" here refers to an instance of the EasySparkplugEdgeNodeCore or its derivative.

This is usually the default mode setting.

See also: DataSourceConnectionMode

The value of this enum member is 0.

WhenControllerOnline4Connect and disconnect the data source together with the online/offline state of its controlling component (edge node, device, or Primary Host Application).

The term "controller" (controlling component) here refers to the Sparkplug edge node or device, if the edge node is not set to use the Primary Host Application, or to the Primary Host Application, if the edge node is set to use it.

When the edge node is not set to use the Primary Host Application (IEasySparkplugEdgeNode.PrimaryHostId is empty), the data source connection will be controlled by the value of the ProducerOnline of the edge node or device.

When the edge node is set to use the Primary Host Application (IEasySparkplugEdgeNode.PrimaryHostId is not empty), the data source will be connected automatically when the Primary Host Application status is online, and the data source will be disconnected when the Primary Host Application status is offline.

The value of this enum member is 4.

WhenProducerOnline2Connect the data source when the Sparkplug producer (edge node or device) is online, and disconnect it when the Sparkplug producer is offline.

In this mode, the data source connection is controlled by the value of the ProducerOnline of the edge node or device.

When the edge node is not set to use the Primary Host Application (IEasySparkplugEdgeNode.PrimaryHostId is empty), this mode is equivalent to WhenSystemConnected. When the edge node is set to use the Primary Host Application (IEasySparkplugEdgeNode.PrimaryHostId is not empty), this is mode is equivalent to WhenApplicationOnline.

See also: DataSourceConnectionMode

The value of this enum member is 2.

WhenSystemConnected1Connect the data source when the edge node is successfully connected to the Sparkplug system (MQTT broker), and disconnect it otherwise.

See also: DataSourceConnectionMode

The value of this enum member is 1.

Remarks

A Sparkplug edge node has two connections: The first to the Sparkplug system (MQTT broker), and the second to its data source. A Sparkplug device has one connection: to its data source; it relies on its edge node for connection to the Sparkplug system.

The choice of the data source connection mode depends on the nature of the implementation. The usual default setting (WhenComponentStarted) attempts to maintain the connection to the data source for the widest possible time. This means that you can e.g. keep collecting the data from the data source even when the connection to the MQTT broker is not available, store the data and publish them later. If you do not have this or similar requirements, you can consider choosing a "narrower" data source connection mode.

Data publishing by the edge node or device only takes place when its data source is connected. Your implementation needs to define what that means - and how the actual connections to and disconnection from the data source are performed. The behavior can be defined by hooking events handlers or overriding virtual methods. If you do not define it, the default behavior is that the data source connection is a "no operation" and it always succeeds.

 

Sparkplug is a trademark of Eclipse Foundation, Inc. "MQTT" is a trademark of the OASIS Open standards consortium. Other related terms are trademarks of their respective owners. Any use of these terms on this site is for descriptive purposes only and does not imply any sponsorship, endorsement or affiliation.

Example
// This example shows how to connect to the data source only when the producer is online.
//
// You can use any Sparkplug application, including our SparkplugCmd utility and the SparkplugApplicationConsoleDemo
// program, to subscribe to the edge node data. 
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-ConnectivityStudio/Latest/examples.html .
// Sparkplug examples in C# on GitHub: https://github.com/OPCLabs/Examples-ConnectivityStudio-CSharp .
// Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
// a commercial license in order to use Online Forums, and we reply to every post.

using System;
using OpcLabs.EasySparkplug;

namespace SparkplugDocExamples.EdgeNode._EasySparkplugEdgeNode
{
     class DataSourceConnectionMode
    {
        static public void Main1()
        {
            // Note that the default port for the "mqtt" scheme is 1883.
            var hostDescriptor = new SparkplugHostDescriptor("mqtt://localhost");

            // Instantiate the edge node object.
            var edgeNode = new EasySparkplugEdgeNode(hostDescriptor, "easyGroup", "easySparkplugDemo");

            // Configure the edge node to connect to the data source only when the producer is online. You can test it out
            // e.g. by stopping or disconnecting from the MQTT broker.
            //
            // You can compare the output of this example with and without the statement below. With the statement, the
            // Poll event will not be raised unless the producer is online. Without the statement, the Poll event will
            // always be raised while the edge node is started, regardless of the MQTT broker connection state.
            edgeNode.DataSourceConnectionMode = SparkplugDataSourceConnectionMode.WhenProducerOnline;

            // Hook the SystemConnectionStateChanged event to handle system connection state changes.
            edgeNode.SystemConnectionStateChanged += (sender, eventArgs) =>
            {
                // Display the new connection state (such as when the connection to the broker succeeds or fails).
                Console.WriteLine($"{nameof(EasySparkplugEdgeNode.SystemConnectionStateChanged)}: {eventArgs}");
            };

            // Hook the ProducerOnlineChanged event to handle changes in the online state of the producer.
            edgeNode.ProducerOnlineChanged += (sender, eventArgs) =>
            {
                // Display the new producer online state.
                Console.WriteLine($"{nameof(EasySparkplugEdgeNode.ProducerOnlineChanged)}: {edgeNode.ProducerOnline}");
            };

            // Hook the Poll event (but do not mark the polling as processed by ourselves).
            edgeNode.Poll += (sender, eventArgs) =>
            {
                // Display when the component is polling for new data.
                Console.WriteLine(nameof(EasySparkplugEdgeNode.Poll));
            };

            // Define a metric providing random integers.
            var random = new Random();
            edgeNode.Metrics.Add(new SparkplugMetric("MyMetric").ReadValueFunction(() => random.Next()));

            // Start the edge node.
            Console.WriteLine("The edge node is starting...");
            edgeNode.Start();

            Console.WriteLine("The edge node is started.");
            Console.WriteLine();

            // Let the user decide when to stop.
            Console.WriteLine("Press Enter to stop the edge node...");
            Console.ReadLine();

            // Stop the edge node.
            Console.WriteLine("The edge node is stopping...");
            edgeNode.Stop();

            Console.WriteLine("The edge node is stopped.");
        }
    }
}
' This example shows how to connect to the data source only when the producer is online.
'
' You can use any Sparkplug application, including our SparkplugCmd utility and the SparkplugApplicationConsoleDemo
' program, to subscribe to the edge node data.
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-ConnectivityStudio/Latest/examples.html .
' Sparkplug examples in C# on GitHub: https://github.com/OPCLabs/Examples-ConnectivityStudio-CSharp .
' Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
' a commercial license in order to use Online Forums, and we reply to every post.

Imports OpcLabs.EasySparkplug

Namespace Global.SparkplugDocExamples.EdgeNode._EasySparkplugEdgeNode
    Class DataSourceConnectionMode
        Public Shared Sub Main1()
            ' Note that the default port for the "mqtt" scheme is 1883.
            Dim hostDescriptor = New SparkplugHostDescriptor("mqtt://localhost")

            ' Instantiate the edge node object.
            Dim edgeNode = New EasySparkplugEdgeNode(hostDescriptor, "easyGroup", "easySparkplugDemo")

            ' Configure the edge node to connect to the data source only when the producer is online. You can test it out
            ' e.g. by stopping or disconnecting from the MQTT broker.
            ' 
            ' You can compare the output of this example with and without the statement below. With the statement, the
            ' Poll event will not be raised unless the producer is online. Without the statement, the Poll event will
            ' always be raised while the edge node is started, regardless of the MQTT broker connection state.
            edgeNode.DataSourceConnectionMode = SparkplugDataSourceConnectionMode.WhenProducerOnline

            ' Hook the SystemConnectionStateChanged event to handle system connection state changes.
            AddHandler edgeNode.SystemConnectionStateChanged,
                Sub(sender, eventArgs)
                    ' Display the new connection state (such as when the connection to the broker succeeds or fails).
                    Console.WriteLine($"{NameOf(EasySparkplugEdgeNode.SystemConnectionStateChanged)}: {eventArgs}")
                End Sub

            ' Hook the ProducerOnlineChanged event to handle changes in the online state of the producer.
            AddHandler edgeNode.ProducerOnlineChanged,
                Sub(sender, eventArgs)
                    ' Display the new producer online state.
                    Console.WriteLine($"{NameOf(EasySparkplugEdgeNode.ProducerOnlineChanged)}: {edgeNode.ProducerOnline}")
                End Sub

            ' Hook the Poll event (but do not mark the polling as processed by ourselves).
            AddHandler edgeNode.Poll,
                Sub(sender, eventArgs)
                    ' Display when the component Is polling for New data.
                    Console.WriteLine(NameOf(EasySparkplugEdgeNode.Poll))
                End Sub

            ' Define a metric providing random integers.
            Dim random = New Random()
            edgeNode.Metrics.Add(New SparkplugMetric("MyMetric").ReadValueFunction(Function() random.Next()))

            ' Start the edge node.
            Console.WriteLine("The edge node is starting...")
            edgeNode.Start()

            Console.WriteLine("The edge node is started.")
            Console.WriteLine()

            ' Let the user decide when to stop.
            Console.WriteLine("Press Enter to stop the edge node...")
            Console.ReadLine()

            ' Stop the edge node.
            Console.WriteLine("The edge node is stopping...")
            edgeNode.Stop()

            Console.WriteLine("The edge node is stopped.")
        End Sub
    End Class
End Namespace
Inheritance Hierarchy

System.Object
   System.ValueType
      System.Enum
         OpcLabs.EasySparkplug.SparkplugDataSourceConnectionMode

Requirements

Target Platforms: .NET Framework: Windows 10 (selected versions), Windows 11 (selected versions), Windows Server 2016, Windows Server 2022; .NET: Linux, macOS, Microsoft Windows

See Also