Skip to main content
1-Visitor
January 20, 2017
Solved

Java: InfoTable addRow of ValueCollection puts empty

  • January 20, 2017
  • 1 reply
  • 6169 views

Hello,

I'm trying to pass an InfoTable as argument/parameter to a service at ThingWorx platform. All service calls work just fine but the InfoTable goes empty.

I'm populating the InfoTable this way (pseudo code):

    

     InfoTable infoTable = new InfoTable();

     ValueCollection definitions = new ValueCollection();

     FOR (<# of properties>) {

          definitions.put(Definition.name, new <T>Primitive(value-1));

          ...

          definitions.put(Definition.name, new <T>Primitive(value-n));


          log.debug(definitions.toJSON().toString());

          infoTable.addRow(definitions.clone());

     }

     log.debug(" InfoTable: " + infoTable.toJSON().toString());

     client.invokeService(...);

    

Now all this comes to output:

     {..., "baseType":"<type>","description":"<desc>","name":"<name>", ... }

     InfoTable: {"dataShape":{"fieldDefinitions":{}},"rows":[{},{},{},{}]}

Basically, an InfoTable object with empty value collections (the right amount of times, at least ).

What am I doing wrong or making a wrong assumption on how to populate an InfoTable?

Much appreciated for everyone that is able to contribute.

Cheers

Best answer by jkaczynski

Hi Marco Silva​,

Don't know if it's just because of short version of pseudocode, but I see that you create an empty InfoTable with no field definitions specified.

Using this way, Thingworx unfortunately doesn't send the data to Thingworx service. Below you can find working sample:

DataShapeDefinition dsd = new DataShapeDefinition();

dsd.addFieldDefinition(new FieldDefinition("field1", "NUMBER"));

dsd.addFieldDefinition(new FieldDefinition("field2", "NUMBER"));

dsd.addFieldDefinition(new FieldDefinition("field3", "NUMBER"));

InfoTable it = new InfoTable(dsd);

ValueCollection row = new ValueCollection();

Random rnd = new Random();

for(int i = 0; i < 5; i++) {

     row.SetNumberValue("field1", new NumberPrimitive(rnd.nextInt(100)));

     row.SetNumberValue("field2", new NumberPrimitive(rnd.nextInt(100)));

     row.SetNumberValue("field3", new NumberPrimitive(rnd.nextInt(100)));

     it.addRow(row.clone());

  }

ValueCollection params = new ValueCollection();

params.SetInfoTableValue("ServiceITParameterName", prop); // my service had just one parameter of type InfoTable

client.invokeService(ThingworxEntityTypes.Things, "ThingName", "ServiceName", params, 1500);

Hope it helps, don't hesitate to ask if something is not clear.

Regards,

Jakub.

1 reply

1-Visitor
January 22, 2017

Hi Marco Silva​,

Don't know if it's just because of short version of pseudocode, but I see that you create an empty InfoTable with no field definitions specified.

Using this way, Thingworx unfortunately doesn't send the data to Thingworx service. Below you can find working sample:

DataShapeDefinition dsd = new DataShapeDefinition();

dsd.addFieldDefinition(new FieldDefinition("field1", "NUMBER"));

dsd.addFieldDefinition(new FieldDefinition("field2", "NUMBER"));

dsd.addFieldDefinition(new FieldDefinition("field3", "NUMBER"));

InfoTable it = new InfoTable(dsd);

ValueCollection row = new ValueCollection();

Random rnd = new Random();

for(int i = 0; i < 5; i++) {

     row.SetNumberValue("field1", new NumberPrimitive(rnd.nextInt(100)));

     row.SetNumberValue("field2", new NumberPrimitive(rnd.nextInt(100)));

     row.SetNumberValue("field3", new NumberPrimitive(rnd.nextInt(100)));

     it.addRow(row.clone());

  }

ValueCollection params = new ValueCollection();

params.SetInfoTableValue("ServiceITParameterName", prop); // my service had just one parameter of type InfoTable

client.invokeService(ThingworxEntityTypes.Things, "ThingName", "ServiceName", params, 1500);

Hope it helps, don't hesitate to ask if something is not clear.

Regards,

Jakub.

msilva11-VisitorAuthor
1-Visitor
January 26, 2017

Right you are Jakub Kaczynski​!

I wasn't making the DataShape definition hence the InfoTable was appearing totally blank.

That was the issue

Manythanks!

1-Visitor
January 26, 2017

You're welcome, always glad to help

Regards,

J.