Example #1
0
        /// <summary>
        /// Applies the index range and data encoding to the value.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="indexRange">The index range.</param>
        /// <param name="dataEncoding">The data encoding.</param>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        protected ServiceResult ApplyIndexRangeAndDataEncoding(
            ISystemContext context,
            NumericRange indexRange,
            QualifiedName dataEncoding,
            DataValue value)
        {
            if (StatusCode.IsBad(value.StatusCode))
            {
                return(value.StatusCode);
            }

            if (indexRange != NumericRange.Empty || !QualifiedName.IsNull(dataEncoding))
            {
                object valueToUpdate = value.Value;

                ServiceResult result = BaseVariableState.ApplyIndexRangeAndDataEncoding(
                    context,
                    indexRange,
                    dataEncoding,
                    ref valueToUpdate);

                if (ServiceResult.IsBad(result))
                {
                    value.Value      = null;
                    value.StatusCode = result.StatusCode;
                    return(result);
                }

                value.Value = valueToUpdate;
            }

            return(value.StatusCode);
        }
Example #2
0
        /// <summary>
        /// Generates a new value each time the value is read.
        /// </summary>
        private ServiceResult DoDeviceRead(
            ISystemContext context,
            NodeState node,
            NumericRange indexRange,
            QualifiedName dataEncoding,
            ref object value,
            ref StatusCode statusCode,
            ref DateTime timestamp)
        {
            BaseVariableState variable = node as BaseVariableState;

            if (variable == null)
            {
                return(ServiceResult.Good);
            }

            if (!SimulationActive.Value)
            {
                return(ServiceResult.Good);
            }

            TestDataSystem system = context.SystemHandle as TestDataSystem;

            if (system == null)
            {
                return(StatusCodes.BadOutOfService);
            }

            try
            {
                value = system.ReadValue(variable);

                statusCode = StatusCodes.Good;
                timestamp  = DateTime.UtcNow;

                ServiceResult error = BaseVariableState.ApplyIndexRangeAndDataEncoding(
                    context,
                    indexRange,
                    dataEncoding,
                    ref value);

                if (ServiceResult.IsBad(error))
                {
                    statusCode = error.StatusCode;
                }

                return(ServiceResult.Good);
            }
            catch (Exception e)
            {
                return(new ServiceResult(e));
            }
        }
Example #3
0
        /// <summary>
        /// Reads the value for the value attribute.
        /// </summary>
        protected override ServiceResult ReadValueAttribute(
            ISystemContext context,
            NumericRange indexRange,
            QualifiedName dataEncoding,
            ref object value,
            ref DateTime sourceTimestamp)
        {
            value = m_value;

            ServiceResult result = ServiceResult.Good;

            VariableCopyPolicy copyPolicy = VariableCopyPolicy.CopyOnRead;

            // use default behavoir.
            if (OnSimpleReadValue != null)
            {
                result = OnSimpleReadValue(
                    context,
                    this,
                    ref value);

                if (ServiceResult.IsBad(result))
                {
                    return(result);
                }

                copyPolicy = VariableCopyPolicy.Never;
            }
            else
            {
                // check if a valid value exists.
                if (value == null)
                {
                    return(StatusCodes.BadAttributeIdInvalid);
                }
            }

            // apply the index range and encoding.
            result = BaseVariableState.ApplyIndexRangeAndDataEncoding(context, indexRange, dataEncoding, ref value);

            if (ServiceResult.IsBad(result))
            {
                return(result);
            }

            // copy returned value.
            if (copyPolicy == VariableCopyPolicy.CopyOnRead)
            {
                value = Utils.Clone(value);
            }

            return(result);
        }
Example #4
0
        private ServiceResult ApplyIndexRangeAndDataEncoding(
            ISystemContext context,
            ReadValueId nodeToRead,
            DataValue value)
        {
            if (StatusCode.IsBad(value.StatusCode))
            {
                return(value.StatusCode);
            }

            if (nodeToRead.ParsedIndexRange != NumericRange.Empty || !QualifiedName.IsNull(nodeToRead.DataEncoding))
            {
                if (nodeToRead.AttributeId != Attributes.Value && !QualifiedName.IsNull(nodeToRead.DataEncoding))
                {
                    return(StatusCodes.BadDataEncodingInvalid);
                }

                object valueToUpdate = value.Value;

                ServiceResult result = BaseVariableState.ApplyIndexRangeAndDataEncoding(
                    context,
                    nodeToRead.ParsedIndexRange,
                    nodeToRead.DataEncoding,
                    ref valueToUpdate);

                if (ServiceResult.IsBad(result))
                {
                    bool useXml = nodeToRead.DataEncoding.Name == DefaultXml;

                    if (!useXml &&
                        !string.IsNullOrEmpty(nodeToRead.DataEncoding.Name) &&
                        nodeToRead.DataEncoding.Name != DefaultBinary)
                    {
                        result = StatusCodes.BadDataEncodingInvalid;
                    }

                    value.Value      = null;
                    value.StatusCode = result.StatusCode;
                    return(result);
                }

                value.Value = valueToUpdate;
            }

            return(value.StatusCode);
        }