Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X
Hi,
I have two things contains same properties, want to call the a subscription on data change of those things properties.
If we write subscription inside things we can call respective on data change event which is fine. But we are looking common subscription which execute on data change of all things.
Is it possible to write common subscription, pass thing name dynamically to it at run time and check on data change property of that ?
Thanks,
Anil Kesana
You mean common code?
If that's the case, you can write a ThingShape which has the Subscription Code, and then just Add Up this ThingShape to the Things which should have the subscription and inside the Subscription just call the ThingShape's Implementing Subscription Service.
Carles.
If those things belong to the same template you should define the subscription at the template level.
Then it will apply at any Thing based on that template.
Same for ThingShapes
That is right, but in our case, we have some properties are thing specific we want to include them as well. If we write subscription template or shape level how can we include thing specific properties?.
If you subscribe to ThingStart event ( The problem here it's that you can only have one subscription to ThingStart event from the same thing ) you can check for property existence, and in the case that it exists add up the subscription by code.
if (me["my_thing_level_property_name"]!=undefined) {
me.AddDynamicSubscription({
propertyName: "my_thing_level_property_name",
thingName: undefined,
eventName: "DataChange",
serviceName: "mySubscriptionServiceImplementation"
});
}
Hi Anil, can you check the following: if you subscribe to AnyDataChange in a ThingShape, won't it react to the Thing level properties as well?
Yes Vladimir this is another option, but you will have the same problem pointed out with ThingStart event, once you subscribed to AnyDataChange you can subscribe to it anymore from another ThingShape/ThingTemplate...