// ---- METHODS(PUBLIC) ---------------------------------------------------------------------------------------

        /// <summary>
        /// Loads the data from the given <paramref name="stream"/>.
        /// </summary>
        /// <param name="stream">The <see cref="Stream"/> to load the data from.</param>
        public void Load(Stream stream)
        {
            // Load all the values from the stream.
            ByamlSerializer serializer = new ByamlSerializer(new ByamlSerializerSettings()
            {
                SupportPaths = true,
                Version = ByamlVersion.Version1
            });
            serializer.Deserialize(stream, this);

            // After loading all the instances, allow references to be resolved.
            Areas?.ForEach(x => x.DeserializeReferences(this));

            EnemyPaths?.ForEach(x => x.DeserializeReferences(this));
            GCameraPaths?.ForEach(x => x.DeserializeReferences(this));
            GlidePaths?.ForEach(x => x.DeserializeReferences(this));
            GravityPaths?.ForEach(x => x.DeserializeReferences(this));
            ItemPaths?.ForEach(x => x.DeserializeReferences(this));
            JugemPaths?.ForEach(x => x.DeserializeReferences(this));
            LapPaths?.ForEach(x => x.DeserializeReferences(this));
            ObjPaths?.ForEach(x => x.DeserializeReferences(this));
            Paths?.ForEach(x => x.DeserializeReferences(this));
            PullPaths?.ForEach(x => x.DeserializeReferences(this));

            Objs.ForEach(x => x.DeserializeReferences(this));

            ReplayCameras?.ForEach(x => x.DeserializeReferences(this));
        }
        /// <summary>
        /// Saves the data into the given <paramref name="stream"/>.
        /// </summary>
        /// <param name="stream">The <see cref="Stream"/> to save the data to.</param>
        public void Save(Stream stream)
        {
            // Before saving all the instances, allow references to be resolved.
            Areas?.ForEach(x => x.SerializeReferences(this));

            EnemyPaths?.ForEach(x => x.SerializeReferences(this));
            GCameraPaths?.ForEach(x => x.SerializeReferences(this));
            GlidePaths?.ForEach(x => x.SerializeReferences(this));
            GravityPaths?.ForEach(x => x.SerializeReferences(this));
            ItemPaths?.ForEach(x => x.SerializeReferences(this));
            JugemPaths?.ForEach(x => x.SerializeReferences(this));
            LapPaths?.ForEach(x => x.SerializeReferences(this));
            PullPaths?.ForEach(x => x.SerializeReferences(this));

            Objs.ForEach(x => x.SerializeReferences(this));

            ReplayCameras?.ForEach(x => x.SerializeReferences(this));

            // Save the serialized values.
            ByamlSerializer serializer = new ByamlSerializer(new ByamlSerializerSettings()
            {
                SupportPaths = true,
                Version = ByamlVersion.Version1
            });
            serializer.Serialize(stream, this);
        }
Beispiel #3
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));
            }
        }