Creating a Data Collection Task
If using the built-in tasker to drive data collection or other types of repetitive or periodic activities, create a function for the task. Task functions are registered with the Tasker and then called at the rate specified after they are registered. The Tasker is a very simple, cooperative multitasker, so these functions should not take long to return and they must not go into an infinite loop.
The signature for a task function is found in src/utils/twTasker.h. The function is passed a DATETIME value with the current time and a void pointer, which is passed into the Tasker when the task is registered.
Here is an example of a data collection task:
/***************
Data Collection Task
****************/
/*
This function is called at the rate defined in the task creation.
The SDK has a simple cooperative multitasker, so the function
cannot infinitely loop.
Use of a task like this is optional and not required in a multithreaded
environment where this functionality could be provided in a separate thread.
*/
#define DATA_COLLECTION_RATE_MSEC 2000
void dataCollectionTask(DATETIME now, void * params) {
/* TW_LOG(TW_TRACE,"dataCollectionTask: Executing"); */
properties.TotalFlow = rand()/(RAND_MAX/10.0);
properties.Pressure = 18 + rand()/(RAND_MAX/5.0);
properties.Location.latitude = properties.Location.latitude +
((double)(rand() - RAND_MAX))/RAND_MAX/5;
properties.Location.longitude = properties.Location.longitude +
((double)(rand() - RAND_MAX))/RAND_MAX/5;
properties.Temperature = 400 + rand()/(RAND_MAX/40);
/* Check for a fault. Only do something if we haven't already */
if (properties.Temperature > properties.TemperatureLimit &&
properties.FaultStatus == FALSE) {
twInfoTable * faultData = 0;
char msg[140];
properties.FaultStatus = TRUE;
properties.InletValve = TRUE;
sprintf(msg,"%s Temperature %2f exceeds threshold of %2f",
thingName, properties.Temperature,
properties.TemperatureLimit);
faultData = twInfoTable_CreateFromString("msg", msg, TRUE);
twApi_FireEvent(TW_THING, thingName,
"SteamSensorFault", faultData, -1, TRUE);
twInfoTable_Delete(faultData);
}
/* Update the properties on the server */
sendPropertyUpdate();
}