YetAnotherForum
Welcome Guest Search | Active Topics | Log In | Register

Connect to PI AF using AFSDK v1.3 Options · View
RJK Solutions
#1 Posted : Tuesday, November 18, 2008 10:38:36 AM
Rank: Administration

Groups: Administration

Joined: 6/20/2008
Posts: 612
Location: Cheshire, United Kingdom.
For the first part of a series of AFSDK tips, we will show you how to connect to an AF server using the AFSDK that is supplied with PI-AF v 1.3x - note, code to achieve this is version 2 will follow shortly.

Using Visual Studio.Net create a new project (windows form is fine) and add 3 references to:

- OSISoft.PI.System
- OSISoft.PI.Modeling
- OSISoft.PI.UnitsOfMeasure

Then import the namespaces into your form class:

Code:
Imports OSIsoft.PI
Imports OSIsoft.PI.Modeling
Imports OSIsoft.PI.UnitsOfMeasure


Next create two class level variables, one to hold the AFDatabase object and the other to hold the AFModelFramework object:

Code:
Private AFDB As AFDatabase
Private AFSS As AFModelFramework


Now within our form load routine (or any routine of your choice) we create an instance of the "PISystems" class and attach our AFSS object to the Modeling subsystem of the PISystem.

Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim PISys As New PISystems
            AFSS = CType(PISys("piservername").SubSystems("Modeling"), AFModelFramework)
            If (AFSS Is Nothing) Then
                ' NO MODELING SUBSYSTEM AVAILABLE?
                Exit Sub
            End If
        Catch ex As Exception
            ' ERROR GETTING SUBSYSTEM
        End Try
    End Sub


Once we are connected to the Modeling SubSystem, we assign our AFDB object to the required database on the AF Server.

Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim PISys As New PISystems
            AFSS = CType(PISys("piservername").SubSystems("Modeling"), AFModelFramework)
            If (AFSS Is Nothing) Then
                ' NO MODELING SUBSYSTEM AVAILABLE?
                Exit Sub
            End If

            AFDB = AFSS.Databases("afdatabasename")
            If (AFDB Is Nothing) Then
                ' NO DATABASE IS AVAILABLE?
                Exit Sub
            End If
        Catch ex As Exception
            ' ERROR GETTING SUBSYSTEM
        End Try
    End Sub


And there you have it, connecting to an AF database using the AFSDK!
Principal Consultant
Real-Time Data Management @ Wipro Technologies
Sponsor  
 

OSIsoft vCampus is a subscription-based, online offering that consists of providing everything people need to develop applications on the PI System.
We invite you to take a "tour" of the OSIsoft Virtual Campus - also feel free to consult the FAQ  or contact OSIsoft vCampus for more details.
RJK Solutions
#2 Posted : Tuesday, November 18, 2008 1:22:55 PM
Rank: Administration

Groups: Administration

Joined: 6/20/2008
Posts: 612
Location: Cheshire, United Kingdom.
Create this as a reusable connection method for your custom applications and you could end up with something similar to this:

Code:
    Private PISys As PISystems
    Private AFDB As AFDatabase
    Private AFSS As AFModelFramework

    Private Function ConnectToAF(ByVal WhichPISystem As String, ByVal WhichDatabase As String) As Boolean

        Dim ret As Boolean = False

        Try
            ' Check PISystems object, create if required
            If (PISys Is Nothing) Then PISys = New PISystems

            ' Check AFModelFramework object, create if required
            If (AFSS Is Nothing) Then AFSS = CType(PISys(WhichPISystem).SubSystems("Modeling"), AFModelFramework)
            ' If object already existed, check it is set to correct PI System
            If Not AFSS.PISystem.Name.ToLower.Equals(WhichPISystem.ToLower) Then AFSS = CType(PISys(WhichPISystem).SubSystems("Modeling"), AFModelFramework)

            ' AFModelFramework now exists, so check AFDatabase object, create if required
            If (AFDB Is Nothing) Then AFDB = AFSS.Databases(WhichDatabase)
            ' If object already existed, check it is set to correct AF Database
            If Not AFDB.Name.ToLower.Equals(WhichDatabase.ToLower) Then AFDB = AFSS.Databases(WhichDatabase)

            ' Final checks to see if AF is connected
            ret = (Not (AFSS Is Nothing)) And (Not (AFDB Is Nothing))
        Catch ex As Exception
            ret = False
        End Try

        Return ret

    End Function


And to use this you would call:
Code:
        If ConnectToAF("af_server_name", "af_database_name") Then

        End If

Principal Consultant
Real-Time Data Management @ Wipro Technologies
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.