Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X
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?
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.
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.
I think I understand what you mean and I fixed it.
I created the subscriptions in source ThingTemplate and subsribe themselves, and send the value to the target Things.
Thank you very much!
We are haveing the same problem here. Unfortunatelly we cannot use a global subscription as we have Converge in place which currently blocks AnyAlert and AnyAlertAck subscription (see: https://support.ptc.com/appserver/cs/view/solution.jsp?n=CS230682)
What we now do is: Adding two subscriptions for e.g. errorCode_1000 property which:
on Alert:
-> send email
on AlertAck:
-> reset everything
this "send email" and "reset everything" is placed into a service.
Now we are generating a lot of properties out of an csv import generated. These properties will be added to a ThingShape so that we won't have to do this for each Thing.
How can we programmatically add this two subscriptions for each property and how can we add custom service code to execute the created services mentioned above as they need some input parameters (e.g. sendEmail needs the eventData, resetEverything needs the name of the property).
What we have tried:
subject.AddDynamicSubscription({
propertyName: propertyName /* STRING */,
thingName: undefined /* THINGNAME - leave blank to subscribe to own events*/,
eventName: "Alert" /* STRING */,
serviceName: "sendEmail" /* STRING <- needs eventData as input param */
});
subject.AddDynamicSubscription({
propertyName: propertyName /* STRING */,
thingName: undefined /* THINGNAME - leave blank to subscribe to own events */,
eventName: "AlertAck" /* STRING */,
serviceName: "resetEverything" /* STRING <- needs propertyName as input param*/
});
// no return
subject.EnableSubscription({
propertyName: propertyName /* STRING */,
thingName: undefined /* THINGNAME - leave blank to subscribe to own events*/,
eventName: "Alert" /* STRING */
});
// no return
subject.EnableSubscription({
propertyName: propertyName /* STRING */,
thingName: undefined /* THINGNAME - leave blank to subscribe to own events*/,
eventName: "AlertAck" /* STRING */
});
When we execute this service we get the following error message:
TypeError: Cannot find function AddDynamicSubscription in object com.thingworx.thingshape.ThingShape@3d6dd257.
What are we doing wrong here?
As a bit of a possibly expensive work around for now.
do a subscription on AnyDataChange and then you can pull the Alert conditions and check if an Alert fired and which one and handle that.
For the Alert Acknowledge, build a custom screen (to start you can duplicate the existing Alert Summary) and add your actions into the Alert Acknowledge.
Unfortunatelly the AnyDataChange subscription won't be triggered at Alerts... I think that the right solution would be to add AnyAlert or AnyAlertAck subscription to handle this problem. Bad that this won't work if you use remote accelerator or now converge...
However. How to add Subscriptions to Thingshapes programmatically?
DataChange should always be firing, the Alert won't always fire based on defined conditions.
i.e. An Alert can only fire, if a Property value changes. That Property Value change causes a DataChange event as well (as long as DataChange is set to Value or Always)
I added an AnyDataChange subscription in that ThingShape and used the following line to get some info about when the subscription will be triggered:
logger.debug("AnyDataChange Event received: '"+sourceProperty+"' Event: '"+eventName+"' - source: '"+source+"' - eventData: '"+eventData+"'");
If you are right then causing an alert for some property in that ThingShape should create a logger entry with Event: "Alert".
This is not happening.