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

Community Tip - Help us improve the PTC Community by taking this short Community Survey! X

How to Automatically Bind Property Data from a Remote Thing

MarkCheli
6-Contributor

How to Automatically Bind Property Data from a Remote Thing

Hi,

I'm using the Java SDK to automatically create a Thing in ThingWorx with the remotething template and I am attempting to have the two bind properties.  Using the below service (which is remotely called from the device), I am able to remotely create a thing.  All that needs to be done in order to get the properties is to go into the thing and select "Manage Bindings".  I would like to automate that as well so when this service runs, the bindings are also added.

var thingName = ThingName;

var template = ThingTemplate;

var tag = Tag;

var description = Description;

//check if thing exists

try {

    var exist = Things[thingName].name;

    //If Thing exists, no exception will be thrown

    throw new Error("Error creating " + thingName + ". Error: \""+thingName+"\" already exists");

} catch (err) {

    // Thing does not exist, does the requested template exist

    try {

        var exist = ThingTemplates[template].name;

    } catch (err){

        throw new Error("Error creating " + thingName + ". Error: ThingTemplate \""+template+"\" doesn't exist");

    }

    //Test is see if thingName meets name rules

    // Does it start with a letter

    var code = thingName.charCodeAt(0);

    if ( !(((code >= 65) && (code <= 90))  //lower case letters

           || ((code >= 97) && (code <= 122)) //uper case letters

           || (code == 95) //underscore

    )){

        throw new Error("Error creating " + thingName + ". Error: \""+thingName+"\" is not a valid name for a Thing");

    } else {

        for(var i = 1; i <thingName.length; i++)

        {

            var code = thingName.charCodeAt(i);

            if ( !(   ((code >= 65) && (code <= 90))  // lower case letters

                   || ((code >= 97) && (code <= 122)) //upper case letters

                   || (code == 95) // underscore

                   || (code == 46) // period

                   || ((code >= 48) && (code <=57))  //numbers

                  )){

                throw new Error("Error creating " + thingName + ". Error: \""+thingName+"\" is not a valid name for a Thing");

            }

        }

    }

    // Apply a Tag if provided

    var applyTag;

    if( tag == undefined){

        applyTag = false;

    } else if( tag.length>0) {

        try{

            var params = {

                name: tag /* STRING */

            };

            var exist = ModelTags["Applications"].GetVocabularyTerm(params);

        } catch(err){

            throw new Error("Error creating " + thingName + ". Error: \""+tag+"\" is not a term in Applications");

        }

        applyTag = true;

    } else {

        applyTag = false;

    }

    try {

            var params = {

                tags: undefined /* TAGS */,

                thingTemplateName: template /* THINGTEMPLATENAME */,

                description: description /* STRING */,

                name: thingName /* STRING */

            };

            // no return

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

    } catch(err) {

            throw("Error creating " + thingName + ". Error:" + err.message);

    }

    Things[thingName].EnableThing();

    Things[thingName].RestartThing();

        

    ///Apply the tag

    if(applyTag)

    {

        var tags = new Array();

        var newTag = {};

        newTag.vocabulary = "Applications";

        newTag.vocabularyTerm = tag;

        tags.push(newTag);

        var params = {

            tags: tags /* TAGS */

        };

        // no return

        Things[thingName].AddTags(params);

    }

}

Please let me know if you have any questions!  Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
MarkCheli
6-Contributor
(To:billrei)

Hi All,

Bill was right, I was overthinking the problem.  I was able to remotely create a thing by invoking the above service to create a thing.  All I had to do was remotely invoke another service that created a property on a thing.  Code here:

var params = {

    defaultValue: undefined /* STRING */,

    description: undefined /* STRING */,

    readOnly: undefined /* BOOLEAN */,

    type: Type /* BASETYPENAME */,

    remote: true /* BOOLEAN */,

    remotePropertyName: PropertyName /* STRING */,

    timeout: undefined /* INTEGER */,

    pushType: undefined /* STRING */,

    dataChangeThreshold: undefined /* NUMBER */,

    logged: undefined /* BOOLEAN */,

    name: PropertyName /* STRING */,

    pushThreshold: undefined /* NUMBER */,

    dataChangeType: undefined /* STRING */,

    category: undefined /* STRING */,

    persistent: undefined /* BOOLEAN */,

    dataShape: undefined /* DATASHAPENAME */

};

Things[ThingName].AddPropertyDefinition(params);

Things[ThingName].RestartThing();

View solution in original post

2 REPLIES 2
billrei
5-Regular Member
(To:MarkCheli)

This one might be easier to solve than you think. If you bind a ThingTemplate to one example virtual thing, then any thing that uses this bound template it will inherit these bindings as well. Hopefully, you are not planning on dynamically binding each device differently as that would be much more complex.

MarkCheli
6-Contributor
(To:billrei)

Hi All,

Bill was right, I was overthinking the problem.  I was able to remotely create a thing by invoking the above service to create a thing.  All I had to do was remotely invoke another service that created a property on a thing.  Code here:

var params = {

    defaultValue: undefined /* STRING */,

    description: undefined /* STRING */,

    readOnly: undefined /* BOOLEAN */,

    type: Type /* BASETYPENAME */,

    remote: true /* BOOLEAN */,

    remotePropertyName: PropertyName /* STRING */,

    timeout: undefined /* INTEGER */,

    pushType: undefined /* STRING */,

    dataChangeThreshold: undefined /* NUMBER */,

    logged: undefined /* BOOLEAN */,

    name: PropertyName /* STRING */,

    pushThreshold: undefined /* NUMBER */,

    dataChangeType: undefined /* STRING */,

    category: undefined /* STRING */,

    persistent: undefined /* BOOLEAN */,

    dataShape: undefined /* DATASHAPENAME */

};

Things[ThingName].AddPropertyDefinition(params);

Things[ThingName].RestartThing();

Top Tags