Skip navigation
All Places > PI Developers Club > Blog > 2014 > February > 13

介绍:

 

PI Web Services 已经发布了较长的时间。 这种数据传输的方法可以向所有支持web services的平台发布数据。PI Web Services 丰富了PI系统数据传输的类型, 并使得跨平台的PI数据传输变得简单方便。PI Web Services可与PI Server和PI AF Server链接并协同工作。李捍永,OSIsoft COE 工程师已经写过一篇博客,关于PI Web Services如何与PI AF Server协同工作。这篇博客将使用一些C#程序的例子介绍PI Web Services与PI Server工作的基本方法。在这篇博客中,您将看到PI Web Services如何从PI Server中获取数据,及如何向PI Server写入数据。

 

首先,PI Web Service 可以通过 “Add Service Reference” 添加到一个新建的项目中。

1.jpg

在点击“Add Service Reference”后, 请输入PI Web Services安装后的URL  (详情请见PI Web Services的安装手册).  注意:请在“Namespace”处填写在本项目中,PI Web Services所使用的名字。

2.jpg

当上述的配置完成后,您可以开始在您的项目中使用PI Web Services了。您可以参考下面的例子:

 

 

 

GetSnapshot from PI server:

PIWebService.PITimeSeriesClient client = new PIWebService.PITimeSeriesClient();    // 创建一个PI Web Services的链接

string[] path = new string[1];

path[0] = @"pi:\\WIN-01ARJPPGTEJ\SINUSOID";  //  PI Web Services可使用批次的概念获取数据

                                                                                             这意味着您可以使用一个数组储存一系列PI点的路径,并获取这些PI点的快照值,而仅仅与PI Server进行一次通信

PIWebService.TimeSeries[] values = client.GetPISnapshotData(path);

foreach (PIWebService.TimedValue value in values[0].TimedValues)

Console.WriteLine("{0} --- {1}", value.Value, value.Time.ToLocalTime());

Console.Read();

 

GetPIarchiveData from PI Server:

 

与GetSnapshot类似, GetPIarchiveData可以使用一次通信获取多个PI点的归档值。

 

PIWebService.PITimeSeriesClient client = new PIWebService.PITimeSeriesClient();
PIWebService.TimeRange timerange = new PIWebService.TimeRange();
timerange.Start = "*-1h";
timerange.End = "*";

 

PIWebService.PIArcDataRequest[] list = new PIWebService.PIArcDataRequest[1];   // 创建一个PIArcDataRequest 数组来储存所有的条件信息。请看下面条件信息的详情:

 

PIWebService.PIArcDataRequest single = new PIWebService.PIArcDataRequest(); //  创建一个PIArcDataRequest实例将来会将这个实例添加到上面的数组中

 

PIWebService.PIArcManner manner = new PIWebService.PIArcManner();  //  PIArcManner 包含很多获取数据的过滤条件条件:

 

manner.Boundaries = PIWebService.PIArcMannerBoundaries.Inside;
manner.RetrievalType = PIWebService.PIArcMannerRetrievalType.Compressed;
manner.Updates = false;

 

single.TimeRange = timerange;

 

single.PIArcManner = manner;    //  将所有的 PIArcManner中的过滤条件添加到PIArcDataRequest实例中

 

single.Path = @"pi:\\WIN-01ARJPPGTEJ\cdt158";

 

list[0] = new PIWebService.PIArcDataRequest();

 

list[0] = single;    //  为了测试的简单明了,本次只创建一个PIArcDataRequest实例

 

PIWebService.TimeSeries[] values = client.GetPIArchiveData(list);
foreach (PIWebService.TimedValue value in values[0].TimedValues)
Console.WriteLine("{0} --- {1}", value.Value, value.Time.ToLocalTime());
Console.Read();

 

 

 

GetPISummaryData (Total) from PI server:

 

GetPISummaryData包括Average, Count, Maximum, Minimum, PStdDev, Range, StdDev and Total等功能.  这些公能实现的代码类似。因此,本次使用Total 作程序示例。

 

GetPISummaryData功能与GetPIarchiveData类似,都使用数组来存储条件信息。不同在于使用不同的类。

 

PIWebService.PITimeSeriesClient client = new PIWebService.PITimeSeriesClient();

PIWebService.PISummaryDataRequest[] list = new PIWebService.PISummaryDataRequest[1];  //  类名变为PISummaryDataRequest.

PIWebService.PISummaryDataRequest request = new PIWebService.PISummaryDataRequest();

PIWebService.PISummaryManner manner = new PIWebService.PISummaryManner();

 

manner.SummaryValue = PIWebService.PISummaryMannerSummaryValue.Total;  //  这里可以选择不同的统计方法,如Average, Count, Maximum, Minimum, PStdDev, Range, StdDev and Total.

manner.WeightType = PIWebService.PISummaryMannerWeightType.EventWeighted;
manner.Updates = false;

manner.Intervals = 1;   //  Intervals是一个非常重要得条件。

 

                                             它将觉得在给定时间段内有多少的返回值。

 

                                              如,如果Intervals = 10, 并且给定的时间段是*-1h”到 “*”, 之后,如果统计方法选择了Average, Maximum and Minimum,那么将有10个值返回。

 

                                             意思是,将给定的时间段做i10等分,每个返回值对应每一等分时间段的统计值。

// manner.TimeStep = "20m";  //  TimeStep是PI Web Services 2012的新参数。

 

                                                           这个参数可以代替Intervals来分割给定的时间段。

 

                                                           如,如果TimeStep = "40m", 并且给定时间段是"*-1h" 到“*"。

 

                                                           那么返回值只有一个,因为1小时内只有一个”40分钟“。

 

                                                           TimeStep的读取优先级高于Intervals。

 

                                                          意思是如果这两个参数都被配置,那么Intervals的配置将被忽视。

PIWebService.TimeRange timerange = new PIWebService.TimeRange();
timerange.Start = "*-1h";
timerange.End = "*";

request.Path = @"pi:\\WIN-01ARJPPGTEJ\cdt158";
request.TimeRange = timerange;
request.PISummaryManner = manner;

list[0] = new PIWebService.PISummaryDataRequest();
list[0] = request;

PIWebService.TimeSeries[] values = client.GetPISummaryData(list);
foreach (PIWebService.TimedValue value in values[0].TimedValues)
Console.WriteLine("{0} --- {1}", value.Value, value.Time.ToLocalTime());
Console.Read();

 

 

 

InsertPIData:

 

这是向PI Server写数据的方法。注意:如果使用这个方法,运行方法的账户必须有目的PI服务器的”写“权限。

 

如果上面的代码已经理解,那么下面的代码将会非常简单。

 

PI Web Services 2012 可以使用本地的 PI Buffer Subsystem来向PI Server或PI集群缓存并发送数据。详情请见PI Web Services 2012用户手册。

 

PIWebService.PITimeSeriesClient client = new PIWebService.PITimeSeriesClient();
PIWebService.TimeSeries[] input = new PIWebService.TimeSeries[1];
PIWebService.TimeSeries time_series = new PIWebService.TimeSeries();
PIWebService.TimedValue my_value = new PIWebService.TimedValue();

my_value.Path = @"pi:\\WIN-01ARJPPGTEJ\cdt158";
my_value.Time = DateTime.Now;
my_value.DataType = "dateTime";
my_value.Value = "300.00";
my_value.Status = null;

time_series.TimedValues = new PIWebServices.TimedValue[1];
time_series.TimedValues[0] = my_value;
input[0] = time_series;

PIWebService.TimeSeries my_result = new PIWebService.TimeSeries();
my_result = client.InsertPIData(input, PIWebService.InsertPIDataDuplicateSwitch.InsertDuplicate)

 

 

 

 

 

 

 

 

 

小结:

 

PI Web Services是在网络编程中非常有用的技术,可以跨越不同的平台,如.net 和 JAVA平台。PI Web Services在安全性和可管理性上优于PI API,因此,它将会在JAVA平台上代替PI API。

Edit: Please note, that PI Web Services is announced to become deprecated some time in future. Not only for this reason, we recommend to decide in favor of PI Web API for recent development projects. Even we consider below content kind of outdated, we've decided for keeping this post.

 

Introduction:

 

PI Web Services has been out for several years.  This PI Data Access package could work with any platform which allows web services technology as the adding reference.  PI Web Services riches PI Data Access type , and makes it easier to transfer PI data across different platforms.  This blog post will use some examples (C# code) to introduce the functions in PI Web Services working with PI Server.  There has been another blog made by Hanyong Lee to introduce how PI Web Services works with PI AF Server.  You can read that post here.  After reading this blog post, you should expect to be able to use PI Web Services to fetch values from the PI Server and insert values to a PI Point.

 

Firstly, PI Web Service could be added from “Add Service Reference” after creating a new project.

1.jpg

After clicking “Add Service Reference”, enter the URL which is the address after installing PI Web Services package (Please see the installation menu).  Note that Namespace field will determine the PI Web Services name in this project.

2.jpg

When the above configuration is complete you can start coding. Below you can find the examples:

 

GetSnapshot from PI server:

PIWebService.PITimeSeriesClient client = new PIWebService.PITimeSeriesClient();    // create PI Web Services connection

string[] path = new string[1];

path[0] = @"pi:\\WIN-01ARJPPGTEJ\SINUSOID";  //  PI Web Services will use batch concept to get the data.

                                                                                             This means you could use an array to store all the pathes of PI points which you need, and you will get all snapshot data of them just consuming one PI server connecting call.

PIWebService.TimeSeries[] values = client.GetPISnapshotData(path);

foreach (PIWebService.TimedValue value in values[0].TimedValues)

Console.WriteLine("{0} --- {1}", value.Value, value.Time.ToLocalTime());

Console.Read();

 

 

GetPIarchiveData from PI Server:

Same as GetSnapshot function, GetPIarchiveData could fetch the data for many PI points also.

PIWebService.PITimeSeriesClient client = new PIWebService.PITimeSeriesClient();
PIWebService.TimeRange timerange = new PIWebService.TimeRange();
timerange.Start = "*-1h";
timerange.End = "*";

 

PIWebService.PIArcDataRequest[] list = new PIWebService.PIArcDataRequest[1];   // create a PIArcDataRequest array to store all requests information. The details of the requests, please see the following.

 

PIWebService.PIArcDataRequest single = new PIWebService.PIArcDataRequest(); //  create one PIArcDataRequest for the PIArcDataRequest array above.

 

PIWebService.PIArcManner manner = new PIWebService.PIArcManner();  //  PIArcManner includes the following contains, which will be the conditions for fetching data:

 

manner.Boundaries = PIWebService.PIArcMannerBoundaries.Inside;
manner.RetrievalType = PIWebService.PIArcMannerRetrievalType.Compressed;
manner.Updates = false;

 

single.TimeRange = timerange;

 

single.PIArcManner = manner;    //  Put all the PIArcManner contains into the request list.

 

single.Path = @"pi:\\WIN-01ARJPPGTEJ\cdt158";

 

list[0] = new PIWebService.PIArcDataRequest();

 

list[0] = single;    //  To be easy for testing, this time, just one request in the request array

 

PIWebService.TimeSeries[] values = client.GetPIArchiveData(list);
foreach (PIWebService.TimedValue value in values[0].TimedValues)
Console.WriteLine("{0} --- {1}", value.Value, value.Time.ToLocalTime());
Console.Read();

 

 

GetPISummaryData (Total) from PI server:

GetPISummaryData includes Average, Count, Maximum, Minimum, PStdDev, Range, StdDev and Total function.  They will have the similar code.  Therefore, Total function will be as the example to show how they work.

GetPISummaryData function is similar with GetPIarchiveData to use an array to store the request conditions also.  The difference is the class name.

 

PIWebService.PITimeSeriesClient client = new PIWebService.PITimeSeriesClient();

PIWebService.PISummaryDataRequest[] list = new PIWebService.PISummaryDataRequest[1];  //  The class name of the request is changed to PISummaryDataRequest.

PIWebService.PISummaryDataRequest request = new PIWebService.PISummaryDataRequest();

PIWebService.PISummaryManner manner = new PIWebService.PISummaryManner();

manner.SummaryValue = PIWebService.PISummaryMannerSummaryValue.Total;  //  Here is the way to choose all different function, such as Average, Count, Maximum, Minimum, PStdDev, Range, StdDev and Total.

manner.WeightType = PIWebService.PISummaryMannerWeightType.EventWeighted;
manner.Updates = false;

manner.Intervals = 1;   //  Intervals is very important condition in the request list.

                                             This will determine how many results should be returned during the given time range.

                                              For example, if Intervals = 10, and the time range is from “*-1h” to “*”, there will be 10 results returned when using Average, Maximum and Minimum methods.

                                             This means the given time range will be average split into small time range, and the results will reflect the value of chosen method in these small time range.

// manner.TimeStep = "20m";  //  TimeStep is the new function for PI Web Services 2012.

                                                           This parameter could replace Interval to seperate the time range.

                                                           For example, it TimeStep = "40m", and the time range is from "*-1h" to "*".

                                                           There will be just 1 result returned, as there is only one "40 minutes" during 1 hour.

                                                           The priority of TimeStep is higher than Intervals.

                                                          This means if both of these 2 parameters are configued, Intervals will not work.

PIWebService.TimeRange timerange = new PIWebService.TimeRange();
timerange.Start = "*-1h";
timerange.End = "*";

request.Path = @"pi:\\WIN-01ARJPPGTEJ\cdt158";
request.TimeRange = timerange;
request.PISummaryManner = manner;

list[0] = new PIWebService.PISummaryDataRequest();
list[0] = request;

PIWebService.TimeSeries[] values = client.GetPISummaryData(list);
foreach (PIWebService.TimedValue value in values[0].TimedValues)
Console.WriteLine("{0} --- {1}", value.Value, value.Time.ToLocalTime());
Console.Read();

 

 

InsertPIData:

This is the method to write data into PI server.  Note: the account to run this method should have the write access to  the target PI Server.

The following code should be easy to understand is you understand the code above.

PI Web Services 2012 could use local PI Buffer Subsystem to buffer and fan data to PI Server or PI Collective.  The details, please see PI Web Services 2012 User Menu.

PIWebService.PITimeSeriesClient client = new PIWebService.PITimeSeriesClient();
PIWebService.TimeSeries[] input = new PIWebService.TimeSeries[1];
PIWebService.TimeSeries time_series = new PIWebService.TimeSeries();
PIWebService.TimedValue my_value = new PIWebService.TimedValue();

my_value.Path = @"pi:\\WIN-01ARJPPGTEJ\cdt158";
my_value.Time = DateTime.Now;
my_value.DataType = "dateTime";
my_value.Value = "300.00";
my_value.Status = null;

time_series.TimedValues = new PIWebServices.TimedValue[1];
time_series.TimedValues[0] = my_value;
input[0] = time_series;

PIWebService.TimeSeries my_result = new PIWebService.TimeSeries();
my_result = client.InsertPIData(input, PIWebService.InsertPIDataDuplicateSwitch.InsertDuplicate);

 

 

Conclusion:

PI Web Services is a very useful technology for network programming, and could work with many different platforms, like .net and JAVA.  PI Web Services is more security and managable than PI API.  Therefore, it could be the good technology to replace PI API on JAVA platform.

Filter Blog

By date: By tag: