7 Replies Latest reply on Mar 28, 2017 7:53 PM by rsun

    PI Web API, PI Coresight and Event Frames

    andre.furtado

      Hello,

       

      I'm trying to display on PI Coresight the last event of a Event Frame as a value symbol. I'm trying to achieve that through javascript by using a GET to this URL:

       

      /piwebapi/eventframes/{database WebID}/eventframes?templateName=T%20Hi&sorfield=starttime&sortorder=descending&starttime=*-2d&searchmode=inclusive&maxcount=1

       

      So I'm following this instructions (PI-Coresight-Custom-Symbols/tutorials/simplevalue at master · osisoft/PI-Coresight-Custom-Symbols · GitHub ) to create a symbol value symbol.

       

      My question is on this part:

       

      symbolVis.prototype.init = function (scope) {

         this.onDataUpdate = dataUpdate;

       

         function dataUpdate(data) {

         if(data) {

         scope.value = data.Value;

         scope.time = data.Time;

         if(data.Label) {

         scope.label = data.Label;

        }

        }

        }

      };

       

      This is where I get the data to the symbol, right? So for the scope argument I should use the GET from PI Web API?

       

      Also, Can I use JQuery getJSON for the GET?

       

      Is this the best way do it? Any help or insights would be appreciated!

       

      Thank you!

        • Re: PI Web API, PI Coresight and Event Frames
          Dan Fishman

          The data for the date update is disconnected from the Web API.  That data from the dataUpdate is from the Coresight item you dragged onto the display and will receive updates when that item updates.  You can make JQuery calls to make your GET request.

          1 of 1 people found this helpful
            • Re: PI Web API, PI Coresight and Event Frames
              David Golverdingen

              Yes, keep in mind that whatever you choose, value, table, timeseries, you will get simple data updates pushed every 5 seconds and detailed data every 2 minutes I believe. (or when changes are made on the symbol scope). The detailed data update is usable for getting a path for the data that is dragged on the screen. (The new added element where you want to get events frames for.) But you can choose to ignore all this, and use the data update just as a trigger for polling new data from PiWebAPI. If you don't need data updates just use it ones and block other update with a flag. An other option is to count the not detailed data updates to create a trigger for every minute or so.

              Put a console.log statement with the data object in you code and see for yourself in in the console. First an empty data object, then the detailed object, then a serie of simple objects etc.

            • Re: PI Web API, PI Coresight and Event Frames
              rsun

              Hi Andre,

              If you are going to use http request in AngularJS, you will have to inject '$http'. More detail here AngularJS

               

              Here is some sample code:

               

              (function (CS) {   

                 'use strict';   

               

                 var def = { 

                    typeName: 'name', 

                    displayName: 'name', 

                    iconUrl: 'Images/picture.png',

                    inject: ['$http''],

                    init: init 

                 };   

               

               

                 function init(scope, elem, $http) {

                        scope.value = function() {$http.get('PI_WEB_API_LINK').then(function(response){var obj = response.data;});};

                 }

              CS.toolCatalog.register(def);

              })(Window.Coresight);

              Thanks.

              Ricky

              1 of 1 people found this helpful
                • Re: PI Web API, PI Coresight and Event Frames
                  Felipe3105

                  Hi Ricky,

                  I`m trying to do the same thing as Andre

                   

                  I already know all the resquests I need to send to coresight to retrieve my data (tested on postman)

                  but I`m not beeing able to put this element in Coresight as you said.

                   

                  When I try to use your code, my console in the browser receives the following error: Uncaught TypeError: Cannot read property 'toolCatalog' of undefined

                   

                  If I use CS.toolCatalog will my element appear next to the others? Why can`t I use symbolCatalog?

                   

                  My .js is looks like this:

                   

                  (function (CS) { 

                     'use strict';

                   

                     var def = {

                        typeName: 'lastEvent',

                        inject: ['$http'],

                        iconUrl: '/Scripts/app/editor/symbols/ext/Icons/amcharts-radialgauge.png', //just for test

                        init: init

                     }; 

                   

                     function init(scope, elem, $http) {

                   

                           $http({

                                url: 'https://localhost/Coresight/Services/EventFrames/RelatedEventFrames',

                                method: "POST",

                                data: {

                                 "ElementPaths":"*",

                                 "StartTime":"-28800s",

                                 "EndTime":"*",

                                 "MaxCount":null,

                                 "tz":"UTC",

                                 "SearchFilter":{

                                    "Acknowledged":"0",

                                    "AFServerDatabaseName":"af:\\\\WIN-GQK3G4RLGHN\\Server",

                                    "AllDescendants":false,

                                    "Annotated":"0",

                                    "Ascending":false,

                                    "CategoryName":null,

                                    "ElementNames":null,

                                    "ElementTemplateName":null,

                                    "EventName":null,

                                    "EventState":"0",

                                    "MaxDurationInSec":null,

                                    "MinDurationInSec":null,

                                    "SearchMode":"4",

                                    "Severity":null,

                                    "EFTemplateName":null

                                 },

                                 "RequestedByUser":true

                              }

                            

                            })

                            .then(function(response) {

                                 console.log(response.data);

                                 var myresponse = response.data;

                   

                                 //assuming my last event is in the first elemnt

                                 var myevent = response.data[0];

                   

                                 //my query

                                 var fix = response.data[0].Path

                                 fix.replace("\\\\", "%5C");

                                 fix.replace("[", "%5B");

                                 fix.replace("{", "%7B");

                                 fix.replace("}","%7D");

                                 fix.replace("]","%5D");

                                 console.log(fix);

                   

                                 var query = 'https://localhost/Coresight/Services/EventFrames/TopLevelAttributes?eventFrameId='+ response.data[0].Id + '&path=' + fix + "&tz=UTC";

                                 scope.value = function() {

                                  $http.get(query)

                                      .then(function(response){

                                         var obj = response.data;

                                      });

                                   };

                                  },

                                  function(response) { // optional

                                      console.log("fail to fetch data");

                                  });

                     }

                   

                  CS.toolCatalog.register(def);

                  })(Window.Coresight);

                   

                  Do you know if I need to send my header with the token? Or it will go automaticaly since I`m using in coresight?

                   

                   

                  Thank you for your help!

                  Felipe