| Rapid Toolkit for Sparkplug > Concepts > Developing Sparkplug Edge Nodes > Rapid Toolkit for Sparkplug Data Provision And Consumption Models > Rapid Toolkit for Sparkplug Pull Data Provision Model |
In the pull data provision model, you write a method that fulfills the requests for data ("Reads") by Rapid Toolkit for Sparkplug (polling for updates) by interrogating the underlying system, and returns the data that should be published to Sparkplug. Rapid Toolkit for Sparkplug calls your method periodically, and by doing so, it pulls the data.
This is the more common data provision model.
Your task as a developer is to provide the code for handling the Read request by either adding an event handler for the Read Event, or overriding the OnRead Method. The code for handling the data pull can be attached to each metric separately, or you can use a common code on the parent level (edge node or device) - for example, a Sparkplug device can have code that handles all Reads for the metrics in the device.
In order to indicate that you have handled the Read request, your code will call the HandleAndReturn Method on the event arguments object. Note that if you use the extension methods for configuration of metrics, the event handler added by the extension methods already does this for you.
The following picture illustrates how the pull data provision model works.

Only the data that have changed are included in the Sparkplug payload and published. This logic is implemented internally in Rapid Toolkit for Sparkplug.
Rapid Toolkit for Sparkplug provides extension methods that allow you to define metrics that use the pull data provision model easily, without having to deal with method overrides, or handle events. This is described in the Sparkplug Metric Configuration article. Typically, you will use some overload of the ReadFunction or ReadValueFunction method to configure the metric for the pull data provision model. With these methods, you use .NET functions (Func Delegate) to specify the code that handles the request. The following example illustrates the use of the ReadValueFunction method.
// This example shows how to create a metric and implement reading its value using a function. // // 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 OpcLabs.EasySparkplug; using System; namespace SparkplugDocExamples.EdgeNode._SparkplugMetric { partial class ReadValueFunction { 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 and hook events. var edgeNode = new EasySparkplugEdgeNode(hostDescriptor, "easyGroup", "easySparkplugDemo"); 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 a metric, defining its read values by a function. // The type of the metric (Int32, in this case) is inferred from the type returned by the function. var random = new Random(); edgeNode.Add(new SparkplugMetric("ReadThisMetric").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 create a metric and implement reading its value using a function. ' ' 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._SparkplugMetric Partial Class ReadValueFunction 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 and hook events. Dim edgeNode = New EasySparkplugEdgeNode(hostDescriptor, "easyGroup", "easySparkplugDemo") 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 a metric, defining its read values by a function. ' The type of the metric (Int32, in this case) is inferred from the type returned by the function. Dim random = New Random() edgeNode.Add(New SparkplugMetric("ReadThisMetric").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
How do you choose between these extension methods?
The following example illustrates the use of the ReadFunction method for returning data with a timestamp specified in your code.
// This example shows how to create a metric and implement reading its data using a function. // // 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 OpcLabs.EasySparkplug; using System; using OpcLabs.EasySparkplug.Generic; namespace SparkplugDocExamples.EdgeNode._SparkplugMetric { class ReadFunction { 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 and hook events. var edgeNode = new EasySparkplugEdgeNode(hostDescriptor, "easyGroup", "easySparkplugDemo"); 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 a metric, defining its attribute data (including timestamp) by a function. // The type of the metric (Int32, in this case) is inferred from the type returned by the function. var random = new Random(); edgeNode.Add(new SparkplugMetric("ReadThisMetric").ReadFunction(() => new SparkplugData<int>( random.Next(), DateTime.UtcNow.AddMinutes(-10)))); // 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 create a metric and implement reading its data using a function. ' ' 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 Imports OpcLabs.EasySparkplug.Generic Namespace Global.SparkplugDocExamples.EdgeNode._SparkplugMetric Class ReadFunction 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 and hook events. Dim edgeNode = New EasySparkplugEdgeNode(hostDescriptor, "easyGroup", "easySparkplugDemo") 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 a metric, defining its attribute data (including timestamp) by a function. ' Create a read-only data metric. Dim random = New Random() edgeNode.Add(New SparkplugMetric("ReadThisMetric").ReadFunction(Function() New SparkplugData(Of Integer)( random.Next(), DateTime.UtcNow.AddMinutes(-10)))) ' 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
You can configure how often Rapid Toolkit for Sparkplug polls for the data, using the PublishingInterval Property on the edge node and each device.
The example below shows how to set a custom polling interval for edge node metrics.
// This example shows how to set the polling (publishing) interval for the metrics on the edge node. // // 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 PublishingInterval { 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 publishing interval, which also defines how often the metrics are polled. edgeNode.PublishingInterval = 3*1000; // 3 seconds // 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}"); }; // 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 set the polling (publishing) interval for the metrics on the edge node. ' ' 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 PublishingInterval 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 publishing interval, which also defines how often the metrics are polled. edgeNode.PublishingInterval = 3 * 1000 ' 3 seconds ' 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 ' 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
If your Sparkplug edge node or device has groups of metrics with basically the same behavior, you would probably want to specify the behavior just once, and not with every metric. In such case, you cannot use the extension methods for metric configuration (described above), and need to define the behavior by handling the Read Event, or overriding the OnRead Method, on the parent level - in our example, the level of the edge node. The following examples illustrate these approaches.
// This example shows how to implement reading of different edge node metrics using a single handler. // // 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; using OpcLabs.EasySparkplug.OperationModel; namespace SparkplugDocExamples.EdgeNode._EasySparkplugEdgeNode { class Read { 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 and hook events. var edgeNode = new EasySparkplugEdgeNode(hostDescriptor, "easyGroup", "easySparkplugDemo"); 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 metrics 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)); // Handle the read event for the edge node. edgeNode.Read += EdgeNodeOnRead; // 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."); } /// <summary> /// Event handler for the read event on the edge node. /// </summary> /// <param name="sender">The edge node object that sends the event.</param> /// <param name="eventArgs">Data for the metric read event.</param> static private void EdgeNodeOnRead(object sender, SparkplugMetricReadEventArgs eventArgs) { // Obtain the state associated with the metric that is being read. 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; // Use the state as the offset for the random value, so that each metric generates values in a unique range. int offset = (int)state * 100; // Generate a random value, indicate that the read has been handled, and return the generated value. eventArgs.HandleAndReturn(Random.Next(offset, offset + 100)); } static private readonly Random Random = new Random(); } }
' This example shows how to implement reading of different edge node metrics using a single handler. ' ' 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 Imports OpcLabs.EasySparkplug.OperationModel Namespace Global.SparkplugDocExamples.EdgeNode._EasySparkplugEdgeNode Class Read 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") 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)) ' Handle the read event for the edge node. AddHandler edgeNode.Read, AddressOf EdgeNodeOnRead ' 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 ''' <summary> ''' Event handler for the read event on the edge node. ''' </summary> ''' <param name="sender">The edge node object that sends the event.</param> ''' <param name="eventArgs">Data for the metric read event.</param> Private Shared Sub EdgeNodeOnRead(ByVal sender As Object, ByVal eventArgs As SparkplugMetricReadEventArgs) ' Obtain the state associated with the metric that is being read. 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 ' Use the state as the offset for the random value, so that each metric generates values in a unique range. Dim offset As Integer = CInt(state * 100) ' Generate a random value, indicate that the read has been handled, and return the generated value. eventArgs.HandleAndReturn(Random.Next(offset, offset + 100)) End Sub Private Shared ReadOnly Random As Random = New Random() End Class End Namespace
// This example shows how to implement reading from 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. // // 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 OnRead { /// <summary> /// A sparkplug edge node, with specialized read behavior for its metrics. /// </summary> class EdgeNodeWithOnRead : EasySparkplugEdgeNode { /// <summary> /// Obtains the data for Sparkplug read. /// </summary> /// <param name="eventArgs">The event arguments.</param> protected override void OnRead(SparkplugMetricReadEventArgs eventArgs) { // Obtain the state associated with the metric that is being read. 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; // Use the state as the offset for the random value, so that each metric generates values in a unique range. int offset = (int)state * 100; // Generate a random value, indicate that the read has been handled, and return the generated value. eventArgs.HandleAndReturn(Random.Next(offset, offset + 100)); } static private readonly Random Random = new Random(); } 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 EdgeNodeWithOnRead { 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 metrics 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 reading from 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. ' ' 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 OnRead ''' <summary> ''' A sparkplug edge node, with specialized read behavior for its metrics. ''' </summary> Class EdgeNodeWithOnRead Inherits EasySparkplugEdgeNode ''' <summary> ''' Obtains the data for Sparkplug read. ''' </summary> ''' <param name="eventArgs">The event arguments.</param> Protected Overrides Sub OnRead(eventArgs As SparkplugMetricReadEventArgs) ' Obtain the state associated with the metric that is being read. Dim state As Object = eventArgs.Metric.State ' Use the state as the offset for the random value, so that each metric generates values in a unique range. Dim offset As Integer = CInt(state * 100) ' Generate a random value, indicate that the read has been handled, and return the generated value. eventArgs.HandleAndReturn(Random.Next(offset, offset + 100)) End Sub Private Shared ReadOnly Random As Random = New Random() 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 EdgeNodeWithOnRead 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
See Sparkplug Metric Data Type Considerations.
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.
Copyright © 2004-2025 CODE Consulting and Development, s.r.o., Plzen. All rights reserved. Web page: www.opclabs.com
Documentation Home, Send Feedback. Resources: Knowledge Base, Product Downloads. Technical support: Online Forums, FAQ.Missing some example? Ask us for it on our Online Forums! You do not have to own a commercial license in order to use Online Forums, and we reply to every post.