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

Extension : Thingworx Event Subscription

jdelaiglesia
1-Visitor

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

ACCEPTED SOLUTION

Accepted Solutions

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.

View solution in original post

12 REPLIES 12

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.

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.

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.

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.

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.

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

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.

Thanks. So, how do I obtain my RemoteThing (that lives on the Thingwrox server) from my VirtualThing (running in my Edge device)?

If you did a bind by name, then you already have the answer, since you had to use the name of the RemoteThing to perform the bind (you can call getBindingName() specifically to get this name).  If you do a bind by identifier, then you can call the GetThingNameForIdentifier service on the EntityServices resource, which will give you the name of the remote thing.

asoma
5-Regular Member
(To:JesseR.Docken)

package com.thingworx.sdk.simplething;

import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.thingworx.communications.client.ConnectedThingClient;
import com.thingworx.communications.client.things.VirtualThing;
import com.thingworx.metadata.EventDefinition;
import com.thingworx.metadata.FieldDefinition;
import com.thingworx.metadata.PropertyDefinition;
import com.thingworx.metadata.ServiceDefinition;
import com.thingworx.metadata.annotations.ThingworxPropertyDefinition;
import com.thingworx.metadata.annotations.ThingworxPropertyDefinitions;
import com.thingworx.metadata.annotations.ThingworxServiceDefinition;
import com.thingworx.metadata.annotations.ThingworxServiceHandler;
import com.thingworx.metadata.annotations.ThingworxServiceParameter;
import com.thingworx.metadata.annotations.ThingworxServiceResult;
import com.thingworx.metadata.annotations.ThingworxSubscription;
import com.thingworx.metadata.annotations.ThingworxSubscriptions;
import com.thingworx.metadata.collections.FieldDefinitionCollection;
import com.thingworx.types.BaseTypes;
import com.thingworx.types.InfoTable;
import com.thingworx.types.collections.AspectCollection;
import com.thingworx.types.collections.ValueCollection;
import com.thingworx.types.constants.Aspects;
import com.thingworx.types.constants.CommonPropertyNames;
@ThingworxSubscriptions(subscriptions = {

@ThingworxSubscription(eventName = "speedover", sourceProperty="spped1", enabled=true, handler="SubscriptionHandler")

})
public class SimpleThing extends VirtualThing {
private static final Logger LOG = LoggerFactory.getLogger(SimpleThing.class);
private static final String SERVICE = "Service1";
private static final String SubscriptionHandler = null;


public SimpleThing(String name, String description, ConnectedThingClient client) throws Exception {

super(name, description, client);

super.initializeFromAnnotations();

}


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

public void SubscriptionHandler(

@ThingworxServiceParameter(name="eventData", baseType="INFOTABLE", aspects = {"datashape:speedover"}) 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("subscription is called");
}

 

I added Subscriptions in my code but still it is not working.

Hope some of you will be able to help me with this!!!

 Have a good day everyone!

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

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.

Announcements


Top Tags