9 Replies Latest reply on Nov 19, 2013 12:23 PM by Gregor

    PI Tag Snapshot Timestamp as Attribute

    JohnFors

      I've got a report I produce in XML format where I drive the XML format by AF modelling.  I.E. An element Template Name becomes the XML record name and all the attributes of the Element become Xml Attribute/Value pairs in the Xml-Record.  One of the element Attributes I'm trying to populate is the Snapshot time-stamp of a tag.  

       

      Is there a way to coherse the PiPoint datareference to put the timestamp into an attribute? Or some other way short of creating a custom Datareference.

       

       

       

      thanks 

        • Re: PI Tag Snapshot Timestamp as Attribute
          xwang

          Hi John,

           

          Yes, you could create a customer data reference.  You could go to "Training Center" to download the training material.  From part 8.16 or page 161, you could find the guide on how to do a customer data reference with one example.

           

          Xi Wang

           

          v-Campus team

            • Re: PI Tag Snapshot Timestamp as Attribute

              Hello John,

               

              You can also use a PI Performance Equation tag of point type Timestamp and reference it as AF Attribute for this purpose. The equation would be like follows:

               
              NextEvent('CDT158')
              

               

                • Re: PI Tag Snapshot Timestamp as Attribute
                  JohnFors

                  To resolve this issue I've ended up writing a custom Datareference.  As part of the of the configuration string I've included a parameter (example "ATT= |megaWatt") to provide a reference to the attribute where the timeseries data would be used to get the timestamp. I've added a second configuration string to allow for a timestamp to be provided calculated by the PITime specification (example "pitime= *-2h).  This all works great... but I wanted to cover one more option.

                   

                  I want to be able to provide a direct reference to a PI Tag name like specified by the PIPoint datareference (example "Tag= \\PiServer\Tagname").  My next question is: what is the best way to implement the functionality of getting the snapshot time of the referenced tag.  

                   

                  I'm thinking of using the PI SDK to directly get the value/timestamp of the tag.  I also thought I might be able to instanciate a temporary AFElementAttribute with the PIPoint DR saving me writing all that SDK code.  

                   

                  Hopefully I'm clear enought for an expert to give me an opinion on this.

                   

                  Thanks John Fors

                    • Re: PI Tag Snapshot Timestamp as Attribute

                      Hello John,

                       

                      Since version 2.5 AF SDK offers a .NET 4.0 based library, also referred to as Rich Data Access (RDA) AF SDK, that provides access to both PI AF Server and PI Server. For new applications like in your case, the recommendation is using this library rather than PI SDK. Please download and install PI AF Developer Tools 2012 (or later) from vCampus Download Center -> category "PI Asset Framework". After installing this package, please check the reference manual (%PIHOME%\Help\AFSDK.chm). Please be sure to read about What's new and .NET 4 Differences within the first chapter "AF SDK Reference". The 3rd chapter titled "Examples" should get you started.

                       

                      Lonnie has created some videos intended to get people started. Please visit the blog post he created yesterday Getting Started Using the PI System SDKs (AF SDK 2012)

                        • Re: PI Tag Snapshot Timestamp as Attribute
                          Rick Davin

                          For data references, you have 2 choices.  You can use .NET 3.5 or 4.0.  With 4.0, you could use the AFSDK based PI Point fairly easily.  For 3.5, you have COM issues to account for with PISDK.  Could get messy.  I wrote a similar DR to extract timestamps.  I limited it to existing attributes, i.e. the config string contains the name of the attribute whose timestamp you desire.  I leave to those outside the DR to get the referenced attribute's data correctly.  If they want a PI Point, a summarized PI Point, or an attribute using another data reference, that is NOT the concern of my timestamp DR.  In this way, I only have a  3.5 version, which doesn't  use PISDK, although the referenced attribute may (but again that's external to my DR).

                           

                          Whether you use PISDK, and therefore PITime, or AFSDK and AFTime, the resulting timestamp should be a .NET DateTime value.  I strongly recommend that the Kind=Utc.  PSE and other visuals will display it in local time but it works out better if internally it is Utc.

                            • Re: PI Tag Snapshot Timestamp as Attribute
                              JohnFors

                              My Data Reference project currently targets .NET 4, and I have installed the latest Developer tools 2012, so I should have access to all the Rich Data Access(SDK). My AFSDK.DLL is version 2.5.2.5396

                               

                              Instanciating a temporary AFAttribute appears to be possible.  Multiple constructors are available for the creating an AFAttribute not related to an AFElement.

                               

                              public AFAttribute(string path)   A string containing the dynamic attribute path used to specify the AFDataReference

                               

                              public AFAttribute(PIPoint piPoint)  configurers an attribute with the PIPoint Datareference

                               

                              It appears that the path to using any of the constructors has challenges:

                               

                              If I use the "string Path" to specify a AFDataReference, how do i derive the PIPoint DR "path"?

                               

                              The alternative constructor specifying a PIPoint looks attractive as the DOCUMENTATION for the OSI.AF.PI namespace looks to support getting a PIPoint object.  

                               

                              In Visual Studio when I view the reference assembly in the object browser, the OSIsoft.AF.PI namespace lists PIConfiguration,PIConnectionException, PIException, PIPointInvalidException, PISerialization, PITimeoutException.  

                               

                              But no PIPoint, PIServers, PiServer... lots missing that I would have expected from the documentation.

                               

                              Why would the full namespace of supported class' be available or visible to my project?

                                • Re: PI Tag Snapshot Timestamp as Attribute

                                  Hello John,

                                   

                                  Did you import the namespaces? For PIPoint this is OSIsoft.AF.PI as you stated.

                                   

                                  C#
                                  using OSIsoft.AF.PI;

                                   

                                  VB.NET
                                  Imports OSIsoft.AF.PI

                                   

                                  Alternatively, one can also the whole object name. Please see the following C# snippet:

                                   

                                   

                                   
                                  OSIsoft.AF.PI.PIServer mySrv = new OSIsoft.AF.PI.PIServers().DefaultPIServer;
                                  mySrv.Connect();
                                  OSIsoft.AF.PI.PIPoint myPt = OSIsoft.AF.PI.PIPoint.FindPIPoint(mySrv, "CDT158");
                                  OSIsoft.AF.Asset.AFValue myVal = myPt.Snapshot();
                                  

                                   

                                   

                                  To see what namespace a certain object belongs to, you can use Visual Studio's Object Browser. Please see the member information in below screenshot:

                                   

                                  3808.Clipboard04.jpg

                                   

                                   

                                    • Re: PI Tag Snapshot Timestamp as Attribute
                                      JohnFors

                                      Thanks Gregor for the snippet.  I've found my first error Friday in that I had referenced the wrong AF-SDK version in my project.  That resolved the access to the class' I needed. 

                                       

                                      Since then I've added the support for the PI-Tag access into an AF-Value.  Commented out is my original code to include some test code snippet.

                                       

                                      case sSourceType.tag:
                                                                  // check if the sMyAttr is null then set it up. 
                                                                  try
                                                                  {
                                                                      //string mypath;
                                                                      //mypath = "test";
                                                                      //OSIsoft.AF.AFPlugIn myPiPointDr = AFDataReference.GetPIPointDataReference(null);
                                                                      //attr = new AFAttribute(mypath);
                                                                      //attr.DataReferencePlugIn = myPiPointDr;
                                                                      //attr.ConfigString = sTAGstr;
                                                                      //sMyValue = attr.GetValue();
                                                                      OSIsoft.AF.PI.PIServer mySrv = new OSIsoft.AF.PI.PIServers().DefaultPIServer;
                                                                      mySrv.Connect();
                                                                      OSIsoft.AF.PI.PIPoint myPt = OSIsoft.AF.PI.PIPoint.FindPIPoint(mySrv, "SPS.EMS.FRISC.34KV.WFMETER.KV");
                                                                      sMyValue = myPt.Snapshot();

                                       

                                                                  }
                                                                  catch
                                                                  {
                                                                      throw new ApplicationException(String.Format("Unknown Tag Name= '{0}'", sTAGstr));
                                                                  }
                                                                  break;

                                       

                                      My DataReference now does nothing but throw and Exception:

                                       

                                       "Invalid dynamic attribute path '' was specified.
                                        Parameter name: path

                                       

                                      As seen in the PiSys-Explore attribute Value cell.  I've wrapped all my code in Try/catch and cannot even change the exception message with one of my own.

                                       

                                      This code was working until I changed the reference to the AF-SDK to be the correct version.  I've unregistered/registered multiply times and have even restarted the AF-Server service.

                                       

                                      It seems that the exception is coming from the DataReference base class but how can I get a stack trace to find?

                                        • Re: PI Tag Snapshot Timestamp as Attribute

                                          Hello John,

                                           

                                          With the code as you've posted it, I wouldn't know why this exception should occur because you have commented those lines dealing with path and attribute. When using your code as it is inside a console application, I am running into an exception only in case the referenced PI Tag doesn't exist.

                                           

                                          Why do you like to replace the original exception inside the exception handler? Did you create a handler for your "Unknown Tag Name" exception?

                                           

                                          Please review the following snippet on how to get the stack trace of the original exception.

                                           

                                           

                                           
                                                      catch (Exception ex)
                                                      {
                                                          Console.ForegroundColor = ConsoleColor.Red;
                                                          Console.WriteLine(ex.StackTrace);
                                                      }