Connectivity Software User's Guide and Reference
EasySparkplugMetricSubscriptionArguments Class
Members  Example 



OpcLabs.EasySparkplug Assembly > OpcLabs.EasySparkplug.OperationModel Namespace : EasySparkplugMetricSubscriptionArguments Class
Provides extended arguments for subscriptions to Sparkplug metrics, including an optional callback handler.
Object Model
EasySparkplugMetricSubscriptionArguments ClassSparkplugHostDescriptor Class
Syntax
'Declaration
 
<ExceptionContractAnnotationAttribute(True)>
<CLSCompliantAttribute(True)>
<TypeConverterAttribute(System.ComponentModel.ExpandableObjectConverter)>
<ValueControlAttribute("OpcLabs.BaseLib.Forms.Common.ObjectSerializationControl, OpcLabs.BaseLibForms, Version=5.83.473.1, Culture=neutral, PublicKeyToken=6faddca41dacb409", 
   DefaultReadWrite=False, 
   Export=True, 
   PageId=10001)>
<SerializableAttribute()>
Public Class EasySparkplugMetricSubscriptionArguments 
   Inherits SparkplugMetricSubscriptionArguments
   Implements LINQPad.ICustomMemberProvider, OpcLabs.BaseLib.ComTypes._Info, OpcLabs.BaseLib.ComTypes._Object2, OpcLabs.BaseLib.OperationModel.ComTypes._OperationArguments, System.ICloneable, System.Runtime.Serialization.ISerializable, System.Xml.Serialization.IXmlSerializable 
[ExceptionContractAnnotation(true)]
[CLSCompliant(true)]
[TypeConverter(System.ComponentModel.ExpandableObjectConverter)]
[ValueControl("OpcLabs.BaseLib.Forms.Common.ObjectSerializationControl, OpcLabs.BaseLibForms, Version=5.83.473.1, Culture=neutral, PublicKeyToken=6faddca41dacb409", 
   DefaultReadWrite=false, 
   Export=true, 
   PageId=10001)]
[Serializable()]
public class EasySparkplugMetricSubscriptionArguments : SparkplugMetricSubscriptionArguments, LINQPad.ICustomMemberProvider, OpcLabs.BaseLib.ComTypes._Info, OpcLabs.BaseLib.ComTypes._Object2, OpcLabs.BaseLib.OperationModel.ComTypes._OperationArguments, System.ICloneable, System.Runtime.Serialization.ISerializable, System.Xml.Serialization.IXmlSerializable  
[ExceptionContractAnnotation(true)]
[CLSCompliant(true)]
[TypeConverter(System.ComponentModel.ExpandableObjectConverter)]
[ValueControl("OpcLabs.BaseLib.Forms.Common.ObjectSerializationControl, OpcLabs.BaseLibForms, Version=5.83.473.1, Culture=neutral, PublicKeyToken=6faddca41dacb409", 
   DefaultReadWrite=false, 
   Export=true, 
   PageId=10001)]
[Serializable()]
public ref class EasySparkplugMetricSubscriptionArguments : public SparkplugMetricSubscriptionArguments, LINQPad.ICustomMemberProvider, OpcLabs.BaseLib.ComTypes._Info, OpcLabs.BaseLib.ComTypes._Object2, OpcLabs.BaseLib.OperationModel.ComTypes._OperationArguments, System.ICloneable, System.Runtime.Serialization.ISerializable, System.Xml.Serialization.IXmlSerializable  
Remarks

 

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 subscribe to changes of multiple metrics and display each change, identifying the different
// subscriptions by an integer.
//
// In order to publish or observe messages for this example, start the SparkplugEdgeNodeConsoleDemo program first.
//
// 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;
using OpcLabs.EasySparkplug.OperationModel;

namespace SparkplugDocExamples.Consumer._EasySparkplugConsumer
{
    class SubscribeMetric
    {
        public static void StateAsInteger()
        {
            // Note that the default port for the "mqtt" scheme is 1883.
            var hostDescriptor = new SparkplugHostDescriptor("mqtt://localhost");

            // Instantiate the consumer object and hook events.
            var consumer = new EasySparkplugConsumer();
            consumer.MetricNotification += consumer_StateAsInteger_MetricNotification;

            Console.WriteLine("Subscribing...");
            consumer.SubscribeMetric(
                new EasySparkplugMetricSubscriptionArguments(SparkplugComponentTypes.EdgeNode, hostDescriptor, 
                    "easyGroup", "easySparkplugDemo", "", "Random", null)
                    { State = 1 });    // An integer we have chosen to identify the subscription
            consumer.SubscribeMetric(
                new EasySparkplugMetricSubscriptionArguments(SparkplugComponentTypes.EdgeNode, hostDescriptor,
                        "easyGroup", "easySparkplugDemo", "", "Simple", null)
                    { State = 2 });    // An integer we have chosen to identify the subscription
            consumer.SubscribeMetric(
                new EasySparkplugMetricSubscriptionArguments(SparkplugComponentTypes.EdgeNode, hostDescriptor,
                        "easyGroup", "easySparkplugDemo", "", "Ramp", null)
                    { State = 3 });    // An integer we have chosen to identify the subscription

            Console.WriteLine("Processing notifications for 20 seconds...");
            System.Threading.Thread.Sleep(20 * 1000);

            Console.WriteLine("Unsubscribing...");
            consumer.UnsubscribeAllMetrics();

            Console.WriteLine("Waiting for 5 seconds...");
            System.Threading.Thread.Sleep(5 * 1000);

            Console.WriteLine("Finished.");
        }


        static void consumer_StateAsInteger_MetricNotification(object sender, EasySparkplugMetricNotificationEventArgs eventArgs)
        {
            // Obtain the integer state we have passed in.
            // Note that the metric name also comes with the notification and can be used to determine which metric the
            // notification relates to. The reason we are using the state is that it allows to pass in the information that
            // your application understands immediately, and is thus more efficient.
            var stateAsInteger = (int)eventArgs.Arguments.State;

            // Display the data.
            if (eventArgs.Succeeded)
            {
                if (eventArgs.HasData)
                    Console.WriteLine($"{stateAsInteger}: {eventArgs.MetricData}");
            }
            else
                Console.WriteLine($"{stateAsInteger} *** Failure: {eventArgs.ErrorMessageBrief}");
        }
    }
}
' This example shows how to subscribe to changes of multiple metrics and display each change, identifying the different
' subscriptions by an integer.
'
' In order to publish or observe messages for this example, start the SparkplugEdgeNodeConsoleDemo program first.
'
' 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
Imports OpcLabs.EasySparkplug.OperationModel

Namespace Global.SparkplugDocExamples.Consumer._EasySparkplugConsumer
    Class SubscribeMetric
        Public Shared Sub StateAsInteger()
            ' Note that the default port for the "mqtt" scheme is 1883.
            Dim hostDescriptor = New SparkplugHostDescriptor("mqtt://localhost")

            ' Instantiate the consumer object and hook events.
            Dim consumer = New EasySparkplugConsumer()
            AddHandler consumer.MetricNotification, AddressOf consumer_StateAsInteger_MetricNotification

            Console.WriteLine("Subscribing...")
            consumer.SubscribeMetric(
                New EasySparkplugMetricSubscriptionArguments(SparkplugComponentTypes.EdgeNode, hostDescriptor,
                    "easyGroup", "easySparkplugDemo", "", "Random", Nothing) With
                    {.State = 1}) ' An integer we have chosen to identify the subscription
            consumer.SubscribeMetric(
                New EasySparkplugMetricSubscriptionArguments(SparkplugComponentTypes.EdgeNode, hostDescriptor,
                    "easyGroup", "easySparkplugDemo", "", "Simple", Nothing) With
                    {.State = 2}) ' An integer we have chosen to identify the subscription
            consumer.SubscribeMetric(
                New EasySparkplugMetricSubscriptionArguments(SparkplugComponentTypes.EdgeNode, hostDescriptor,
                    "easyGroup", "easySparkplugDemo", "", "Ramp", Nothing) With
                    {.State = 3}) ' An integer we have chosen to identify the subscription

            Console.WriteLine("Processing notifications for 20 seconds...")
            Threading.Thread.Sleep(20 * 1000)

            Console.WriteLine("Unsubscribing...")
            consumer.UnsubscribeAllMetrics()

            Console.WriteLine("Waiting for 5 seconds...")
            Threading.Thread.Sleep(5 * 1000)

            Console.WriteLine("Finished.")
        End Sub

        Private Shared Sub consumer_StateAsInteger_MetricNotification(ByVal sender As Object, ByVal eventArgs As EasySparkplugMetricNotificationEventArgs)
            ' Obtain the integer state we have passed in.
            ' Note that the metric name also comes with the notification and can be used to determine which metric the
            ' notification relates to. The reason we are using the state is that it allows to pass in the information that
            ' your application understands immediately, and is thus more efficient.
            Dim stateAsInteger = CInt(eventArgs.Arguments.State)

            ' Display the data.
            If (eventArgs.Succeeded) Then
                If (eventArgs.HasData) Then
                    Console.WriteLine($"{stateAsInteger}: {eventArgs.MetricData}")
                End If
            Else
                Console.WriteLine($"{stateAsInteger} *** Failure: {eventArgs.ErrorMessageBrief}")
            End If
        End Sub
    End Class
End Namespace
Inheritance Hierarchy

System.Object
   OpcLabs.BaseLib.Object2
      OpcLabs.BaseLib.Info
         OpcLabs.BaseLib.OperationModel.OperationArguments
            OpcLabs.EasySparkplug.OperationModel.SparkplugArguments
               OpcLabs.EasySparkplug.OperationModel.SparkplugSubscriptionArguments
                  OpcLabs.EasySparkplug.OperationModel.SparkplugMetricSubscriptionArguments
                     OpcLabs.EasySparkplug.OperationModel.EasySparkplugMetricSubscriptionArguments

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