11 Replies Latest reply on Jan 10, 2017 1:31 PM by gregor

    how to fetch digital state information for time duration

    NEERAJ

      Hi ,

       

      I can read the current state for digital tag,below snap sort shows pipoint,time,and value....now i need to fetch the same info for a particular duration, can someone refer a sample code

      snapsort.PNG

       

      Thanks!

        • Re: how to fetch digital state information for time duration
          stang

          Hello, perhaps PIPointlLst.RecordedValues is what you're looking for?

          Request Rejected

          • Re: how to fetch digital state information for time duration
            jyi

            Hi Neeraj,

             

            Like what Sam said already, you can supply PIPointList.RecordedValues() method with TimeRange to return AFValues.

            For each AFValue, you can then check if value is of AFEnumerationValue type. If it is AFEnumerationValue type, then you can get its detailed EnumerationValue via its value name:

            Else you can get its Value.ToString() as usual. Here is code snippet from Patrice Thivierge:

            if (obj.Value is AFEnumerationValue)  
              {  
              
              // in this case, obj.Value.ToString() give same result as obj.Value.ToString(), but in many circumstances it is   
              // better to split the treatment of this type of values because they can be processed differently  
              
              var digValue = (AFEnumerationValue)obj.Value;  
              Console.WriteLine("Value is {0}: {1}", obj.Value.GetType().ToString(), digValue.Name);  
              }  
            

            More detailed information is in Getting Digital State through AFAttribute.GetValue().  Hope this helps a bit.

             

            Best,

            Jin

            • Re: how to fetch digital state information for time duration
              gregor

              Hello Neeraj,

               

              My understanding is that you've managed to retrieve current values (Snapshots) but like to fetch historical values over a period of time. Hence I agree with Samuel Tang that PIPointList.RecordedValues Method should suffice. Not sure why the link Samuel posted shows "Request Rejected". Just in case this happens too in my post, please find a sample below:

               

                  class Program
                  {
                      static void Main(string[] args)
                      {
                          PIServer server = new PIServers()["PIServer"];
                          string cdm158 = "CDM158";
                          string ba_active1 = "BA:ACTIVE.1";
                          string ba_phase1 = "BA:ACTIVE.1";
                          PIPointList ptList = new PIPointList(PIPoint.FindPIPoints(server, new[] { cdm158, ba_active1, ba_phase1 }));
                          AFTime endTime = AFTime.NowInWholeSeconds;
                          AFTime startTime = endTime.LocalTime.AddHours(-8);
                          AFTimeRange timeRange = new AFTimeRange(startTime, endTime);
                          PIPagingConfiguration pageConfig = new PIPagingConfiguration(PIPageType.EventCount, 1000);
                          IEnumerable<AFValues> piValues = ptList.RecordedValues(timeRange, AFBoundaryType.Inside, "", true, pageConfig, 0);
                          foreach (AFValues values in piValues)
                          {
                              Console.WriteLine("Values for PIPoint: {0}", values.PIPoint);
                              foreach (AFValue value in values)
                              {
                                  Console.WriteLine("  {0} at {1}", value.Value, value.Timestamp);
                              }
                              Console.WriteLine();
                          }
                          Console.WriteLine();
                          Console.Write("Done. Press any key to quit .. ");
                          Console.ReadKey();
                      }
                  }
              
              1 of 1 people found this helpful
                • Re: how to fetch digital state information for time duration
                  NEERAJ

                  Thanks Gregor for your help!

                  I am able to fetch values now,can you help me in converting the result to c# data table.

                   

                  I need to have datatable which have 3 fields ( you can see the values in the last tree item circled with red )

                  date time                        name             value

                  12/22/2016 10:55:00      FullPower      6

                   

                  FYI

                  result1.png

                   

                  I need to something similar to how summaries are converted to data table by following code

                  Capture.GIF

                   

                  Thanks!

                    • Re: how to fetch digital state information for time duration
                      gregor

                      Hello Neeraj,

                       

                      You have an example for the summaries. Have you tried it? Is it working for you?

                      At which point are you facing an issue returning recorded values in a DataTable?

                        • Re: how to fetch digital state information for time duration
                          NEERAJ

                          Hi Gregor,

                          Summaries are working but i need to get recorded values in datatable.

                          date time                        name             value

                          12/22/2016 10:55:00      FullPower      6

                           

                          Thanks!

                            • Re: how to fetch digital state information for time duration
                              gregor

                              Hello Neeraj,

                               

                              Is this what you are looking for?

                               

                                      public static DataTable GetDigValues(IEnumerable<AFValues> digValues)
                                      {
                                          DataTable dt = new DataTable();
                                          dt.Clear();
                                          dt.Columns.Add("PIPointName");
                                          dt.Columns.Add("DigitalValue");
                                          dt.Columns.Add("Time");
                                          foreach (AFValues values in digValues)
                                          {
                                              foreach (AFValue value in values)
                                              {
                                                  DataRow row = dt.NewRow();
                                                  row["PIPointName"] = value.PIPoint.ToString();
                                                  row["DigitalValue"] = value.Value.ToString();
                                                  row["Time"] = value.Timestamp.LocalTime.ToShortTimeString();
                                                  dt.Rows.Add(row);
                                              }
                                          }
                                          return dt;
                                      }
                              
                              1 of 1 people found this helpful
                                • Re: how to fetch digital state information for time duration
                                  NEERAJ

                                  Thanks Gregor ! Its working fine

                                   

                                  I have couple of more questions , need your help to find out

                                   

                                  IEnumerable<AFValues> listResults = myPIPointList.RecordedValues(timerange, AFBoundaryType.Interpolated, null, false, config);

                                   

                                  1. I need to pass specify time internal as 1 hr,similar to how we do it in pi summaries(AFTimeSpan.Parse("1h")),can we do it here?

                                  2. If I specify AFBoundaryType.Interpolated it will give interpolated values only when there are no values returned?

                                   

                                  Thanks!

                                    • Re: how to fetch digital state information for time duration
                                      gregor

                                      Hello Neeraj,

                                       

                                      1. There's no RecordedValues method that uses an AFTimeSpan as parameter.

                                      2. Please see the AFBoundaryType Enumeration for the explanation of AFBoundaryType.Interpolated

                                       

                                      Can you please elaborate on what you like to accomplish?

                                        • Re: how to fetch digital state information for time duration
                                          NEERAJ

                                          Hi Gregor,

                                           

                                          There's no RecordedValues method that uses an AFTimeSpan as parameter.

                                          1. I am fetching summaries for 1h timespan, now for the same tag i need to fetch recorded values from another server...... right now I can pass 5am to 8am and get the recorded values but it wont be for hourly timespan, task is to get the recorded values for hourly timespan.   One way of doing it would be to pass

                                          5-6,6-7,7-8 in timerange and get recorded values...but i was looking for some other way to get the recorded values by some means which would take input timerange as well as timespan.


                                          Please see the AFBoundaryType Enumeration for the explanation of AFBoundaryType.Interpolated

                                          When i am retrieving recorded values for some tag it does not return the result, for these only i need to get interpolated values.... i was wondering weather to pass AFBoundaryType.Interpolated  in RecordedValues or use  PIPoint.InterpolatedValuesAtTimes Method to find interpolated values for which no result is there.

                                           

                                          Thanks!