Connectivity Software User's Guide and Reference
OnWrite Method (EasySparkplugEdgeNodeCore)
Example 



OpcLabs.EasySparkplug Assembly > OpcLabs.EasySparkplug Namespace > EasySparkplugEdgeNodeCore Class : OnWrite Method
The event arguments.

The value of this parameter cannot be null (Nothing in Visual Basic).

Raises the Write event.
Syntax
'Declaration
 
Protected Overridable Sub OnWrite( _
   ByVal eventArgs As SparkplugMetricWriteEventArgs _
) 
'Usage
 
Dim instance As EasySparkplugEdgeNodeCore
Dim eventArgs As SparkplugMetricWriteEventArgs
 
instance.OnWrite(eventArgs)
protected virtual void OnWrite( 
   SparkplugMetricWriteEventArgs eventArgs
)
protected:
virtual void OnWrite( 
   SparkplugMetricWriteEventArgs^ eventArgs
) 

Parameters

eventArgs
The event arguments.

The value of this parameter cannot be null (Nothing in Visual Basic).

Exceptions
ExceptionDescription

A null reference (Nothing in Visual Basic) is passed to a method that does not accept it as a valid argument.

This is a usage error, i.e. it will never occur (the exception will not be thrown) in a correctly written program. Your code should not catch this exception.

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 implement writing to edge node metrics using a single overriden method.
//
// You can use any Sparkplug application, including our SparkplugCmd utility and the SparkplugApplicationConsoleDemo
// program, to subscribe to the edge node data. SparkplugCmd, or other capable Sparkplug application, can be used to write
// data into the metric.
//
// 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.EdgeNode._EasySparkplugEdgeNode
{
    class OnWrite
    {
        /// <summary>
        /// A sparkplug edge node, with specialized write behavior for its metrics.
        /// </summary>
        class EdgeNodeWithOnWrite : EasySparkplugEdgeNode
        {
            /// <summary>
            /// Processes the supplied Sparkplug write data.
            /// </summary>
            /// <param name="eventArgs">The event arguments.</param>
            protected override void OnWrite(SparkplugMetricWriteEventArgs eventArgs)
            {
                // Obtain the state associated with the metric that is being written.
                object state = eventArgs.Metric.State;

                // The state is null in metrics that we have not created, such as the "node rebirth" metric.
                if (state is null)
                    return;

                // Display the state on the console together with the new value.
                Console.WriteLine($"Metric {eventArgs.Metric.State}, value written: {eventArgs.Data.Value}");
            }
        }
        
        static public void Main1()
        {
            // Note that the default port for the "mqtt" scheme is 1883.
            var hostDescriptor = new SparkplugHostDescriptor("mqtt://localhost");

            // Instantiate our derived edge node object and hook events.
            var edgeNode = new EdgeNodeWithOnWrite
            {
                EdgeNodeId = "easySparkplugDemo",
                GroupId = "easyGroup",
                SystemDescriptor = hostDescriptor
            };
            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}");
            };

            // Create data variables in the folder. Distinguish them by their state.
            edgeNode.Add(new SparkplugMetric("MyMetric1").ValueType<int>().SetState(1));
            edgeNode.Add(new SparkplugMetric("MyMetric2").ValueType<int>().SetState(2));
            edgeNode.Add(new SparkplugMetric("MyMetric3").ValueType<int>().SetState(3));
            edgeNode.Add(new SparkplugMetric("MyMetric4").ValueType<int>().SetState(4));
            edgeNode.Add(new SparkplugMetric("MyMetric5").ValueType<int>().SetState(5));

            // 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 implement writing to edge node metrics using a single overriden method.
'
' You can use any Sparkplug application, including our SparkplugCmd utility and the SparkplugApplicationConsoleDemo
' program, to subscribe to the edge node data. SparkplugCmd, or other capable Sparkplug application, can be used to write
' data into the metric.
'
' 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.EdgeNode._EasySparkplugEdgeNode
    Class OnWrite
        ''' <summary>
        ''' A sparkplug edge node, with specialized write behavior for its metrics.
        ''' </summary>
        Class EdgeNodeWithOnWrite
            Inherits EasySparkplugEdgeNode

            ''' <summary>
            ''' Processes the supplied Sparkplug write data.
            ''' </summary>
            ''' <param name="eventArgs">The event arguments.</param>
            Protected Overrides Sub OnWrite(eventArgs As SparkplugMetricWriteEventArgs)
                ' Obtain the state associated with the metric that is being written.
                Dim state As Object = eventArgs.Metric.State

                ' The state is null in metrics that we have not created, such as the "node rebirth" metric.
                If state Is Nothing Then
                    Return
                End If

                ' Display the state on the console together with the new value.
                Console.WriteLine($"Metric {eventArgs.Metric.State}, value written: {eventArgs.Data.Value}")
            End Sub
        End Class

        Public Shared Sub Main1()
            ' Note that the default port for the "mqtt" scheme is 1883.
            Dim hostDescriptor = New SparkplugHostDescriptor("mqtt://localhost")

            ' Instantiate our derived edge node object and hook events.
            Dim edgeNode = New EdgeNodeWithOnWrite With
            {
                .EdgeNodeId = "easySparkplugDemo",
                .GroupId = "easyGroup",
                .SystemDescriptor = hostDescriptor
            }
            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

            ' Create metrics in the folder. Distinguish them by their state.
            edgeNode.Add(New SparkplugMetric("MyMetric1").ValueType(Of Integer)().SetState(1))
            edgeNode.Add(New SparkplugMetric("MyMetric2").ValueType(Of Integer)().SetState(2))
            edgeNode.Add(New SparkplugMetric("MyMetric3").ValueType(Of Integer)().SetState(3))
            edgeNode.Add(New SparkplugMetric("MyMetric4").ValueType(Of Integer)().SetState(4))
            edgeNode.Add(New SparkplugMetric("MyMetric5").ValueType(Of Integer)().SetState(5))

            ' 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
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