/* * Copyright (C) 2017 ThingWorx Inc. * * Sample Application of starting a connection to ThingWorx and * binding a single Thing. */ #include "twExt.h" #include "SteamThing.h" /* Server Defaults */ #define TW_HOST "baz.ngrok.io" #define TW_APP_KEY "5c59d79b-79e1-46de-afc7-f49f3552cce3" #define DATA_COLLECTION_RATE_MSEC 5000 #define DEFAULT_THING_NAME "SteamSensor" #define RETRY_COUNT 3 #if defined NO_TLS #define TW_PORT 80 #else #define TW_PORT 443 #endif void bindEventHandler(char *entityName, char isBound, void *userdata) { /* First NULL says "tell me about all things that are bound */ if (isBound) TW_LOG(TW_FORCE,"bindEventHandler: Entity %s was Bound", entityName); else TW_LOG(TW_FORCE,"bindEventHandler: Entity %s was Unbound", entityName); } void authEventHandler(char *credType, char *credValue, void *userdata) { /* Callbacks only when we have connected & authenticated */ if (!credType || !credValue) return; TW_LOG(TW_FORCE,"authEventHandler: Authenticated using %s = %s. Userdata = 0x%x", credType, credValue, userdata); } void synchronizeStateHandler(char *entityName, twInfoTable *subscriptionInfo, void *userdata){ /* * Called after binding to notify your application about what fields are bound on the server. * Will also be called each time bindings on a thing are edited. */ TW_LOG(TW_FORCE,"synchronizeStateHandler: Entity %s was synchronized with your ThingWorx Server", entityName); } int main( int argc, char** argv ) { int err = 0; /* Allow the user to override the default values for connection settings using command line arguments */ char * thingName = DEFAULT_THING_NAME; char* appKey = TW_APP_KEY; char* hostname = TW_HOST; int16_t port = TW_PORT; /* Parse command line parameters */ if(argc == 1){ printf("Syntax: %s \n",argv[0]); exit(0); } if(argc > 1){ hostname = argv[1]; } if(argc > 2){ port = (short)atoi(argv[2]); } if(argc > 3){ appKey = argv[3]; } if(argc > 4){ thingName = argv[4]; } /* Configure Logging */ twLogger_SetLevel(TW_TRACE); twLogger_SetIsVerbose(TRUE); /* Will show all messages network at TRACE level, reduces performance */ TW_LOG(TW_FORCE, "Starting up..."); /* Initialize the API */ err = twApi_Initialize(hostname, port, TW_URI, appKey, NULL, MESSAGE_CHUNK_SIZE, MESSAGE_CHUNK_SIZE, TRUE); if (TW_OK != err) { TW_LOG(TW_ERROR, "Error initializing the API"); exit(err); } /* Allow self signed or unvalidated certs (Neither should be used in production) */ /* twApi_SetSelfSignedOk(); */ /* Requires a certificate that is self signed */ twApi_DisableCertValidation(); /* The server certificate will not be validated at all. */ /* Disable HTTPS support, connect using HTTP only */ if(80 == port||8080 == port){ twApi_DisableEncryption(); } /* Register Event Handlers (optional) */ twApi_RegisterOnAuthenticatedCallback(authEventHandler, TW_NO_USER_DATA); twApi_RegisterBindEventCallback(NULL, bindEventHandler, TW_NO_USER_DATA); twApi_RegisterSynchronizeStateEventCallback(NULL, synchronizeStateHandler, TW_NO_USER_DATA); createSteamSensorThing(thingName); err = twApi_Connect(CONNECT_TIMEOUT, RETRY_COUNT); if(TW_OK != err){ exit(-1); } /* Add a path to load edge extensions from at runtime */ putenv("TWXLIB=../ExtUseExample/ext"); /* Start All SDK Threads, Does not return. If you want control back call twExt_Start() instead. */ twExt_Idle(DATA_COLLECTION_RATE_MSEC, TW_THREADING_MULTI, 5); }