stang

AF SDK 开发基础篇

Blog Post created by stang on Aug 19, 2016

: 为了更好的利用站内资源营造一个更好的中文开发资源空间,本文为转发修正帖,原作者为OSIsoft技术工程师王曦(Xi Wang),原帖地址:AF SDK 基础篇

 

本帖旨在介绍AF SDK的基础功能,包括建立于AF服务器的连接,建立数据库,建立节点,创建节点属性及属性的底层数据连接。本帖所介绍功能不可直接粘贴使用。AF SDK只可使用在.net语言中,因此,以下均为C#程序。

 

连AF服务器

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OSIsoft.AF;
using OSIsoft.AF.Asset;
using OSIsoft.AF.Time;

namespace AF_Element_creation
{
    class Program
    {
        static void Main(string[] args)
        {
            string servername = "XWANG-KQ8HT8KU8";                                                                 // 给一个AF服务器名
            PISystem ConnectedAFServer = AFServerConnect(servername);                                              // 将服务器名作为参数传递给连接函数
            Console.WriteLine("the Connected AF Server is: {0}", ConnectedAFServer.Name.ToString());
            Console.Read();
        }

        static PISystem AFServerConnect(string servername)
        {
            PISystems myAFSDK = new PISystems();                            // 创建一个新的AF SDK实例
            PISystem myAFServer = myAFSDK[servername];                      // 创建AF服务器的链接
            myAFServer.Connect();                                                                    // 连接AF服务器
            return myAFServer;
        }

 

以下的所有函数均需要连接上AF服务器才可使用,因此,连接服务器部分不做重复。

 

1. 创建数据库:

 

static AFDatabase AFDBCreation(string DBName, PISystem ConnectedServer)
        {
            AFDatabase myDB = ConnectedServer.Databases.Add(DBName);                    // 加入一个新的数据库
            return myDB;
        }

 

2. 创建根节点:

 

static AFElement AFRootElementCreation(PISystem ConnectedAFServer)
        {
            AFDatabase ConnectedAFDatabase = ConnectedAFServer.Databases["test"];         // 连接数据库
            AFElement root = ConnectedAFDatabase.Elements.Add("test");                                  // 创建根节点
            root.CheckIn();                                                                                                                          // 执行CheckIn功能
            return root;
        }

 

3. 创建子节点:

 

static AFElement AFElementCreation(PISystem ConnectedAFServer)
        {
            AFDatabase ConnectedAFDatabase = ConnectedAFServer.Databases["test"];                // 连接数据库
            AFElement ConnectedAFElement = ConnectedAFDatabase.Elements["test"];                 // 连接根节点
            lock (ConnectedAFElement)                                                                                                            
            { 
                ConnectedAFElement.CheckOut();                                                                                          // 锁定根节点
            }
            AFElement newElement = ConnectedAFElement.Elements.Add("the first");                      // 创建子节点
            newElement.Description = "this is the description";                                                                // 增加子节点“描述”属性
            ConnectedAFElement.CheckIn();                                                                                                // 解锁子节点
            return newElement;
        }

 

4. 创建属性,及其连接值

 

static AFAttributeCreation(AFElement ConnectedAFElement)
        {
            lock (ConnectedAFElement) {ConnectedAFElement.CheckOut();}                                                                                                                // 锁定子节点
            AFAttribute newAFAttribute_forPIPoint = ConnectedAFElement.Attributes.Add("test for PI Point");                                                          // 创建属性
            newAFAttribute_forPIPoint.Type = typeof(double);                                                                                                                                             // 定义属性数据类型
            AFPlugIn Plugin = ConnectedAFElement.Database.PISystem.DataReferencePlugIns["PI Point"];                                                       // 定义数据连接源为PI数据点
            newAFAttribute_forPIPoint.DataReferencePlugIn = Plugin;                                                                                                                            // 定义属性的数据源连接为PI数据点
            newAFAttribute_forPIPoint.ConfigString = @"\\XWANG-KQ8HT8KU8\cdt158";                                                                                           // 连接PI点的路径

            AFAttribute newAFAttribute_for_RelationalDatabase = ConnectedAFElement.Attributes.Add("test for RelationalDatabase");         // 创建属性
            newAFAttribute_for_RelationalDatabase.Type = typeof(Int32);                                                                                                                      // 定义属性数据类型
            AFPlugIn Plugin2 = ConnectedAFElement.Database.PISystem.DataReferencePlugIns["Table Lookup"];                                          // 定义数据连接源为关系库数据
            newAFAttribute_for_RelationalDatabase.DataReferencePlugIn = Plugin2;                                                                                                // 定义属性的数据源连接为关系库数据
            newAFAttribute_forPIPoint.ConfigString = @"SELECT Number FROM Table1 WHERE people = 'beijing'";                                         // 使用SQL语句连接数据源数据
            ConnectedAFElement.CheckIn();                                                                                                                                                                         // 执行CheckIn功能
        }

 

5. 锁定与释放:

对于AF Element中的各种更改,首先要做的是锁定这个Element为自己操作;当更改完毕后,需要释放这个Element,使之可以被其他人操作。

锁定

private static void LockElement(AFElement e)
        {
            int retries = 0;
            while (retries < 60)
            {
                try
                {
                    lock (e) { e.CheckOut(); }

                    if (e.CheckOutInfo.IsCheckedOutThisThread)
                    {
                        return;
                    }
                }
                catch
                {
                    System.Threading.Thread.Sleep(1000);
                    e.Refresh();
                }

                if (e.CheckOutInfo != null &&
                    e.CheckOutInfo.CheckOutTime.UtcTime < DateTime.UtcNow.AddSeconds(-60))
                    retries++;
            }

            throw new ApplicationException("Cannot obtain lock");
        }:

释放

private static void UnlockElement(AFElement e)
        {
            if (e != null &&
                e.CheckOutInfo != null &&
                e.CheckOutInfo.IsCheckedOutToMe)
            {
                e.CheckIn();
            }
        }:

Outcomes