Populate a Thingworx DataTable

A DataTable entity provides a container to store structured row data with an IoT-oriented schema. If you want to capture rows of data and associate each entry with a time and a place, DataTables provide this.

This is also a good choice if there is a need to keep track of when a row of data arrives into the platform. There may be a time lapse between when data was recorded on a device and when it was received. A DataTable will capture this.

Create a DataTable Thing entity and you will have a Thing that comes with a predefined set of fields and services to support it’s base model. But the DataShape for a DataTable will not contain all of these predefined fields. Instead, a DataShape is required to describe the fields used by the values field within the DataTable. This values field is like an InfoTable nested within the DataTable.

This structure is revealed when we look at how to populate a DataTable. Each row requires the creation of an InfoTable that is assigned to its values parameter:

//identify the DataTable
var currentDT = Things['DT.logsReceived'];
//construct the InfoTable
var valueParams = {
	infoTableName: "IT.logsReceived",
	dataShapeName: "DS.logsReceived"
};
var values = Resources['InfoTableFunctions'].CreateInfoTableFromDataShape(valueParams);
// create the new row in the InfoTable
try {
	values.AddRow({
		deviceID: deviceID,
		entryID: dateTimeReceived
	});
} catch (e) {
	logger.error(me.name + "myServiceName _ recordLogReceipt: Error in line " + e.lineNumber + ": " + e);
}
// wrap the infotable in DataTable shape
var params = {
	sourceType: undefined /* STRING */ ,
	values: values /* INFOTABLE*/ ,
	location: undefined /* LOCATION */ ,
	source: undefined /* STRING */ ,
	tags: undefined /* TAGS */
};
try {
// add the row to the DataTable
	var id = currentDT.AddOrUpdateDataTableEntry(params);
	result = id;
} catch (e) {
	logger.info(me.name + "myServiceName _ recordLogReceipt: Error in line " + e.lineNumber + ": " + e);
}

The DataTable rows will contain the fields required by DataTables, plus the additional fields defined in the DataShape used to define the values field. But the values fields aren’t nested: they are flattened into a common row. The location, source, sourceType, tags and timestamp don’t need to be assigned values – only required fields in the values parameter do. If a field is assigned to be a key in the DataShape for values, its values will be repeated in a “key” field in the first field (as shown below where dateTimeReceived is assigned to be the key).

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Captcha loading...

This site uses Akismet to reduce spam. Learn how your comment data is processed.