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.