Connecting to a PI AF Database

To retrieve data from the PI Asset Framework, the PIAFDatabase object should be used. The following code connects to the default database on the default server, and prints its server name:

import PIconnect as PI

with PI.PIAFDatabase() as database:
    print(database.server_name)

The Asset Framework represents a hierarchy of elements, with attributes on the elements. The database has a dictionary of children, which you can loop over as follows:

import PIconnect as PI

with PI.PIAFDatabase() as database:
    for root in database.children.values():
        print("Root element: {r}".format(r=root))

The keys of the dictionary are the names of the elements inside. The following snippet first gets the first key in the dictionary, and the uses that to get the corresponding PIAFElement from the dictionary. Then from this element its PIAFAttribute are extracted:

import PIconnect as PI

with PI.PIAFDatabase() as database:
    key = next(iter(database.children))
    element = database.children[key]
    for attr in element.attributes:
        print(element.attributes[attr])

To get the data for the last 48 hours from a given attribute you need the PIAFAttribute.recorded_values method:

import PIconnect as PI

with PI.PIAFDatabase() as database:
    key = next(iter(database.children))
    element = database.children[key]
    attribute = next(iter(element.attributes.values()))
    data = attribute.recorded_values("*-48h", "*")
    print(data)

Note

Attributes on root elements within the database might not have meaningful summaries. To get a better result take a look at Finding descendants in the hierarchy below.

Finding descendants in the hierarchy

Whilst it is possible to traverse the hierarchy one at a time, by using the PIAFElement.children dictionaries, it is also possible to get a further descendant using the PIAFElement.descendant method. Assuming the database has a root element called Plant1 with a child element Outlet, the latter element could be accessed directly as follows:

import PIconnect as PI

with PI.PIAFDatabase() as database:
    element = database.descendant(r"Plant1\Outlet")

Note

Elements in the hierarchy are separated by a single backslash \, use either raw strings (using the r prefix, as in the example above) or escape each backslash as \\\\.

Searching attributes based on full path

To get the direct attribute based on the entire element/attributes path you can use the PIAFDatabase.search method. You can provide a single string or list of strings with the full path and returns a list of attribute objects.

import PIconnect as PI

with PI.PIAFDatabase() as database:
    attributes = database.search([r"Plant1\Outlet|Flow|PV", r"Plant1\Outlet|Flow|SP"])

Note

Elements in the hierarchy are separated by a single backslash \, use either raw strings (using the r prefix, as in the example above) or escape each backslash as \\\\.

Connecting to other servers or databases

When no arguments are passed to the PIAFDatabase constructor, a connection is returned to the default database on the default server. It is possible to connect to other servers or databases, by passing the name of the server and database as arguments to the PIAFDatabase constructor.

import PIconnect as PI

with PI.PIAFDatabase(server="ServerName", database="DatabaseName") as database:
    print(database.server_name)

Note

It is also possible to specify only server or database. When only server is specified, a connection to the default database on that server is returned. Similarly, when only a database is specified, the connection is made to that database on the default server.

A list of the available servers can be found in the PIAFDatabase.servers attribute. This is a dictionary, where the keys are the server names. To get the list of server names you can use the following code.

import PIconnect as PI
print(list(PI.PIAFDatabase.servers.keys()))

A list of the databases on a given server can be retrieved from the same PIAFDatabase.servers attribute. Each item in the dictionary of servers is a dictionary with two items, server and databases. The first contains the raw server object from the SDK, while the databases item is a dictionary of {name: object} pairs. So to get the databases for a given server you can use the following code:

import PIconnect as PI
print(list(PI.PIAFDatabase.servers["ServerName"]["databases"].keys()))