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

Community Tip - When posting, your subject should be specific and summarize your question. Here are some additional tips on asking a great question. X

Java ThingShape Extension context not finding correct thing

HD_10298120
2-Guest

Java ThingShape Extension context not finding correct thing

I have created a Java Extension as a ThingShape, when I add it to multiple Things on the platform, the ThreadLocalContext.getMeContext() returns the information for the Thing that was connected to first NOT the Thing that the current ThingShape is connected to.

 

Here is how it is set up

Thing1

 - ThingShape Extension connected to Thing1

 

Thing2

 - ThinShape Extension connected to Thing2

 

If I connect to Thing1 -- the first connection - everything works, Then I disconnect my device and connect a different device to Thing2 (which has the same type of extension) The Thing2 calls will return information grabbed by the extension about Thing1 -- even though it is NOT connected to Thing1.

Here is the code I am using in the java extension (ThingShape) I use this to send information to the Thing the extension is connected to as well as the device.

 

 

Object me = ThreadLocalContext.getMeContext();
if (me instanceof Thing) {
  // ------------------------------
  // Assign connected thing to me
  // ------------------------------
  Thing connectedThing = (Thing) me;
  // -------------------------------------------
  // Get thing name for personalization string
  // -------------------------------------------
  _thingName = _connectedThing.getName();
}

 

 

How do I get the Thing object in the java extension of the thing I am calling  - I have tried extending my class to Thing instead of ThingShape and using 

StringPrimitive name = (StringPrimitive)this.getPropertyValue("name");

But this returns a null value or blank, I have tried totally disconnecting the previous edge device from the platform before making the calls as well as making the calls at the same time -- the extension will ONLY see the Thing that the device made the first connection to, no subsequent connections to other Things are found unless I restart the platform. If you can connect to multiple "Things" and those things each have the thingshape extension added individually - there should be a way to ensure the extension uses the Thing that the edge device is connected to.

 

In short if I call the Thing from the edge device to call a service that is within the extension -- it call the service on the Thing that was first connected to NOT the Thing that the edge device is calling.

 

Any help would be greatly appreciated!

 

1 ACCEPTED SOLUTION

Accepted Solutions
slangley
23-Emerald II
(To:HD_10298120)

Hi @HD_10298120.

 

Have you tried using the AddShapeToThingTemplate located in Resources > EntityServices?

 

Regards.

 

--Sharon

 

 

View solution in original post

3 REPLIES 3

Hi @HD_10298120 ,

I performed a test on my TW 9.3.3 with an extension build with the Extension SDK 9.1.0.

I created two things, one Generic, the other Remote Thing and for both of them the following code correctly returned the Thing name,

 

                _logger.trace("Entering Service: GetContextVR");
		Object me = ThreadLocalContext.getMeContext();
		
		_logger.trace("Exiting Service: GetContextVR");
		return ((Thing)me).getName() ;

 

A couple of Things regarding how the ThingShapes work:

  • A Thing Shape code will run only in the context of the object (Thing) on which is instantiated.
  • You should add the ThingShape to a template from which your Things are created. Adding it manually is against best practices (in lack of a specific reason) .
  • It should not be possible to have such Thing Shape code access other object's properties when using a simple code like yours.
  • Whenever you say you call a service within the extension, it's actually calling a service defined in the extension. The service itself will run in the context of the Thing you run into.

Note I did not connect an Edge agent to the platform so I can fully test the behavior you explained.

If there's no other magic done behind the scenes (read: more custom coding), I think this should be opened as a Technical Support case to ensure proper follow up.

(Saying this because historically speaking the community focuses 99% on the ThingWorx core service/UI usage and such Extension knowledge is just not present here at that level).

 

Thank you for your response -

 

Is there somewhere I can find some sample code of how to add a ThingShape to the template from which the Thing is created? Can this be done programmatically within the ThingShape extension without needing to hard code in the extension a specific Thing?

 

The purpose of my extension is to allow users to add the "ThingShape" to an already existing Thing via the "General Information" screen by simply selecting my extension (which was imported) when adding an Implemented Shapes +. The extension uses our proprietary C library to encrypt outgoing and incoming communications between a Thing and an Edge Thing. We have corresponding Edge SDK's available that the user can add to their Edge Device that uses your SDK, we have embedded our software with your SDK to allow correct responses and messages that work between the Thing and the Edge Thing using our software. 

 

Ideally the user should be able to add our ThingShape to their Thing and add our edge software to their device which has your edge SDK already implemented. Thus with minimal changes to the already created Things and Edge Things the user would be able to secure their communication using our proprietary software without it being a painful process.

 

The code in the ThingShape needs to point to the Thing it has been added to without specifically "naming" the Thing in the code, so it can be used on any Thing that connects with an Edge Thing in your system, but once it is added to a Thing it needs to be able to know which specific Thing it has been added to.

slangley
23-Emerald II
(To:HD_10298120)

Hi @HD_10298120.

 

Have you tried using the AddShapeToThingTemplate located in Resources > EntityServices?

 

Regards.

 

--Sharon

 

 

Top Tags