2 Replies Latest reply on Oct 26, 2016 4:49 AM by jgonçalves RSS
    jgonçalves Explorer

    Add a dynamic subscription

    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 */,

       url: "http://localhost/Thingworx/ThingTemplates/SimpleTestTemplate?reason=subscriptions%20%3A%20New%20subscription%20%3A%20Source%20updated%2C%20Code%20Updated%2C%20Timer5s%3ATimer%20%3A%20New%20subscription%2C%20Subscription%20Added%0A" /* 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!