Community Tip - Did you get an answer that solved your problem? Please mark it as an Accepted Solution so others with the same problem can find the answer easily. X
Hi all!
I want to add a subscription dynamically, on run-time, from the mashup. I tried a solution provided on the post with the title "AddDynamicSubscription help", and it worked as expected, but the problem is that the properties and services of the thing template, for which the subscription is created, are deleted on the process.
The solution provided by Costin Badic is the following:
"Hi Jenna,
The indicated way to add a subscription is at ThingTemplate level, but why is it necessary to add them dynamically at runtime? Normally, these subscriptions are defined in the Composer and as new Things based on that template are created, they will inherit the subscription.
There is no straight solution for this, but I can show how you can add a subscription to a template dynamically, at runtime, by leveraging the ThingWorx REST API. Basically, it is the same request that it would be made if you had defined the subscription in the Composer, only you can use a snippet to "put" the JSON to the server, instead of the browser doing that for you.
Assume you have a thing template called SimpleThingTemplate and you want to subscribe to a Timer5s Timer event in order to execute some code (in my simple example, I just log something in the Script log). The service used in this case is PutJSON in order to make an HTTP(s) PUT request to the server. Nor all parameters are necessary, but the body of the request is very large. I have highlighted the important parts.
In order for the request to be successfully completed, you need a form of authentication. For testing purposes, I used a username and a password but normally you should use an application key. The result of the service execution is irrelevant so you can eliminate the var result at the last line.
Other than that, you need just to pass the URL and the JSON request content like below.
You can test this and let me know whether you have additional questions.
Costin
var requestContent = {
"entityType":"ThingTemplates",
"effectiveThingPackage":"ConfiguredThing",
"thingShape":
{"propertyDefinitions":{
},
"subscriptions":
{"Timer5s:Timer":
{"enabled":true,"source":"Timer5s","eventName":"Timer","serviceImplementation":
{"allowOverride":false,"description":"","name":"Timer5s:Timer","configurationTables":
{"Script":{"description":"","isMultiRow":false,"name":"Script",
"dataShape":{"description":"","name":"","fieldDefinitions":{"code":
{"baseType":"STRING","description":"code","name":"code","aspects":{
},"ordinal":0}
}}
,
"rows":[{"code":"logger.warn(\"subscription added\");\n"}
]}
},"handlerName":"Script"}
}}
,
"eventDefinitions":{
},
"serviceImplementations":{
},
"serviceMappings":{
},
"serviceDefinitions":{
}} ,
"thingTemplate":"GenericThing",
"name":"SimpleTestTemplate",
"id":"SimpleTestTemplate"
};// JSON
var params = {
proxyScheme: undefined /* STRING */,
headers: undefined /* JSON */,
ignoreSSLErrors: undefined /* BOOLEAN */,
useNTLM: undefined /* BOOLEAN */,
workstation: undefined /* STRING */,
useProxy: undefined /* BOOLEAN */,
withCookies: undefined /* BOOLEAN */,
proxyHost: undefined /* STRING */,
content: requestContent /* JSON */,
timeout: undefined /* NUMBER */,
proxyPort: undefined /* INTEGER */,
password: "admin" /* STRING */,
domain: undefined /* STRING */,
username: "Administrator" /* STRING */
};
// result: JSON
var result = Resources["ContentLoaderFunctions"].PutJSON(params);"
Does anyone have any idea to avoid this situation, and keep all thing template data after the subscription is created using this process?
Thank you!
The code given here doesn't seem like it should be deleting properties and and services. Are you saying that after you run the above code, and that code alone, your thing template has a subscription, but has none of its services or properties remaining? What version of ThingWorx is this?
Thank you for your reply.
Yes, I just run only this code and the behavior is what you describe. The version I'm using is 7.2.3-b48.
Cheers!