Skip to main content
1-Visitor
July 27, 2015
Question

How to add a subscription programmatically?

  • July 27, 2015
  • 1 reply
  • 6370 views

I want to subcribe values from other things, but the target things are created programmatically and the quantity is large. It will take too much time to create subscriptions mannually, so I want to add subscriptions programmatically too.

My script is like this:

var params = {

     propertyName: "sourceProperty",

     thingName: "sourceThing",

     eventName: "DataChange",

     serviceName: "DynamicSubscriptionTest"

}

Things["TargetThing"].AddDynamicSubscription(params);

But it looks like doing nothing. No subscription was added. Nothing happened. I added restart thing function, but it didn't work too.

So, how to use AddDynamicSubscription function? Or is there any other method to realize my intention?

1 reply

1-Visitor
July 27, 2015

Did you consider doing this the opposite way? If each of your Things trigger the subscription, usually we recommend adding that subscription to the ThingTemplate (or one of the ThingShapes) that the Thing is based on.

And if you have 'centralized' logic, you can use that subscription to call that service. (And just pass me.name to provide context)

This also saves you the need to manually (or programmatically) create subscriptions.

1-Visitor
July 28, 2015

I have tried this, but my things subscribe different things, for example:

My target ThingTemplate is Devices, and the instance Thing is Dev_1, Dev_2, Dev_3.

Dev_1 need to subscribe SourceSubDev_1a and SourceSubDev_1b;

Dev_2 need to subscribe SourceSubDev_2a and SourceSubDev_2b;

Dev_3 need to subscribe SourceSubDev_3a and SourceSubDev_3b.

SourceSubDev Things have their ThingTemplate and created programmatically.

When creating subscriptions in Devices (ThingTemplate), I must choose a Thing source, not a ThingTemplate, so I don't how to do.