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

Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X

Do we have a hook to "Save" action when a new ThingTemplate is created and saved

rad1
9-Granite

Do we have a hook to "Save" action when a new ThingTemplate is created and saved

I want to run a custom script that will syncronize ThingTemplate property names and types to 2 columns in a DataTable so when somebody uses a mashup with that DataTable as the data source will always see up to date information. The goal is to add columns ThingTemplateName and ThingTemplatePropertyName as Primary Keys and additional columns that will depend of this Primary Key.

This way I can add some extra Aspects to each column. 

 

I am aware of me.ThingStart(); method, but this only happens on a Thing.

Is this triggered when a Thing is saved for the first time and Enabled is selected?

What about going from Enabled to Not Enabled, Save action and at some later point Re-Enable and Save it again. Will that again cause me.ThingStart to trigger? 

But this is not related to Property additions, changes etc. 

 

Thanks,

Rad

5 REPLIES 5

ThingStart it's triggered (maybe I miss some case):

  • If you call thing.Restart() service
  • If you restart your ThingWorx Instance
  • If you update any base ThingTemplate for the given thing
  • I'm not sure if it's called when you update a ThingShape attached to the given thing, and or a base ThingTemplate, I think it's not.

You don't have a ThingTemplate Update event, you may simulate one with a Timer which diggs on ConfigurationLog and triggers a custom event. On configuration Log if you have Info level enabled you will find entries like:

 

action = MODIFY .... Instance = "ThingTemplateName"

 

 

 

rad1
9-Granite
(To:CarlesColl)

ThanksCarles,

 

That is an idea for sure. I am using TW 8.1. 

I couldn't find log entry like: action = MODIFY .... Instance = "ThingTemplateName"

I could only see: 

   "added ThingTemplate" and

   "Updating ThingTemplate"

as the log entries below show.

 

 Are you suggesting I call a REST API endpoint like: 

/Thingworx/Logs/ApplicationLog/Services/QueryLogEntries

or 

var logEntries =  Logs["SecurityLog"].QueryLogEntries(....);

var logEntries =  Logs["SecurityLog"].GetLogEntries(...);

and search for "added ThingTemplate" and "Updating ThingTemplate" in searchExpression to see if something was added or modified.

What is the proper json body requestion REST call? I tried this buy it doesn't honor search expression 

*added ThingTemplate*

{"value":"{\"searchExpression\":\"*added ThingTemplate*\",\"origin\":\"\",\"instance\":\"\",\"thread\":\"\",
\"startDate\":1462457344702,\"endDate\":1462543744702,\"maxItems\":100}"}

 

Will I need to change Log level in order for this to work?

 

 

I can see this message in received log when I updated and saved ThingTemplate [Template3] for the first time
        {
            "content": "Updating ThingTemplate [Template3]",
            "instance": "",
            "level": "INFO",
            "origin": "c.t.c.ImportProcessor",
            "session": "",
            "thread": "https-openssl-apr-443-exec-11",
            "timestamp": 1525468553360,
            "user": "me@me.com"
        },
		
I can see this message in received log when I created and saved ThingTemplate [Template3] for the first time
        {
            "content": "Successfully added ThingTemplate [Template3]",
            "instance": "",
            "level": "INFO",
            "origin": "c.t.c.ImportProcessor",
            "session": "",
            "thread": "https-openssl-apr-443-exec-29",
            "timestamp": 1525468434158,
            "user": "me@me.com"
        },

 

 

Thanks,

Rad

amittal-3
13-Aquamarine
(To:rad1)

Hi Rad,

I too had initial queries about ThingStart Event, and how and when is it generated. So I posted a query about the same in forum. I hope, you can get benefited from it in addition to Carles's reply.

The topic is - Information on ThingStart Event and constructor and destructor for a Thing

https://community.ptc.com/t5/ThingWorx-Developers/Information-on-ThingStart-Event-and-constructor-and-destructor/m-p/547384#M28073

 

Another aspect that I would like to suggest is, since you might have observed that ThingStart event can be generated by many different conditions, so to prevent same thing (service execution/code execution) from happening again and again, you might want to use a local variable for your thing.

Create a property for your thing (maybe bool), and assign it true or false once in thingstart event subscription, when you see it fit. And start your thingstart event subscription, with a check to continue forward only if the property is false and exit otherwise.

Regards

Aditya Mittal

rad1
9-Granite
(To:amittal-3)

Hi Aditya,

 

Thanks for adding to the discussion. Can you elaborate on this:

"you might have observed that ThingStart event can be generated by many different conditions, so to prevent same thing (service execution/code execution) from happening again and again"

 

Aren't all these conditions valid once to trigger the fact that something needs to be done when a Thing is becoming active in the platform? I would like to know if that condition is only "Save", but I don't know how to determine that, so how would a temporary property inside the subscription help in this case.

Maybe I can check the timestamp of the thing save event (time when the thing was saved) and check against a property of DataTime and if they don't match I update that property with the timestamp of Save event and do one time processing and next time the subscription fires I can compare these timestamps and skip processing for non Save events.

Thanks

 

amittal-3
13-Aquamarine
(To:rad1)

Hello,

What I meant by the following statement - 

"you might have observed that ThingStart event can be generated by many different conditions, so to prevent same thing (service execution/code execution) from happening again and again"

is...

That in my case I wanted to assign a value stream to a thing, when my thing was created. So for that I added a code to create valuestream and assign it to my thing in the Thingstart subscription. Then later I realised that even when I restarted my platform, I used to get the Thingstart event again, but in this case I didn't want to create and assign valuestream to my thing as it was done previously, so I added a local boolean variable flag to determine when to take action on thingstart event. If that action has been taken anytime previously in Thing's lifetime, then no need to take it again.

So I hope I was able to clarify this point contextually. :-)

 

Now coming back to your point of discussion, if you want to do something exclusively on Save event, I am not sure how can you just capture the save event. But I think your idea of comparing the timestamp of thing, might work!!

Thanks

Top Tags