Retrieve object fom UaModeler instances

Questions regarding the use of the .NET SDK 2.0 for Server or Client development or integration into customer products ...

Moderator: uasdknet

Post Reply
SirTobi
Full Member
Full Member
Posts: 7
Joined: 20 Dec 2023, 14:05

Retrieve object fom UaModeler instances

Post by SirTobi »

Hello,

With the UaModeler I am able to create object instances and export these into an XML file. If I load this XML file in the UaServer with ImportUaNodeset than the server will create all the instances and objects which were created with the UaModeler.

The UaServer has the method BindModel which helps me to create C# classes and these are connected to the OPC-UA Nodes.

I really like the BindModel approach because it is very easy to use and helps with unit testing. Albeit I am struggeling with creating the required nodes/objects and variables.

I tried to use both approaches togehter: Create the nodes in UaModeler and instanciate a c# model and use it with the BindModel method. But now I have two instances of the nodes and every event is raised twice.

Is there a way to let the UaModeler create the nodes and instances and retreive/cast them to the generated C# classes?

User avatar
Support Team
Hero Member
Hero Member
Posts: 3072
Joined: 18 Mar 2011, 15:09

Re: Retrieve object fom UaModeler instances

Post by Support Team »

Hi,
There are two approaches with BindModel and UA object instances:

1) The first approach is that you create an instance in XML nodeset file (via the UaModeler) or that you create an instance in code with the CreateObject() method. To begin with, these Ua instances have nothing to do with the BindModel. The BindModel only serves to breathe life into them, i.e. to implement the business logic. For this purpose, a C# instance of the model class is created for the UA instance. Using BindModel, all value changes are mirrored into the nodes and method calls and write requests are redirected to the model. But to stress that again, the instance nodes are present even without BindModel, the server does not have a (hidden) C# instance for them.

2) The second approach is to create UA objects (or variables) dynamically with BindModel, i.e., the model implements the INotifyCollectionChanged and IEnumerable interface. In that case the nodes and references belonging to that UA instance are generated by the BindModel mechanism. After the creation of the UA instance, the C# model is bond to the created nodes.

Regarding your statment:
But now I have two instances of the nodes and every event is raised twice.
Which events are raised twice? Are you talking about C# events or are you talking about UA events?

If you have two UA instances, created in the nodeset file. Let's call them Heater1 and Heater2.
Than you would create a C# model instance for each UA instance.
The code could look similar to this:

Code: Select all

NodeId id1 = ObjectIds.Heater1.ToNodeId(Server.NamespaceUris);
var model1 = new HeaterModel();
Server.RootNodeManager.BindModel(id1, model1, CreateInstanceSettingsFor, sc);

NodeId id2 = ObjectIds.Heater2.ToNodeId(Server.NamespaceUris);
var model2 = new HeaterModel();
Server.RootNodeManager.BindModel(id2, model2, CreateInstanceSettingsFor, sc);
Best regards
Unified Automation Support Team

SirTobi
Full Member
Full Member
Posts: 7
Joined: 20 Dec 2023, 14:05

Re: Retrieve object fom UaModeler instances

Post by SirTobi »

Hello,

thank you for your reply.

I found two problems on my side:
The BindModel was introduced with the Sdk 3.4.0 and my project in the UaModeler was still targeting the version 3.2.0. Therefore the BindModel mechanism didn't work. Now I changed it and it works.

The fault with the events was the following:
In the event view I was observing the events of two objects: My custom object and the server object.
However both objects were connected because the server object had a HasNotifier reference to my object. Therefore when my object emited the event it was shown in the view because of my object and a second time because of the server.


Currently I have another issue:

I am using the FileModel to make a file available via OPC-UA. From time to time the content of this file should be replaced.
Currently I'm overriding the file on the file system and I create a new FileModel with the updated file. However in the UaExpert the Size property does not change. When I'm saving the file via UaExpert and the transfer has finished then the Size property is updated.
I'm using the BindModel way to connect the C# class to OPC-UA.
Do I have to emit something for the UaServer to notice a change in the file size?

User avatar
Support Team
Hero Member
Hero Member
Posts: 3072
Joined: 18 Mar 2011, 15:09

Re: Retrieve object fom UaModeler instances

Post by Support Team »

Hi,

good to hear that you found the mistake. Using the correct version of UaModeler-Template is a good hint. The event hierarchy should be modeled as a strict hierarchy to avoid two references ending in the same parent, hence creating double event.

Your second question is a new topic. Please create new thread here in forum, so we can mark this one as solved.
Best regards
Unified Automation Support Team

Post Reply