How to Programmatically Check If a PI Notification Service is Targeting a PI AF Database
Learn how to use the OSIsoft AN SDK to check if a PI Notification Scheduler service targets a specific PI AF Database, including code examples for robust monitoring and automation.
Roshan Soni
How to Programmatically Check If a PI Notification Service is Targeting a PI AF Database
For engineers working with OSIsoft PI System, knowing whether the PI Notification Scheduler service is actively targeting a specific PI AF Database can be crucial for maintaining robust operations and troubleshooting notification issues. Fortunately, the OSIsoft AN SDK (part of the Asset Framework SDK suite) offers straightforward ways to programmatically perform this check.
In this blog post, we’ll explore how you can use the ANServiceInformation class from the OSIsoft.AN.Service namespace to:
- Check if any Notification Scheduler service is running targeting your PI AF Database
- Retrieve information on which PI Notifications host is linked to your AF server
And, crucially, we will share robust code examples that make your solution both effective and user-friendly.
Understanding the Tools: ANServiceInformation Class
The ANServiceInformation class is your route to asking questions about Notification Scheduler services running in your PI environment. It offers several static methods, but two are especially useful:
GetHost: Returns the host name of the PI Notification Scheduler targeting a specific AF server. Throws an exception if no service is running.TryGetLocation: Returns a boolean indicating if the Scheduler is running, and provides the host name without throwing exceptions if the service isn’t found.
Example: Detect the Notification Service for a PI AF Database
Let’s see how to use both methods in practice. The following examples use C# with the AF and AN SDK APIs:
Using GetHost (Exception-Based Approach)
using OSIsoft.AF;
using OSIsoft.AN.Service;
PISystems piSystems = new PISystems();
PISystem afServer = piSystems["<your AF server name>"];
try
{
string notificationHost = ANServiceInformation.GetHost(afServer.ID);
Console.WriteLine($"PI Notifications host: {notificationHost}");
}
catch (Exception ex)
{
Console.WriteLine("No PI Notification Service is running: " + ex.Message);
}
While effective, this method isn’t the cleanest, as relying on exceptions for standard flow control isn’t considered best practice.
Using TryGetLocation (Preferred Approach)
TryGetLocation is a more robust method. It takes the AF Server and Database IDs, and it returns a boolean to indicate service availability, placing the host name in an out parameter.
using OSIsoft.AF;
using OSIsoft.AN.Service;
PISystems piSystems = new PISystems();
PISystem afServer = piSystems["<your AF server name>"];
AFDatabase afDatabase = afServer.Databases["<your database name>"];
string notificationMachine = string.Empty;
bool isServiceRunning = ANServiceInformation.TryGetLocation(
afServer.ID,
afDatabase.ID,
out notificationMachine);
if (isServiceRunning)
{
Console.WriteLine($"PI Notifications service is running on: {notificationMachine}");
}
else
{
Console.WriteLine("PI Notifications service is NOT running for this database.");
}
Why Use TryGetLocation?
- No Exceptions for Control Flow: Errors are handled gracefully with a boolean return value, improving reliability.
- Database Specific: You can target a specific AF Database for more granularity.
Conclusion
Checking the status of PI Notification Scheduler services programmatically is simple using the OSIsoft AN SDK. TryGetLocation is the preferred method for a smooth, exception-free experience. Regularly incorporating these checks can significantly streamline your PI System monitoring scripts, automated tests, or custom dashboards.
Do you have any other tips or want to share how you monitor or automate your PI notifications? Drop your thoughts in the comments below!
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