Esempio n. 1
0
        /// <summary>
        /// Updates the value of an tag.
        /// </summary>
        private bool UpdateTagValue(
            UnderlyingSystemTag tag,
            Opc.Ua.Test.DataGenerator generator)
        {
            // don't update writeable tags.
            if (tag.IsWriteable)
            {
                return false;
            }

            // check if a range applies to the value.
            int high = 0;
            int low = 0;

            switch (tag.TagType)
            {
                case UnderlyingSystemTagType.Analog:
                {
                    if (tag.EuRange != null && tag.EuRange.Length >= 2)
                    {
                        high = (int)tag.EuRange[0];
                        low = (int)tag.EuRange[1];
                    }

                    break;
                }

                case UnderlyingSystemTagType.Digital:
                {
                    high = 1;
                    low = 0;
                    break;
                }

                case UnderlyingSystemTagType.Enumerated:
                {
                    if (tag.Labels != null && tag.Labels.Length > 0)
                    {
                        high = tag.Labels.Length-1;
                        low = 0;
                    }

                    break;
                }
            }

            // select a value in the range.
            int value = -1;

            if (high > low)
            {
                value = (generator.GetRandomUInt16()%(high - low + 1)) + low;
            }

            // cast value to correct type or generate a random value.
            switch (tag.DataType)
            {
                case UnderlyingSystemDataType.Integer1:
                {
                    if (value == -1)
                    {
                        tag.Value = generator.GetRandomSByte();
                    }
                    else
                    {
                        tag.Value = (sbyte)value;
                    }

                    break;
                }

                case UnderlyingSystemDataType.Integer2:
                {
                    if (value == -1)
                    {
                        tag.Value = generator.GetRandomInt16();
                    }
                    else
                    {
                        tag.Value = (short)value;
                    }

                    break;
                }

                case UnderlyingSystemDataType.Integer4:
                {
                    if (value == -1)
                    {
                        tag.Value = generator.GetRandomInt32();
                    }
                    else
                    {
                        tag.Value = (int)value;
                    }

                    break;
                }

                case UnderlyingSystemDataType.Real4:
                {
                    if (value == -1)
                    {
                        tag.Value = generator.GetRandomFloat();
                    }
                    else
                    {
                        tag.Value = (float)value;
                    }

                    break;
                }

                case UnderlyingSystemDataType.String:
                {
                    tag.Value = generator.GetRandomString();
                    break;
                }
            }

            tag.Timestamp = DateTime.UtcNow;
            return true;
        }