Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X
I created a service to create Things programmatically from unbound remote Things. I modeled it upon the service created in the PTC University Learning Exchange tutorial, Programmatically Add Things and Automatically Assign an Identifier.
I extended the example a little bit, with the intent of adding model tags to the Things as they were created. Here's the code for my service:
var identifiers = { maxItems: undefined /* NUMBER */ }; // result: INFOTABLE dataShape: EntityList var result = Resources["DeviceFunctions"].GetUnboundIdentifiers(identifiers); for (var i = 0; i < result.getRowCount(); i++) { var sensorName = result.getRow(i).name; var params = { tags: [ { vocabulary: "Applications", vocabularyTerm: "ThingWorxTraining" } ] /* TAGS */ , thingTemplateName: "RemoteThing" /* THINGTEMPLATENAME */ , description: undefined /* STRING */ , name: "SteamSensor" + sensorName /* STRING */ }; // no return Resources["EntityServices"].CreateThing(params); Things[params.name].EnableThing(); Things[params.name].RestartThing(); var ident = { identifier: result.getRow(i).name /* STRING */ }; Things[params.name].SetIdentifier(ident); Things[params.name].RestartThing(); }
My service creates the Things just fine. It doesn't tag my Things, though.
Here's what my, "tags," element looks like in the JSON param above:
tags: [ { vocabulary: "Applications", vocabularyTerm: "ThingWorxTraining" } ] /* TAGS */
To see what the ThingWorx Platform does, I opened up my developer tools, created a Thing, and watched what went across the wire. The request the Composer made to create my Thing contained the following JSON pertaining to the model tag:
"tags": [{ "vocabulary": "Applications", "vocabularyTerm": "ThingWorxTraining", "_magicPickerId": "d19dc787-cd85-4271-9d04-40719202e561" }]
The only differences I see are:
Is there a problem with the syntax of my params variable that's preventing application of the model tag to my new Thing?
Solved! Go to Solution.
It seems this may be a possible bug. For now, after the thing is created, enabled, and restarted, you will need to use the SetTags() snippet.
It seems this may be a possible bug. For now, after the thing is created, enabled, and restarted, you will need to use the SetTags() snippet.
I need to get my testing infrastructure set back up, but this is the code I plan to test. I've moved the application of the tags from object creation to post-creation steps:
var identifiers = {
maxItems: undefined /* NUMBER */
};
// result: INFOTABLE dataShape: EntityList
var result = Resources["DeviceFunctions"].GetUnboundIdentifiers(identifiers);
for (var i = 0; i < result.getRowCount(); i++) {
var sensorName = result.getRow(i).name;
var params = {
thingTemplateName: "RemoteThing" /* THINGTEMPLATENAME */ ,
description: undefined /* STRING */ ,
name: "SteamSensor" + sensorName /* STRING */
};
// no return
Resources["EntityServices"].CreateThing(params);
Things[params.name].EnableThing();
Things[params.name].RestartThing();
var ident = {
identifier: result.getRow(i).name /* STRING */
};
var params = {
tags: [{
vocabulary: "Applications",
vocabularyTerm: "ThingWorxTraining"
}] /* TAGS */
};
Things[params.name].SetTags(params);
Things[params.name].SetIdentifier(ident);
Things[params.name].RestartThing();
}