Invoking a Service
While connected to the ThingWorx Platform, a client application can invoke a service on a Thing. The example code here provides two different services, one that creates a file and one that retrieves information about the file:
Before creating the file, declare a ValueCollection to contain the set of parameters for the service. Then, use the put method to populate the collection. Finally, invoke the service. Note that, after creating the file, this service returns NOTHING, and in this example, the response is ignored.
In the second part of the example, clear the ValueCollection and then write the parameter for the second service to the collection, specifically the name of the file whose information is being requested. Note that for services that return a result, an InfoTable is used to store the results. In this example, the service returns all the information about the file in an InfoTable.

//
// Invoking a service on a Thing
///////////////////////////////////////////////////////////////

//A ValueCollection is used to specify a service's parameters
ValueCollection params = new ValueCollection();

params.put("path", new StringPrimitive("/simple.txt"));
params.put("data", new StringPrimitive("Here is the content of the file."));
params.put("overwrite", new BooleanPrimitive(true));

// Use the SystemRepository Thing to create a text file on the ThingWorx Platform.
// This service's result type is NOTHING, so we can ignore the response.
client.invokeService(ThingworxEntityTypes.Things, "SystemRepository",
"CreateTextFile", params, 5000);

// If a service does have a result, it is returned within an InfoTable.
params.clear(); // Clear the params used in the previous service invocation.
params.put("path", new StringPrimitive("/simple.txt"));

// This service queries the SystemRepository for information about the file
// that was just created.
result = client.invokeService(ThingworxEntityTypes.Things, "SystemRepository",
"GetFileInfo", params, 5000);

// The rows of an InfoTable are ValueCollections.
ValueCollection row = result.getFirstRow();

LOG.info("The file info is: name: {}", row.getStringValue("name"));
LOG.info(" path: {}", row.getStringValue("path"));
LOG.info(" type: {}", row.getStringValue("fileType"));
LOG.info(" date: {}", row.getPrimitive("lastModifiedDate").
getStringValue());
LOG.info(" size: {}", row.getValue("size"));
Was this helpful?