AnsweredAssumed Answered

Values with structured annotations from PI SDK can not be updated

Question asked by elia-acc on Feb 15, 2018
Latest reply on Mar 6, 2018 by elia-acc

Hello,

I am adding some values to a PIPoint using the PISDK.

In most of the case it works well, I see them in the archive editor, I can update if needed, set the questionnable flag, etc...

 

However, when the new value contains structured annotations, when I try to update it, an error message is raised:

---------------------------

Archive Editor Error

---------------------------

Error 0: External component has thrown an exception. (Update Values)

---------------------------

OK  

---------------------------

 

Here below the code extract responsible to add the value.

Please note the comment around the 'SetAnnotation'.

 

Many thanks in advance for your support!

 

public async Task<bool> Execute(AFElement[] checkedoutElements)

{

    try

    {

        var element = checkedoutElements.Single(c => c.ID == _afElementId);

        var groupedTimedData = _values.GroupBy(td => td.DataKey);

 

        AFErrors<AFValue> allErrors = new AFErrors<AFValue>();

        foreach (var timedDatas in groupedTimedData)

        {

            var afAttribute = element.Attributes[timedDatas.Key];

           

            var values = timedDatas.Select(td =>

            {

                var timeStamp = td.ValueTimestamp.ToUniversalTime();

 

                var afValue =

                    new AFValue(afAttribute, td.Value, new AFTime(timeStamp))

                    {

                        Questionable = td.IsQuestionnable

                    };

 

                AFAnnotations annotations = new AFAnnotations();

               

                foreach (var stringAnnotation in td.Annotations ?? Enumerable.Empty<KeyValuePair<string, string>>())

                {                           

                    var annotation = annotations.Add(stringAnnotation.Key, stringAnnotation.Value);

                    annotation.Description = stringAnnotation.Key;                           

                }

               

                if (annotations.Any())

                {

                    afValue.SetAnnotation(annotations);

 

                    // When adding a normal annotation as below, the problem does NOT occur

                    // afValue.SetAnnotation("If the annotation is simply text, there is no problem");

                }

 

                return afValue;

            }).ToList();

 

            var startTime = _startTime.HasValue ? new AFTime(_startTime.Value.ToUniversalTime()) : AFTime.MinValue;

            var endTime = _endTime.HasValue ? new AFTime(_endTime.Value.ToUniversalTime()) : AFTime.Now;

 

            var replaceRange = new AFTimeRange(startTime, endTime);

            AFErrors<AFValue> errors = await afAttribute.PIPoint.ReplaceValuesAsync(replaceRange, values, AFBufferOption.DoNotBuffer);

           

 

            if (errors?.HasErrors ?? false)

            {

                foreach (var error in errors.Errors)

                {

                    allErrors.AddError(error.Key, error.Value);

                }

            }

        }

 

        if (allErrors.HasErrors)

        {

            _errorMessage = string.Join(Environment.NewLine, allErrors.Errors.Select(kp => $"{kp.Key}: {kp.Value.Message}"));

            _insertionSucceeded = false;

        }

        else

        {

            _insertionSucceeded = true;

            _errorMessage = "No error";

        }

    }

    catch (Exception e)

    {

        Logger.Exception(e);

        _insertionSucceeded = false;

        _errorMessage = e.Message;

    }

 

    return _insertionSucceeded;

}

Outcomes