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

Community Tip - You can change your system assigned username to something more personal in your community settings. X

Extension Package Development Questions

jasong
1-Newbie

Extension Package Development Questions

I am working on an Extension Package and had some questions. Rather than test all of these myself, perhaps someone knows? If not, I will test and post results here. Maybe this can be turned into a Q&A at some point.

I've numbered my questions so that readers and responders can quickly identify the question being answered.

(1) - Answered

If I build a package as version 1.0 and then again as 1.1, can I install 1.1 over top of 1.0 without uninstalling?
Am I protected from Installing 1.0 over top of 1.1? Boy I hope I get the right answer from you folks here. Having to uninstall first would be really bad news.

Assuming I can install v1.1 over v1.0...

(2)

Lets say v1.0 has a ThingTemplate in it called MyThingTemplate and it's defined purely as XML metadata.

Now v1.1 has created a new ThingPackage and defined MyThingTemplate using that new package, using the now very thin XML.

Can I install v1.1 over top of v1.0 in this case? I would guess yes but have not yet tried it.

(3) - Answered

If a ThingTemplate or DataShape or StyleDefinition or any Entity is marked as "isEditableExtensionObject == true", when you upgrade from v1.0 to v1.1, will that entity overwrite any changes that have been made? Or will it be skipped?

(4) - Answered

You can define properties (@ThingworxPropertyDefinition) in Java at the ThingPackage level or on the Template in the XML. Is there one advantage over the other?

(5) - Answered

initializeThing() - When does this run, ThingStart? Any other time? If not, should I use a standard Constructor to execute code on ThingStart?

(6) - Answered

The extension package examples show that you define a Property in Java and then include a private variable in your thing class. And then in initializeThing(), you grab that value from the Thing's persistence I guess and put it into the local private variable. But when you go to write this, don't you need to call this.setPropertyValue(...)?

And since you are only writing these values during initializeThing() , don't all of yoru services that you make that use those properties need to refresh those local copies to see if they have changed? And if so, why bother with the local class level private variable.

(7) - Answered

Can I make a local private variable that essentially acts as a hidden Property on the thing, knowing that it does not have persistence? Meaning, I can manipulate it from any of my java code from various services, but it won't be an actual visible thing property, and of course if the thing restarts, poof goes the value. Is that ok to do?

(8) - Answered

Can you define self-subscriptions on a ThingPackage in Java Code? Or only in the ThingTemplate XML metadata.

(9) - Answered

The guide say that ThingShapes do not need to extend Thing. But to get logging, I think it does.

protected static Logger aLog = LogUtilities.getInstance().getApplicationLogger(MYCLASSNAME.class);

Can I extend Thing and still use it as a ThingShape?

I tried this and did not see any errors but maybe this is not kosher. Is this okay? Or should I find a different way to make the logger work?

(10) - Answered

Can I write to ReadOnly properties from the Java side? That would be handy.

(11) - Answered

ThingShapes cannot have configuration tables: true or false?

(12) - Answered

Is there any way to code against another extension package? Do you just include the extension package's jar in Eclipse, careful not to include it in the build?

(13) - Answered

I seem to be able to, instead of extends Thing, use say, extends SchedulerThing. This seems to be ok but is this bad practice? If I wanted to make a SuperDuperSchedulerThing, this would work. But it would also work to simply have the ThingPackage extend Thing and then the ThingTemplate to use the ThingPackage I made with a BaseThingTemplate of SchedulerThing. Right? Either or? One way better than another?

(14) - Answered

In HelloWorldThing SDK example we see variables defined as Java primitives and not ThingWorx primitives.

e.g.

private String _stringProperty1 = "";

private Double _numberProperty1 = 0.0;

vs

private com.thingworx.types.primitives.StringPrimitive _stringProperty1 = "";

So, that's fine. I just want to know that this is the way to move forward with more complex ThingWorx types, such as JSON or Location.

private JSONObject _locationProperty = new JSONObject('"latitude":"0","longitude":"0","elevation":"0"}');

private string _jsonProperty = "";

and not

using primitives like

com.thingworx.types.primitives.JSONPrimitive

com.thingworx.types.primitives.LocationPrimitive

... and so on

The same question applies to Service inputs as well.

(15) - Answered

I need a good way to convert JSON Objects into POJOs. I see in the SDK that I have access to some of the Jackson json package but it appears to be missing a bunch of it, as all Jackson examples that I see out there reference objects that are not included in the SDK. I have service inputs that take JSON based input payloads and in the Java service they end up being JSONObjects, but that is not a very friendly tool, it'd be nice to be able to convert JSONObject to an actual POJO. Maybe I am missing something. I suppose I could deserialize the JSONObject (obj.toString()) but then I still need to deserialize it back into a POJO, and I'd like to use the Jackson libraries to do this, looks pretty simple but like I said, SDK does not seem to include the full Jackson package.

(16) - Solved

I cannot figure out how to execute a method on an extended Thing class from Java

To get an set properties, you grab the thing and then call the service to get or set

Thing myThing = ThingManager.getInstance().getEntityDirect("MyThingName");

myThing.setPropertyValue(...)

But I'd like to access public class variables or methods on my Thing. If I include a public bool or a public bool MyService() in my Thing's class, I still have no way of getting to it.

Additionally, I have no way of executing the methods I have defined as @ThingworxServiceDefinition

I thought casing might allow me to do this

MyThing myThing = (MyThing)ThingManager.getInstance().getEntityDirect("MyThingName");

but I get a cast exception. Thoughts?


(17) - Answered

My package is going to depend on several other extension packages. I suspect that some of these will cause errors upon package install if they are missing because they will be referenced as BaseThingTemplates or something. But others will only be referenced in code, and I am unsure if those missing packages will be caught. Is there any way to deal with that situation on package install? Like, some place I can place an Install Script to check for those and throw errors if they don't exist?

(This would also be helpful to have so that on install I could create some Things - see #18).

Otherwise I will just have to catch those errors and log and/or return errors to the user.. not terrible, but just wondering if there was a cleaner option.

(18) - Answered

I don't seem to be able to include actual Things in my metadata.xml and have them show up once the extension is installed.

Is this not allowed or perhaps did I do something dumb?

I am sure I will have more but that is all for now. Thanks for anyone who comes to help me out, I already appreciate it!

40 REPLIES 40
dcbb
1-Newbie
(To:sharmon)

Hi all,

  I have doubt, Actually I have to get properties from logger file using java code + that properties will store it in Thing COmposer inside thing or ThingTemplate. If u have idea Can you please tell me using java code.......Please

I want to execute some logic at the time of installation of an extension or even post-install is fine. For e.g. setting up some permissions on system entities. Is it possible?

what is the hook to make this code execute?

          OR

Is it possible to override some permission via Entity import? What would be the syntax if this is possible?

TIA

Sajid

You may create a property called "Initialized" and on ThingStart event check if Initialized it's false, on that case initialize whatever and then set Initialized to true.

Hi Carles,

    Thanks for responding.

    Not yet clear to me. Do I create a property on the extension or package?

    Are there other events that are fired on installation of extension?

EDIT: Got it, you meant utilize one of the things created by extension, Thanks! that would work.

Thanks,

Sajid

You got it

Hi Carles,

     It worked.

     Now new issue - how to undo the changes made (for e.g. delete permissions) when extension is being deleted?

Thanks,

Sajid

I think you are out of luck on that case...

Oh ok - thanks Carles!

Maybe the cleanup can be manual and can be documented.

Hi Carles,

   I was able to achieve it by overriding cleanupEntity();

Thanks,

Sajid

Perfect then! I'm not used on extension development and this cleanupEntity() service isn't available at Javascript Side.

I would suggest reading through the Javadoc on the Thing class itself.  It has a very good overview of the lifecycle hooks that you can tie into. This is, of course, from a Java extension only, as it would mean overriding some functions.

Top Tags