3 Replies Latest reply on Jul 5, 2017 10:59 AM by fma RSS
    gsantandrea-2 Explorer

    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[i].name}); 
            Things[newRemoteThings[i].name].EnableThing(); 
            Things[newRemoteThings[i].name].RestartThing(); 
           
        }catch(e){
            logger.warn("Unable to create Thing:" + newRemoteThings[i].name);
      var params = {
            name: newRemoteThings[i].name // THINGNAME
        };
        // no return
        Resources["EntityServices"].DeleteThing(params);
      }
      result.AddRow({ThingName:newRemoteThings[i].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