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

Extension : Thingworx Event Subscription

SOLVED
Highlighted

Extension : Thingworx Event Subscription

Hi,

 

I have created an extension in java. I want to create a subscription.

I've found "@ThingworxSubscription" annotation to create a subscription. But I didn't find source event !

 

For exemple, I can't create a subscription on event "DataChange".

Is it possible to get event list in java code ?

 

Thanks.

 

Regards,

 

    Julien

Tags (1)
1 ACCEPTED SOLUTION

Accepted Solutions

Re: Extension : Thingworx Event Subscription

To specify a subscription on the DataChange event in your example, you would want to do the following:

On the class, specify the following annotation (or similar):
@ThingworxSubscriptions(subscriptions = {

@ThingworxSubscription(eventName = "DataChange", sourceProperty="myProp", enabled=true, handler="SubscriptionHandler")

})

Then within your template or shape you will want to define the SubscriptionHandler service:

@ThingworxServiceDefinition(name="SubscriptionHandler", isPrivate=true)

public void SubscriptionHandler(

    @ThingworxServiceParameter(name="eventData", baseType="INFOTABLE", aspects = {"datashape:DataChangeEvent"}) InfoTable eventData,

    @ThingworxServiceParameter(name="eventName", baseType="STRING") String eventName,

    @ThingworxServiceParameter(name="eventTime", baseType="DATETIME") DateTime eventTime,

    @ThingworxServiceParameter(name="source", baseType="STRING") String source,

    @ThingworxServiceParameter(name="sourceProperty", baseType="STRING") String sourceProperty)

You should name the service something more useful than just SubscriptionHandler, I just used that as an example.

We're hoping to improve the documentation surrounding extension development in the next few releases, so hopefully some of these mysteries will be a little less mysterious.

12 REPLIES 12

Re: Extension : Thingworx Event Subscription

I have never done this, and there are no examples but from what you can tell from Eclipse IDE autocomplete, you get the following

@ThingworxSubscriptions(subscriptions = {

  @ThingworxSubscription(source="", sourceProperty="", eventName="", enabled=true, handler="")

})

Not sure if this will work but you can try it. Source and Source property, eventName and enabled, seem obvious but handler, I don't know. Maybe looking at at an XML export of a ThingTemplate where you have defined a subscription might lead to come clues.

Re: Extension : Thingworx Event Subscription

hi, Thanks

I've found the same thing. This code is placed before class définition.

A subscription is created but i don't know where write the subscription code and where to found event input. I've look a XML export and i'm surprised to see a service definition in subscriptions tag.

Re: Extension : Thingworx Event Subscription

Yeah, I would imagine that is what the handler is, but how to write that and wire into Java code, I don't know. I have the same question posted in my lengthly list of questions elsewhere. Hopefully someone comes to our rescue - it's gotta be possible.

Re: Extension : Thingworx Event Subscription

To specify a subscription on the DataChange event in your example, you would want to do the following:

On the class, specify the following annotation (or similar):
@ThingworxSubscriptions(subscriptions = {

@ThingworxSubscription(eventName = "DataChange", sourceProperty="myProp", enabled=true, handler="SubscriptionHandler")

})

Then within your template or shape you will want to define the SubscriptionHandler service:

@ThingworxServiceDefinition(name="SubscriptionHandler", isPrivate=true)

public void SubscriptionHandler(

    @ThingworxServiceParameter(name="eventData", baseType="INFOTABLE", aspects = {"datashape:DataChangeEvent"}) InfoTable eventData,

    @ThingworxServiceParameter(name="eventName", baseType="STRING") String eventName,

    @ThingworxServiceParameter(name="eventTime", baseType="DATETIME") DateTime eventTime,

    @ThingworxServiceParameter(name="source", baseType="STRING") String source,

    @ThingworxServiceParameter(name="sourceProperty", baseType="STRING") String sourceProperty)

You should name the service something more useful than just SubscriptionHandler, I just used that as an example.

We're hoping to improve the documentation surrounding extension development in the next few releases, so hopefully some of these mysteries will be a little less mysterious.

Re: Extension : Thingworx Event Subscription

To elaborate a little on this topic - for those wanting to use Events and Subscriptions in Java code, with the 7.1.0 release of the Eclipse Plugin, there will be wizards to generate the code for Events and Subscriptions within your .java files.  Hopefully this will help take some of the mystery out of the whole process.


The release date is still unofficial, but is planned for the same week as the 7.1.0 ThingWorx Platform release.

Re: Extension : Thingworx Event Subscription

Hi,

I've encountered some problems similar to what's been discussed here.

1) Is there an equivalent of this in the Edge .NET Sdk. I looked at the com.thingworx.metada and com.thingworx.metada.annotations, and couldnt find neither a type definition nor an attribute for "ThingworxSubscription" or something like that.

2) Also, I do wonder if the implementation above differs from adding a subscription in the platform composer, and calling the remote service callback in the handler script in the platform server.

I would really appreciate your help.

Thanks

Re: Extension : Thingworx Event Subscription

Not directly, no.  The easiest way to do this would be to create a service on your Edge virtual thing, then create a subscription on the RemoteThing that binds to your virtual thing where the subscription calls that service, passing it the event data as a parameter.

Re: Extension : Thingworx Event Subscription

Hi,

Using the information above I modified the SimpeThing example to include at the top of the SimpleThing Java class:

@ThingworxSubscriptions(subscriptions = {

   @ThingworxSubscription(eventName = "DataChange", sourceProperty="SetPoint", enabled=true, handler="SubscriptionHandler")

})

/**
* A very basic VirtualThing with two properties and a service implementation.
* It also implements processScanRequest to handle periodic actions.
*/
public class SimpleThing extends VirtualThing {

and then, at the bottom of the class:

@ThingworxServiceDefinition(name="SubscriptionHandler", isPrivate=true)

public void SubscriptionHandler(

   @ThingworxServiceParameter(name="eventData", baseType="INFOTABLE", aspects = {"datashape:DataChangeEvent"}) InfoTable eventData,

   @ThingworxServiceParameter(name="eventName", baseType="STRING") String eventName,

   @ThingworxServiceParameter(name="eventTime", baseType="DATETIME") DateTime eventTime,

   @ThingworxServiceParameter(name="source", baseType="STRING") String source,

   @ThingworxServiceParameter(name="sourceProperty", baseType="STRING") String sourceProperty){

   LOG.info("SubscriptionHandler called!");

}

By doing this I was expecting that when, from within TWX composer whilst browsing my live Remote SimpleThing instance, I could set the SetPoint property to a new value and my SubscriptionHandler method would be called but this doesn't happen. I guess I've misunderstood something or missed a key point?

Thanks - Brad

Re: Extension : Thingworx Event Subscription

There's a difference between how you define a subscription in an extension and how you define a subscription on an Edge client.

Extension things add behavior directly to a Thing entity; they exist as persisted entities, so the behavior is predictable.

Edge virtual things don't exist on the Platform, they only exist on the Edge client.  The RemoteThing doesn't see subscriptions that the virtual thing is interested in, and (currently) the Edge SDK doesn't support these kinds of subscriptions.

What you can do is create the subscription on the RemoteThing and have it call a remote service (so it passes the data to the edge).  Or you can call AddDynamicRemoteSubscription on your RemoteThing from the Edge client, which effectively does the same thing but is more automatable.