/// <summary>
        /// Read the Address Space ValueQT's
        /// </summary>
        /// <param name="session"></param>
        /// <param name="itemId"></param>
        /// <param name="itemValues"></param>
        /// <returns></returns>
        public bool ReadAddresSpaceItem(DaSession session, string itemId, out ValueQT[] itemValues)
        {
            //read the item values
            this.panelBottom.Visible = false;
            isDaItem = false;

            int[] itemResults;
            int   result = session.Read(MAX_AGE_DEVICE, new string[] { itemId }, null, out itemValues, out itemResults, null);

            if (itemResults.Length == 0)
            {
                return(false);
            }
            if (ResultCode.SUCCEEDED(result) && ResultCode.SUCCEEDED(itemResults[0]))
            {
                if (itemValues == null)
                {
                    return(false);
                }
                if (itemValues.Length == 0)
                {
                    return(false);
                }
                this.propertyGrid.SelectedObject = FillReadService(itemId, true, itemValues[0]);
                return(true);
            }
            return(false);
        }
        public override TestResult Execute()
        {
            if (string.IsNullOrEmpty(Session))
            {
                return(TestResult.ParameterMissing("Session"));
            }

            DaSession session = GetProperty(Session) as DaSession;

            if (session == null)
            {
                return(TestResult.ParameterInvalid("Session"));
            }


            if (Count <= 0)
            {
                return(TestResult.ParameterInvalid("Count"));
            }

            try
            {
                // prepare values to write
                List <string> itemToWrite = new List <string>();
                itemToWrite.Add(m_ItemCountNodeId);
                itemToWrite.Add(m_ItemTypeNodeId);
                itemToWrite.Add(m_ItemIsAnalogNodeId);
                itemToWrite.Add(m_CreateItemsNodeId);

                string[] itemIDs = itemToWrite.ToArray();

                List <ValueQT> valuesToWrite = new List <ValueQT>();
                valuesToWrite.Add(new ValueQT(Count, EnumQuality.GOOD, DateTime.Now));
                valuesToWrite.Add(new ValueQT((uint)NodeType, EnumQuality.GOOD, DateTime.Now));
                valuesToWrite.Add(new ValueQT(IsAnalog, EnumQuality.GOOD, DateTime.Now));
                guid = Guid.NewGuid().ToString();
                valuesToWrite.Add(new ValueQT(guid, EnumQuality.GOOD, DateTime.Now));

                ValueQT[] values = valuesToWrite.ToArray();

                int   result = (int)EnumResultCode.S_FALSE;
                int[] results;

                // send CreateItemsRequest
                result = session.Write(itemIDs, null, values, out results, null);

                if (ResultCode.SUCCEEDED(result))
                {
                    // wait for Variables to be created
                    System.Threading.Thread.Sleep(1000);

                    string[]  itemsToRead = new string[] { ItemSetIDNodeId };
                    ValueQT[] readValues;

                    result = session.Read(0, itemsToRead, null, out readValues, out results, null);

                    if (ResultCode.SUCCEEDED(result))
                    {
                        string   itemSetIdValue = string.Empty;
                        string[] stringArray    = readValues[0].Data as string[];
                        string   item           = string.Empty;
                        for (int i = 0; i < stringArray.Length; i++)
                        {
                            item = stringArray[i];
                            if (item.Split('#')[1] == guid)
                            {
                                itemSetIdValue = item.Split('#')[0];
                                break;
                            }
                        }
                        if (itemSetIdValue == string.Empty)
                        {
                            return(TestResult.Failed(string.Format("ItemSetId value was not found! {0}", guid)));
                        }

                        itemSetIdValue = m_nodeSetPrefix + itemSetIdValue;

                        SetProperty(Property, itemSetIdValue);

                        return(TestResult.Success());
                    }
                    else
                    {
                        EnumResultCode resCode = (EnumResultCode)Enum.Parse(typeof(EnumResultCode), result.ToString());

                        return(TestResult.Failed(string.Format("Call returned: {0}  ({1})", resCode.ToString(), result)));
                    }
                }
                else
                {
                    EnumResultCode resCode = (EnumResultCode)Enum.Parse(typeof(EnumResultCode), result.ToString());

                    return(TestResult.Failed(string.Format("Call returned: {0}  ({1})", resCode.ToString(), result)));
                }
            }
            catch (Exception ex)
            {
                return(new TestResult(ex));
            }
        }
        /// <summary>
        /// Write Address Space Element
        /// </summary>
        /// <param name="session"></param>
        /// <param name="itemId"></param>
        public void WriteAddresSpaceItem(DaSession session, string itemId, object selectedObject, bool isAddSpaceElem)
        {
            Helper.DataTypes dataTypeHelper = new DataTypes();
            this.panelBottom.Visible = true;

            isDaItem     = false;
            this.session = session;

            ValueQT[] itemValues;
            int[]     itemResults;
            int       result = session.Read(MAX_AGE_DEVICE, new string[] { itemId }, null, out itemValues, out itemResults, null);

            if (itemResults.Length == 0)
            {
                return;
            }
            if (itemValues.Length == 0)
            {
                return;
            }

            Type             itemDataType    = typeof(short);
            EnumAccessRights itemAccessRighs = EnumAccessRights.READWRITEABLE;
            bool             hasWriteAccess  = true;

            if (ResultCode.SUCCEEDED(result) && ResultCode.SUCCEEDED(itemResults[0]))
            {
                DaGetPropertiesOptions propertyGetOptions = new DaGetPropertiesOptions();
                propertyGetOptions.WhatPropertyData = EnumPropertyData.ALL;

                DaProperty[] properties = null;
                if (isAddSpaceElem)
                {
                    if (ResultCode.SUCCEEDED(session.GetDaProperties(itemId,
                                                                     null,
                                                                     propertyGetOptions,
                                                                     out properties,
                                                                     null)))
                    {
                        if (properties == null)
                        {
                            this.propertyGrid.SelectedObject = FillEmptyValueQT(itemId, null);
                            return;
                        }
                        if (properties.Length < 5)
                        {
                            this.propertyGrid.SelectedObject = FillEmptyValueQT(itemId, null);
                            return;
                        }
                        itemDataType    = DataTypeConverter.GetSysType((short)properties[0].ValueQT.Data);
                        itemAccessRighs = ObjToAccesssRights(properties[4].ValueQT.Data);
                    }
                }
                else
                {
                    itemDataType = (selectedObject as DaProperty).DataType;
                }

                //the item value is null
                if (itemValues[0] == null)
                {
                    this.propertyGrid.SelectedObject = FillEmptyValueQT(itemId, itemDataType);
                    return;
                }

                if (itemAccessRighs == EnumAccessRights.READABLE)
                {
                    hasWriteAccess = false;
                }
            }
            this.propertyGrid.SelectedObject = FillWriteService(itemId, itemDataType, hasWriteAccess, itemValues[0]);
        }
        public override TestResult Execute()
        {
            if (string.IsNullOrEmpty(Session))
            {
                return(TestResult.ParameterMissing("Session"));
            }

            DaSession session = GetProperty(Session) as DaSession;

            if (session == null)
            {
                return(TestResult.ParameterInvalid("Session"));
            }

            try
            {
                int       result = (int)EnumResultCode.S_FALSE;
                ValueQT[] values;
                int[]     results;

                string[] itemIDs   = ItemIds.ToArray();
                string[] itemPaths = ItemPaths.Count > 0 ? ItemPaths.ToArray() : null;

                if (ExecutionType == EnumExecutionType.SYNCHRONOUS)
                {
                    result = session.Read(MaxAge, itemIDs, itemPaths, out values, out results, null);
                }
                else
                {
                    m_asyncExecutionContext = DaExecutionContext.Next;
                    ExecutionOptions executionOptions = new ExecutionOptions(EnumExecutionType.ASYNCHRONOUS, m_asyncExecutionContext);

                    m_waitHandle = new System.Threading.AutoResetEvent(false);

                    SessionReadEventHandler asyncHandler = new SessionReadEventHandler(session_ReadCompleted);
                    session.ReadCompleted += asyncHandler;

                    result = session.Read(MaxAge, itemIDs, itemPaths, out values, out results, executionOptions);

                    if (m_waitHandle.WaitOne(30000))
                    {
                        result  = m_asyncResult;
                        values  = m_asyncValues;
                        results = m_asyncResults;
                    }

                    session.ReadCompleted -= asyncHandler;
                }

                if (ResultCode.SUCCEEDED(result))
                {
                    if (!string.IsNullOrEmpty(ValuesProperty))
                    {
                        SetProperty(ValuesProperty, values);
                    }

                    if (!string.IsNullOrEmpty(ResultsProperty))
                    {
                        SetProperty(ResultsProperty, results);
                    }

                    //validate with expected values
                    if (ExpectedValues.Count > 0)
                    {
                        if (ExpectedValues.Count != values.Length)
                        {
                            return(new DaReadTestResult((int)EnumResultCode.S_OK, values, results, "Unexpected number of results"));
                        }

                        for (int i = 0; i < ExpectedValues.Count; i++)
                        {
                            Softing.OPCToolbox.ValueQT receivedValue = values[i];
                            Value expectedValue = ExpectedValues[i];

                            if (expectedValue.Data != null)
                            {
                                if (!expectedValue.Data.Equals(receivedValue.Data))
                                {
                                    return(new DaReadTestResult((int)EnumResultCode.S_OK, values, results, string.Format("Unexpected result Value for item #{0}", i)));
                                }
                            }
                            else if (expectedValue.DataArray != null)
                            {
                                IList receivedList = receivedValue.Data as IList;

                                if (receivedList != null)
                                {
                                    if (expectedValue.DataArray.Count != receivedList.Count)
                                    {
                                        return(new DaReadTestResult((int)EnumResultCode.S_OK, values, results, string.Format("Result Value array is of different size then expected for item #{0}", i)));
                                    }

                                    for (int j = 0; j < receivedList.Count; j++)
                                    {
                                        if (!expectedValue.DataArray[j].Equals(receivedList[j]))
                                        {
                                            return(new DaReadTestResult((int)EnumResultCode.S_OK, values, results, string.Format("Unexpected result Array Item Value for item #{0}", i)));
                                        }
                                    }
                                }
                            }

                            if (expectedValue.Quality != null && expectedValue.Quality != receivedValue.Quality)
                            {
                                return(new DaReadTestResult(result, values, results, string.Format("Unexpected result Quality for item #{0}", i)));
                            }

                            if (expectedValue.TimeStamp != null && expectedValue.TimeStamp != receivedValue.TimeStamp)
                            {
                                return(new DaReadTestResult(result, values, results, string.Format("Unexpected result Timestamp for item #{0}", i)));
                            }
                        }
                    }

                    return(new DaReadTestResult(result, values, results));
                }
                else
                {
                    EnumResultCode resCode = (EnumResultCode)Enum.Parse(typeof(EnumResultCode), result.ToString());
                    if (!string.IsNullOrEmpty(ExpectedServiceResult))
                    {
                        EnumResultCode expectedResCode = (EnumResultCode)Enum.Parse(typeof(EnumResultCode), ExpectedServiceResult.ToString());
                        if (expectedResCode != resCode)
                        {
                            return(TestResult.Failed(string.Format("Unexpected service result: expected {0}, actual {1}", resCode, expectedResCode)));
                        }

                        return(TestResult.Success());
                    }

                    return(TestResult.Failed(string.Format("Call returned: {0}  ({1})", resCode.ToString(), result)));
                }
            }
            catch (Exception ex)
            {
                return(new TestResult(ex));
            }
        }