2018-OSIsoft-PI World-Intro To PI System Dev Tech-Student
2018-OSIsoft-PI World-Intro To PI System Dev Tech-Student
Introduction to PI System
Developer Technologies
OSIsoft, LLC
1600 Alvarado Street
San Leandro, CA 94577 USA
Tel: (01) 510-297-5800
Web: http://www.osisoft.com
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or
by any means, mechanical, photocopying, recording, or otherwise, without the prior written permission
of OSIsoft, LLC.
OSIsoft, the OSIsoft logo and logotype, Analytics, PI ProcessBook, PI DataLink, ProcessPoint, Asset
Framework (AF), IT Monitor, MCN Health Monitor, PI System, PI ActiveView, PI ACE, PI AlarmView, PI
BatchView, PI Vision, PI Data Services, Event Frames, PI Manual Logger, PI ProfileView, PI
WebParts, ProTRAQ, RLINK, RtAnalytics, RtBaseline, RtPortal, RtPM, RtReports and RtWebParts are all
trademarks of OSIsoft, LLC. All other trademarks or trade names used herein are the property of their
respective owners.
Use, duplication or disclosure by the U.S. Government is subject to restrictions set forth in the OSIsoft,
LLC license agreement and as provided in DFARS 227.7202, DFARS 252.227-7013, FAR 12.212, FAR
52.227, as applicable. OSIsoft, LLC.
2|Page
Table of Contents
1. Welcome ................................................................................................................... 4
1.1 Directed Exercise – Check This Out! ........................................................... 4
1.2 Introducing the PI System ............................................................................. 5
3. AF SDK ...................................................................................................................19
3.1 Introduction ..................................................................................................19
3.2 Naming Conventions ...................................................................................20
3.3 Directed Exercise – Explore AF SDK Reference .......................................21
3.4 Directed Exercise – Read and Write Values with AF SDK .......................23
3|Page
2018 PI World Labs
1. Welcome
In this lab, we will take a look at the PI System from a software developer’s perspective. We will talk
about the use cases that each PI Developer Technology addresses and demonstrate the capabilities of
each of them. At the end of the lab, you will walk away with knowledge about the basic capabilities of
each of the PI Developer Technologies so that you will know which one to select for your custom
application and also know where to go if you want to dive deeper into them.
Anytime we come across this green button, you will perform a learning activity to
explore the different concepts presented in this chapter or section. Your instructor
will have directions.
At the end of this lab, you will use GitHub to download some open source code and modify some parts
of it to build and run your very own web application that connects to the PI System.
The end product should look like the following link. To preview this, open up internet explorer inside
your remote session and direct it to this web page:
http://localhost/
You can also get here by the “Plotting with PI Web API” bookmark on the bookmark bar. Your instructor
will direct you on use of this web application.
Building this Web App is straightforward when you take advantage of the code samples available on
GitHub. We’re going to debug this at the end of the lab using the downloaded source code, with minor
changes.
4|Page
Introduction to PI System Developer Technologies
The PI System is an infrastructure that sits between data sources in a facility, and the users and the
applications that require access to that data. A “data source” can be almost anything, and the data can
be accessed by employing many different methods, standards or client tools. The PI System is essentially
a “universal translator and aggregator” for plant data. Rather than developing with many different
standards, requiring specific languages or operating systems to access process data from different places
in the plant, you’ll only need to use one and that is the PI System. This class will focus on the kinds of
data that can be accessed using the PI System and the options you have when accessing this data
programmatically.
Let’s say for example, that you are tasked with designing an application to report on pump data. To do
so, you need data from several disparate sources. You’ll probably require time-series data from the
Pump SCADA system, times, dates and names from a maintenance database, and maybe location based
weather data that you have to scrape from a weather website. Utilizing a PI System, you could have a PI
Server in the centre gathering and storing all of this data, ready to be accessed easily with your choice of
standard methods of data retrieval.
As the developer, you are going to be accessing this data from the PI System. You won’t have to spend
time working out where the data is and how to access it.
5|Page
2018 PI World Labs
PI Interfaces, usually installed within the control system network, constantly gather time-series data
exposed by a data source, and continuously send that data through to the PI Data Archive. For example,
many organizations have a SCADA system that exposes data using the OPC standard and this data can be
gathered by the PI Interface for OPC DA.
1.2.3 PI Server
The central component of the PI System is the PI Server which stores, categorises and references data.
The PI Server is comprised of two main parts, the PI Data Archive, which efficiently stores time-series
data, and PI Asset Framework (PI AF), which categorizes and references not only time-series data, but
also relational data from all over the organization. Together, these server applications act as a high
performance view into any plant data you require, whether it be in real time, historical, static, relational,
or future.
In the first part of this lab, we’re going to take a look at what can be stored or referenced in the PI
Server, by reviewing the Asset Framework with the administrative tool, PI System Explorer. But first, we
should define some terms we’re going to use throughout this lab.
The Asset Framework (AF) Server is a part of the PI System. It contains asset or “metadata” usually
organized according to the assets containing the attributes being monitored. An Element is an object in
AF that can represent an asset, geographical location or process. To generalise, an Element is an object
that can be compared to another like object. Two boilers can be compared, two plants can be
compared, two different processes in a plant can be compared, and two company employees can be
compared; all of these things would be represented in AF as Elements. There’s one exception here,
events – such as a batch or downtime – as an element. There’s a different object type for that, the Event
Frame. Elements can be thought of as “permanent” objects.
AF can be helpful to users of the Data Archive who know the assets, but are not familiar with point
nomenclature. With Elements, data can be located without understanding the technical details of each
6|Page
Introduction to PI System Developer Technologies
piece of equipment. Organized assets (aka elements) assist in finding attributes associated with a
specific piece of equipment, or asset.
Attributes represent a unique property associated with an element in AF. If we have a “Pump” element,
as above, then it might have attributes such as Power, Status, Manufacturer, Type, Installation Date
and Bearing Temperature.
7|Page
2018 PI World Labs
PI Point, PI Tag and PI Data Stream have all been used historically and are all interchangeable terms. A PI
Point is a unique storage point for a stream of data in the Data Archive. It is a single point of measurement.
An example PI Point is the bearing temperature of a pump, read from the associated stream of data in a
SCADA system. The time-series data coming from the bearing temperature is stored in the PI Data Archive,
then referenced in AF to better represent its data context to a user or application.
8|Page
Introduction to PI System Developer Technologies
Use PI System Explorer (aka PSE) to view the data that the PI System can store or reference. PI System
Explorer is more of an administration tool than a client tool, but it lets us easily view the Asset
Framework structure in the PI Server. PSE can be described as something akin to SQL Server
Management Studio, but for Asset Framework. We’ll be looking at a demonstration database that can
be downloaded from our support site and slightly modified. For more information on this database and
to download the original, browse to:
https://pisquare.osisoft.com/community/all-things-pi/asset-based-pi-example-
kits
Exercise Objectives
List the kinds of data that can be shown in PI System Explorer (PSE)
Approach
2. Ensure you are connected to the “OSIDemo Pump CBM” database. You can tell this by looking at
the title bar of the PSE window, which should say “\\PISRV01\OSIDemo Pump CBM...” If you are
not connected to this database, select the “Databases” button in the top left of the window,
then select the “OSIDemo Pump CBM” database and press “OK”.
9|Page
2018 PI World Labs
PSE Components
1. Menu bar
2. Tool bar
3. Browser
4. Navigator
5. Viewer
6. Status bar
3. This database lists out a number of pumps at a pump station. Expand out the “Pump Station”
element, and select one of the pumps.
5. Take a look at the kind of data that is stored in attributes under the pump. Navigate around the
elements in the left hand pane and see what kind of data is under other assets.
10 | P a g e
Introduction to PI System Developer Technologies
Would it make sense to put ambient temperatures of the pump station in the AF Database? Where
would be the best place put the ambient temperature?
Would it make sense to put company employees and their details in the AF Database? How would
this look?
11 | P a g e
2018 PI World Labs
2.1 Introduction
PI Developer Technologies are designed to support the development of custom applications on top of
the PI System, as well as the integration of PI System data with other applications and business systems
such as Microsoft Office or SQL Server, Enterprise Resource Planning systems (ERPs), reporting and
analytics platforms, web portals, or geospatial and maintenance systems among other things.
They cover a wide range of use cases in various environments, programming languages, operating
systems and infrastructures. This chapter covers the different technologies in the family, including:
AF SDK provides comprehensive, high performance, Windows based .NET programmatic access
to the PI System. Other technologies in the family might use AF SDK calls internally.
PI Web API exposes a REST API, enabling operating system and device independent
programmatic access to the PI System.
PI SQL Framework family of products exposes the data on a PI Server as if it were on a relational
database. In this lab we will be focusing on PI OLEDB Enterprise and PI ODBC driver.
In this lab, we will also cover an extra programming technology that do not belong to the PI Developer
Technologies family but is nevertheless still useful to PI developers.
PowerShell Tools for the PI System provide a host of Microsoft PowerShell cmdlets to help with
the administration of the PI Data Archive and PI AF servers.
During this lab, you’ll get some hands on experience with each of the above technologies.
AF SDK
o Has the best performance of all PI Development Technologies.
o More methods and options available than any other Technology.
o Limited to Windows operating systems.
PI OLEDB Enterprise
o Compatible with other systems that use Structured Query Language (SQL).
12 | P a g e
Introduction to PI System Developer Technologies
PI Web API
o Operating system and programming language agnostic.
o Limited methods and options available compared with AF SDK.
13 | P a g e
2018 PI World Labs
2.3 Details
2.4 Documentation
AF SDK
https://techsupport.osisoft.com/Documentation/PI-AF-SDK/html/1a02af4c-1bec-4804-a9ef-
3c7300f5e2fc.htm
PI OLEDB Enterprise
https://livelibrary.osisoft.com/LiveLibrary/content/en/oledb-ent-v4/GUID-A5BDB5E5-8818-
4A71-BFE7-A846623ADA60
PI Web API
https://techsupport.osisoft.com/Documentation/PI-Web-API/help.html
PowerShell Tools
https://techsupport.osisoft.com/Documentation/PI-Powershell/title.html
14 | P a g e
Introduction to PI System Developer Technologies
2.5 Licensing
PI Developer Technologies are freely available for download. However, this does not mean that the PI
Developer Technologies are license-free. You need a PI System Access (PSA) license to deploy your
applications. You might think of this as a run-time license. The PSA runtime license enables end users to
access PI System data, including time series data in Data Archives and asset metadata in AF Servers,
using any of the PI Developer Technologies.
https://techsupport.osisoft.com/Troubleshooting/Alerts/AL00278
15 | P a g e
2018 PI World Labs
Questions
1. You want to use SQL Server Reporting Services to generate reports based on data that is present in
your AF structure
2. You want to develop a mobile application for displaying a dashboard showing the status of your
pumps in the plant
3. You want to develop a desktop application for Windows platform so that your users can view the
most recent downtime events for your drills
4. You want to automate writing the current temperature of your boilers to a text file everyday so
that this text file can be consumed by other services
5. You want to create an application to integrate PI Data with another third party Java API
16 | P a g e
Introduction to PI System Developer Technologies
One of the best resources you’ll find when developing with the PI System is
PI Square. Let’s get started!
Exercise Objectives
Log in to PI Square
Approach
https://pisquare.osisoft.com/community/developers-club
2. Click the “Log in” link on the top right of the page
3. If you already have an SSO account, use it to log in. If you don’t, create one with “Register”. If
you’re having trouble logging in, call the instructor over for assistance.
17 | P a g e
2018 PI World Labs
Take a look at the “PI .NET Framework (PI AF SDK)”, “PI SQL Data Access Technologies” and
“RESTful PI System Access” spaces. If you ever have questions on the use of these technologies,
these are the spaces to post in.
5. Click on the magnifying “search” icon on the top right of the page and type the following into
the search box that appears:
PI AF SDK guidelines
No matter what you do in your future development with the PI System, you should have a look
through these guidelines. Most of the principles and lessons do not only apply to AF SDK, but
development with the PI System as a whole.
18 | P a g e
Introduction to PI System Developer Technologies
3. AF SDK
3.1 Introduction
If you need to develop your own application to interact with the PI System, you have a few options. The
whole family of PI Developer Technologies is at your disposal. However, if you want to get the best
performance out of your application, the AF SDK has the greatest potential - assuming you’re developing
and running the application in a Microsoft Windows environment.
The AF SDK provides a comprehensive, Windows based, programmatic interface to the PI System. It
provides an object-oriented approach to interact with AF structures and data, as well as the PI Data
Archive, directly. The AF SDK is designed for easy access from Microsoft .NET languages like Visual Basic
.NET, C#, and Managed C++. The development machine and the machine that will eventually run any
applications developed with AF SDK must have at least Microsoft .NET Framework 3.5 installed.
However, for full use of all features we recommend using the AF SDK .NET 4.5 assemblies, which would
require .NET 4.5 to be installed on both the development machine, and any other machines that will run
your application.
Some reasons why you might choose to develop with the AF SDK:
You can quickly build complex AF hierarchies with the AF SDK. Many of our customers and
partners prefer to build AF hierarchies this way, rather than using the PI Builder plugin to
Microsoft Excel.
You have the power to create rich and customized Windows client applications with AF SDK.
Client applications such as PI Datalink are developed using the AF SDK and its predecessors.
The AF SDK is well suited to developing middleware applications sitting between the PI System
and other systems. AF SDK gives you flexibility on how you want to integrate other systems with
the PI System, and allows you to do so in the most resource efficient way possible. It is also
possible to automate element generation and/or synchronization between AF and other
systems.
During this chapter we will examine AF SDK documentation and develop a simple application using the
.NET 4.5 assemblies.
19 | P a g e
2018 PI World Labs
Data Archive PIServer A server application that stores high fidelity time-series
asset data.
20 | P a g e
Introduction to PI System Developer Technologies
Exercise Objectives
Approach
An important resource when developing with the AF SDK is the AF SDK Reference on the PI Live Library.
We’ll look at this reference and try to find some of the methods we’ll use in the next exercise. Our
instructor will guide you through the reference and highlight important features.
Navigate to livelibrary.osisoft.com
Scroll down the page to “</> Developer Technologies” and select “AF SDK Reference”. It takes
you to a different site, hosting the full reference manual.
Search the manual for the following methods, and write down what they do. We’ll use these
methods in the next exercise:
PISystem.Connect()
AFAttribute.GetValue()
21 | P a g e
2018 PI World Labs
AFAttribute.Data.UpdateValue ()
22 | P a g e
Introduction to PI System Developer Technologies
In this exercise, we’re going to write an application to get the latest value of a
pump’s attribute, and then change that value.
Exercise Objectives
Connect to a AF Server
Approach
2. Choose File > New > Project. Select Visual C# in the left pane. Then select Console App (.NET
Framework) in the right pane.
3. Enter a name for your project, select OK, then wait for your project to be built – may take a
while.
b) Select Assemblies > Extensions. You may need to wait a few seconds for these to load.
c) Check the checkbox next to OSIsoft.AFSDK, ensuring you select version 4.0.0.0 of the
extension.
d) Press OK.
5. Add the following using statements to the top of your code. These are mandatory; you’ll add
them to every application you write with AF SDK.
using OSIsoft.AF;
using OSIsoft.AF.Asset;
using OSIsoft.AF.Search;
23 | P a g e
2018 PI World Labs
6. Next, insert the following code to your ‘Main’ method to connect to your AF Server and the
OSIDemo Pump CBM AF Database.
7. Debug your application by pressing F5. Your application should connect to your PI System, and
show you the database it has selected. If successful, you will see:
8. Press Enter on the keyboard and you will be brought back to Microsoft Visual Studio.
9. The database we’re looking at is simple; in the real world you may have hundreds or thousands
of elements. To find an element with AF SDK in the real world, you’ll have to do a search. This is
how we’re going to find Pump02. Search the AF SDK reference for the AFElementSearch Class.
https://techsupport.osisoft.com/Documentation/PI-AF-
SDK/html/T_OSIsoft_AF_Search_AFElementSearch.htm
10. Find Pump02 using this class with the following code.
// Retrieve all pumps named "Pump02" then select the first result
AFElementSearch query = new AFElementSearch(PumpCBM, "ElementSearch", @"Name:Pump02");
int count = query.GetTotalCount();
if (count < 1) return;
Console.WriteLine($"Found {count} pump.");
AFNamedCollectionList<AFElement> pumps = new AFNamedCollectionList<AFElement>
(query.FindElements());
AFElement myPump = pumps[0];
Console.WriteLine($"Found pump - { myPump.Name }.");
Console.ReadLine();
12. After finding Pump2, we need to read the attribute called “Total Head” and read its current
value.
24 | P a g e
Introduction to PI System Developer Technologies
14. A proper solution at this point will have something like this:
15. Just to make sure you’ve got it, let’s do it again and get the value for Horsepower.
16. As we no longer need them, delete (or comment out) all code that is:
Console.ReadLine();
// change the Head value, and then write it to AF, remember to refresh PSE if watching the
values
THValue.Value = "42";
THValue.Timestamp = System.DateTime.Now;
THAttribute.Data.UpdateValue(THValue,OSIsoft.AF.Data.AFUpdateOption.Insert);
Console.WriteLine($"Changed HEAD at: {THValue.Timestamp} to {THValue.Value} and updated the
Pump Head value. Wait, maybe count to 6");
System.Threading.Thread.Sleep(6000);
Console.ReadLine();
18. Start debugging. You may have to wait to see the change. Refresh the display in PSE to see the
change in the Total Head value of Pump02.
25 | P a g e
2018 PI World Labs
Congratulations! You have read from, and written to, your first object in AF. The code you have written
is the beginnings of your own custom-built application.
26 | P a g e
Introduction to PI System Developer Technologies
4. PowerShell Tools
4.1 Introduction
OSIsoft has a group of PowerShell cmdlets that allow access to PI within the PowerShell context. It is not
our intention in this class to teach PowerShell or the correct way to use the cmdlets; only to show you
its capability. For full documentation go to
https://techsupport.osisoft.com/Documentation/PI-Powershell/title.html
PowerShell Tools for the PI System provide PI System administrators with many PowerShell cmdlets to
help with the administration of their Data Archive and Asset Framework servers. These cmdlets can be
used in a variety of ways such as including them in scripts for commonly needed functionality, or for
bulk system management operations.
PowerShell cmdlets are functions that follow the naming pattern of Verb-Noun, such as Get-PIValue,
which gets PI data, or Connect-AFServer, which connects to an AF server. In essence, a cmdlet is a script
that has been defined with a Verb-Noun name. They can be pipelined together, meaning the output of
one can be passed as input into another, leading to simplified scripts.
Search help for the following cmdlets, and write down what they do
Get-AFServer
27 | P a g e
2018 PI World Labs
https://msdn.microsoft.com/powershell/scripting/getting-started/fundamental/windows-powershell-
integrated-scripting-environment--ise-
We are going to create a simple script that retrieves a value from the AF system.
In this exercise, we will create a script that uses PowerShell PI cmdlets to retrieve a
value from an attribute in an AF pump CBM database.
Exercise Objectives
Approach
Press the Windows Start button and select Windows PowerShell ISE. The Windows PowerShell
Integrated Scripting Environment (ISE) is one of two hosts for the Windows PowerShell engine and
language. With it you can write, run, and test scripts in ways that are not available in the Windows
PowerShell Console. The ISE adds syntax-colouring, tab completion, IntelliSense, visual debugging, and
context sensitive Help.
Select the code, paste it in the Script pane, then press F5 to run it.
28 | P a g e
Introduction to PI System Developer Technologies
Now connect to the OSIDemo Pump CBM database. Test the code by running it in the command pane,
then paste to the script pane.
With the attribute object you can retrieve its value and display it.
Next, you can also update its value with a different number
29 | P a g e
2018 PI World Labs
Now save the script as Ex4.2.ps1 and run it by pressing F5. It will provide output such as:
30 | P a g e
Introduction to PI System Developer Technologies
Now retrieve the value of the Current Draw for all pumps.
Code solution:
31 | P a g e
2018 PI World Labs
5. PI SQL Framework
5.1 Introduction
PI OLEDB Enterprise,
PI OLEDB Provider
PI JDBC Driver
PI ODBC Driver
The products in this category expose PI asset structures and time series data as if the PI Server were a
relational database. That is, each exposes PI data (both real time and contextual) as tables available for
access via SQL queries. These technologies are typically used when integrating the PI System with other
systems that can import data in this format. This lab will focus on PI OLEDB Enterprise and PI ODBC
driver.
These Developer Technologies are particularly useful when the PI System has a role to play in Reporting
or Business Intelligence (BI) scenarios, where it must interact with other products or systems that
communicate using Structured Query Language (SQL). If you’re confused as to which one to use, you’ll
need to read the documentation of the software or device that you’re using to read the data and see
which standard it supports. For example, you could import data into a Microsoft SQL Server database
with either ODBC or OLEDB as it supports these two standards, but you’d need to use JDBC to import
data into an Oracle database unless you’re using an additional wrapper.
In general terms, if you want to access the PI System as if it were a relational database, you’ll most likely
use one of these four products.
5.2 Architecture
PI ODBC and PI JDBC rely on a middleware component called the PI SQL Data Access Server (PI SQL DAS)
for query execution. This allows reducing the software being installed on the client to a thin driver that
has no local dependency on bigger components such as a query engine and PI SDK or AF SDK.
PI SQL DAS in turn relies on the PI OLEDB Enterprise and PI OLEDB Provider query engines. There are two
main architecture scenarios, standalone and middleware.
32 | P a g e
Introduction to PI System Developer Technologies
In this architecture, all products are installed on one machine. Use the standalone deployment if a single
application must be supported with maximum performance.
33 | P a g e
2018 PI World Labs
In this architecture, all the heavy lifting is done in the middleware server. This allows just a thin and
lightweight driver to be installed on client machines. This is only applicable for PI ODBC and PI JDBC.
PI OLEDB Enterprise and PI OLEDB Provider does not depend on PI SQL DAS and can access the PI
Servers without it. To be clear, here are two query flows to show the difference.
PI OLEDB
PI ODBC/PI JDBC PI SQL DAS Enterprise/PI AF SDK/PI SDK PI Server
OLEDB Provider
PI OLEDB Enterprise/PI
AF SDK/PI SDK PI Server
OLEDB Provider
34 | P a g e
Introduction to PI System Developer Technologies
Therefore, you can expect that SQL queries targeting PI Data Archive that work on PI ODBC/PI JDBC will
also work on PI OLEDB Provider while SQL queries targeting AF that work on PI ODBC/PI JDBC will also
work on PI OLEDB Enterprise. You will see these in action in the following exercises.
The easiest way to see how PI SQL Framework works is to get straight into the action
with PI SQL Commander Lite. The queries we use here are in exactly the same form
as queries used with PI ODBC or PI JDBC – the only difference is the connection
protocol.
Exercise Objectives
Approach
Open “PI SQL Commander Lite” using the link on your taskbar. Follow these steps along with your
instructor.
Part 1: Elements
1. Right click on OLEDB Data Sources > PI OLEDB Enterprise > PISRV01 and choose Connect > OK
2. Expand OSIDemo Pump CBM > Asset > Tables
3. Right click on the “Element” table and “Execute Predefined Query”
4. There are only 2 rows found which correspond to the root elements. We want to find our
Pumps. Remove the “WHERE” clause from the query and execute again.
5. You will now see your full list of pumps in the results.
Part 2: Attributes
6. Open up a New Query Window using File > New
7. Right click on the “ElementAttribute” table and “Execute Predefined Query”
8. Remove the “WHERE” clause and execute the query again
9. You now see all attributes of all elements in the Pump CBM Database. Look through the table
and find the “Bearing Temperature” attribute of Pump02.
… Can’t find it? Why not?
35 | P a g e
2018 PI World Labs
This predefined query already has most of what we need, we just need to add the correct column in
order to see the name of the Element. The column with the Element name exists in the
“ElementHierarchy” table. Normally, you’re not this lucky and you need to join the tables up yourself. If
you’re already familiar with joining tables this should be fairly straightforward, but if you’re new to SQL
it can be a little confusing. Teaching about joining tables is outside the scope of this lab. If you’re in this
position, we recommend taking a class or looking at online resources that cover basic SQL queries.
11. Execute your query. You will now have the Element Name, along with every attribute of each
element all in one table.
Now, there aren’t any values, just other associated data. For values, we have to go to another
table…
36 | P a g e
Introduction to PI System Developer Technologies
Exercise Objectives
Approach
Modify the last query we ran so you can see the current value of the Bearing Temperature of Pump02,
along with the element name and attribute name only. For extra credit, display only Pump02 Bearing
Temperature value with a WHERE clause.
Hint: You’ll need to do something similar to step 10 in the previous exercise, but with
one extra table.
Solution
37 | P a g e
2018 PI World Labs
We will be executing the same query found in Exercise 5.3.1 using PI ODBC instead of
PI OLEDB Enterprise
Exercise Objectives
Approach
4. Copy and paste your SQL query from Exercise 5.3.1 into the Query Window
5. Execute the query
6. You should get the same results as Exercise 5.3.1
38 | P a g e
Introduction to PI System Developer Technologies
So far, we have been working with the Standalone scenario. We will now install PI
SQL DAS on another machine, PISRV02 to demonstrate the scenario where PI SQL
DAS is not on the same machine as the PI ODBC driver.
Exercise Objectives
Recognize that query processing can be done on a separate machine from the client
Approach
Create DSN
39 | P a g e
2018 PI World Labs
19. Copy and paste your SQL query from Exercise 5.3.1 into the Query Window
20. Execute the query
21. You should get the same results as Exercise 5.3.1
22. The resulting query flow is as below. Effectively, the heavy lifting has been outsourced to pisrv02
PI ODBC PI OLEDB
PI SQL DAS AF SDK AF Server
Driver Enterprise
(pisrv02) (pisrv02) (pisrv01)
(pisrv01) (pisrv02)
40 | P a g e
Introduction to PI System Developer Technologies
6. PI Web API
6.1 Introduction
PI Data
Archive
HTTP / HTTPS
PI Web API
Client
Server
PI Asset
Framework
In a world that is increasingly full of “Internets of Things”, it’s becoming more and more important to
integrate systems of different functions and standards with each other. The PI Web API makes it easy to
do just this; integrate the PI System with non-Windows systems such as Smartphones, UNIX servers or
other network-connected devices. The PI Web API gives rich, cross-platform access to the PI System from
any application that can send and receive HTTPS. Adding a PI Web API Server to a PI System will enable
client access to the PI Server via a RESTful interface.
Under normal operation, a client makes HTTPS calls to the PI Web API Service. The PI Web API Service
gets the data needed from the PI Data Archive and AF servers, and returns the data in the form of JSON
to the requesting client. The PI Web API also features an Index Search Crawler, a highly efficient way to
search for objects within the PI System. Every now and again, this service will crawl the entire system,
keeping an index of available objects for quick searching by clients that use this feature.
We will start exploring simple queries with a manual client called Postman, a plugin to Google Chrome.
Then, we will use source code downloaded from GitHub to see an application that trends data obtained
with the PI Web API.
41 | P a g e
2018 PI World Labs
Exercise Objectives
Approach
Open Google Chrome to the PI Live Library. Scroll down to “Developer Technologies” and select “PI
Web API Reference”. The Controllers section lists the top-level endpoints provided by the service. Each
controller's detail page provides links to the methods exposed by the controller. Use these pages as a
reference when programming client applications. Keep this Window open, you’ll refer to it in the next
exercise. Your instructor will guide you through the help, and you will go through the following
questions as a class:
1. Which PI Web API controller would you use for seeing the names and properties of child
elements of a given element in your AF database?
42 | P a g e
Introduction to PI System Developer Technologies
3. You are designing an application that requires the results of time-weighted averages of a few
different data streams. You can get the results of a summary calculation like this with PI Web
API. Which controller would you use?
4. Your application needs to be able to write back to an AF Attribute. Which controller would you
use to accomplish this?
43 | P a g e
2018 PI World Labs
Read & write data using PI Web API. Experience the Batch request feature of PI Web
API
Activity Objectives
Approach
Read data
1. Probably the simplest way to query PI web API is to use a browser. Open Chrome and type the
following into the address bar:
https://PISRV01/piwebapi/
This query retrieved the top level objects that PI Web API can see. We could do most of this
exercise with a browser in this manner, but we’re not only going to read data, but also write
data. Browsers can only perform “GET” requests on RESTful services, but we want to perform
“POST” requests too.
2. We’re going to use a client called Postman for the rest of this exercise. Postman is an extension
of Google Chrome that makes it easy to test RESTful services by manually crafting HTTP
requests. While you are learning to use the PI Web API you will most likely make many queries
using a tool like this. Open Postman from the Start menu.
3. Inside Postman, type the following into the address field, making sure that the GET method is
selected.
https://PISRV01/piwebapi/
44 | P a g e
Introduction to PI System Developer Technologies
After typing, hit enter or press the “Send” button. You will get a response similar to the above.
Note that the format of the response body is in JSON. For more information about JSON, see
https://www.json.org/
This returns the same top level properties of the PI Web API that we saw before in the browser
in Step 1. Note that none of these properties are actual production or configuration data, they
are links to other queries that we can further drill into. The retrieval of the top level properties
can also serve as a test that your PI Web API is functioning properly.
4. Let’s go one level deeper. Write the following in the address field (or just click the link for it
that’s already in the body field) and press enter or click “Send”:
https://PISRV01/piwebapi/assetservers/
45 | P a g e
2018 PI World Labs
Displayed above is the response body to the query we just ran. We are already familiar with
most of the properties that are returned. However, there is two different IDs for the AF Server
object. We see an “Id” and a “WebId”. The “Id” is the same unique AF Server ID that we see
when connecting with PI System Explorer. The WebId, however, is something new altogether.
When retrieving information, PI Web API encodes both the ID and path to the requested object
to form this “WebId”. This forms a new unique identifier for use in further queries. Most PI
Web API queries require one or more WebIds be included in the call.
When you make a query with an input WebId, the PI Web API decodes this WebId and finds the
object’s unique ID along with its path. The PI Web API then attempts to perform the query using
the object’s unique ID. If this fails, it will perform the query using the path instead. This gives
the PI Web API a redundant, efficient way to reference objects in the PI System.
5. In the previous call, the PI Web API gave us “Links”, pointing to queries that can be performed
on the returned object. Examine the syntax of the URL next to the “Databases” link. It is in the
form:
https://PISRV01/piwebapi/assetservers/’PI-Asset-Server-WebID’/databases
This query references the WebId of the AF Server, and asks for the databases that belong to that
server. Click on the link and click on the “Send” button.
6. Now we have our database names, IDs and associated information. Try to find the “OSIDemo
Pump CBM” database, then display its “Elements”. You will see a “Pump Station” element. Drill
down one level more by viewing its “Elements” and you’ll see a list of pumps. Get the attributes
of “Pump02”, then get “Interpolated data” for the “Bearing Temperature” attribute. You will get
a set of values something like this:
46 | P a g e
Introduction to PI System Developer Technologies
Write data
7. Up until now, we have been dealing exclusively with GET requests. The PI Web API can also
write to the PI Server with the use of POST requests. We’re going to use a POST to add a new
value to an existing element in our database.
We are going to write a value of “999” to the Bearing Temperature of Pump02, so we will have
to prepare a URL referring to the Bearing Temperature attribute, and a JSON packet with the
data that we want to send.
What method do we use to post a value to an attribute? We can use the methods under
“Stream” controller.
https://techsupport.osisoft.com/Documentation/PI-Web-API/help/controllers/stream.html
There are two Update methods, one for multiple values and one for a single value. We will be
using the single value one called UpdateValue. We need to make a query in the form
POST streams/{webId}/value.
47 | P a g e
2018 PI World Labs
Therefore, we will need the WebId of the attribute that we’re going to write a value to. We can
find this by sending a GET request to
https://PISRV01/piwebapi/attributes?path=\\pisrv01\osidemo pump
cbm\pump station\pump02|bearing temperature
8. Put the following into your address bar with the WebID:
Finally, click on the “Send” button. Look at the “Status” down below your value entry. You
should receive a “202 Accepted” status, but you may get “204 No Content”, meaning success
and there is no additional content.
9. Change the request back to GET and click the “Send” button. This will do a simple request for
the current value of your attribute. Confirm that the value’s attribute has been changed to
whatever you set it. Also check in PSE.
48 | P a g e
Introduction to PI System Developer Technologies
10. Notice that in order to write data, we had to first get the WebId with a GET request before doing
a POST request. What if we do not want to do two requests and we only wanted to do a single
request? Fortunately, this is possible with the “Batch” controller.
https://pisrv01/piwebapi/help/controllers/batch/actions/execute
We will be combining Steps 7 and 8 into a single POST request. Do a POST request to
https://PISRV01/piwebapi/batch with a JSON body of
{
"1": {
"Method": "GET",
"Resource": "https://PISRV01/piwebapi/attributes?path=\\\\pisrv01\\osidemo pump
cbm\\pump station\\pump02|bearing temperature"
},
"2": {
"Method": "POST",
"Resource": "$.1.Content.Links.Value",
"ParentIds": [
"1"
],
"Content":"{\"Value\":\"999\"}"
}
}
$.1.Content.Links.Value is a JSONPath Syntax which refers to the Value URL from the response of the “1”
request. The dollar sign ($) represents the root member of a JSON structure no matter it is an object or
array.
49 | P a g e
2018 PI World Labs
50 | P a g e
Introduction to PI System Developer Technologies
If you’re new to GitHub or just want to read more about how we’re using GitHub at OSIsoft, see our
community guidelines:
https://pisquare.osisoft.com/docs/DOC-1864
51 | P a g e
2018 PI World Labs
We want to give you the resources you need to continue your PI System
Development education after you leave this lab. One of the best ways to learn is to
download, inspect, and modify applications made by others.
Exercise Objectives
Debug an application
Approach
1. Open a browser and navigate to the OSIsoft LLC Organization GitHub page:
https://github.com/elee3/PI-Web-API-Samples
2. Click the “Download Zip” from “Clone or Download” button on the right hand side of the page:
3. Once your download has completed, find the file in your “Downloads” folder and extract it to
your desktop and extract the PI-Web-API-Samples-master folder to the desktop.
4. Find the pi-web-api-js-plotting.sln file inside the JavaScript\pi-web-api-js-plotting folder and
double click on it. Microsoft Visual Studio will open the project.
5. Try debugging with the “> Google Chrome” button at the top. You should see a web page open
with a few boxes and buttons in it – but the “Search for tags” button is not enabled. Press F12
on the keyboard to open Chrome’s developer tools and look at the errors shown. What’s the
52 | P a g e
Introduction to PI System Developer Technologies
problem?
6. This code runs the application we looked at right at the beginning of this lab, but attempts to
connect to a PI Web API server that doesn’t exist. If you’re running this when you’re back at
home, you’ll have the very same problem that we have right now. To correct this, open the
Scripts/app.js file. You’ll need to make the following changes to the file:
Replace all instances of ‘eugeneweb’ with ‘pisrv01’. You can do this with Ctrl+H dialog
box and fill out as below. Then use the Replace all button. 3 occurrences should be
replaced.
7. Save your code, and debug again with the “> Google Chrome” button. Type in CDT158 as the tag
name, enter a time range, and your trend should show data.
This open source project demonstrates how to create a simple web page using PI Web API to bring data
into the web page. It also shows the use of the PI Web API Swagger library. For example, the following
lines of code uses the Swagger library.
piwebapi.dataServer.getByName(piServer).then(function (data) {
piServerWebId = data.data.WebId;
$('#search-btn').removeAttr('disabled');
}, function (error) {
console.log(error);
});
The Swagger library allows you to compose PI Web API calls without having to build the complex URL
strings yourself. This helps to speed up development and also allows for more maintainable code. We
have already released several libraries for different programming languages. For more information,
please see:
https://pisquare.osisoft.com/community/developers-club/blog/2017/11/01/pi-web-api-client-libraries-
already-released
If you’re trying to configure this web application later and are having issues with security, another good
place to look for more information or advice is PI Square. If you can’t find an answer to your problems,
then the best way is to make a post!
53 | P a g e
2018 PI World Labs
7. Next steps
Congratulations on completing this lab. If you would like to learn more about the PI Developer
Technologies, here are some of the best places to go:
The OSIsoft GitHub repositories: https://github.com/osisoft. You can find source code for many
different example applications here. It may also help to search GitHub for the technology you’d like
to learn about for work done by other members of the community.
You can also sign up for a free 45 days PI Developers Club Trial here.
https://pisquare.osisoft.com/docs/DOC-3301-pi-developers-club-free-trial
https://pisquare.osisoft.com/docs/DOC-1101-pi-developers-club-faq
54 | P a g e
Save the Date!
Register your interest now to receive updates and notification early bird registration opening.
http://pages.osisoft.com/UC-CORP-Q3-18-
EMEAUsersConference_RegisterYourInterest2018.html
2018UCSF-LAB-100