2 Replies Latest reply on Dec 11, 2015 12:27 AM by byang RSS
    byang 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"}),

      })

      • Re: How to dynamic add properties by using JAVA Edge SDK
        vladimirros Creator

        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();