Skip to main content
1-Visitor
April 4, 2018
Solved

Information on ThingStart Event and constructor and destructor for a Thing

  • April 4, 2018
  • 1 reply
  • 3200 views

Hello,

I have a requirement to perform some action at startup (like filling some data tables or running some services associated with Thingworx entities), for that I am using a subscription to ThingStart Event. But I would like to have some more insights on how and when does ThingStart event occurs/triggers. I read in some documentation, that ThingStart event is fired when the Thing is created, so does that mean it occurs only once in the lifetime of a Thing? I tried to look for some detailed information for ThingStart event, but was unable to find it.

 

Another part of my question is what is the provision to have constructor and destructor feature for Thingworx entities, say for example, I would like to associate my Things with a Value Stream, when it is created (so I could do - Create a new Thing, Create a new Value Stream and assign the ValueStream to a that thing).

Similarly while destroying a Thing I would like to de-assign the Value stream, delete the thing and delete the value stream.

 

So how can I achieve this kind of behaviour in Thingworx?

 

Thanks in advance

Regards

Aditya Mittal

Best answer by AdamR

Hi Aditya,

 

ThingStart occur every time a Thing is started (not just when it is created).  This occurs each time the Thing is saved (the platform removes the current running thing from the JVM in order to load/start the newly saved structure).  It will also occur when the platform starts up, when it enters and active state (if it was de-activated) or if the RestartThing service runs.

Do keep in mind that when the platform starts there is no predefined order in which each Thing will become active.  So if you are relying on another Thing in the platform (like a DataTable) to be available at this event,  you may have errors.

 

To create a Thing you can use the Resources built in to the platform.  There is an EntityServices resource (CreateThing) that you can use to create the Thing and the ValueStream (which is just a Thing with the ValueStream template).  After creating both you use the SetValueStream service on the new Thing to assign the value stream.  

 

You should not need to de-assign the ValueStream from the Thing if you are deleting that Thing.  They existing independently so you can just delete them both.  However you would need to delete the Thing before the ValueStream since there is a dependency there.

 

Let me know if that makes sense or if you have additional questions.

 

Adam

1 reply

AdamR14-AlexandriteAnswer
14-Alexandrite
April 4, 2018

Hi Aditya,

 

ThingStart occur every time a Thing is started (not just when it is created).  This occurs each time the Thing is saved (the platform removes the current running thing from the JVM in order to load/start the newly saved structure).  It will also occur when the platform starts up, when it enters and active state (if it was de-activated) or if the RestartThing service runs.

Do keep in mind that when the platform starts there is no predefined order in which each Thing will become active.  So if you are relying on another Thing in the platform (like a DataTable) to be available at this event,  you may have errors.

 

To create a Thing you can use the Resources built in to the platform.  There is an EntityServices resource (CreateThing) that you can use to create the Thing and the ValueStream (which is just a Thing with the ValueStream template).  After creating both you use the SetValueStream service on the new Thing to assign the value stream.  

 

You should not need to de-assign the ValueStream from the Thing if you are deleting that Thing.  They existing independently so you can just delete them both.  However you would need to delete the Thing before the ValueStream since there is a dependency there.

 

Let me know if that makes sense or if you have additional questions.

 

Adam

amittal-31-VisitorAuthor
1-Visitor
April 5, 2018

Hello Adam,

Thanks for the detailed reply on the queries that I had.

I think that would be all the information I would need as of now on these topics. I shall 'accept it as Solution'.

Regards

Aditya

14-Alexandrite
May 2, 2018

Hi Aditya,

 

Apologies for not seeing your reply sooner.  There is not any built in descructor event or call that can be caught in a subscription or service.  You could do a periodic check (using a daily scheduler) to see if there are value steams that are no longer attached to any other entity.  There is a service on each Thing named HasIncomingDependencies which can be called to check this.

 

Thanks,

Adam