Property Change Listeners
When using TW_SET_PROPERTY() and twExt_GenericPropertyHandler(), which is a C SDK edge extension, for your property handler, you can take advantage of the Observer pattern. You can declare C functions that will be notified when the value of a property has changed, either on the server or locally as the result of a TW_SET_PROPERTY() command. To do this, use the following set of C SDK functions:
void twExt_AddPropertyChangeListener(char* entityName, char* propertyName,
propertyChangeListenerFunction PropertyChangeListenerFunction);
void twExt_RemovePropertyChangeListener
(propertyChangeListenerFunction PropertyChangeListenerFunction);
By calling twExt_AddPropertyChangeListener(), you can designate a function with the parameters shown below. This function is called when a property on the Thing specified in the entityName parameter changes. If propertyName is set to NULL or the constant TW_OBSERVE_ALL_PROPERTIES, this function is notified on all property changes. If a specific property is passed in the propertyName field, the function is called only if that property changes. Here is an example:
void simplePropertyObserver(const char * entityName,
const char * thingName,twPrimitive* newValue){
printf("My Value has changed\n");
}

void test_simplePropertyChangeListener() {
{
TW_MAKE_THING("observedThing",TW_THING_TEMPLATE_GENERIC);
TW_PROPERTY("TotalFlow", TW_NO_DESCRIPTION, TW_NUMBER);
}

twExt_AddPropertyChangeListener("observedThing",TW
_OBSERVE_ALL_PROPERTIES,simplePropertyObserver);
TW_SET_PROPERTY("observedThing","TotalFlow",TW_MAKE_NUMBER(50));
twExt_RemovePropertyChangeListener(simplePropertyObserver);
}
twExt_AddPropertyChangeListener() can be called multiple times, allowing multiple observers to be attached to a single Thing. twRemovePropertyChangeListener() removes a function as a change listener.
Was this helpful?