Ejemplo n.º 1
0
        private TestResult ValidateBrowsedElements(Softing.OPCToolbox.Client.DaAddressSpaceElement[] elements, int result)
        {
            if (ResultCode.SUCCEEDED(result))
            {
                if (!string.IsNullOrEmpty(Property))
                {
                    SetProperty(Property, elements);
                }

                if (ExpectedElements.Count != 0 && ExpectedElements.Count != elements.Length)
                {
                    return(new DaBrowseResult(result, elements, "Unexpected number of results"));
                }

                for (int i = 0; i < ExpectedElements.Count; i++)
                {
                    Softing.OPCToolbox.Client.DaAddressSpaceElement receivedElement = elements[i];
                    DaAddressSpaceElement expectedElement = ExpectedElements[i];

                    if ((expectedElement.ItemId != null && expectedElement.ItemId != receivedElement.ItemId) ||
                        (expectedElement.ItemPath != null && expectedElement.ItemPath != receivedElement.ItemPath) ||
                        (expectedElement.Name != null && expectedElement.Name != receivedElement.Name) ||
                        (expectedElement.IsItem.HasValue && expectedElement.IsItem.Value != receivedElement.IsItem) ||
                        (expectedElement.IsBranch.HasValue && expectedElement.IsBranch.Value != receivedElement.IsBranch) ||
                        (expectedElement.IsLeaf.HasValue && expectedElement.IsLeaf.Value != receivedElement.IsLeaf))
                    {
                        return(new DaBrowseResult(result, elements, string.Format("Unexpected result for item #{0}", i)));
                    }
                }

                return(new DaBrowseResult(result, elements));
            }
            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)));
            }
        }
Ejemplo n.º 2
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"));
            }

            ValueQT[] values;

            if (Values.Count == 0)
            {
                if (!string.IsNullOrEmpty(ValuesProperty))
                {
                    values = GetProperty(ValuesProperty) as ValueQT[];
                    if (values == null)
                    {
                        return(TestResult.ParameterInvalid("ValuesProperty"));
                    }
                }
                else
                {
                    return(TestResult.ParameterMissing("Values"));
                }
            }
            else
            {
                //if the quality was not set ; set it to the value QUALITY_NOT_SET
                for (int i = 0; i < Values.Count; i++)
                {
                    if (Values[i].Wrapped.Quality == Softing.OPCToolbox.EnumQuality.BAD_WAITING_FOR_INITIAL_DATA)
                    {
                        Values[i].Wrapped.SetData(Values[i].Wrapped.Data, Softing.OPCToolbox.EnumQuality.QUALITY_NOT_SET, Values[i].Wrapped.TimeStamp);
                    }
                }
                values = Value.CreateSDKCollection(Values);
            }

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

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

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

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

                    SessionWriteEventHandler asyncHandler = new SessionWriteEventHandler(session_WriteCompleted);
                    session.WriteCompleted += asyncHandler;

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

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

                    session.WriteCompleted -= asyncHandler;
                }

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

                    if (ValidateAllResults)
                    {
                        for (int i = 0; i < results.Length; i++)
                        {
                            if (ResultCode.FAILED(results[i]))
                            {
                                EnumResultCode resCode = (EnumResultCode)Enum.Parse(typeof(EnumResultCode), results[i].ToString());

                                return(TestResult.Failed(string.Format("Result {0} failed with the code: {1}  ({2})", i, resCode.ToString(), results[i])));
                            }
                        }
                    }
                    else if (ExpectedResults.Count > 0)
                    {
                        //validate with expected values
                        if (ExpectedResults.Count != results.Length)
                        {
                            return(new DaWriteTestResult(result, results, "Unexpected number of results"));
                        }

                        for (int i = 0; i < results.Length; i++)
                        {
                            if (results[i] != ExpectedResults[i])
                            {
                                EnumResultCode resCode         = (EnumResultCode)Enum.Parse(typeof(EnumResultCode), results[i].ToString());
                                EnumResultCode resExpectedCode = (EnumResultCode)Enum.Parse(typeof(EnumResultCode), ExpectedResults[i].ToString());

                                return(new DaWriteTestResult(result, results, string.Format("Unexpected result code for item {0}: received {1}  ({2}) instead of {3}  ({4})", i, resCode.ToString(), results[i], resExpectedCode.ToString(), ExpectedResults[i])));
                            }
                        }
                    }

                    return(new DaWriteTestResult(result, 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));
            }
        }
        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));
            }
        }