OnSubscription Payload
- First of all, make sure that the FESA-device you want to subscribe to is listed in the FESA-DB ! More info about this can be found here .
- Lets assume the FESA-class you want to Subscribe to is "ClassB" and the class which establishes the subscription is "ClassA".
- Furter lets assume you want to subscribe to the Property "PropertyB" which has the value-items: "ValueItemB1" and "ValueItemB2" which are scalar.
- In order to use this code-snippet, you need to replace all occurences of "ClassA", "ClassB", "PropertyB", "ValueItemB1" and "ValueItemB2" with your concrete names.
// In the RTAction of ClassA which receives the onSubscriptionEvent, add these line to the includes:
#include <ClassB/GeneratedCode/PropertyData.h>
#include <fesa-core/RealTime/OnSubscriptionRTEventPayload.h>
...
// Use the following code in the execute-method
// extract the payload from the RTEvent
const OnSubscriptionRTEventPayload* payload = dynamic_cast<const OnSubscriptionRTEventPayload*> (pEvt->getPayload().get());
std::auto_ptr<const ClassB::PropertyBPropertyData> propData;
try
{
// extract the right data-format from the payload
propData = fesa::OnSubscriptionRTEventPayload::extract<ClassB::PropertyBPropertyData>(*pEvt);
}
catch (const std::exception& ex)
{
throw FesaException(__FILE__, __LINE__, "Payload extraction to type PropertyBPropertyData failed");
}
// probably this check is not needed, however it will not hurt either
if (payload == NULL)
{
throw FesaException(__FILE__, __LINE__, "Payload is not of type OnSubscriptionRTEventPayload");
}
// This snippet only couvers the UpdateType::NORMAL
// Other possible update-types are: UpdateType::FIRST(first update of a new subscription) and UpdateType::IMMEDIATE
if (payload->getData().getUpdateType() != UpdateType::NORMAL)
{
std::ostringstream error;
error << "Notification was not of type NORMAL but " << payload->getData().getUpdateType();
throw FesaException(__FILE__, __LINE__, error.str());
}
// Perform different operations on the data-container "propData". E.g. get the data of the received value-items
// Depending on the type of the value item, the methods of propData look different.
// Take a look at "ClassB/GeneratedCode/PropertyData.h" for details of the API.
std::cout << "Event received. Event-data: " << std::endl;
std::cout << "ValueItemB1: " << propData->getValueItemB1() << std::endl;
std::cout << "ValueItemB1: " << propData->getValueItemB2() << std::endl;