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

Community Tip - Stay updated on what is happening on the PTC Community by subscribing to PTC Community Announcements. X

sample code for automatic VirtualThing discovery and binding

gsantandrea
1-Newbie

sample code for automatic VirtualThing discovery and binding

Hello ThingWorx community,

I could not find any reference in the documentation on the device autodiscovery, intended as the automatic creation of a RemoteThing for every unbound VirtualThing, and the automatic browsing of the remote services, remote properties, and remote events.

From what I can see, Thingworx does not have any builtin functionality to do this kind of things, and you have to manually create a service to fulfill this goal. This service must scan for new unboud things and it must be periodically executed with the help of a Timer Thing. This functionality cannot be executed in an evented fashion because a "newUnboundDevice" event does not currently exist in ThingWorx.

Based on this post I came up with this code:

var newRemoteThings=Resources["DeviceFunctions"].GetUnboundRemoteThings({maxItems:3});

var result=Resources["InfoTableFunctions"].CreateInfoTableFromDataShape({InfoTableName:"CreatedThings",dataShapeName:"NewThingsDataShape"});

logger.info(newRemoteThings);

logger.info(result);

//result.AddRow({ThingName:"ssd"});

for (var i=0;i<newRemoteThings.length;i++){

    try {

        Resources["EntityServices"].CreateThing({thingTemplateName:"MyTestThingTemplate",description:undefined,name:newRemoteThings.name});

        Things[newRemoteThings.name].EnableThing();

        Things[newRemoteThings.name].RestartThing();

     

    }catch(e){

        logger.warn("Unable to create Thing:" + newRemoteThings.name);

  var params = {

        name: newRemoteThings.name // THINGNAME

    };

    // no return

    Resources["EntityServices"].DeleteThing(params);

  }

  result.AddRow({ThingName:newRemoteThings.name});

}

logger.info("finished method");

NOTE: I created

- MyTestThingTemplate that inherits from the RemoteThing template,  with no fields

- NewThingsDataShape: not sure which fields should be...  I created a "ThingName" field of basetype STRING. Is it correct? Probably not, because the result variable is empty when I execute the service.

Then I started the SimpleThingClient of the Java SDK.

The SimpleThing_1 Thing is correctly created, however it has no properties/services. I have to do it manually.


Now, how can I also automate the remote property/service auto discovery/binding step?

Giuliano

3 REPLIES 3

In general you have to be very careful with auto-creation and we recommend that there is always a verification step beyond the appkey exchange to make sure that the device you are going to create, indeed is a valid device.

The other approach btw, can be that you create it in the EMS as well and leverage twAPI to POST a request to the Platform to create a representative Remote Thing.

mkhalil1
1-Newbie
(To:paic)

Dear Pai Chung​ i target to create things from andriod App, any documentation for this please?

Figo
1-Newbie
(To:paic)

Dear Pai Chung,  how to automated bind the remote property/service after auto creating an unbinded RemoteThing from EMS through twAPI ?

Announcements


Top Tags