You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用C#(AFSDK 2016版)创建PI Server Collective并设置主服务器

使用OSIsoft AFSDK 2016在C#中操作PI Server Collective

首先得明确一点:PI Collective的核心配置(比如添加服务器到Collective)通常是通过PI System Management Tools(SMT)完成的,但AFSDK确实提供了API来在代码中识别、管理Collective并设置主服务器,同时向其发送数据。下面是针对你的需求的完整代码示例和关键说明:

前提准备

  • 确保你的项目引用了OSIsoft.AFSDK(2016版本),可以通过NuGet或者直接添加本地DLL引用。
  • 运行代码的账号需要有PI Data Archive的管理员权限,才能修改Collective配置。

代码示例

1. 初始化AFSDK并连接到PI服务器

首先要初始化SDK环境,然后连接到目标PI服务器:

using OSIsoft.AF;
using OSIsoft.AF.PI;
using OSIsoft.AF.Time;
using OSIsoft.AF.Data;
using System.Linq;

class PICollectiveManager
{
    static void Main(string[] args)
    {
        // 初始化AFSDK环境
        PIServer.Connect();

        // 替换为你的PI服务器名称或Collective名称
        string collectiveName = "MyPICollective";
        PIServer collectiveServer = PIServer.FindPIServer(collectiveName);

        if (collectiveServer == null)
        {
            Console.WriteLine("找不到指定的Collective或PI服务器");
            return;
        }

        // 打开连接
        collectiveServer.Open();
        Console.WriteLine($"已连接到Collective: {collectiveServer.Name}");

2. 识别并配置Collective(若需要创建/修改)

如果你的Collective已经存在,直接获取即可;如果需要通过代码将现有PI服务器添加到Collective(注意:此操作需要管理员权限):

// 检查当前服务器是否是Collective
        if (!collectiveServer.IsCollective)
        {
            Console.WriteLine("当前连接的服务器不是Collective,需要先创建Collective");
            
            // 替换为要添加到Collective的其他PI服务器名称
            string secondaryServerName = "MySecondaryPIServer";
            PIServer secondaryServer = PIServer.FindPIServer(secondaryServerName);
            
            if (secondaryServer != null)
            {
                // 创建Collective(仅示例,实际操作需确认权限和服务器状态)
                PICollective newCollective = PICollective.Create(collectiveServer, "MyNewCollective", "Collective描述");
                newCollective.AddMember(secondaryServer);
                newCollective.Save();
                Console.WriteLine("Collective创建并添加成员成功");
                collectiveServer = newCollective.PIServer;
            }
        }

3. 设置Collective的主服务器

通过AFSDK可以指定Collective的主服务器,同样需要管理员权限:

// 获取Collective对象
        PICollective collective = collectiveServer.Collective;
        if (collective != null)
        {
            // 替换为要设置为主服务器的成员名称
            string primaryServerName = "MyPrimaryPIServer";
            PIServer primaryServer = collective.Members.FirstOrDefault(m => m.Name.Equals(primaryServerName, StringComparison.OrdinalIgnoreCase));
            
            if (primaryServer != null)
            {
                // 设置为主服务器
                collective.SetPrimary(primaryServer);
                collective.Save();
                Console.WriteLine($"已将{primaryServerName}设置为Collective的主服务器");
            }
            else
            {
                Console.WriteLine("找不到指定的主服务器成员");
            }
        }

4. 向Collective发送数据

连接到Collective后,发送数据的方式和普通PI服务器一致,AFSDK会自动处理数据路由到主服务器:

// 创建/获取PI点
        PIPoint piPoint = PIPoint.FindPIPoint(collectiveServer, "MyTestTag");
        if (piPoint == null)
        {
            // 如果点不存在,创建一个示例点
            PIPointConfiguration config = new PIPointConfiguration();
            config.PointClass = PIPointClass.Classic;
            config.PointType = PIPointType.Float32;
            piPoint = PIPoint.CreatePIPoint(collectiveServer, "MyTestTag", config);
            Console.WriteLine("创建测试PI点成功");
        }

        // 发送数据值
        AFValue value = new AFValue(123.45, AFTime.Now);
        piPoint.UpdateValue(value, AFUpdateOption.Replace, AFBufferOption.BufferIfPossible);
        Console.WriteLine("数据已成功发送到Collective");

        // 关闭连接
        collectiveServer.Close();
    }
}

关键说明

  • 权限注意:修改Collective配置(创建、添加成员、设置主服务器)需要PI Data Archive的管理员权限,普通只读权限只能查询Collective信息。
  • AFSDK版本兼容:2016版本的AFSDK已经支持PICollective类的核心操作,上述代码在该版本下可以正常运行。
  • 数据发送:向Collective发送数据时,不需要额外处理,AFSDK会自动将数据发送到当前的主服务器,主服务器会同步数据到其他Collective成员。

内容的提问来源于stack exchange,提问作者N.avraham

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:09:19