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!

