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

Community Tip - Want the oppurtunity to discuss enhancements to PTC products? Join a working group! X

Remote thing created using Java Edge SDK always shows as not connected

mkreinin
1-Visitor

Remote thing created using Java Edge SDK always shows as not connected

Hi,

I am trying to create and bind remote thing using java edge sdk. Following is an essential part of the code:

...

params = new ValueCollection();
params.SetStringValue("name", "MyThing");
params.SetStringValue("thingTemplateName", "RemoteThing");
client.invokeService(ThingworxEntityTypes.Resources, "EntityServices", "CreateThing", params,
0);

    client.invokeService(ThingworxEntityTypes.Things, "MyThing", "EnableThing",        params, 0);

client.invokeService(ThingworxEntityTypes.Things, "MyThing", "RestartThing",
params, 0);

params = new ValueCollection();
params.SetStringValue("name", "HelloService");
params.SetBooleanValue("remote", true);
params.SetStringValue("remoteServiceName", "HelloService");
client.invokeService(ThingworxEntityTypes.Things, "MyThing",
"AddServiceDefinition", params, 0);
       VrtualThing thing =
new MyThing("MyThing", "", "MyThing", client);
  client.bindThing(thing);
  client.start();

.......

MyThing class exists and extends VirtualThing.

I can see my thing created and HelloService added to it, however its isConnected property is always false and "lastConnection" date is set to Jan 1, 1970. If I try to invoke HelloService I get "Unable to Invoke Service HelloService on MyThing : No service handler defined for service HelloService on thing [MyThing]".

What am I missing?

Thanks,

Michael

6 REPLIES 6
ibanham
12-Amethyst
(To:mkreinin)

Hi

It's unclear as to whether the code you have posted is from the platform or the edge. To me it looks like platform code, but I may be wrong... Can you explain the reason behind why you are trying to write the code in this way?

Is your Edge "device" showing up in the Unbound Remote Things page in composer? If it is, it should auto-bind as long as the name of your Edge Thing is the same as your ThingName on the platform.

Regards

Ian

We have a server that has to connect to ThingWorx server and expose some parts of its data model as remote things - to be further used in mashup. The above code is executed on our server. I omitted the initialization part in my original post, here it is:

ClientConfigurator configurator = new ClientConfigurator();    

configurator.setUri("ws://localhost:8080/Thingworx/WS");   

SecurityClaims sc = SecurityClaims.fromCredentials("aaa", "bbb");   

configurator.setSecurityClaims(sc);   

configurator.setReconnectInterval(15);   

configurator.setAsSDKType();   

configurator.setName("MyServer");   

try {     

client = new ConnectedThingClient(configurator);     

client.connect();     

client.checkConnection();

......

Looking at remote things, MyServer shows up as connected "SDKGateway" thing with green icon while MyThing shows with red icon as unbound.

supandey
19-Tanzanite
(To:mkreinin)

Michael, as Ian mentioned the Thing from the Unbound state will auto bind if you have a thing existing in the platform with the exact name, which in your case would be "MyThing"

Have you tried creating a Thing with that exact name?

Now I am confused. By "platform" do you mean "thingworx server"? Please look at my code - I am already creating remote thing MyThing and I expect it to be bound to my local virtual thing which does have the same name. So, what am I missing in the code?

OK, figured it out - the missing part was "SetIdentifier" call:

..

params = new ValueCollection();   

params.SetStringValue("identifier", "MyThing1");   

client.invokeService(ThingworxEntityTypes.Things, "MyThing", "SetIdentifier", params, 0);

Now I just need to make sure my virtual thing uses the same identifier:

VirtualThing thing = new IterationVirtualThing("MyThing1", "", "MyThing1", client);

Now, the next step is to add services to MyThing. This works:

      params = new ValueCollection();

      params.SetStringValue("name", "GetHello");

      params.SetBooleanValue("remote", true);

      params.SetStringValue("remoteServiceName", "GetHello");

      client.invokeService(ThingworxEntityTypes.Things, "MyThing",    "AddServiceDefinition", params, 0);

What I am missing is the ability to define Input and Output parameters for my service. I cannot find any documentation on proper syntax and property names for it. Can somebody please provide some examples?

supandey
19-Tanzanite
(To:mkreinin)

Michael, by platform i meant the thingworx server. Concerning your next question there is a documentation and a sample available on working directly with the Services, not sure if you have already seen this. This code sample is from the Edge side, not sure if you have already gone through this :

@ThingworxServiceDefinition(name = "AddNumbers", description = "Add Two Numbers")
@ThingworxServiceResult(name = CommonPropertyNames.PROP_RESULT, description = "Result", baseType = "NUMBER")
public Double AddNumbers(@ThingworxServiceParameter(name = "a", description = "Value 1", baseType = "NUMBER") Double a, @ThingworxServiceParameter(name = "b", description = "Value 2", baseType = "NUMBER") Double b)throws Exception {
return a + b;
}

You can find more samples in the Java SDK samples and is covered under the documentation ThingWorx Java SDK Developer's Guide (which is part of the Java SDK)

Announcements


Top Tags