This lesson demonstrates how to add Event support to a Server.
We already created a custom EventType in Lesson 2 having two properties, “Temperature” and “State” (see Figure 5.1). Now we will implement the generation of Events.
Figure 5.1: ControllerEventType
When creating the instances, we have to create an Event hierarchy defining nodes that can be used to subscribe for Events. This should be done by specifying the NotifierParent property in CreateObjectSettings.
From Server Object to Controller Folder:
CreateObjectSettings settings = new CreateObjectSettings()
{
ParentNodeId = ObjectIds.ObjectsFolder,
RequestedNodeId =
new NodeId(
"Controllers", InstanceNamespaceIndex),
TypeDefinitionId = ObjectTypeIds.FolderType,
NotifierParent = ObjectIds.Server
};
CreateObject(
Server.DefaultRequestContext, settings);
From Controller Folder to Controller (i.e. the Event source):
settings = new CreateObjectSettings()
{
ParentNodeId =
new NodeId(
"Controllers", InstanceNamespaceIndex),
RequestedNodeId =
new NodeId(block.Name, InstanceNamespaceIndex),
TypeDefinitionId = typeDefinitionId,
NotifierParent =
new NodeId(
"Controllers", InstanceNamespaceIndex)
};
CreateObject(
Server.DefaultRequestContext, settings);
Then we register an event handler at our sample system:
m_system.BlockStateChanged += new BlockStateChangedEventHandler(UnderlyingSystem_BlockStateChanged);
Finally we report the Event. The class GenericEvent stores the Event data. The Initialize method sets the common Event fields. Using the method Set we can set a field defined at our custom EventType. ReportEvent reports the Event to the SDK.
private void UnderlyingSystem_BlockStateChanged(int blockAddress, string blockName, int state)
{
GenericEvent e =
new GenericEvent(
Server.FilterManager);
e.Initialize(
null,
new NodeId(yourorganisation.BA.ObjectTypes.ControllerEventType, TypeNamespaceIndex),
new NodeId(blockName, InstanceNamespaceIndex),
blockName,
"A controller state changed.");
e.Set(e.ToPath(
new QualifiedName(yourorganisation.BA.BrowseNames.State, TypeNamespaceIndex)), state);
ReportEvent(e.SourceNode, e);
}
To test the implementation, open an Event View in UaExpert (choose Document → Add… from the menu). Then drag and drop the Server object to the Event View. To trigger an Event, call the methods Start oand Stop on on a controller object (see Figure 5.2).
Figure 5.2: Events in UaExpert