9 Replies Latest reply on Feb 1, 2018 2:13 PM by pmartin

    PI Vision and extension communication

    ayu

      I would like to have an extension that was aware of which AF Database the user is in as he/she browses through the Assets. Is this possible with just PI Vision extensions?

        • Re: PI Vision and extension communication
          pmartin

          Hi Alen,

           

          Just as a warning, this is an extremely hacky solution that relies on crawling through the DOM.  There is very likely a cleaner solution but it would take a while for me to figure it out.

           

          This solution checks for your current location in the Asset Browser every 5 seconds and writes it to the console.  Feel free to modify the logic to do whatever you need.

           

          window.PIVisualization = window.PIVisualization || {};
          (function (PV) {
            'use strict';
          
            function symbolVis() { }
            PV.deriveVisualizationFromBase(symbolVis);
          
            symbolVis.prototype.init = function (scope, elem, $interval){
                 var breadcrumb_parent = $('.breadcrumblist-parents');
          
                 var intervalTimer = $interval(function() {  
                           checkBreadcrumbs();
                  }, 5000);
          
                 function checkBreadcrumbs(){
                      if( breadcrumb_parent.children().length == 0){
                           console.log('not in a database');
                      }
                      else if(breadcrumb_parent.children().length == 1){
                           console.log(breadcrumb_parent.find('.breadcrumblist-parent-link:last-child .item-name').text());
                      }
                      else{
                           console.log(breadcrumb_parent.find('.breadcrumblist-parent-link:nth-child(2) .item-name').text());
                      }
                 }
            }
          
            var def = {
                 typeName: 'whereweat',
                 datasourceBehavior: PV.Extensibility.Enums.DatasourceBehaviors.Single,
                 visObjectType: symbolVis,
                 inject: ['$interval'],
                 getDefaultConfig: function(){
                      return{
                           DataShape: 'Value',
                           Height: 150,
                           Width: 150,
                           BackgroundColor: 'rgb(0,0,0)',
                           TextColor: 'rgb(0,255,0)',
                      };
                 }
            };
          
            PV.symbolCatalog.register(def);
          })(window.PIVisualization);
          
          6 of 6 people found this helpful
            • Re: PI Vision and extension communication
              ayu

              Hi Paul,

               

              Would it be possible to add event listeners onto the asset browser or is that not possible/too hacky?

              • Re: PI Vision and extension communication
                nicka

                Here's a less hacky solution which involves the 'assetSearchModel' injection:

                 

                assetSearchModel.currentState.listData.parent.Title

                 

                This returns the AF path for the selected element, or:

                 

                assetSearchModel.currentState.listData.children[i].Title

                 

                returns each of the child elements.

                 

                You can also invoke these functions from the browser command line using:

                 

                angular.element(document.body).injector().get('assetSearchModel').currentState.listData.parent.Title

                 

                 

                If you are only interested in whether or not you're in a database you can use the following (.parent is null if not in a database):

                 

                if(assetSearchModel.currentState.listData.parent != null){

                console.log("In a database");

                }

                1 of 1 people found this helpful
                • Re: PI Vision and extension communication
                  ayu

                  Is it possible to call these functions without using $interval? I've tried using $rootscope.$on with some broadcasts but couldn't get $on to fire.

                    • Re: PI Vision and extension communication
                      nicka

                      Most likely, but it wouldn't be very pretty, you're better of using an interval, is there any reason that you'd prefer not to?

                       

                      And when I say wouldn't be pretty, I mean, it would be *very* hacky, are require a lot more work than an interval.

                        • Re: PI Vision and extension communication
                          ayu

                          I was thinking that the extension only updated when the user selected a different database. I don't have a very good reason to not use interval. I'm playing around with the extension feature and wondering if it was doable without it.

                            • Re: PI Vision and extension communication
                              nicka

                              That would be doable, you'd simply need to store the last database selected, something like:

                               

                                      scope.lastDatabase = "";

                                      var intervalTimer = $interval(function(){

                                              var currentDatabase = assetSearchModel.currentState.listData.parent;

                                              if(currentDatabase != null){

                                                      if(currentDatabase != scope.lastDatabase){

                                                              scope.lastDatabase = currentDatabase;

                                                              console.log("User is in different database to the last one");

                                                      }

                                              }

                                      }, 250);

                               

                              I don't have access to a PI Vision server right now, but this is the gist of it, you may also need to check the length of `currentDatabase` to ensure that it is a database rather than an element.

                              1 of 1 people found this helpful
                        • Re: PI Vision and extension communication
                          Snehal118

                          I am creating one extension in PI vision and wants child elements of selected AF element. By using above solution got current location in asset Browser and currently selected element's children only. Is it possible to get child elements when one have path of parent element ?