cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Your Friends List is a way to easily have access to the community members that you interact with the most! X

How to dynamic add properties by using JAVA Edge SDK

byang1
1-Newbie

How to dynamic add properties by using JAVA Edge SDK

Hi all,

I check JAVA sample code, and it use ThingworxPropertyDefinitions to push properties to ThingWorx.

If I want to add a new properties in ThingworxPropertyDefinitions at runtime, how can I achieve this...?

Thank you.

@ThingworxPropertyDefinitions(properties = {

  @ThingworxPropertyDefinition(name="Temperature", description="Current Temperature", baseType="NUMBER", category="Status", aspects={"isReadOnly:true"}),

  @ThingworxPropertyDefinition(name="Pressure", description="Current Pressure", baseType="NUMBER", category="Status", aspects={"isReadOnly:true"}),

  @ThingworxPropertyDefinition(name="FaultStatus", description="Fault status", baseType="BOOLEAN", category="Faults", aspects={"isReadOnly:true"}),

  @ThingworxPropertyDefinition(name="InletValve", description="Inlet valve state", baseType="BOOLEAN", category="Status", aspects={"isReadOnly:true"}),

  @ThingworxPropertyDefinition(name="TemperatureLimit", description="Temperature fault limit", baseType="NUMBER", category="Faults", aspects={"isReadOnly:false"}),

  @ThingworxPropertyDefinition(name="TotalFlow", description="Total flow", baseType="NUMBER", category="Aggregates", aspects={"isReadOnly:true"}),

  })

1 ACCEPTED SOLUTION

Accepted Solutions

Hi Brandon,

See below a snippet (I tested a value received from XML to see if is number, boolean or string and created specific property per type):

Also note at the end the use of super.initialize(); instead of super.initializeFromAnnotations() (you can either use one or another as I've seen)

                         PropertyDefinition pd;

                        AspectCollection aspects = new AspectCollection();

                        if (NumberUtils.isNumber(str_AttributeValue)) {

                            pd = new PropertyDefinition(str_AttributeName,

                                    " ", BaseTypes.NUMBER);

                        } else if (str_AttributeValue == "true"

                                | str_AttributeValue == "false") {

                            pd = new PropertyDefinition(str_AttributeName,

                                    " ", BaseTypes.BOOLEAN);

                        } else

                            pd = new PropertyDefinition(str_AttributeName,

                                    " ", BaseTypes.STRING);

                        aspects.put(

                                Aspects.ASPECT_DATACHANGETYPE,

                                new StringPrimitive(DataChangeType.VALUE.name()));

                        // Add the dataChangeThreshold aspect

                        aspects.put(Aspects.ASPECT_DATACHANGETHRESHOLD,

                                new NumberPrimitive(0.0));

                        // Add the cacheTime aspect

                        aspects.put(Aspects.ASPECT_CACHETIME,

                                new IntegerPrimitive(0));

                        // Add the isPersistent aspect

                        aspects.put(Aspects.ASPECT_ISPERSISTENT,

                                new BooleanPrimitive(true));

                        // Add the isReadOnly aspect

                        aspects.put(Aspects.ASPECT_ISREADONLY,

                                new BooleanPrimitive(true));

                        // Add the pushType aspect

                        aspects.put("pushType", new StringPrimitive(

                                DataChangeType.VALUE.name()));

                        aspects.put(Aspects.ASPECT_ISLOGGED,

                                new BooleanPrimitive(true));

                        pd.setAspects(aspects);

                        super.defineProperty(pd);

                        super.initialize();

View solution in original post

2 REPLIES 2

Hi Brandon,

See below a snippet (I tested a value received from XML to see if is number, boolean or string and created specific property per type):

Also note at the end the use of super.initialize(); instead of super.initializeFromAnnotations() (you can either use one or another as I've seen)

                         PropertyDefinition pd;

                        AspectCollection aspects = new AspectCollection();

                        if (NumberUtils.isNumber(str_AttributeValue)) {

                            pd = new PropertyDefinition(str_AttributeName,

                                    " ", BaseTypes.NUMBER);

                        } else if (str_AttributeValue == "true"

                                | str_AttributeValue == "false") {

                            pd = new PropertyDefinition(str_AttributeName,

                                    " ", BaseTypes.BOOLEAN);

                        } else

                            pd = new PropertyDefinition(str_AttributeName,

                                    " ", BaseTypes.STRING);

                        aspects.put(

                                Aspects.ASPECT_DATACHANGETYPE,

                                new StringPrimitive(DataChangeType.VALUE.name()));

                        // Add the dataChangeThreshold aspect

                        aspects.put(Aspects.ASPECT_DATACHANGETHRESHOLD,

                                new NumberPrimitive(0.0));

                        // Add the cacheTime aspect

                        aspects.put(Aspects.ASPECT_CACHETIME,

                                new IntegerPrimitive(0));

                        // Add the isPersistent aspect

                        aspects.put(Aspects.ASPECT_ISPERSISTENT,

                                new BooleanPrimitive(true));

                        // Add the isReadOnly aspect

                        aspects.put(Aspects.ASPECT_ISREADONLY,

                                new BooleanPrimitive(true));

                        // Add the pushType aspect

                        aspects.put("pushType", new StringPrimitive(

                                DataChangeType.VALUE.name()));

                        aspects.put(Aspects.ASPECT_ISLOGGED,

                                new BooleanPrimitive(true));

                        pd.setAspects(aspects);

                        super.defineProperty(pd);

                        super.initialize();

Hi Vladimir,

Thanks for your reply, this method works.

But I have one more question, I want to push a table like below structure to ThingWorx by using JAVA SDK.

Bind the table data to PieChart and then display it.

How do I define the table and pass value?

NameValue
fieldName15
fieldName210
fieldName315
......
Top Tags