4 Replies Latest reply on Oct 10, 2012 2:25 AM by hanyong

    How to get the number of events for a period of time?

    wilson.correa

      Hi All,

       

      I have to build a code in ACE (.NET) that will give me two results:

       

       - First:  Total of events for a tag in the past 24 hours.

       

       - Second: For the same 24 hours, the total of events for the tag that will be between a range value (less than one value and greater than other value).

       

      I couldn't find a way to do this so far. Any ideas?

       

      Thanks,
      Wilson

        • Re: How to get the number of events for a period of time?
          hanyong

          Hi Wilson,

           

          There are no bulid-in functions to get you the results that, the way you can find out the number of events for a certain time period by:

           

          piacepoint.Values(start,end).Count

           

          As the Values method doesn't support filtering, you can either loop through the returned values for the time period and find out how many values satisfy your condition. 

           

          Alternatively, you can deploy PI SDK directly in the PI ACE calculation and use the IPIData2.FilteredSummaries method.

           

           

           


            • Re: How to get the number of events for a period of time?

              Hello Wilson,

               

              just in case PI ACE isn't the only Option for you, using PI Totalizer subsystem would be the first option coming into my mind for your purpose. For the second one a filter expression can be used to identify those events that are supposed being considered.

               

              Gregor

                • Re: How to get the number of events for a period of time?
                  wilson.correa

                  Hi Hang and Gregor, thanks for the responses.

                   

                  I made an example code, but unfortunately, for the second case, I'm getting error (Object reference not set to an instance of an object), please check below part of the code:

                   

                   

                   

                  Time1 = "*-24h"

                   

                  Time2 = "*"

                   

                  Count_1.Value = Tag1.Values(Time1, Time2).Count                'Working!!

                   

                  '-------------------------------------------------------------------------------------

                   

                  Duration = "1d"

                   

                  Expr = "'Tag1' >= 1 AND 'Tag1' <= 50"

                   

                  ExtPICalc = DirectCast(PIServer, PISDK.IPICalculation)

                   

                  Results = ExtPICalc.FilteredSummaries(Time1, Time2, Duration, Expr,

                   

                                                       ArchiveSummariesTypeConstants.asAll,

                   

                                                       CalculationBasisConstants.cbTimeWeighted,

                   

                                                       FilterSampleTypeConstants.fstExpRecordedValues)

                   

                  Count_2.Value = Results ("Count").Value                   'Not Working...

                   

                   

                   

                  So, if you can figure out the problem, please, let me know.

                   

                  Thanks.

                   

                  Wilson

                    • Re: How to get the number of events for a period of time?
                      hanyong

                      Hi Wilson,

                       

                      You need to do a bit more to extract the results from the returned NamedValues collection from the FilteredSummaries call.

                       

                      Because Summaries and FilteredSummaries can breakdown the time period into smaller intervals and perform summary calculations for each of them. So you can get multiple values for 1 summary type. Thus the Value property of the NamedValue object is a PIValues collection. You can revise the code to be something like:

                      Duration = "1d"
                      Expr = "'Tag1' >= 1 AND 'Tag1' <= 50"
                      ExtPICalc = DirectCast(PIServer, PISDK.IPICalculation)
                      Results = ExtPICalc.FilteredSummaries(Time1, Time2, Duration, Expr,
                                                           ArchiveSummariesTypeConstants.asAll,
                                                           CalculationBasisConstants.cbTimeWeighted,
                                                           FilterSampleTypeConstants.fstExpRecordedValues)
                      NamedValue countSummary = Results("Count")
                      PIValues values = DirectCast(countSummary.Value, PISDK.PIValues)

                      ' go through PIValues collection to get the values

                      If your duration parameter is the same as Time2 - Time1, then the PIValues collection should have only 1 value that represents the count of the entire period.

                       

                      Also, your current SummariesType setting is set as asAll which means that you are calculating all supported summary types. You can cut down the amount of processing on the server end by just calculating count, ArchiveSummariesTypeConstants.asCount.