Hi again with the second chapter. In the previous chapter we created a new project and added PI AF SDK library reference.

Just to be clear I made an mistake and add reference to 2.0 version of the library, but we need version 4.0. It is corrected now.

 

So we wand to connect to the server now, be need to differ between connecting to the AF Server and PI Data Archive. It is why I divided this chapter to two parts. One about AF Server and second about PI Data Archive.

 

What is very useful is structure of the library objects. You can find it here: PISystem Hierarchy.

 

 

How to read the diagram

Firs to understand is that objects with 's' at the end are usually collections of another objects.

Example: AFDatabases object is collection of AFDadatase objects.

If we would like to choose something from the collection we can use '[' and  ']'

Example:

AFDatabase myDatabase = myDatabases["database_name"]

where myDatabases is object of type AFDatabases

So basically:

- If I would like to get a collection of objects, I need to have object higher in the hierarchy.

If I would like to have collection of databases "AFDatabases", I need to have "PISystem" object.

- If I would like to get a single object, I need to have collection of this objects.

If I would like to get an "AFDatabase" object I need to have collection of databases "AFDatabases"

Connect to the AF Database

Now we know how to read the diagram so we can try to connect to the AF Database.

First we need to add using of Asset. We already added using of AF in the previous chapter:

using OSIsoft.AF;

, but we will need this too:

using OSIsoft.AF.Asset;

 

Rest of the code is written to the Main function.

namespace PI_Square_project

{
    class Program
    {
        static void Main(string[] args)
        {
              // Your code
        }
    }
}

// starts comments in the C#.

Initialize of the first object

First object in the diagram is PISystems and we need to have it at the beginning. So we need to create the object of type PISystems and construct this object using constructor.

Constructor is method  which is designed to build the object.

Method is used instead of function in object oriented programming.

Constructor is called with word "new" before the constructor method PISystems()

 

PISystems myPIsystems = new PISystems()

 

So with this line we obtain collection of known systems available from the machine which is running this application. This collection is now stored in the myPIsystems object.

 

Choose the System

Now we need to choose the AF Server. I want to show two options how to do this.

First is used, when you have just one system or when you want to work with default system. Second is used when you want to choose the AF Server by name.

Basically you need to create an object PISystem which will be representing your AF Server and put selected AF Server inside of this object:

a)

PISystem myAFserver = myPIsystems.DefaultPISystem;

b)

PISystem myAFserver = myPIsystems["name"];

Choose the Database

Now we are able to choose the database. First we need to get a collection of databases and after this we can choose a database.

There is option to select the Default Database or select database by a name agtooain.

AFDatabases myDatabases = myPIserver.Databases;
AFDatabase myDatabase = myDatabases["name"];

or

AFDatabase myDatabase = myDatabases.DefaultDatabase;

 

Connect to the PI Data Archive

This is easiest, because we don't need to choose the database.

1. We need to add using of PI objects:

using OSIsoft.AF.PI;

2. We need to construct the collection of available PI Data Archives and choose the right server

PIServers myPIservers = new PIServers();
PIServer myPIserver = myPIservers[“name”];

or

PIServer myPIserver = myPIservers.DefaultPIServer;

 

Recapitulation

So for connection do the database we can use this:

PISystems myPIsystems = new PISystems();
PISystem myPIserver = myPIsystems["name"];
AFDatabases myDatabases = myPIserver.Databases;
AFDatabase myDatabase = myDatabases["name"];

We can use the default.. options too.

Or faster version:

PISystems myPIsystems = new PISystems();
AFDatabase myDatabase = myPIsystems["name"].Databases["name"];

Her you can see the ‘dot’ notation. This is the biggest advantage of the object programming.

 

For PI Data Archive

PIServers myPIservers = new PIServers();
PIServer myPIserver = myPIservers["name"];

End

So this is all for this chapter. In the next chapter I would like to show how to print some information about objects which we created.