11 Replies Latest reply on May 4, 2017 11:50 AM by jdocken RSS
    jdelaiglesia Newbie

    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

      • Re: Extension : Thingworx Event Subscription
        jasong Creator

        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
          jdocken Explorer

          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
              thearonh Apprentice

              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
                brsmith Explorer

                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
                    jdocken Explorer

                    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.

                • Re: Extension : Thingworx Event Subscription
                  bkaba Newbie

                  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