UaModeler
HowTo: Create a Server supporting Structures and Events

This tutorial describes how to support structures and events on the server side.

Step 1: Create a New Project

Create a new project as described in steps 1–5 of HowTo: Create a New UaModeler Project With a Method first.

Step 2: Add a Structured DataType

Add a new structured DataType called “Vector” as described in Step 5: Add a Structured Data Type of HowTo Create a New Project With a Structured Data Type.

Step 3: Add an ObjectType

Add a new ObjectType called MyObjectType to your project (see screenshot).

new_objecttype_net.png

Add the following children (see screenshot):

  • Variable Count, DataType UInt32
  • Variable Position, DataType Vector
  • Method Move
howto_se_add_objecttype.png

Add input and output arguments to the method Move (see screenshot):

  • input argument: Direction, DataType Vector
  • output argument: NewPosition, DataType Vector
howto_se_add_method_arguments.png

Step 4: Add an EventType

Add a new EventType called MyEventType (see screenshot).

new_eventtype_net.png

Add the following variables to the new EventType (see screenshot):

  • Variable Count, DataType UInt32
  • Variable OldPosition, DataType Vector
  • Variable NewPosition, DataType Vector
howto_se_eventtype_children.png

Step 5: Add a GeneratesEvent Reference

Select MyObjectType in the Information Model Window and add a new GeneratesEvent reference: Choose NonHierarchicalReferences → GeneratesEvent as Reference Type and MyEventType as Target (see screenshot). This reference has only semantic use.

howto_se_generatesevent_ref.png

Step 6: Generate Code

Generate code for your project as shown in Step 9: Generating Code of HowTo: Create a New UaModeler Project With a Method.

Step 7: Create an Object Instance

Open the project file created in Step 6: Generate Code.

First we’re creating an instance of MyObjectType in the Startup method of the generated NodeManager (in our case in the file ExampleModelNsNodeManager.cs).

// Create instance
CreateObjectSettings settings = new CreateObjectSettings()
{
BrowseName = new QualifiedName("MyObject", DefaultNamespaceIndex),
EventNotifier = EventNotifiers.SubscribeToEvents,
NotifierParent = UnifiedAutomation.UaBase.ObjectIds.Server,
ParentNodeId = UnifiedAutomation.UaBase.ObjectIds.ObjectsFolder,
ReferenceTypeId = UnifiedAutomation.UaBase.ReferenceTypeIds.Organizes,
RequestedNodeId = new NodeId("MyObject", DefaultNamespaceIndex),
TypeDefinitionId = new NodeId(ObjectTypes.MyObjectType, DefaultNamespaceIndex)
};
ObjectNode node = CreateObject(Server.DefaultRequestContext, settings);

Step 8: LinkModelToNode

Add the following code to the Startup method:

// Model
MyObjectModel model = new MyObjectModel();
LinkModelToNode(node.NodeId, model, null, null, 500);
// Set UserData to model for usage in method implemenation
model.UserData = node;

Step 9: Initial Values

Set initial values for Count and Position in the Startup method:

// Initial value
model.Count = 0;
model.Position = new Vector();

Step 10: Implement Method and Fire Event

In addition, it is necessary to implement the method Move. The generated code for methods can be found in the file MyObjectTypeMethods.cs.

Replace the generated code with the following snippet. The initial Position is moved in Direction, Count is increased, and an event is fired.

MyEventModel eventModel = new MyEventModel()
{
OldPosition = model.Position
};
// Method output argument
NewPosition = new Vector()
{
X = model.Position.X + Direction.X,
Y = model.Position.Y + Direction.Y,
Z = model.Position.Z + Direction.Z
};
// Set Variable
model.Position = NewPosition;
model.Count++;
// Fire Event
eventModel.Count = model.Count;
eventModel.NewPosition = model.Position;
GenericEvent e = eventModel.CreateEvent(context.Server.FilterManager);
e.Initialize(
null,
ObjectTypeIds.MyEventType,
(model.UserData as Node).NodeId,
(model.UserData as Node).DisplayName.Text,
EventSeverity.Medium,
"Object moved"
);
context.Server.ReportEvent(e);
return StatusCodes.Good;

Step 11: Trigger and Receive Events with UaExpert

Compile and run the server, then connect to the server with UaExpert.

To receive Events, choose Document → Add... → Event View from the menu and drag the Server object to the Event View (see screenshot).

howto_se_drag_and_drop_server_obj.png

Expand the tree and select the event fields of MyEventType (see screenshot).

howto_se_select_event_fields.png

Right-click on the method Move in the Address Space Window and select Call. Enter a Direction at Input Arguments and call the method. As the initial values for Position were (0|0|0), the NewPosition is identical to the method’s input argument (see screenshot).

howto_se_call_method.png

The method call triggers an event, which shows up in the respective window in the Event View. Select the event to view the values for each event field (see screenshot).

howto_event_details.png