8 Replies Latest reply on Jul 22, 2010 10:50 AM by andreas

    Equivalent PI-SDK method call for the PI-DataLink Advanced Calculated Data function

    rbungubung

      Hello,

       

      I'm wondering if anyone knows what the equivalent PI-SDK method to do the same thing as the Advanced Calculated Data function in PI DataLink?

       

      I need to retrieve the same data as what the Advance Calculated Data function is doing in PI DataLink with the following parameters:

       

      - Calc. Mode = "total"

       

      - Calculation Basis = "event-weighted"

       

      The rest of the parameters are left as is.

       

      Thanks in advance.

        • Re: Equivalent PI-SDK method call for the PI-DataLink Advanced Calculated Data function
          rbungubung

           

           

          I think I got it.... I'm looking at the Summaries2 method of the IPIData2 object.

           

          Thanks.

            • Re: Equivalent PI-SDK method call for the PI-DataLink Advanced Calculated Data function
              hanyong

              Hi Renato,

               

              Glad that you manged to figure it out within a pretty short period of time.

               

              You are right that you can use IPIData2.Summaries2 call to perform similar calculations as Advanced Calculated Data in DataLink. These would be doing summary calculation(s) based on a tag. Another aspect of Advanced Calculated Data is also to perform summary calculation on expressions as well. In that case what you can look at would be IPICalculation.ExpressionSummaries method.

                • Re: Equivalent PI-SDK method call for the PI-DataLink Advanced Calculated Data function
                  rbungubung

                  Hi Han,  thanks for the reply.

                   

                  I got it working if I only call the method for one tag.  As is always the case, my need is a little bit more than this. 

                   

                  I needed to call the method more than once (4 to be exact) so I have a list that contains 4 tags and I iterate through the list 4 times calling the IPIData2.Summaries2() method.  However, for some reason, I only get the total for the first tag in the list, the other 3 don't return anything.  The puzzling thing is that if I put a breakpoint inside the loop and step through the code, it fills in all 4.

                   

                  Has anyone seen this behaviour before?

                   

                  Thanks again.

                    • Re: Equivalent PI-SDK method call for the PI-DataLink Advanced Calculated Data function

                      Can you share the relevant portion of the code with us? Also, what version of PI SDK are you using?

                        • Re: Equivalent PI-SDK method call for the PI-DataLink Advanced Calculated Data function
                          rbungubung

                          I am using PI-SDK version 1.3.8 build 387

                           

                          Below is the code.  Hopefully you guys can see something that I don't.  And like I said, the foreach loop only displays the messagebox once (for the first tag which is "tag1", but if I run the code with only one tag it works ok or if I put a breakpoint inside the foreach loop.

                           

                          Thanks for the help.

                          List<string> tagNames = new List<string>();
                          tagNames.Add("tag1");
                          tagNames.Add("tag2");
                          tagNames.Add("tag3");
                          tagNames.Add("sinusoid");

                          try{
                              string startTimePi = "*-24h";
                              string endTimePi = "*" ;
                              PISDK.ServerManager serverManager = null;
                              PISDK.Server myPiServer = null;
                              PISDK.PISDKClass myPISDK = new PISDK.PISDKClass();
                              object server = "gvdevpi01"; 
                              serverManager = new PISDK.ServerManager();
                              myPiServer = serverManager.get_Item(ref server);
                              foreach (string tagName in tagNames)
                              {
                                  PISDKCommon.PIAsynchStatus asyStatus = new PISDKCommon.PIAsynchStatusClass();
                                  PISDKCommon.NamedValues nvs = new PISDKCommon.NamedValuesClass();
                                  PISDK.PIPoint pPoint = myPiServer.PIPoints[tagName];
                                  PISDK.IPIData2 pData2 = (PISDK.IPIData2)pPoint.Data;
                                  nvs = pData2.Summaries2(startTimePi,
                                                                        endTimePi,
                                                                        "1d",
                                                                        PISDK.ArchiveSummariesTypeConstants.asTotal, 
                                                                        PISDK.CalculationBasisConstants.cbEventWeighted,
                                                                        asyStatus);
                                  foreach (PISDKCommon.NamedValue nameValue in nvs)
                                  {
                                       string namev = nameValue.Name;
                                       PISDK.PIValues values = (PISDK.PIValues)nameValue.Value;
                                       foreach (PISDK.PIValue piValue in values) 
                                       {
                                            string timeStamp = piValue.TimeStamp.LocalDate.ToString();
                                            object value = piValue.Value.ToString(); 
                                            MessageBox.Show("tagName=" + tagName + ": timeStamp=" + timeStamp + ", value=" + value); 
                                       }

                                  }
                                  pData2 = null;
                                  pPoint = null; 
                             }
                          }
                          catch (System.Runtime.InteropServices.COMException comExc)
                          {
                             // some interop error
                             MessageBox.Show(comExc.Message);
                          }
                          catch (Exception ex)
                          {
                             //general error
                             MessageBox.Show(ex.Message);
                          }

                            • Re: Equivalent PI-SDK method call for the PI-DataLink Advanced Calculated Data function
                              hanyong

                              Hi Renato

                               

                              Seems like you are using a asynchronous Summaries2 call here. You probably should check if the asynchronous call has completed. There is an example code posted by Andreas here, look out for a portion of the code where a loop test for PIAsynchStatus.ProgressPercent. Another thing that Andreas' example illustrates is to initiate multiple asynchronous calls that runs at the same time. So you can initiate the Summaries2 call for all 4 tags at once and get the result at the end when all asynchronous calls are completed.

                               

                              You can also use synchronous call instead of asynchronous call. Just use replace the asynchstatus parameter as null:

                              nvs = pData2.Summaries2(startTimePi, 
                                                 endTimePi, 
                                                 "1d", 
                                                 PISDK.ArchiveSummariesTypeConstants.asTotal, 
                                                 PISDK.CalculationBasisConstants.cbEventWeighted, 
                                                 null);

                               

                              Hope this helps

                               

                               

                                • Re: Equivalent PI-SDK method call for the PI-DataLink Advanced Calculated Data function
                                  rbungubung

                                  Thanks, Han. 

                                   

                                  I'm pretty sure the issue was to do with the loop is executing faster than the call to the Summaries2 method, because I put 2 seconds delay right after the Summaries2() call and I do get the values correctly.  I believe this also explains why it works if I step through the code one line at a time, because that slight delay allows the call to finish in time.

                                   

                                  Thanks for you suggestion, I changed the call to synchronous and it work nicely.  I'm sure it'll be faster if I implement that multiple asynchronous call, but for my needs, this'll do. 

                                   

                                  Thanks again.