PeterJ

Problem using  AFValue.GetAnnotation() when regional setting are "uk English"

Discussion created by PeterJ on Feb 19, 2013
Latest reply on Feb 20, 2013 by PeterJ

I am having problems retrieving a structured annotation from PI use  AFValue.GetAnnotation() - AF 2012. 

 

I have raised a support call for this (Call # 461272), but don't seem to be getting anywhere. I thought I'd post it here in the hope that someone has something to add

 

Running the code below, which is adapted from the sample in the AFsdk documentation, I get an exception on a machine with "UK english" regional settings.

 

It works if I change the regional setting to "US english"

 

The exception message I am getting is :

 

"The string '13/02/2013 17:05:43' was not recognized as a valid DateTime."  And the time corresponds to the creation date of the annotation.

 

The date string in the message  is in UK fomat (day-month-year) and appears to be the creation date of the annotation. 

 

The below  is the Call-stack from Visual studio at the point when the exception is thrown:

 

It is difficult, without the source code, but it looks to me as if "AFAnnotation.CreateAnnotation"  is converting the creation date of the annotation into a string (AFAnnnotations.cs line 120?) and then  the AfTime constructor is failing to parse the string (AFAnnnotations.cs line 122?)

 

 

 

                OSIsoft.AFSDK.dll!OSIsoft.AF.Time.AFTime.TryParseImpl(string parmName, string input, OSIsoft.AF.Time.AFTime? referenceTime, OSIsoft.AF.Time.AFTimeZone timeZone, System.IFormatProvider provider, bool throwErrors, out System.DateTime result) Line 773  C#

 

    OSIsoft.AFSDK.dll!OSIsoft.AF.Time.AFTime.AFTime(string timeString, System.IFormatProvider provider) Line 60 + 0xad bytes C#

 

     OSIsoft.AFSDK.dll!OSIsoft.AF.Data.AFAnnotations.CreateAnnonation(OSIsoft.AF.AFObject owner, System.Guid id, OSIsoft.PI.Net.NameTable annonationTable) Line 122 + 0x30 bytes C#

 

     OSIsoft.AFSDK.dll!OSIsoft.AF.Data.AFAnnotations.CreateAnnotations(OSIsoft.AF.AFObject owner, OSIsoft.PI.Net.NameTable nameTable) Line 180 + 0x71 bytes     C#

 

     OSIsoft.AFSDK.dll!OSIsoft.AF.PI.PIPoint.GetAnnotation(OSIsoft.AF.Asset.AFValue value) Line 961 + 0x23 bytes    C#

 

     OSIsoft.AF.Asset.DataReference.PIPoint.dll!OSIsoft.AF.Asset.DataReference.PIPointDR.GetAnnotation(OSIsoft.AF.Asset.AFValue value) + 0x75 bytes 

 

 

 

Here is the Code:

 

 

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OSIsoft.AF;
using OSIsoft.AF.Asset;
using OSIsoft.AF.Data;
using OSIsoft.AF.Time;

namespace ConsoleApplication1
{
    class Program
    {
        static private void DisplayAnnotation(AFValue theValue, object theAnnotation)
        {
            AFAnnotations theAnnotations = theAnnotation as AFAnnotations;
            if (theAnnotations == null)
            {
                Console.WriteLine("Simple Annotation for Value '{0}' = '{1}'", theValue, theAnnotation);
            }
            else
            {
                Console.WriteLine("Structured Annotation for Value '{0}':", theValue);
                foreach (AFAnnotation item in theAnnotations)
                {
                    Console.WriteLine("  Value = '{0}', Description = '{1}', Creator = '{2}'",
                        item.Value, item.Description, item.Creator);
                }
            }
        }
       static void Main(string[] args)
        {
            // This example demonstrates how to create an attribute for an
            // element and retrieve and set annotations on values.

            // Get the Database
            PISystems myPISystems = new PISystems();
            PISystem myPISystem = myPISystems.DefaultPISystem;
            AFDatabase myDB = myPISystem.Databases.DefaultDatabase;
            AFTime myEventTime = new AFTime("*-1h");
            // Create an Element
            AFElement myElement = myDB.Elements.Add("MyElement");

            // Create an Attribute
            AFAttribute myAttribute = myElement.Attributes.Add("MyAttribute");
            myAttribute.DefaultUOM = myPISystem.UOMDatabase.UOMs["Kelvin"];
            myAttribute.DataReferencePlugIn = AFDataReference.GetPIPointDataReference(myPISystem);
            myAttribute.ConfigString = @"\\%Server%\sinusoid;ReadOnly=False";

        
          //  // Create New Structured Annotation
            AFAnnotations myAnnotations = new AFAnnotations();
            AFValue myValue = new AFValue(255.543,myEventTime);
          
            AFAnnotation myAnnotation = myAnnotations.Add("Annotation", "My Annotation Value");
            myAnnotation.Description = " Annotation is a String.";
            myValue.SetAnnotation(myAnnotations);
            myAttribute.Data.UpdateValue(myValue, AFUpdateOption.InsertNoCompression);

            //// Read Annotations
            
            AFValue myValueFromPI = myAttribute.GetValue(myEventTime);
            try
            {
                object theAnnotation = myValueFromPI.GetAnnotation();
                DisplayAnnotation(myValueFromPI, theAnnotation);
            }
            catch (Exception ex)
            {
                Console.WriteLine("exception : {0}", ex.Message);
            }
        }
    }
}
<!--[if gte mso 9]>                                                                                                                                                                                                                                                                                   <![endif]--><!--[if gte mso 10]> <![endif]-->

Outcomes