Using Edge Extensions with the ThingWorx Edge C SDK > ThingWorx Edge SDK Extensions for the C SDK > Tasks for EdgeThingShape and EdgeThingTemplate Constructors
Tasks for EdgeThingShape and EdgeThingTemplate Constructors
Up until now, you have learned what constructor functions exist for Edge Extensions. Now you will learn how to implement one. Below is an example of how to implement an EdgeThingShape (ETS) with a few properties and a service using the C SDK. The sample ETS adds a string property and a number property to any Thing to which it is applied. Note that global variables are used to store property values.
* 
This usage is not a best practice for ETS construction and is intended as an example to show how the core API can be used to construct an ETS.

#include "twShapes.h"

char propertyAValueBuffer[256];
double propertyBValue;

enum msgCodeEnum propertyHandler(const char * entityName,
const char * propertyName,
twInfoTable ** value,
char isWrite, void * userdata) {
TW_LOG(TW_TRACE,"propertyHandler - Function called for Entity %s,
Property %s", entityName, propertyName);
if (value) {
if (isWrite && *value) {
/* Property Writes */
if (strcmp(propertyName, "propertyB") == 0) {
twInfoTable_GetNumber(*value, propertyName, 0, &propertyBValue);
}
else if (strcmp(propertyName, "propertyA") == 0) {
char * propertyAValue;
twInfoTable_GetString(*value, propertyName, 0, &propertyAValue);
TW_LOG(TW_INFO,"Got propertyAValue. Value:\n%s", propertyAValue);
strcmp(propertyAValue,propertyAValueBuffer);
TW_FREE(propertyAValue);
}
else return TWX_NOT_FOUND;
return TWX_SUCCESS;
} else {
/* Property Reads */
if (strcmp(propertyName, "propertyB") == 0) {
*value = twInfoTable_CreateFromNumber(propertyName, propertyBValue);
}
else if (strcmp(propertyName, "propertyA") == 0) {
*value = twInfoTable_CreateFromString(propertyName, propertyAValueBuffer, TRUE);
}
else return TWX_NOT_FOUND
return TWX_SUCCESS;
}
else {
/* Property Reads */
if (strcmp (propertyName, "propertyB"_== 0) (
*value = twInfoTable_CreateFromNumber(properptyName, propertyBValue);
)
else if (strcmp(propertyName, "propertyA") -- 0)(
*value = twInfoTable_CreateFromString(propertyName, propertyAValueBuffer, TRUE);
)
else return TWX_NOT_FOUND;
)
return TWX_SUCCESS;
)
else (
TW_LOG(TW_ERROR,"propertyHandler - NULL pointer for value");
return TWX_BAD_REQUEST;
}
}


void constructSimpleShape(char* thingName){
twExt_Api_RegisterProperty(TW_THING, thingName, "propertyA",
TW_STRING, "A Simple String Property", PUSH_TYPE_ALWAYS,
REPORT_ALL_CHANGES, propertyHandler, NO_USER_DATA);
twExt_Api_RegisterProperty(TW_THING, thingName, "propertyB",
TW_STRING, "A Simple Number Property", PUSH_TYPE_ALWAYS,
REPORT_ALL_CHANGES, propertyHandler, NO_USER_DATA);
}

void* init_simpleshapelib(){
twExt_RegisterShape("SimpleShape", constructSimpleShape);
}
Once the ETS is constructed, a Thing can be constructed using this Thing Shape and linking it statically in the following manner:

int main(void) {
init_simpleshapelib();
twExt_CreateThingFromTemplate("myNewThing",TW_THING_TEMPLATE_GENERIC,"SimpleShape",NULL);
}
This example does not create a client connection and bind the Thing. It is meant to show how to construct a Thing using a single ETS.
Notes on Creating an Edge Thing Template (ETT) Constructor Function
Everything discussed above for creating an EdgeThingShape (ETS) constructor also applies to creating your own EdgeThingTemplate (ETT) except for the following changes:
An ETT must call its parent template constructor as the first act it performs.
An ETT constructor function must call:
void twExt_InheritFromTemplate(char *thingName, char *templateNameToInherit);
If you are basing your template on the GenericEdgeThingTemplate, this call looks like this:
void twExt_InheritFromTemplate(thingName,TW_THING_TEMPLATE_GENERIC);
You must also call twExt_RegisterTemplate() instead of twExt_RegisterShape() in your equivalent to init_simpleshapelib().
Was this helpful?