4 Replies Latest reply on Aug 18, 2017 8:29 AM by benny

    Array of values from PIASK

    benny

      Since years I'm using VB.NET make graphs and tables from vaules in PI. So far I have always used PIOLEDB to connect to PI and fetch the values. Using a SQL something like:

      SELECT value from piarchive..piavg WHERE tag= 'Test-Value' AND time BETWEEN '01-12-2011' AND '01-01-2012' AND timestep= '1m' AND calcbasis='TimeWeighted'

      I'm now wondering if I can upgrade my code to use AFSDK. Maybe i'ts faster or at least more modern. :-) Reading the documentation I'm a bit confused. I have tried several snippets but none are giving me the desired effect. Basically I want a function giving me a array (or list or something) with the values over a given timeframe averaged in the fragments I want. Like two days of data,1 minute averages giving 1440 X 2 = 2880 values. The closed I came is interpolated values but this is just not good enough. Can someone point me in the correct direction or does someone has a code sample? I looked read hard on the net but found nothing.

        • Re: Array of values from PIASK
          Rick Davin

          Hi Benny,

           

          Sounds to me like you would be interested in the AFData.Summaries call if you were using AFAttributes, and for PIPoints there is there are 2 Summaries overloads here and here.

            • Re: Array of values from PIASK
              benny

              Yes, I had found that to but I can’t get it to work for me. Guess I have to define “instance” but can find no good example.

              Does anyone has a code sample for this function? Probably the solution is simple but I don’t see it yet. ☺

               

               

               

               

              Benny VERELST

               

               

               

              Van: Rick Davin

              Verzonden: donderdag 17 augustus 2017 14:57

              Aan: Verelst, Benny <Benny.Verelst@edfluminus.be>

              Onderwerp: Re:  - Re: Array of values from PIASK

               

              PI Square <https://pisquare.osisoft.com/?et=watches.email.thread>

               

               

              Re: Array of values from PIASK

               

              reply from Rick Davin<https://pisquare.osisoft.com/people/rdavin?et=watches.email.thread> in PI .NET Framework (PI AF SDK) - View the full discussion<https://pisquare.osisoft.com/message/102899-re-array-of-values-from-piask?et=watches.email.thread#comment-102899>

                • Re: Array of values from PIASK
                  Rick Davin

                  You can try this bit of C# code:

                   

                  PIServer dataArchive = new PIServers().DefaultPIServer;
                  PIPoint tag = PIPoint.FindPIPoint(dataArchive, "Test/Float32");
                  AFTimeRange timeRange = new AFTimeRange("t-2d", "t");
                  AFTimeSpan timeStep = new AFTimeSpan(TimeSpan.FromMinutes(1));
                  
                  IDictionary<AFSummaryTypes, AFValues> summaries = tag.Summaries(timeRange, timeStep, AFSummaryTypes.Average, AFCalculationBasis.TimeWeighted, AFTimestampCalculation.Auto);
                  AFValues averages = summaries[AFSummaryTypes.Average];
                  
                  Console.WriteLine(averages.Count); // should display 2880
                  

                   

                  Let me walk you through some of that since you seem new to AF SDK.

                   

                  Hopefully the first 4 lines are easy to understand.  You will note in Line 03 that I use a time range of 2 days and Line 04 I set the interval to 1 minute.

                   

                  Line 06 makes the Summaries call and returns an IDictionary object.  The key to the dictionary is the AFSummaryTypes because you can bundle more than one summary type in one call.  In this example, we only use one summary type, namely Average.

                   

                  So what do you do with that dictionary?  You peel back the AFValues collection for each summary type.  I could have used a foreach loop to iterate over all the key value pairs in the dictionary, but since I only asked for Averages, I jump straight to the heart of the matter and get the AFValues for the Average (see Line 07).

                   

                  Line 09, I display the AFValues Count to the console and it returns 2880.  The AFValues collection is not an array per se, but does implement IList<AFValue>.

                   

                  Here is the same example in VB.NET:

                   

                          Dim dataArchive As PIServer = New PIServers().DefaultPIServer
                          Dim tag As PIPoint = PIPoint.FindPIPoint(dataArchive, "Test/Float32")
                          Dim timeRange As AFTimeRange = New AFTimeRange("t-2d", "t")
                          Dim timeStep As AFTimeSpan = New AFTimeSpan(TimeSpan.FromMinutes(1))
                  
                          Dim summaries As IDictionary(Of AFSummaryTypes, AFValues) = tag.Summaries(timeRange, timeStep, AFSummaryTypes.Average, AFCalculationBasis.TimeWeighted, AFTimestampCalculation.Auto)
                          Dim averages As AFValues = summaries(AFSummaryTypes.Average)
                  
                          Console.WriteLine(averages.Count) ' should display 2880
                  
                  2 of 2 people found this helpful
                    • Re: Array of values from PIASK
                      benny

                      Rick,

                       

                      Thanks for the explanation. With this snippet I was able to make a working function giving me an array of values for a given time span and time step. It is still a bit rough but great starting point. Again thanks.

                       

                      For reference the function:

                       

                          Public Function GetValues_AFSDK(ByVal PIName As String, ByVal BeginTime As Date, ByVal EindTime As Date, resolution As Resoluties, ByVal Val As Boolean) As Object() 'via de AF-SDK

                              Dim dataArchive = New PIServers().DefaultPIServer

                              Dim tag = PIPoint.FindPIPoint(dataArchive, PIName)

                              Dim timeRange = New AFTimeRange(BeginTime.ToString, EindTime.ToString)

                              Dim SecInResolution

                              SecInResolution = ResolutieStap(resolution)

                              Dim timeStep = New AFTimeSpan(TimeSpan.FromSeconds(SecInResolution))

                              Dim summaries = tag.Summaries(timeRange, timeStep, AFSummaryTypes.Average, AFCalculationBasis.TimeWeighted, AFTimestampCalculation.Auto)

                              Dim averages = summaries(AFSummaryTypes.Average)

                              Dim ReturnArrayValues(averages.Count) As Object

                              Dim ReturnArrayDates(averages.Count) As Date

                              Dim ReturnArrayStatus(averages.Count) As OSIsoft.AF.Asset.AFValueStatus

                       

                              averages.GetValueArrays(ReturnArrayValues, ReturnArrayDates, ReturnArrayStatus)

                       

                              GetValues_AFSDK = ReturnArrayValues

                       

                          End Function

                       

                       

                       

                       

                       

                      Benny VERELST

                       

                       

                       

                      Van: Rick Davin

                      Verzonden: donderdag 17 augustus 2017 15:37

                      Aan: Verelst, Benny <Benny.Verelst@edfluminus.be>

                      Onderwerp: Re:  - Re: Array of values from PIASK

                       

                      PI Square <https://pisquare.osisoft.com/?et=watches.email.thread>

                       

                       

                      Re: Array of values from PIASK

                       

                      reply from Rick Davin<https://pisquare.osisoft.com/people/rdavin?et=watches.email.thread> in PI .NET Framework (PI AF SDK) - View the full discussion<https://pisquare.osisoft.com/message/102901-re-array-of-values-from-piask?et=watches.email.thread#comment-102901>