22 Replies Latest reply on Jul 15, 2014 9:11 AM by Gregor

    Trying to add 10 points with 1000 values each for 1 year.

    ouemaR2

      Hi, i'm trying to add points with values and I end up with like 10 values instead of 1000. 

       

      compressing = 0 and compdev = 0.

       

      this is the main functions i'm using.

       
      NamedValues val      = new NamedValues();
      
      val.Add( "compressing"ref disableCompress );
                  val.Add( "compdev"ref disableCompDev );
                  val.Add( "compmax"ref disablecompmax );
      point = piPoints.Add( "test" +  i, "classic"PointTypeConstants.pttypInt32, val );
      
      double dbl = random.NextDouble() * ( 5 - 0 ) + 0;
      
      point.Data.UpdateValue( dbl, startTime, DataMergeConstants.dmReplaceDuplicates );
        • Re: Trying to add 10 points with 1000 values each for 1 year.
          Marcos Vainer Loeff

          Hello Marc,

           

          Could you please post the complete code snippet? We cannot understand the logic you are using with only those methods.

            • Re: Trying to add 10 points with 1000 values each for 1 year.
              ouemaR2

              public bool Write( int numberTags, int startTimeStamps, int endTimeStamps, int totalTimeStamps, DateTime startDate, out string[] tags )
              {
              DateTime startTime = DateTime.MinValue;
              bool isOk = false;
              tags = new string[numberTags];
              PIPoint point = null;
              PITime timeStart = null;
              PIValues vals = null;
              PIPoints piPoints = server.PIPoints;
              NamedValues valds = null;
              Random random = new Random();
              DateTime test = DateTime.MinValue;
              double[] array = null;
              PIErrors piErr = null;
              NamedValues val = new NamedValues();

               

              object disableCompress = 0;
              object disableCompDev = 0;
              val.Add( "compressing", ref disableCompress );
              val.Add( "compdev", ref disableCompDev );

               

              for( int i = 0; i < numberTags; i++ )
              {
              tags = "test" + i;

               

              try
              {
              point = piPoints["test" + i];

              }
              catch( Exception )
              {
              point = piPoints.Add( "test" + i, "classic", PointTypeConstants.pttypInt32, val );

               

              }

               

              startTime = startDate;

               

              for( int j = startTimeStamps; j <= endTimeStamps; j++ )
              {
              float dbl = (float)random.NextDouble() * ( 50 - 0 ) + 0;
              startTime = new DateTime( startTime.Year, startTime.Month, startTime.Day, startTime.Hour, startTime.Minute, startTime.Second );
              point.Data.UpdateValue( dbl, startTime, DataMergeConstants.dmReplaceDuplicates );

              if( endTimeStamps == totalTimeStamps )
              {
              test = startTime;
              while( test == startTime )
              test = startTime.AddHours( ( 8000 / ( double )totalTimeStamps ) ); // 1 year (8760 hours)

               

              startTime = test;
              }
              }

              }

               

              isOk = true;

               


              return isOk;
              }

                • Re: Trying to add 10 points with 1000 values each for 1 year.
                  Marcos Vainer Loeff

                  Hi Marc,

                   

                  There are some issues with your code:

                  • You are creating integer PI Points but fetching float values on them.
                  • Your logic of timestamps is not working. You are sending the same timestamp for every iteration within the for block.
                  • If you are sending so many values is preferable to use UpdateValues() instead of UpdateValue().
                  I have created an example using PI SDK. Nevertheless, it is recommend using PI AF SDK instead of PI SDK in order to improve performance.
                  using PISDK;
                  using PISDKCommon;
                  using PITimeServer;
                  using System;
                  using System.Collections.Generic;
                  using System.Linq;
                  using System.Text;
                  using System.Threading.Tasks;
                  
                  namespace TestPISDKPIPointsCreating
                  {
                      class Program
                      {
                          private static Server myPIServer = null;
                          static void Main(string[] args)
                          {
                              PISDK.PISDK sdkroot = new PISDK.PISDK();
                              myPIServer = sdkroot.Servers.DefaultServer;
                              myPIServer.Open();
                              bool NoError = Write(10, DateTime.Now.AddDays(-90), DateTime.Now, 1000);
                              myPIServer.Close();
                          }
                  
                  
                          private static bool Write(int numberTags, DateTime startTime, DateTime endTime, int numberOfIntervals)
                          {
                              bool NoError = true;
                              try
                              {
                                  PointList myPIPointList = CreatePIPoints(numberTags);
                                  DateTime[] DateTimeArray = BuildDateTimeArray(startTime, endTime, numberOfIntervals);
                                  foreach (PIPoint myPIPoint in myPIPointList)
                                  {
                                      WriteValuesToPIPoint(myPIPoint, DateTimeArray);
                                  }
                              }
                              catch (Exception ex)
                              {
                                  Console.WriteLine(ex.Message);
                                  NoError = false;
                              }
                              return NoError;
                          }
                  
                          private static PointList CreatePIPoints(int numberTags)
                          {
                              PointList myPointList = new PointList();
                              NamedValues myPointAttributes = new NamedValues();
                              myPointAttributes.Add("compressing", 0);
                              myPointAttributes.Add("compdev", 0);
                              for (int i = 0; i < numberTags; i++)
                              {
                                  PIPoint currentPIPoint = null;
                                  try
                                  {
                                      currentPIPoint = myPIServer.PIPoints["test" + i];
                                  }
                                  catch (Exception)
                                  {
                                      currentPIPoint = myPIServer.PIPoints.Add("test" + i, "classic", PointTypeConstants.pttypFloat32, myPointAttributes);
                                  }
                                  finally
                                  {
                                      myPointList.Add(currentPIPoint);
                                  }
                              }
                              return myPointList;
                          }
                  
                          private static DateTime[] BuildDateTimeArray(DateTime startTime, DateTime endTime, int numberOfIntervals)
                          {
                              DateTime[] DateTimeArray = new DateTime[numberOfIntervals + 1];
                              TimeSpan totalDifference = (endTime - startTime);
                              double intervalDifferenceInSeconds = (totalDifference.TotalSeconds) / numberOfIntervals;
                              for (int i = 0; i <= numberOfIntervals; i++)
                              {
                                  DateTimeArray[ i ] = startTime.AddSeconds(intervalDifferenceInSeconds * i);
                              }
                              return DateTimeArray;
                          }
                  
                          private static void WriteValuesToPIPoint(PIPoint myPIPoint, DateTime[] myDateTimeArray)
                          {
                              PIValues myPIValues = new PIValues();
                              myPIValues.ReadOnly = false;
                              Random random = new Random();
                              foreach (DateTime myDateTime in myDateTimeArray)
                              {
                                  double myPIValue = random.NextDouble() * (50 - 0) + 0;
                                  myPIValues.Add(myDateTime, myPIValue, null);
                              }
                              myPIPoint.Data.UpdateValues(myPIValues, DataMergeConstants.dmReplaceDuplicates);
                          }
                      }
                  }
                  

                   

                  Hope it helps!