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

Community Tip - Did you know you can set a signature that will be added to all your posts? Set it here! X

Programmatically add a property to a Thing Template

agondek
12-Amethyst

Programmatically add a property to a Thing Template

Adding a property definition to a Thing programmatically requires a Thing restart in order for the property to come up (https://community.ptc.com/t5/ThingWorx-Developers/Unable-to-add-new-property-programatically-using-service/m-p/553678#M29081). Is there a similar process for a Thing Template? There is no "RestartThing" service on the template and it doesn't look like my properties that I add programmatically work.

ACCEPTED SOLUTION

Accepted Solutions

Yes you will have to.

 

If you manually add a property to a Thing Template you have to save the changes. When a Thing Template is saved, all the Things with that Thing Template are automatically restarted. In this case, you are not saving so the Things are not restarting. Here is the improved generalized code snippet:

try{
    Things["SimpleTest"].property = 5;
    
    }catch(err){
       var error=err.message;
        if(error.match(/not found on SimpleTest/)!=null){
            var params = {
                defaultValue: undefined /* STRING */,
                remoteBindingAspects: undefined /* JSON */,
                description: undefined /* STRING */,
                readOnly: undefined /* BOOLEAN */,
                type: "NUMBER" /* BASETYPENAME */,
                remote: undefined /* BOOLEAN */,
                remotePropertyName: undefined /* STRING */,
                timeout: undefined /* INTEGER */,
                pushType: undefined /* STRING */,
                dataChangeThreshold: undefined /* NUMBER */,
                logged: undefined /* BOOLEAN */,
                name: "property" /* STRING */,
                pushThreshold: undefined /* NUMBER */,
                dataChangeType: undefined /* STRING */,
                category: undefined /* STRING */,
                persistent: true /* BOOLEAN */,
                dataShape: undefined /* DATASHAPENAME */
            };

            // no return
            ThingTemplates["SimpleTestTemplate"].AddPropertyDefinition(params);
            
            // no return
            Things["SimpleTest"].RestartThing();

	}
}

 Kind regards,

Ezanne

View solution in original post

4 REPLIES 4

Hi agondek

 

I have just tried to add a property to a Thing Template without restarting it and it was successful. I used the ADDPropertyDefinition service under the Thing Template that I wanted to add the property to. Could you please attach the script you are using or any error messages that you are receiving.

What version of ThingWorx are you using?

 

Kind regards,

Ezanne 

    try{
        Things["ImplementedThing"].property = value;
    }
    catch(err){
        var error=err.message;
        if(error.match(/not found on ImplementedThing/)!=null){
            var params = {
                defaultValue: undefined /* STRING */,
                remoteBindingAspects: undefined /* JSON */,
                description: undefined /* STRING */,
                readOnly: undefined /* BOOLEAN */,
                type: "NUMBER" /* BASETYPENAME */,
                remote: undefined /* BOOLEAN */,
                remotePropertyName: undefined /* STRING */,
                timeout: undefined /* INTEGER */,
                pushType: undefined /* STRING */,
                dataChangeThreshold: undefined /* NUMBER */,
                logged: undefined /* BOOLEAN */,
                name: property /* STRING */,
                pushThreshold: undefined /* NUMBER */,
                dataChangeType: undefined /* STRING */,
                category: undefined /* STRING */,
                persistent: undefined /* BOOLEAN */,
                dataShape: undefined /* DATASHAPENAME */
            };

            // no return
            ThingTemplates["Template"].AddPropertyDefinition(params);
}

Generalized code snippet above. I'm finding that on the next iteration of this service, it attempts to add the property again (ie: the property is still not found on the implemented thing after adding it to the template) Do I need to restart the Implemented Thing?

Yes you will have to.

 

If you manually add a property to a Thing Template you have to save the changes. When a Thing Template is saved, all the Things with that Thing Template are automatically restarted. In this case, you are not saving so the Things are not restarting. Here is the improved generalized code snippet:

try{
    Things["SimpleTest"].property = 5;
    
    }catch(err){
       var error=err.message;
        if(error.match(/not found on SimpleTest/)!=null){
            var params = {
                defaultValue: undefined /* STRING */,
                remoteBindingAspects: undefined /* JSON */,
                description: undefined /* STRING */,
                readOnly: undefined /* BOOLEAN */,
                type: "NUMBER" /* BASETYPENAME */,
                remote: undefined /* BOOLEAN */,
                remotePropertyName: undefined /* STRING */,
                timeout: undefined /* INTEGER */,
                pushType: undefined /* STRING */,
                dataChangeThreshold: undefined /* NUMBER */,
                logged: undefined /* BOOLEAN */,
                name: "property" /* STRING */,
                pushThreshold: undefined /* NUMBER */,
                dataChangeType: undefined /* STRING */,
                category: undefined /* STRING */,
                persistent: true /* BOOLEAN */,
                dataShape: undefined /* DATASHAPENAME */
            };

            // no return
            ThingTemplates["SimpleTestTemplate"].AddPropertyDefinition(params);
            
            // no return
            Things["SimpleTest"].RestartThing();

	}
}

 Kind regards,

Ezanne

Alex_Disigner
6-Contributor
(To:agondek)

The template does not need a restart, as it does not store the values of the parameters, only their description

Announcements


Top Tags