9 Replies Latest reply on Mar 22, 2017 1:54 PM by jasong RSS
    jasong Creator

    Extension Package: Detecting ThingStart in a ThingShape (but not using the Event)

    I am developing a ThingShape in Java and I would like to detect ThingStart on the Thing(s) that my ThingShape is implemented by to get some default values set up for the my ThingShape class. The obvious way to do this is to self-subscribe to the ThingStart event, and I suspect that would likely work, but my requirements need to allow for ThingStart to still be be self-subscribed from any of these Things that my ThingShape will reside on.

     

    As of ThingWorx 6.x, you can no longer subscribe to events at multiple levels of the ThingTemplate and ThingShape stack, so once I subscribe to ThingStart, nothing else can do it, no other ThingShapes or Templates can, it's no unavailable. So I am looking for an alternative.

     

    This is easy with Things, as we have access to protected void initializeThing() which gets us an ability to initialize some things at what is essentially ThingStart.

    But I cannot find a way to detect this event in a ThingShape.

     

    I added a standard constructor, but the constructor seems to be fired at ThingWorx start.

     

    Are they any options here? If not, can there be in future versions? I have a workaround for this immediate need but I felt the question should be asked anyway.

      • Re: Extension Package: Detecting ThingStart in a ThingShape (but not using the Event)
        carlesc Heavyweight Champ

        Hi Jason,

         

        I faced the same problem on normal (Javascript) ThingShape's, ThingTemplates when they added the limitation on 6.5.

         

        What I came out was to add a ThingShape to all the things which it's the only one that subscribes to ThingStart event, and that ThingShape subscription it's the one which does the "constructor" function. The idea it's to call a "Init_"+Shape/Template name on each implemented / inherited, and then you just need to implement that one whenever you want to initialize something on the corresponding ThingShape/ThingTemplate. It's not the most efficient way, but without multiple subscriptions to the same thing/event from the same Thing, this it's the best we can get. It's a shame they limited it, still I don't understand why, and I had though conversations with R&D about it...

         

        Here it's the code of my ThingStart Subscription ( you can do it with a Try/Catch or with a check !=undefined on the Service Name ):

         

        // -- Initialize Implemented Shapes

        var implementedShapes = me.GetImplementedShapes();

        var nImplementedShapes = implementedShapes.rows.length;

        for (var i=0;i<nImplementedShapes;i++) {

          var shape = implementedShapes.rows[i];

          if (shape.name!=="myThingShapeName") {

              try {

                  me["Init_"+shape.name]();

              } catch(err) {

              }

           }

        }

         

        // -- Initialize ThingTemplate/Shape Hierarchy Top-Down

        var methodsToCall = [];

        methodsToCall.push("Init_"+me.name);

        var parentName = me.thingTemplate;

        methodsToCall.push("Init_"+parentName);

        var parent = ThingTemplates[parentName];

        while (parentName) {

            parentName = parent.GetBaseThingTemplate();

            if (!parentName) {

                break;

            }

            methodsToCall.push("Init_"+parentName);

            parent = ThingTemplates[parentName];

        }

         

        while (methodsToCall.length) {

            try {

                me[methodsToCall.pop()]();

             } catch(err) {

            }

        }

        • Re: Extension Package: Detecting ThingStart in a ThingShape (but not using the Event)
          brue Apprentice

          Jason,

           

          Would the initializeEntity() API help you? Here's its javadoc:

           

          "Initializes the internal state of the entity. This method may be called more than once on the same entity
          during initialization.

          All metadata, including aspects and configuration tables, are available at this point in the lifecycle.
          throws Exception if an error occurs during the initialization process"