Learn the Basics of PI Programming - Accessing the PI Data Archive through Rich Data Access

Version 2

    Exercise Objectives

    • Develop a custom console application in order to get data from the PI Data Archive.
    • Explore the objects from OSIsoft.AF.PI namespace.

     

    Problem Description

    PI AF SDK provides the ability to access the PI Data Archive directly without the need to use attribute references in the PI AF Server. This is useful when you need highly efficient writes to the PI Data Archive. Follow the steps below to build a console application that will:

    • Connect to the default PI Data Archive.
    • Get the snapshot, recorded and interpolated values from the PI Point Sinusoid.
    • Find all the PI Points whose name contains the letter A.
    • Get the following PI Point attributes from the sinusoid: location1, descriptor, excdev.
    • Write a value to the PI Point Sinusoid.

     

    Approach

    1.    Open Visual Studio on your development machine

     

    2.    Create a new project, and select Visual C# > Console Application. Name the project “AccessingThePIDataArchive”.

     

    3.    Add a reference to the PI AF SDK assembly to the project. The procedure may be slightly different depending on your version of Microsoft Visual Studio:

      • Click on Project > Add Reference...
      • Select Assemblies > Extensions.
      • Check only OSIsoft.AFSDK. Ensure you check the 4.0 version of the extension.
      • Press OK.

     

    4.    Add PI AF SDK namespace to the using Directive.

     

    using OSIsoft.AF.PI;
    using OSIsoft.AF;
    using OSIsoft.AF.Time;
    using OSIsoft.AF.Asset;
       

     

    5.    Connect to the default PI Data Archive.

     

    PIServers myPIServers = new PIServers();
    PIServer myPIServer = myPIServers.DefaultPIServer;
    myPIServer.Connect();
      

     

    6.    Find the Sinusoid PI Point.

     

    PIPoint myPIPoint = PIPoint.FindPIPoint(myPIServer, "sinusoid");
      

     

    7.    Get the snapshot value.

     

    AFValue SinusoidSnapshot = myPIPoint.Snapshot();
    Console.WriteLine("Snapshot from Sinusoid: " + SinusoidSnapshot.Value + "\t" + SinusoidSnapshot.Timestamp.LocalTime);
      

     

    8.    Get the recorded values from the last day.

     

    AFValues SinusoidRecorded = myPIPoint.RecordedValues(new AFTimeRange("*-1d", "*"), OSIsoft.AF.Data.AFBoundaryType.Inside, "", false);
    Console.WriteLine("Showing Recorded data from Sinusoid...");
    foreach (AFValue myValue in SinusoidRecorded)
    {
    Console.WriteLine(myValue.Value + "\t" + myValue.Timestamp.LocalTime);
    }
      

     

    9.    Get the interpolated values from the last day with one hour of interval.

     

    AFValues SinusoidInterpolated = myPIPoint.InterpolatedValues(new AFTimeRange("*-1d", "*"), new AFTimeSpan(0, 0, 0, 1, 0, 0, 0), "", false);
    Console.WriteLine("Showing Interpolated data from Sinusoid...");
    foreach (AFValue myValue in SinusoidInterpolated)
    {
    Console.WriteLine(myValue.Value + "\t" + myValue.Timestamp.LocalTime);
    }
      

     

    10.    Find all the PI Points that starts with the letter s.

    PIPointQuery query1 = newPIPointQuery(PICommonPointAttributes.Tag, OSIsoft.AF.Search.AFSearchOperator.Equal, "s*");
    IEnumerable<PIPoint> foundPoints = PIPoint.FindPIPoints(myPIServer, new PIPointQuery[] { query1 });
    foreach (PIPoint currentPIPoint in foundPoints)
    {
    Console.WriteLine(currentPIPoint.Name);
    }
      

     

    11.    Get the descriptor, location1 and excdev from the Sinusoid PI Point.

     

    IDictionary<string, object> PIPointAttributes = myPIPoint.GetAttributes(new string[] { "descriptor","location1","excdev" });
    Console.WriteLine("\n\nSinusoid PI Point attributes: \n");
    Console.WriteLine("descriptor = " + PIPointAttributes["descriptor"]);
    Console.WriteLine("location1 = " + PIPointAttributes["location1"]);
    Console.WriteLine("excdev = " + PIPointAttributes["excdev"]);
      

     

    12.    Start debugging your code checking the content of the variables on each line in order to have a glimpse about the logic of the program.

     

    13.    Write a few lines of code to change the current value of the point Sinusoid to 200 with a timestamp of the current time. You’ll need to use the PIPoint.UpdateValue method. Search the documentation for more information on this method. After changing its value, print the new value and timestamp to the console.

     

    Solution: There are many possible solutions here. You need to set the value and timestamp properties of an AFValue object, then write it back to the PI Data Archive with PIPoint.UpdateValue(). The below example reuses the SinusoidSnapshot object declared previously in the exercise.

     

    //set the value of the SinusoidSnapshot object to 200
    SinusoidSnapshot.Value = "200";
    //set the timestamp of the SinusoidSnapshot object to now
    SinusoidSnapshot.Timestamp = System.DateTime.Now;
    //use updatevalue to update the value of the sinusoidsnapshot object in the PI Data Archive
    myPIPoint.UpdateValue(SinusoidSnapshot,AFUpdateOption.Insert);
    //grab the current value again as proof that it was written to
    SinusoidSnapshot = myPIPoint.Snapshot();
    //print the value to the console
    Console.WriteLine("New Snapshot from Sinusoid: " + SinusoidSnapshot.Value + "\t" + SinusoidSnapshot.Timestamp.LocalTime);