|
After having implemented the custom ExampleUnit, this section will explain how to implement the matching observer, ExampleUnitObserver. An observer consists of three parts:
The following sections will discuss these steps in detail.
The ExampleUnitObserverInterface is the interface that will be implemented by the observer component. This interface is only needed, because an observer needs to implement at least two interfaces: The general observer interface and the interface of the units, it wants to listen to. In this case the observer-interface inherits from ObserverInterface and ExampleUnitListener.
Both interfaces that ExampleUnitObserverInterface inherits need to be implmeneted in the observer-component:
In the observer-initialization process, determined by the methods onInitObserver and onConnectObserver, the observer defines the resources it requires and produces. The observer calls usingTopic to subscribe to a specific topic. The system takes care of postponing the call of onConnectObserver until the respective topic is found. After that the observer states which channels it will offer (offerChannel) and which data fields will be available in these channels (offerDataField). Each data field is a stream of data of a single type. In this case, the observer uses the data published under the topic "PeriodicExampleValue" and offers a channel called "exampleChannel". This channel consists of two data fields: "exampleDataFieldType" for the type of the value published by ExampleUnit and "exampleDataFieldInt" for the value, if it is an integer.
Finally, two ConditionChecks are offered. By calling offerConditionCheck, the observer states that its data fields allow the respective condition checks. The ConditionHandler takes care that conditions are only installed for the data types they are defined for.
The method reportPeriodicValue gets called whenever ExampleUnit publishes a new value. The value is forwarded to the two data fields by calling setDataField. A call to updateChannel starts the re-evaluation of all conditions installed on the respective channel.
The application that makes the observer executable is implemented in the same manner as the ExampleUnit-application:
As any other component, observers can define properties. This happens in the same manner as for units.
Now, two new components exist: A unit and an observer. Once both components are startetd, they start to interact with each other. This interaction can be examined using the ObserverView Gui Widget.