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

Community Tip - Need help navigating or using the PTC Community? Contact the community team. X

How to add a Thing from Extension / How to add new properties to existing ThingTemplate from Extension.

aniketa
1-Visitor

How to add a Thing from Extension / How to add new properties to existing ThingTemplate from Extension.

I am building a Extension , ThingTemplate is one of the input parameter of my service.

After some processing, Extension is suppose to create new Thing in my TW database.

I have found two methods :

objThing.SaveConfigurationTables();

objThing.persistConfigurationTables();

Are these the methods I should use for storing the Thing ?

Also what is way to add few more properties to ThingTemplate from Extension ?

2 REPLIES 2
smanley
14-Alexandrite
(To:aniketa)

Hi Aniket,

To add a property to a thing template, you will need to import the property definition package member into your Thing.java file.


import com.thingworx.metadata.annotations.ThingworxPropertyDefinition;

import com.thingworx.metadata.annotations.ThingworxPropertyDefinitions;


You will then create the properties using @ThingworxPropertyDefinitions


e.g.

@ThingworxPropertyDefinitions(properties = {

@ThingworxPropertyDefinition(name="StringProperty1", description="Sample string property", baseType="STRING", aspects={"isPersistent:false","isReadOnly:false"}),

@ThingworxPropertyDefinition(name="NumberProperty1", description="Sample number property", baseType="NUMBER", aspects={"isPersistent:false","isReadOnly:false"}),

})


Once a thing is created, and enabled, it is saved in your Thingworx database. The objThing.SaveConfigurationTables() and objThing.persistConfigurationTables() methods are used to add a configuration table to a thing 



aniketa
1-Visitor
(To:smanley)

Hi SaeedMa,

I have created Thing using following code :

<span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.2" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">ThingTemplate</span> <span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.3" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">parentThingTemplate</span> =  <span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.4" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">ThingTemplateManager</span>.<span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.5" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">getInstance</span>().<span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.6" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">getEntity</span>(<span class="">"<span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.7" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">FalkonryPredictor</span>"</span>);

<span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class=""></span>Thing <span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.8" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">objThing</span> = <span class="">new</span> Thing();

<span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.9" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">objThing</span>.<span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.10" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">setDescription</span>("<span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.11" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">VendingMachineTemplateFalkonryPredictor</span><span class=""> - Thing created by <span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.12" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">Falkonry</span> Extension"</span>);

<span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.13" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">objThing</span>.<span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.14" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">setName</span>("<span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.15" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">VendingMachineTemplateFalkonryPredictor</span>");

<span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.16" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">objThing</span>.<span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.17" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">setThingTemplate</span>(<span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.18" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">parentThingTemplate</span>);

<span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.19" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">objThing</span>.<span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.20" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">setThingTemplateName</span>(<span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.21" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">parentThingTemplate</span>.<span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.22" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">getName</span>());

<span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.23" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">objThing</span>.activate();

<span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.24" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">objThing</span>.<span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.25" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">EnableThing</span>();

<span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.26" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">objThing</span>.start();

<span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.27" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">objThing</span>.<span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.28" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">RestartThing</span>();

<span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class=""></span><span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.29" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">ThingManager</span>.<span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.30" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">getInstance</span>().<span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.31" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">createThing</span>(<span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.32" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">objThing</span>, <span class="">true</span>);

Point 1 : 

By this code thing is created and visible in Composer, but I am not able to run a service related to it. 

Getting following error  : 

"context: com.thingworx.webservices.context.HttpExecutionContext@2474886a][message: Unable to Invoke Service loadResults on VendingMachineTemplateFalkonryPredictor : object is not an instance of declaring class]"

Point 2 :

In this thing properties are inherited from ThingTemplate "FalkonryPredictor". e.g. ID

While creating thing, I want to set ID with predefined value, How to set this ? 

I tried setting ID by following code :

<span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.41" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">objThing</span>.<span class="J-JK9eJ-PJVNOc" data-g-spell-status="2" id=":24s.42" tabindex="-1" role="menuitem" aria-haspopup="true" style="background: yellow;">setPropertyValue</span>(<span class="">"ID"</span>, obj.get(<span class="">"id"</span>));

But it's not working.



Announcements


Top Tags