ANSI C Based OPC UA Client/Server SDK
1.8.3.398
|
The goal of this lesson is to show how to provide alarm conditions in the address space.
Files used in this lesson:
Figure 6-1 shows the new alarm type that we will create in this lesson.
Figure 6-1 TemperatureAlarmType
In this lesson, the machine will be extended to have an alarm that is active whenever the temperature sensor’s value is below or above certain limits. The alarm type will have the same event fields as the event type of Lesson 5: Adding Support for Events. For this purpose, CustomProvider_RegisterEventTypes is extended to create and register the new alarm type (see custom_provider_events.c).
In order to inform clients about the new alarm type, the respective nodes of the type and the event fields have to be created in the server’s address space. The code is similar as shown in the previous lesson.
The function CustomProvider_CreateMachine is used to create and initialize an instance of the new alarm type. In our case, the lifetime of the alarm is as long as the machine’s, so we will append the alarm to the user data of the machine to be able to work with it later (see custom_provider.c for the complete code):
The helper function CustomProvider_InitializeTemperatureAlarm is used to set reasonable initial values to all the event fields that won’t change during the lifetime of the alarm. For the event in the last lesson this was done inside CustomProvider_FireHeaterSwitchedEvent, as the event was fired and then deleted immediately; the TemperatureAlarm however is stored permanently with the Machine, this is why we use the custom initialization function CustomProvider_InitializeTemperatureAlarm (see custom_provider_events.c for the complete code).
In our example, we use the CustomProvider_SimulationTimerCallback function to check whether the alarm should be sent. A static variable is used to remember whether the alarm is currently active. Depending on this variable and the current temperature of the machine, the alarm is sent with either active or inactive state.
The convenience function CustomProvider_FireTemperatureAlarm is used to fill the dynamic event fields of the alarm and to fire it. Each time the alarm is fired, a new EventId is set and all timestamps are updated (see custom_provider_events.c for full code).
The EnabledState, Retain and ActiveState event fields are set depending on the state of the machine as well. This results in the alarm being displayed (Retain == true) only if the temperature is above or below the limits, otherwise the alarm is inactive and not displayed.
Finally the custom event fields SwitchPosition and CurrentTemperature are filled with current values and the alarm is fired:
The AcknowledgeableConditionType defines methods that can be called on its instances. Thus, we have to react on calls of this methods in the CallAsync callback method of the provider. The instance of the alarm the method was called on can easily be retrieved by the UaServer_Events_GetConditionByNodeId method provided by the SDK (see custom_provider_call.c for full code):
The following code shows how to retrieve the Machine instance that generated the alarm which currently gets acknowledged.
After implementing firing of alarms, we can test if they are received by a client.
For receiving events with UaExpert, create an Event View by clicking “Add Document”, selecting “Event View” and clicking “Add” (see Figure 6-2).
Figure 6-2 Create an Event View using UaExpert
Now you can drag and drop either the “Server” or the “MyCustomMachine” object into the Configuration section of the Event View. The client will then subscribe to events that are emitted by the according object.
If the temperature excesses 100 degrees or falls below 0 degrees, the machine’s TemperatureAlarm is fired and displayed in the “Alarms” tab of the Event View. When Toggling the switch and thus causing the temperature to change to a value between 0 and 100 degrees, the alarm will be disabled as the Retain event field is set to false. This is the signal for the client to not display the alarm anymore and it will disappear from the “Alarms” tab of the Event View (see Figure 6-3).
Figure 6-3 Receive Alarms using UaExpert