public override TestResult Execute() { if (string.IsNullOrEmpty(Subscription)) { return(TestResult.ParameterMissing("Subscription")); } DaSubscription subscription = GetProperty(Subscription) as DaSubscription; if (subscription == null) { return(TestResult.ParameterInvalid("Subscription")); } try { int result = subscription.Refresh(MaxAge, new ExecutionOptions()); if (ResultCode.SUCCEEDED(result)) { 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))); } } catch (Exception ex) { return(new TestResult(ex)); } }
public override TestResult Execute() { if (string.IsNullOrEmpty(ObjectProperty)) { return(TestResult.ParameterMissing("Object")); } ObjectSpaceElement element = GetProperty(ObjectProperty) as ObjectSpaceElement; if (element == null) { return(TestResult.ParameterInvalid("Object")); } try { int result = element.Disconnect(new ExecutionOptions()); if (ResultCode.SUCCEEDED(result)) { 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))); } } catch (Exception ex) { return(new TestResult(ex)); } }
public DefaultException(string message, EnumResultCode?code = null) { Message = message; if (code != null && code.HasValue) { Code = code.Value; } }
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 { DaSubscription subscription = new DaSubscription(UpdateRate, session); if (!subscription.Valid) { return(TestResult.Failed("DaSubscription not Valid")); } if (RequestedKeepAliveTime.HasValue) { subscription.RequestedKeepAliveTime = RequestedKeepAliveTime.Value; } // set the global property anyhow. SetProperty(Property, subscription); if (Connect) { int result = subscription.Connect(Deep, Active, null); if (ResultCode.FAILED(result)) { EnumResultCode resCode = (EnumResultCode)Enum.Parse(typeof(EnumResultCode), result.ToString()); return(TestResult.Failed(string.Format("Call returned: {0} ({1})", resCode.ToString(), result))); } } if (Deadband.HasValue) { subscription.Deadband = Deadband.Value; } } catch (Exception ex) { if (ExpectedSuccess) { return(new TestResult(ex)); } } return(TestResult.Success()); }
public override TestResult Execute() { if (string.IsNullOrEmpty(ObjectProperty)) { return(TestResult.ParameterMissing("Object")); } ObjectSpaceElement element = GetProperty(ObjectProperty) as ObjectSpaceElement; ObjectSpaceElement[] elements = null; if (element == null) { elements = GetProperty(ObjectProperty) as DaItem[]; if (elements == null) { return(TestResult.ParameterInvalid("Object")); } } try { int result = (int)EnumResultCode.E_FAIL; if (element != null) { result = element.Connect(Deep, Active, new ExecutionOptions()); } else { foreach (ObjectSpaceElement el in elements) { result = el.Connect(Deep, Active, new ExecutionOptions()); if (!ResultCode.SUCCEEDED(result)) { break; } } } if (ResultCode.SUCCEEDED(result)) { 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))); } } catch (Exception ex) { return(new TestResult(ex)); } }
public MessageException(EnumResultCode code, string message) { Code = code; Message = EnumHelper.GetName(code); if (!VerifyHelper.IsEmpty(message)) { Message = message; } }
public DefaultException(EnumResultCode code, Exception exception) { Code = code; Message = EnumHelper.GetName(code); if (!VerifyHelper.IsEmpty(exception)) { Message = exception.ToString(); } }
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 (string.IsNullOrEmpty(NodeSetId)) { return(TestResult.ParameterMissing("NodeSetId")); } string nodeSetId = GetProperty(NodeSetId) as string; if (nodeSetId == null) { return(TestResult.ParameterInvalid("NodeSetId")); } try { int result = (int)EnumResultCode.S_FALSE; int[] results; string[] itemIDs; ValueQT[] values; // set stop simulation params itemIDs = new string[] { nodeSetId + m_deleteVariablesCommand }; values = new ValueQT[] { new ValueQT(true, EnumQuality.GOOD, DateTime.Now) }; // send write request result = session.Write(itemIDs, null, values, out results, null); if (ResultCode.SUCCEEDED(result)) { 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))); } } catch (Exception ex) { return(new TestResult(ex)); } }
/// <summary> /// 将返回代码转化为消息 /// </summary> /// <param name="code"></param> /// <returns></returns> public static string ToMessage(this EnumResultCode code) { var type = code.GetType(); //获取类型 var memberInfos = type.GetMember(code.ToString()); //获取成员 if (memberInfos.Length > 0) { var attrs = memberInfos[0].GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[]; //获取描述特性 if (attrs != null && attrs.Length > 0) { return(attrs[0].Description); //返回当前描述 } } return("未知错误"); }
public DefaultException(Exception exception, EnumResultCode?code = null) { if (!VerifyHelper.IsEmpty(exception)) { Message = (exception.InnerException != null && exception.InnerException.Message != null) ? exception.InnerException.Message : exception.Message; Message = !VerifyHelper.IsEmpty(Message) ? Message : exception.ToString(); } if (code != null && code.HasValue) { Code = code.Value; } }
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))); } }
public override TestResult Execute() { if (string.IsNullOrEmpty(Url)) { return(TestResult.ParameterMissing("Url")); } try { ExecutionOptions executionOptions = new ExecutionOptions(EnumExecutionType.SYNCHRONOUS, 0); DaSession session = new DaSession(ExpandProperty(Url)); if (!session.Valid) { return(TestResult.Failed("DaSession not Valid")); } // set the global prioperty no matter what SetProperty(Property, session); if (ForcedOpcSpecification != EnumOPCSpecification.DEFAULT) { session.ForcedOpcSpecification = ForcedOpcSpecification; } if (Connect) { int result = session.Connect(Deep, Active, executionOptions); if (ResultCode.FAILED(result)) { EnumResultCode resCode = (EnumResultCode)Enum.Parse(typeof(EnumResultCode), result.ToString()); return(TestResult.Failed(string.Format("Call returned: {0} ({1})", resCode.ToString(), result))); } } } catch (Exception ex) { if (ExpectedSuccess) { return(new TestResult(ex)); } } return(TestResult.Success()); }
/// <summary> /// 参数错误 /// </summary> /// <param name="args">args 参数列表 </param> public DefaultException(EnumResultCode code, params string[] args) { Code = code; Message = EnumHelper.GetName(code); if (!VerifyHelper.IsEmpty(args)) { StringBuilder msgBuild = new StringBuilder(); for (int i = 0; i < args.Length; i++) { msgBuild.AppendFormat("{0},", args[i]); } if (code != EnumResultCode.参数错误) { Message = StringHelper.ToBuildString(msgBuild); } else { Message = string.Format("{0}:({1})", EnumHelper.GetName(Code), StringHelper.ToBuildString(msgBuild)); } } }
public ResultDto(EnumResultCode code) { Code = (int)code; Message = code.ToMessage(); Data = null; }
public DataResult(EnumResultCode code, string msg, T data) { Code = code; Msg = msg ?? code.ToString(); Data = data; }
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")); } 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)); } }
public override TestResult Execute() { if (string.IsNullOrEmpty(ItemsProperty)) { return(TestResult.ParameterMissing("ItemsProperty")); } DaItem[] items = GetProperty(ItemsProperty) as DaItem[]; if (items == null) { return(TestResult.ParameterInvalid("ItemsProperty")); } 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 { values = Value.CreateSDKCollection(Values); } try { int[] results = new int[items.Length]; for (int i = 0; i < items.Length; i++) { DaItem item = items[i]; ValueQT value = values[i]; int writeResult; int result = item.Write(value, out writeResult, new ExecutionOptions()); if (ResultCode.SUCCEEDED(result)) { results[i] = writeResult; if (ValidateAllResults) { 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 > i) { //validate with expected values 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]))); } } } else { EnumResultCode resCode = (EnumResultCode)Enum.Parse(typeof(EnumResultCode), result.ToString()); return(TestResult.Failed(string.Format("Call returned: {0} ({1})", resCode.ToString(), result))); } } if (!string.IsNullOrEmpty(ResultsProperty)) { SetProperty(ResultsProperty, results); } return(TestResult.Success()); } catch (Exception ex) { return(new TestResult(ex)); } }
public override TestResult Execute() { if (string.IsNullOrEmpty(Session)) { return(TestResult.ParameterMissing("Session")); } if (string.IsNullOrEmpty(RootID)) { return(TestResult.ParameterMissing("RootID")); } // if (string.IsNullOrEmpty(RootPath)) // return TestResult.ParameterMissing("RootPath"); Softing.OPCToolbox.Client.DaSession session = GetProperty(Session) as DaSession; if (session == null) { return(TestResult.Failed("Session non existent")); } try { ExecutionOptions executionOptions = new ExecutionOptions(EnumExecutionType.SYNCHRONOUS, 0); session.Connect(false, false, executionOptions); Softing.OPCToolbox.Client.DaAddressSpaceElement[] elements; Softing.OPCToolbox.Client.DaAddressSpaceElement rootDaAddressSpaceElement = new Softing.OPCToolbox.Client.DaAddressSpaceElement( EnumAddressSpaceElementType.BRANCH, String.Empty, string.Empty, string.Empty, 0, null); rootDaAddressSpaceElement.Session = session; int result = rootDaAddressSpaceElement.Browse(new Softing.OPCToolbox.Client.DaAddressSpaceElementBrowseOptions(), out elements, new ExecutionOptions(EnumExecutionType.SYNCHRONOUS, m_asyncExecutionContext)); if (!ResultCode.SUCCEEDED(result)) { EnumResultCode resCode = (EnumResultCode)Enum.Parse(typeof(EnumResultCode), result.ToString()); return(TestResult.Failed(string.Format("Call returned: {0} ({1})", resCode.ToString(), result))); } Softing.OPCToolbox.Client.DaAddressSpaceElement addressSpaceElement = null; for (int i = 0; i < elements.Length; i++) { if (elements[i].ItemId == RootID) { addressSpaceElement = elements[i]; break; } } if (addressSpaceElement == null) { return(TestResult.Failed(string.Format("Address space element {0} not found!", RootID))); } if (BrowseOptions == null) { BrowseOptions = new DaAddressSpaceElementBrowseOptions(); } if (ResultCode.SUCCEEDED(addressSpaceElement.Browse(BrowseOptions.Wrapped, out elements, new ExecutionOptions(EnumExecutionType.SYNCHRONOUS, 0)))) { //Softing.OPCToolbox.Client.DaAddressSpaceElement daElement; Softing.OPCToolbox.Client.DaSubscription workerSubscription = new Softing.OPCToolbox.Client.DaSubscription(updateRate, session); foreach (Softing.OPCToolbox.Client.DaAddressSpaceElement daElement in elements) { DaItem newItem = new DaItem(daElement.ItemId, workerSubscription); // result = Connect(true, true, executionOptions); } result = workerSubscription.Connect(true, true, executionOptions); if (ResultCode.FAILED(result)) { EnumResultCode resCode = (EnumResultCode)Enum.Parse(typeof(EnumResultCode), result.ToString()); return(TestResult.Failed(string.Format("Subscribe Call returned: {0} ({1})", resCode.ToString(), result))); } SetProperty(Property, workerSubscription); }//end if else { EnumResultCode resCode = (EnumResultCode)Enum.Parse(typeof(EnumResultCode), result.ToString()); return(TestResult.Failed(string.Format(" Browse Call returned: {0} ({1})", resCode.ToString(), result))); } } catch (Exception ex) { if (ExpectedSuccess) { return(new TestResult(ex)); } } return(TestResult.Success()); }
public override TestResult Execute() { if (string.IsNullOrEmpty(Session)) { return(TestResult.ParameterMissing("Session")); } if (string.IsNullOrEmpty(RootID)) { return(TestResult.ParameterMissing("RootID")); } // if (string.IsNullOrEmpty(RootPath)) // return TestResult.ParameterMissing("RootPath"); Softing.OPCToolbox.Client.DaSession session = GetProperty(Session) as DaSession; if (session == null) { return(TestResult.Failed("Session non existent")); } try { ExecutionOptions executionOptions = new ExecutionOptions(EnumExecutionType.SYNCHRONOUS, 0); session.Connect(false, false, executionOptions); int result = 1; Softing.OPCToolbox.Client.DaAddressSpaceElementBrowseOptions browseOptions = new Softing.OPCToolbox.Client.DaAddressSpaceElementBrowseOptions(); browseOptions.ElementTypeFilter = EnumAddressSpaceElementType.LEAF; Softing.OPCToolbox.Client.DaAddressSpaceElement[] DaElements; //get the nodes children if (ResultCode.SUCCEEDED(session.Browse(RootID, RootPath, browseOptions, out DaElements, executionOptions))) { //Softing.OPCToolbox.Client.DaAddressSpaceElement daElement; Softing.OPCToolbox.Client.DaSubscription workerSubscription = new Softing.OPCToolbox.Client.DaSubscription(updateRate, session); foreach (Softing.OPCToolbox.Client.DaAddressSpaceElement daElement in DaElements) { DaItem newItem = new DaItem(daElement.ItemId, workerSubscription); // result = Connect(true, true, executionOptions); } result = workerSubscription.Connect(true, true, executionOptions); if (ResultCode.FAILED(result)) { EnumResultCode resCode = (EnumResultCode)Enum.Parse(typeof(EnumResultCode), result.ToString()); return(TestResult.Failed(string.Format("Subscribe Call returned: {0} ({1})", resCode.ToString(), result))); } } //end if else { EnumResultCode resCode = (EnumResultCode)Enum.Parse(typeof(EnumResultCode), result.ToString()); return(TestResult.Failed(string.Format(" Browse Call returned: {0} ({1})", resCode.ToString(), result))); } } catch (Exception ex) { if (ExpectedSuccess) { return(new TestResult(ex)); } } return(TestResult.Success()); }
public ResultDto(EnumResultCode code, object data) { Code = (int)code; Message = code.ToMessage(); Data = data; }
/// <summary> /// 将object自动转化为ResultDto /// </summary> /// <param name="data"></param> /// <param name="code"></param> /// <returns></returns> public static ResultDto ResultTo <T>(this object data, EnumResultCode code = EnumResultCode.Success) { return(new ResultDto <T>(code, data)); }
/// <summary> /// 将返回代码转化为ResultDto /// </summary> /// <param name="code"></param> /// <returns></returns> public static ResultDto ToResultDto(this EnumResultCode code) { return(new ResultDto(code)); }
/// <summary> /// Checks whether a result code is <b>not</b> an error and returns true in that case. /// </summary> /// <param name = "res">A <see cref="EnumResultCode"/> that represents a result to be tested on success.</param> /// <returns> /// <i>true</i>, given result doesn't represent an error; /// <i>false</i>, given result represents an error.</returns> /// <include /// file='TBN.doc.xml' /// path='//class[@name="ResultCode"]/method[@name="SUCCEEDED_2"]/doc/*' /// /> public static bool SUCCEEDED(EnumResultCode res) { return ((((int) res) & ERROR_MASK) == SUCCESS_MASK); }
public override TestResult Execute() { if (string.IsNullOrEmpty(Subscription)) { return(TestResult.ParameterMissing("Subscription")); } DaSubscription subscription = GetProperty(Subscription) as DaSubscription; if (subscription == null) { return(TestResult.ParameterInvalid("Subscription")); } try { ExecutionOptions executionOptions = new ExecutionOptions(EnumExecutionType.SYNCHRONOUS, 0); int result; List <DaItem> items = new List <DaItem>(ItemIds.Count); foreach (string itemId in ItemIds) { DaItem item = new DaItem(itemId, subscription); if (!item.Valid) { return(TestResult.Failed("Item not Valid")); } if (Deadband.HasValue) { item.Deadband = Deadband.Value; } if (Connect) { result = item.Connect(false, Active, executionOptions); if (ResultCode.FAILED(result)) { EnumResultCode resCode = (EnumResultCode)Enum.Parse(typeof(EnumResultCode), result.ToString()); return(TestResult.Failed(string.Format("Call returned: {0} ({1})", resCode.ToString(), result))); } } items.Add(item); } SetProperty(Property, items.ToArray()); } catch (Exception ex) { if (ExpectedSuccess) { return(new TestResult(ex)); } } return(TestResult.Success()); }
public override TestResult Execute() { if (string.IsNullOrEmpty(SessionProperty)) { return(TestResult.ParameterMissing("Session")); } DaSession session = GetProperty(SessionProperty) as DaSession; if (session == null) { return(TestResult.ParameterInvalid("Session")); } try { Softing.OPCToolbox.Client.DaAddressSpaceElement[] adElements; Softing.OPCToolbox.Client.DaAddressSpaceElement rootDaAddressSpaceElement = new Softing.OPCToolbox.Client.DaAddressSpaceElement( EnumAddressSpaceElementType.BRANCH, String.Empty, string.Empty, string.Empty, 0, null); rootDaAddressSpaceElement.Session = session; int result = rootDaAddressSpaceElement.Browse(new Softing.OPCToolbox.Client.DaAddressSpaceElementBrowseOptions(), out adElements, new ExecutionOptions(EnumExecutionType.SYNCHRONOUS, m_asyncExecutionContext)); Softing.OPCToolbox.Client.DaAddressSpaceElement addressSpaceElement = null; if (ResultCode.SUCCEEDED(result)) { for (int i = 0; i < adElements.Length; i++) { if (adElements[i].ItemId == RootID) { addressSpaceElement = adElements[i]; } } } else { EnumResultCode resCode = (EnumResultCode)Enum.Parse(typeof(EnumResultCode), result.ToString()); return(TestResult.Failed(string.Format("Call returned: {0} ({1})", resCode.ToString(), result))); } if (addressSpaceElement == null) { return(TestResult.Failed(string.Format("Address space element {0} not found!", RootID))); } Softing.OPCToolbox.Client.DaProperty[] elements; result = int.MaxValue; if (ExecutionType == EnumExecutionType.SYNCHRONOUS) { result = addressSpaceElement.GetDaProperties(DaGetPropertiesOptions, out elements, new ExecutionOptions(ExecutionType, 0)); } else { m_asyncExecutionContext = DaExecutionContext.Next; session.GetDaPropertiesCompleted += session_GetDaPropertiesCompleted; m_waitHandle = new System.Threading.AutoResetEvent(false); result = addressSpaceElement.GetDaProperties(DaGetPropertiesOptions, out elements, new ExecutionOptions(EnumExecutionType.ASYNCHRONOUS, m_asyncExecutionContext)); if (m_waitHandle.WaitOne(3000)) { result = m_browseAsyncResult; elements = m_browseAsyncResults; } session.GetDaPropertiesCompleted -= session_GetDaPropertiesCompleted; } if (ResultCode.SUCCEEDED(result)) { if (!string.IsNullOrEmpty(Property)) { SetProperty(Property, elements); } if (ExpectedElements.Count != 0 && ExpectedElements.Count != elements.Length) { return(TestResult.Failed(string.Format("Unexpected number of results, expected {0}, actual {1}", ExpectedElements.Count, elements.Length))); } for (int i = 0; i < ExpectedElements.Count; i++) { Softing.OPCToolbox.Client.DaProperty receivedElement = elements[i]; TestClientGui.Support.DaProperty expectedElement = ExpectedElements[i]; if ((expectedElement.ItemId != null && expectedElement.ItemId != receivedElement.ItemId) || (expectedElement.ItemPath != null && expectedElement.ItemPath != receivedElement.ItemPath) || (!string.IsNullOrEmpty(expectedElement.Name) && expectedElement.Name != receivedElement.Name) || (expectedElement.DataType != null && expectedElement.DataType != receivedElement.DataType) || (!string.IsNullOrEmpty(expectedElement.Description) && expectedElement.Description != receivedElement.Description) || (expectedElement.ValueQT != null && !expectedElement.ValueQT.Data.Equals(receivedElement.ValueQT.Data)) || (expectedElement.ValueQT != null && expectedElement.ValueQT.Quality != receivedElement.ValueQT.Quality)) { return(TestResult.Failed(string.Format("Unexpected result for item #{0}", i))); } if (DaGetPropertiesOptions.WhatPropertyData == EnumPropertyData.ITEMID || DaGetPropertiesOptions.WhatPropertyData == EnumPropertyData.NONE) { if (receivedElement.ValueQT.Data != null) { return(TestResult.Failed(string.Format("Value was not NULL for element #{0}", i))); } } if (DaGetPropertiesOptions.WhatPropertyData == EnumPropertyData.VALUE) { if (!string.IsNullOrEmpty(receivedElement.ItemId)) { return(TestResult.Failed(string.Format("ItemId was not NULL for element #{0}", i))); } } } return(TestResult.Success()); } else { EnumResultCode resCode = (EnumResultCode)Enum.Parse(typeof(EnumResultCode), result.ToString()); return(TestResult.Failed(string.Format("Call returned: {0}\t({1})", resCode.ToString(), result))); } } catch (Exception ex) { return(new TestResult(ex)); } }
/// <summary> /// Checks whether a result code represents an error and returns true in case of an error. /// </summary> /// <param name = "res">A <see cref="EnumResultCode"/> that represents a result to be tested on failure.</param> /// <returns> /// <i>true</i>, given result represents an error; /// <i>false</i>, given result doesn't represent an error.</returns> /// <include /// file='TBN.doc.xml' /// path='//class[@name="ResultCode"]/method[@name="FAILED_2"]/doc/*' /// /> public static bool FAILED(EnumResultCode res) { return ((((int) res) & ERROR_MASK) == ERROR_MASK); }
public override TestResult Execute() { if (string.IsNullOrEmpty(ItemsProperty)) { return(TestResult.ParameterMissing("ItemsProperty")); } DaItem[] items = GetProperty(ItemsProperty) as DaItem[]; if (items == null || items.Length == 0) { return(TestResult.ParameterInvalid("ItemsProperty")); } DaSubscription subscription = items[0].DaSubscription; try { int result = (int)EnumResultCode.S_FALSE; ValueQT[] values; int[] results; if (ExecutionType == EnumExecutionType.SYNCHRONOUS) { result = subscription.Read(MaxAge, items, 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); SubscriptionReadEventHandler asyncHandler = new SubscriptionReadEventHandler(subscription_ReadCompleted); subscription.ReadCompleted += asyncHandler; result = subscription.Read(MaxAge, items, out values, out results, executionOptions); if (m_waitHandle.WaitOne(30000)) { result = m_asyncResult; values = m_asyncValues; results = m_asyncResults; } subscription.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))); } //BUG in TB 4.0 //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()); 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(ItemsProperty)) { return(TestResult.ParameterMissing("ItemsProperty")); } DaItem[] items = GetProperty(ItemsProperty) as DaItem[]; if (items == null) { return(TestResult.ParameterInvalid("ItemsProperty")); } try { ValueQT[] values = new ValueQT[items.Length]; int[] results = new int[items.Length]; for (int i = 0; i < items.Length; i++) { DaItem item = items[i]; ValueQT value; int readResult; int result = item.Read(MaxAge, out value, out readResult, new ExecutionOptions()); values[i] = value; results[i] = result; if (ResultCode.FAILED(result)) { EnumResultCode resCode = (EnumResultCode)Enum.Parse(typeof(EnumResultCode), result.ToString()); return(TestResult.Failed(string.Format("Call returned: {0} ({1})", resCode.ToString(), result))); } } if (!string.IsNullOrEmpty(ValuesProperty)) { SetProperty(ValuesProperty, values); } if (!string.IsNullOrEmpty(ResultsProperty)) { SetProperty(ResultsProperty, results); } //validate with expected values if (ExpectedValues.Count != 0 && 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((int)EnumResultCode.S_OK, values, results, string.Format("Unexpected result Quality for item #{0}", i))); } //BUG in TB 4.0 //if( expectedValue.TimeStamp != null && expectedValue.TimeStamp != receivedValue.TimeStamp ) // return new DaReadTestResult( (int) EnumResultCode.S_OK, values, results, string.Format( "Unexpected result Timestamp for item #{0}", i ) ); } return(new DaReadTestResult((int)EnumResultCode.S_OK, values, results)); } catch (Exception ex) { return(new TestResult(ex)); } }
} // end HandleReadRequests public override int HandleWriteRequests() { int count = Requests.Count; lock (OpcServer.LockTransactionForWrite) { for (int i = 0; i < count; i++) { DaRequest request = Requests[i] as DaRequest; if (request != null) { MyDaAddressSpaceElement element = request.AddressSpaceElement as MyDaAddressSpaceElement; if (element != null) { ValueQT valueQT = request.Value; request.Result = (EnumResultCode)element.ValueChanged(valueQT); } // end if DaCreateItemsCommand createCommand = request.AddressSpaceElement as DaCreateItemsCommand; if (createCommand != null) { ValueQT valueQT = request.Value; // Execute create items command EnumResultCode result = createCommand.CreateItems(valueQT); request.Result = (EnumResultCode)createCommand.ValueChanged(valueQT); if (result != EnumResultCode.S_OK) { request.Result = result; } } DaDeleteItemsCommand deleteCommand = request.AddressSpaceElement as DaDeleteItemsCommand; if (deleteCommand != null) { // Execute delete items command request.Result = deleteCommand.DeleteItems(); } DaStartStopSimulationCommand simulationCommand = request.AddressSpaceElement as DaStartStopSimulationCommand; if (simulationCommand != null) { ValueQT valueQT = request.Value; request.Result = (EnumResultCode)simulationCommand.ValueChanged(valueQT); EnumResultCode result; bool startCommand = (bool)valueQT.Data; if (startCommand) { // Execute Start simulation command result = simulationCommand.StartSimulation(); } else { // Execute Stop simulation command result = simulationCommand.StopSimulation(); } if (result != EnumResultCode.S_OK) { request.Result = result; } } } // end if } // end for } return(CompleteRequests()); } // end HandleWriteRequests