In the custom data provision model, you write an independently running code that assembles the payload to be published at the time of your choice, and then calls the PublishDataPayload Method on the edge node or device. Alternatively, you can update individual metric data in the edge node and device, and let Rapid Toolkit for Sparkplug assemble and publish the payload.
The main difference from the pull and push data provision models is in the fact that there is no periodic polling made by Rapid Toolkit for Sparkplug. The execution flow is fully under the control of your code.
The Custom Data Provision Model is selected by setting the PublishingInterval Property on the edge node or device to Timeout.Infinite (-1).
One approach to use in the custom data provision model is to take responsibility for assembling the Sparkplug payload to be published, when the edge node or device has data to be published, and then passing the assembled payload to Rapid Toolkit for Sparkplug. With this approach, your code does the following steps when it wants to publish a data payaload:
An example of this approach, together with code for processing publish errors, is here: Examples - Sparkplug Edge Node - Handle publishing errors.
If you do not want to repeat publishing values that have not changes, the approach described above requires your code to keep track of the values that were published, compare them with the new values, and only include in the payload the metrics whose values have changed. Rapid Toolkit for Sparkplug can help with this and make the code shorter and easier to understand, with support for reporting by exception.
Reporting by exception is turned on by setting the ReportByException Property on the edge node or device object to true. With it, your code then calls the UpdateReadData Method on the metric object (SparkplugMetric Class) whenever it has new data that it wants to publish for the metric. If the metric value has changes, Rapid Toolkit for Sparkplug then creates the payload with the metric data, and publishes it.
When this is done without additional measures, each metric change publishes a new Sparkplug payload with just one metric. Of course, in many cases this is not what you want, especially if the nature of your edge node/device is such that it collects data for multiple metrics together, and also wants to publish them together, in a single Sparkplug payload. To group the metric updates together, and let Rapid Toolkit for Sparkplug assemble the payload containing all metrics that have changes, your code needs to lock the publishing before it starts updating the metric data, and unlock the publishing when it is done with updating. When the publishing is unlocked, if at least one metric has changed, Rapid Toolkit for Sparkplug wull publish the metrics that have changed, in a single Sparkplug payload.
The publishing is locked by calling the LockPublishing Method on the edge node or device. The publishing is unlocked by calling the UnlockPublishing Method on the same object. The calls to LockPublishing Method and UnlockPublishing Method must be paired under all conditions.
The following example shows the custom data provision model with reporting by exception, and with the use of locking an unlocking methods.
Making sure that calls to LockPublishing Method and UnlockPublishing Method are always paired in your code, including exceptional cases, is demanding and can be error prone. It is possible to make this pairing a little bit easier, using the Disposable Lock Object. This is an object that makes the locking when created, implements the IDisposable Interface, and makes the unlocking when disposed of (the IDisposable.Dispose method).
Many .NET languages have specialized code constructs to deal with IDisposable objects that assure that the object is properly disposed. These constructs, when properly used, make it easier to write code that guarantees that the methods calls are properly paired. In C#, the code construct is the using statement. In VB.NET, the code construct is a similar Using statement. In the end, using the disposable lock object does the same thing as calling the methods.
The following example shows the custom data provision model with reporting by exception, and with the use of the disposable lock object.
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.