Ejemplo n.º 1
0
        /// <summary>
        /// Increment value
        /// maxAllowedValue - maximum incremented value before reset value to initial value
        /// step - the increment value
        /// </summary>
        /// <param name="variable"></param>
        /// <param name="namespaceIndex"></param>
        /// <param name="maxAllowedValue"></param>
        /// <param name="step"></param>
        private void IncrementValue(FieldMetaData variable, ushort namespaceIndex, long maxAllowedValue = Int32.MaxValue, int step = 0)
        {
            // Read value to be incremented
            DataValue dataValue = m_dataStore.ReadPublishedDataItem(new NodeId(variable.Name, namespaceIndex), Attributes.Value);

            if (dataValue.Value == null)
            {
                return;
            }

            bool valueUpdated = false;

            BuiltInType expectedType = TypeInfo.GetBuiltInType(variable.DataType);

            switch (expectedType)
            {
            case BuiltInType.Boolean:
                if (variable.ValueRank == ValueRanks.Scalar)
                {
                    Boolean boolValue = Convert.ToBoolean(dataValue.Value);
                    dataValue.Value = !boolValue;
                    valueUpdated    = true;
                }
                break;

            case BuiltInType.Byte:
                if (variable.ValueRank == ValueRanks.Scalar)
                {
                    byte byteValue = Convert.ToByte(dataValue.Value);
                    dataValue.Value = (byte)(byteValue + 1);
                    valueUpdated    = true;
                }
                break;

            case BuiltInType.Int16:
                if (variable.ValueRank == ValueRanks.Scalar)
                {
                    Int16 int16Value    = Convert.ToInt16(dataValue.Value);
                    int   intIdentifier = int16Value;
                    Interlocked.CompareExchange(ref intIdentifier, 0, Int16.MaxValue);
                    dataValue.Value = (Int16)Interlocked.Increment(ref intIdentifier);
                    valueUpdated    = true;
                }
                break;

            case BuiltInType.Int32:
                if (variable.ValueRank == ValueRanks.Scalar)
                {
                    Int32 int32Value = Convert.ToInt32(dataValue.Value);
                    if (step > 0)
                    {
                        int32Value += (step - 1);
                    }
                    if (int32Value > maxAllowedValue)
                    {
                        int32Value = 0;
                    }
                    dataValue.Value = Interlocked.Increment(ref int32Value);
                    valueUpdated    = true;
                }
                break;

            case BuiltInType.SByte:
                if (variable.ValueRank == ValueRanks.Scalar)
                {
                    SByte sbyteValue    = Convert.ToSByte(dataValue.Value);
                    int   intIdentifier = sbyteValue;
                    Interlocked.CompareExchange(ref intIdentifier, 0, SByte.MaxValue);
                    dataValue.Value = (SByte)Interlocked.Increment(ref intIdentifier);
                    valueUpdated    = true;
                }
                break;

            case BuiltInType.UInt16:
                if (variable.ValueRank == ValueRanks.Scalar)
                {
                    UInt16 uint16Value   = Convert.ToUInt16(dataValue.Value);
                    int    intIdentifier = uint16Value;
                    Interlocked.CompareExchange(ref intIdentifier, 0, UInt16.MaxValue);
                    dataValue.Value = (UInt16)Interlocked.Increment(ref intIdentifier);
                    valueUpdated    = true;
                }
                break;

            case BuiltInType.UInt32:
                if (variable.ValueRank == ValueRanks.Scalar)
                {
                    UInt32 uint32Value    = Convert.ToUInt32(dataValue.Value);
                    long   longIdentifier = uint32Value;
                    Interlocked.CompareExchange(ref longIdentifier, 0, UInt32.MaxValue);
                    dataValue.Value = (UInt32)Interlocked.Increment(ref longIdentifier);
                    valueUpdated    = true;
                }
                else if (variable.ValueRank == ValueRanks.OneDimension)
                {
                    uint[] values = dataValue.Value as uint[];
                    if (values != null)
                    {
                        for (int i = 0; i < values.Length; i++)
                        {
                            UInt32 uint32Value    = values[i];
                            long   longIdentifier = uint32Value;
                            Interlocked.CompareExchange(ref longIdentifier, 0, UInt32.MaxValue);
                            values[i] = (UInt32)Interlocked.Increment(ref longIdentifier);
                        }
                        valueUpdated = true;
                    }
                }
                break;

            case BuiltInType.UInt64:
                if (variable.ValueRank == ValueRanks.Scalar)
                {
                    UInt64 uint64Value    = Convert.ToUInt64(dataValue.Value);
                    float  longIdentifier = uint64Value + 1;
                    Interlocked.CompareExchange(ref longIdentifier, 0, UInt64.MaxValue);
                    dataValue.Value = (UInt64)longIdentifier;
                    valueUpdated    = true;
                }
                break;

            case BuiltInType.Float:
                if (variable.ValueRank == ValueRanks.Scalar)
                {
                    float floatValue = Convert.ToSingle(dataValue.Value);
                    Interlocked.CompareExchange(ref floatValue, 0, float.MaxValue);
                    dataValue.Value = floatValue + 1;
                    valueUpdated    = true;
                }
                break;

            case BuiltInType.Double:
                if (variable.ValueRank == ValueRanks.Scalar)
                {
                    double doubleValue = Convert.ToDouble(dataValue.Value);
                    Interlocked.CompareExchange(ref doubleValue, 0, double.MaxValue);
                    dataValue.Value = doubleValue + 1;
                    valueUpdated    = true;
                }
                break;

            case BuiltInType.DateTime:
                if (variable.ValueRank == ValueRanks.Scalar)
                {
                    dataValue.Value = DateTime.UtcNow;
                    valueUpdated    = true;
                }
                break;

            case BuiltInType.Guid:
                if (variable.ValueRank == ValueRanks.Scalar)
                {
                    dataValue.Value = Guid.NewGuid();
                    valueUpdated    = true;
                }
                break;

            case BuiltInType.String:
                if (variable.ValueRank == ValueRanks.Scalar)
                {
                    m_aviationAlphabetIndex = (m_aviationAlphabetIndex + 1) % m_aviationAlphabet.Length;
                    dataValue.Value         = m_aviationAlphabet[m_aviationAlphabetIndex];
                    valueUpdated            = true;
                }
                break;
            }

            if (valueUpdated)
            {
                // Save new updated value to data store
                WriteFieldData(variable.Name, namespaceIndex, dataValue);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        ///  Create and return a DataSet object created from its dataSetName
        /// </summary>
        /// <param name="dataSetName"></param>
        /// <returns></returns>
        public DataSet CollectData(string dataSetName)
        {
            PublishedDataSetDataType publishedDataSet = GetPublishedDataSet(dataSetName);

            if (publishedDataSet != null)
            {
                if (publishedDataSet.DataSetSource != null)
                {
                    DataSet dataSet = new DataSet(dataSetName);
                    PublishedDataItemsDataType publishedDataItems = ExtensionObject.ToEncodeable(publishedDataSet.DataSetSource) as PublishedDataItemsDataType;

                    if (publishedDataItems != null && publishedDataItems.PublishedData != null && publishedDataItems.PublishedData.Count > 0)
                    {
                        dataSet.Fields = new Field[publishedDataItems.PublishedData.Count];
                        for (int i = 0; i < publishedDataItems.PublishedData.Count; i++)
                        {
                            try
                            {
                                PublishedVariableDataType publishedVariable = publishedDataItems.PublishedData[i];
                                dataSet.Fields[i] = new Field();
                                // set FieldMetaData property
                                dataSet.Fields[i].FieldMetaData = publishedDataSet.DataSetMetaData.Fields[i];

                                // retrieve value from DataStore
                                DataValue dataValue = null;
                                if (publishedVariable.PublishedVariable != null)
                                {
                                    //todo handle missing value in data store
                                    dataValue = m_dataStore.ReadPublishedDataItem(publishedVariable.PublishedVariable, publishedVariable.AttributeId);
                                }

                                if (dataValue == null)
                                {
                                    //try to get the dataValue from ExtensionFields

                                    /*If an entry of the PublishedData references one of the ExtensionFields, the substituteValue shall contain the
                                     * QualifiedName of the ExtensionFields entry.
                                     * All other fields of this PublishedVariableDataType array element shall be null*/
                                    QualifiedName extensionFieldName = publishedVariable.SubstituteValue.Value as QualifiedName;
                                    if (extensionFieldName != null)
                                    {
                                        KeyValuePair extensionField = publishedDataSet.ExtensionFields.Find(x => x.Key == extensionFieldName);
                                        if (extensionField != null)
                                        {
                                            dataValue = new DataValue(extensionField.Value);
                                        }
                                    }
                                    if (dataValue == null)
                                    {
                                        dataValue = new DataValue(StatusCodes.Bad, DateTime.UtcNow);
                                    }
                                }
                                else
                                {
                                    dataValue = Utils.Clone(dataValue) as DataValue;

                                    //check StatusCode and return SubstituteValue if possible
                                    if (dataValue.StatusCode == StatusCodes.Bad && publishedVariable.SubstituteValue != Variant.Null)
                                    {
                                        dataValue.Value      = publishedVariable.SubstituteValue.Value;
                                        dataValue.StatusCode = StatusCodes.UncertainSubstituteValue;
                                    }
                                }

                                dataValue.ServerTimestamp = DateTime.UtcNow;

                                #region FieldMetaData -> MaxStringLength size validation

                                Field   field   = dataSet.Fields[i];
                                Variant variant = dataValue.WrappedValue;

                                bool shouldBringToConstraints(uint givenStrlen)
                                {
                                    if (field.FieldMetaData.MaxStringLength > 0 &&
                                        givenStrlen > field.FieldMetaData.MaxStringLength)
                                    {
                                        return(true);
                                    }

                                    return(false);
                                }

                                switch ((BuiltInType)field.FieldMetaData.BuiltInType)
                                {
                                case BuiltInType.String:
                                    if (field.FieldMetaData.ValueRank == ValueRanks.Scalar)
                                    {
                                        string strFieldValue = variant.Value as string;
                                        if (strFieldValue != null && shouldBringToConstraints((uint)strFieldValue.Length))
                                        {
                                            variant.Value   = strFieldValue.Substring(0, (int)field.FieldMetaData.MaxStringLength);
                                            dataValue.Value = variant;
                                        }
                                    }
                                    else if (field.FieldMetaData.ValueRank == ValueRanks.OneDimension)
                                    {
                                        string[] valueArray = variant.Value as string[];
                                        if (valueArray != null)
                                        {
                                            for (int idx = 0; idx < valueArray.Length; idx++)
                                            {
                                                if (shouldBringToConstraints((uint)valueArray[idx].Length))
                                                {
                                                    valueArray[idx] = valueArray[idx].Substring(0, (int)field.FieldMetaData.MaxStringLength);
                                                }
                                            }
                                        }
                                        dataValue.Value = valueArray;
                                    }
                                    break;

                                case BuiltInType.ByteString:
                                    if (field.FieldMetaData.ValueRank == ValueRanks.Scalar)
                                    {
                                        byte[] byteStringFieldValue = variant.Value as byte[];
                                        if (byteStringFieldValue != null && shouldBringToConstraints((uint)byteStringFieldValue.Length))
                                        {
                                            byte[] byteArray = (byte[])byteStringFieldValue.Clone();
                                            Array.Resize(ref byteArray, (int)field.FieldMetaData.MaxStringLength);
                                            variant.Value   = byteArray;
                                            dataValue.Value = variant;
                                        }
                                    }
                                    else if (field.FieldMetaData.ValueRank == ValueRanks.OneDimension)
                                    {
                                        byte[][] valueArray = variant.Value as byte[][];
                                        if (valueArray != null)
                                        {
                                            for (int idx = 0; idx < valueArray.Length; idx++)
                                            {
                                                if (shouldBringToConstraints((uint)valueArray[idx].Length))
                                                {
                                                    byte[] byteArray = (byte[])valueArray[idx].Clone();
                                                    Array.Resize(ref byteArray, (int)field.FieldMetaData.MaxStringLength);
                                                    valueArray[idx] = byteArray;
                                                }
                                            }
                                        }
                                        dataValue.Value = valueArray;
                                    }
                                    break;

                                default:
                                    break;
                                }

                                #endregion

                                dataSet.Fields[i].Value = dataValue;
                            }
                            catch (Exception ex)
                            {
                                dataSet.Fields[i].Value = new DataValue(StatusCodes.Bad, DateTime.UtcNow);
                                Utils.Trace(Utils.TraceMasks.Information, "DataCollector.CollectData for dataset {0} field {1} resulted in ex {2}",
                                            dataSetName, i, ex);
                            }
                        }
                        return(dataSet);
                    }
                }
            }
            return(null);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Increment value
        /// maxAllowedValue - maximum incremented value before reset value to initial value
        /// step - the increment value
        /// </summary>
        /// <param name="variable"></param>
        /// <param name="namespaceIndex"></param>
        /// <param name="maxAllowedValue"></param>
        /// <param name="step"></param>
        private void IncrementValue(FieldMetaData variable, ushort namespaceIndex, long maxAllowedValue = Int32.MaxValue, int step = 0)
        {
            // Read value to be incremented
            DataValue dataValue = m_dataStore.ReadPublishedDataItem(new NodeId(variable.Name, namespaceIndex), Attributes.Value);

            if (dataValue.Value == null)
            {
                return;
            }

            bool valueUpdated = false;

            BuiltInType expectedType = TypeInfo.GetBuiltInType(variable.DataType);

            switch (expectedType)
            {
            case BuiltInType.Boolean:
                if (variable.ValueRank == ValueRanks.Scalar)
                {
                    Boolean boolValue = Convert.ToBoolean(dataValue.Value);
                    dataValue.Value = !boolValue;
                    valueUpdated    = true;
                }
                break;

            case BuiltInType.Byte:
                if (variable.ValueRank == ValueRanks.Scalar)
                {
                    Byte byteValue = Convert.ToByte(dataValue.Value);
                    dataValue.Value = ++byteValue;
                    valueUpdated    = true;
                }
                break;

            case BuiltInType.Int16:
                if (variable.ValueRank == ValueRanks.Scalar)
                {
                    Int16 int16Value    = Convert.ToInt16(dataValue.Value);
                    int   intIdentifier = int16Value;
                    Interlocked.CompareExchange(ref intIdentifier, 0, Int16.MaxValue);
                    dataValue.Value = (Int16)Interlocked.Increment(ref intIdentifier);
                    valueUpdated    = true;
                }
                break;

            case BuiltInType.Int32:
                if (variable.ValueRank == ValueRanks.Scalar)
                {
                    Int32 int32Value = Convert.ToInt32(dataValue.Value);
                    if (step > 0)
                    {
                        int32Value += (step - 1);
                    }
                    if (int32Value > maxAllowedValue)
                    {
                        int32Value = 0;
                    }
                    dataValue.Value = Interlocked.Increment(ref int32Value);
                    valueUpdated    = true;
                }
                break;

            case BuiltInType.SByte:
                if (variable.ValueRank == ValueRanks.Scalar)
                {
                    SByte sbyteValue    = Convert.ToSByte(dataValue.Value);
                    int   intIdentifier = sbyteValue;
                    Interlocked.CompareExchange(ref intIdentifier, 0, SByte.MaxValue);
                    dataValue.Value = (SByte)Interlocked.Increment(ref intIdentifier);
                    valueUpdated    = true;
                }
                break;

            case BuiltInType.UInt16:
                if (variable.ValueRank == ValueRanks.Scalar)
                {
                    UInt16 uint16Value   = Convert.ToUInt16(dataValue.Value);
                    int    intIdentifier = uint16Value;
                    Interlocked.CompareExchange(ref intIdentifier, 0, UInt16.MaxValue);
                    dataValue.Value = (UInt16)Interlocked.Increment(ref intIdentifier);
                    valueUpdated    = true;
                }
                break;

            case BuiltInType.UInt32:
                if (variable.ValueRank == ValueRanks.Scalar)
                {
                    UInt32 uint32Value    = Convert.ToUInt32(dataValue.Value);
                    long   longIdentifier = uint32Value;
                    Interlocked.CompareExchange(ref longIdentifier, 0, UInt32.MaxValue);
                    dataValue.Value = (UInt32)Interlocked.Increment(ref longIdentifier);
                    valueUpdated    = true;
                }
                break;

            case BuiltInType.Float:
                if (variable.ValueRank == ValueRanks.Scalar)
                {
                    float floatValue = Convert.ToSingle(dataValue.Value);
                    Interlocked.CompareExchange(ref floatValue, 0, float.MaxValue);
                    dataValue.Value = ++floatValue;
                    valueUpdated    = true;
                }
                break;

            case BuiltInType.Double:
                if (variable.ValueRank == ValueRanks.Scalar)
                {
                    double doubleValue = Convert.ToDouble(dataValue.Value);
                    Interlocked.CompareExchange(ref doubleValue, 0, double.MaxValue);
                    dataValue.Value = ++doubleValue;
                    valueUpdated    = true;
                }
                break;

            case BuiltInType.DateTime:
                if (variable.ValueRank == ValueRanks.Scalar)
                {
                    dataValue.Value = DateTime.UtcNow;
                    valueUpdated    = true;
                }
                break;
            }

            if (valueUpdated)
            {
                // Save new updated value to data store
                WriteFieldData(variable.Name, namespaceIndex, dataValue);
            }
        }