PI Web API vs AF SDK: A Detailed Comparison
This blog compares PI Web API and AF SDK, highlighting their differences in performance, use cases, and limitations. We'll explore their functionalities and provide code snippets to illustrate their usage. Discover which tool best suits your needs when interacting with PI Servers, accessing time-series data, and manipulating the PI Asset Framework.
Roshan Soni
In the world of OSIsoft’s PI System, both PI Web API and AF SDK (Asset Framework SDK) are essential tools that allow developers to interact with PI Servers, access time-series data, and manipulate the PI Asset Framework (AF). However, each tool has its strengths and weaknesses depending on the use case. In this blog, we’ll explore the differences between PI Web API and AF SDK, covering their performance, use cases, and limitations, and we’ll provide code snippets to illustrate their usage.
Overview of PI Web API and AF SDK
What is PI Web API?
PI Web API is a RESTful interface to the PI System that allows developers to interact with the PI Data Archive and Asset Framework using standard HTTP requests. It is platform-agnostic, which means it can be used with almost any programming language (Python, Java, JavaScript, etc.).
What is AF SDK?
AF SDK is a .NET-based software development kit that provides deep integration with the PI System and PI AF. It enables developers to create, modify, and manage PI data, Asset Framework elements, and much more using the .NET ecosystem (primarily in C# or VB.NET).
Key Differences Between PI Web API and AF SDK
1. Performance and Latency
AF SDK provides direct, in-process access to the PI System. This means operations are performed locally, resulting in low latency and high-speed performance, especially when handling large volumes of time-series data. In contrast, PI Web API uses HTTP(S) to communicate with the PI System, introducing overhead due to network calls and data serialization.
2. Platform and Language Support
- PI Web API is platform-independent and can be used from any environment that supports HTTP requests, such as Python, Java, JavaScript, etc.
- AF SDK, on the other hand, is built on the .NET Framework, which means it is limited to environments that support .NET (Windows primarily). However, it offers richer functionality in these environments.
3. Data Retrieval
- AF SDK allows for fast and efficient bulk data retrieval and writing, making it ideal for high-frequency operations on large datasets.
- PI Web API can retrieve data, but it incurs performance penalties due to the HTTP communication and lacks some bulk retrieval efficiency.
Use Cases of PI Web API vs AF SDK
PI Web API Use Cases:
- Cross-platform applications that need to interact with the PI System from non-Windows environments.
- Web-based applications where the PI data needs to be accessed remotely from web browsers or cloud-based systems.
- Lightweight RESTful integrations with other systems or applications.
AF SDK Use Cases:
- Internal enterprise applications running on Windows where speed and performance are critical.
- Complex PI System and AF configurations, such as bulk creation of elements, templates, and managing advanced PI analyses.
- Handling large data volumes in real-time where performance is paramount.
Code Snippets: PI Web API vs AF SDK
To help illustrate the differences, let’s look at some basic tasks in both PI Web API and AF SDK: retrieving a PI tag value and performing a search for AF elements.
1. Retrieving a PI Tag Value
Using PI Web API (Python Example)
import requests
import json
# Define the PI Web API endpoint and credentials
base_url = 'https://my-piwebapi-server/piwebapi'
pi_tag = 'sinusoid'
auth = ('username', 'password')
# Make the GET request to fetch the current value of the PI tag
url = f'{base_url}/streams/{pi_tag}/value'
response = requests.get(url, auth=auth, verify=False)
# Parse the JSON response and display the value
data = json.loads(response.text)
print(f"Current value of {pi_tag}: {data['Value']}")
In this example, you are making an HTTP GET request to the PI Web API to retrieve the current value of a PI tag (sinusoid). The data is returned in JSON format, and you can access the Value key to get the tag’s value.
Using AF SDK (C# Example)
using OSIsoft.AF;
using OSIsoft.AF.PI;
using OSIsoft.AF.Time;
using System;
class Program
{
static void Main(string[] args)
{
// Connect to the PI Data Archive
PIServer piServer = new PIServers()["MyPIServer"];
// Find the PI point (tag)
PIPoint piPoint = PIPoint.FindPIPoint(piServer, "sinusoid");
// Get the current value
AFValue currentValue = piPoint.CurrentValue();
// Output the value
Console.WriteLine($"Current value of sinusoid: {currentValue.Value}");
}
}
In the AF SDK version, you directly connect to the PI Data Archive and retrieve the current value of the sinusoid tag. This is done in-process, making the operation fast and efficient. AF SDK directly interfaces with the PI System without the overhead of HTTP requests.
2. Searching for AF Elements
Using PI Web API (Python Example)
# Define the URL for the element search
search_url = f'{base_url}/assetdatabases/{database_id}/elements?searchFullHierarchy=true&nameFilter=Tank*'
# Perform the search request
response = requests.get(search_url, auth=auth, verify=False)
# Display the list of elements matching the search criteria
elements = json.loads(response.text)
for element in elements['Items']:
print(f"Element Name: {element['Name']}")
Here, you are using the PI Web API to search for AF elements within a specific database. The nameFilter parameter allows you to search for elements that match a certain pattern (e.g., elements starting with "Tank"). This request also incurs the overhead of HTTP communication.
Using AF SDK (C# Example)
using OSIsoft.AF;
using OSIsoft.AF.Asset;
class Program
{
static void Main(string[] args)
{
// Connect to the AF Server and Asset Database
PISystem piSystem = new PISystems()["MyAFServer"];
AFDatabase afDatabase = piSystem.Databases["MyAFDatabase"];
// Search for AF elements that match a name pattern
AFElementSearch elementSearch = new AFElementSearch(afDatabase, "Tank Search", "Name:TANK*");
foreach (AFElement element in elementSearch.FindElements())
{
Console.WriteLine($"Element Name: {element.Name}");
}
}
}
In the AF SDK example, you are directly searching for AF elements using the AFElementSearch class. This method allows you to efficiently search the entire hierarchy with fewer performance penalties compared to PI Web API. The search operation is executed in-process, making it faster than the web-based search.
Performance Considerations
When to Use PI Web API:
- When you need remote access to PI data from different platforms (Linux, Mac, etc.).
- When you're building a cross-platform application or integrating PI System data with other web services.
- When ease of use and simplicity are more important than raw performance.
When to Use AF SDK:
- When you need high-speed access to large datasets or require real-time data processing.
- When your application needs full control over PI AF configurations such as creating complex analyses, templates, event frames, etc.
- When you’re building Windows-based enterprise applications that need deep integration with PI System and Asset Framework.
Conclusion
Both PI Web API and AF SDK are powerful tools for interacting with the PI System, but their strengths and use cases differ. PI Web API is ideal for remote, cross-platform, and web-based applications, while AF SDK excels in high-performance, in-process operations, and deep configuration of the PI System and Asset Framework.
For performance-critical applications that require bulk data access or advanced configurations, AF SDK is the clear choice. On the other hand, for applications that need platform independence and can tolerate a bit of overhead, PI Web API is a flexible and easy-to-use solution.
By understanding the differences between these two technologies, you can choose the right tool for your specific needs and use cases.
Further Reading:
This blog post provides an in-depth comparison of PI Web API and AF SDK, with examples to show how each is used in practice. Whether you're working on a cross-platform web application or a high-performance .NET-based enterprise solution, you can now make an informed decision about which tool best fits your needs.
Tags
About Roshan Soni
Expert in PI System implementation, industrial automation, and data management. Passionate about helping organizations maximize the value of their process data through innovative solutions and best practices.
No comments yet
Be the first to share your thoughts on this article.
Related Articles
Enhancing PI ProcessBook Trends with Banding and Zones: User Needs, Workarounds, and the Road Ahead
A look at the user demand for trend banding/zoning in OSIsoft PI ProcessBook, current VBA workarounds, UI challenges, and how future PI Vision releases aim to address these visualization needs.
Roshan Soni
Migrating PIAdvCalcFilVal Uptime Calculations from PI DataLink to PI OLEDB
Learn how to translate PI DataLink's PIAdvCalcFilVal advanced calculations—like counting uptime based on conditions—into efficient PI OLEDB SQL queries. Explore three practical approaches using PIAVG, PIINTERP, and PICOunt tables, and get tips for validation and accuracy.
Roshan Soni
Understanding PI Web API WebID Encoding: Can You Generate WebIDs Client-Side?
Curious about how PI Web API generates WebIDs and whether you can encode them client-side using GUIDs or paths? This article explores the encoding mechanisms, current documentation, and best practices for handling WebIDs in your applications.
Roshan Soni