/// <summary> /// Constructs a object from an exception. /// </summary> /// <remarks> /// The code, symbolicId, namespaceUri and localizedText parameters are ignored for ServiceResultExceptions. /// </remarks> public ServiceResult( Exception e, uint defaultCode, string defaultSymbolicId, string defaultNamespaceUri, LocalizedText defaultLocalizedText) { ServiceResultException sre = e as ServiceResultException; if (sre != null) { m_code = sre.StatusCode; m_namespaceUri = sre.NamespaceUri; m_symbolicId = sre.SymbolicId; m_localizedText = sre.LocalizedText; m_innerResult = sre.Result.InnerResult; if (LocalizedText.IsNullOrEmpty(m_localizedText)) { m_localizedText = defaultLocalizedText; } } else { m_code = defaultCode; m_symbolicId = defaultSymbolicId; m_namespaceUri = defaultNamespaceUri; m_localizedText = defaultLocalizedText; } m_additionalInfo = BuildExceptionTrace(e); }
/// <summary> /// Converts the value to a human readable string. /// </summary> public override string ToString() { StringBuilder buffer = new StringBuilder(); buffer.Append(LookupSymbolicId(m_code)); if (!String.IsNullOrEmpty(m_symbolicId)) { if (!String.IsNullOrEmpty(m_namespaceUri)) { buffer.AppendFormat(" ({0}:{1})", m_namespaceUri, m_symbolicId); } else if (m_symbolicId != buffer.ToString()) { buffer.AppendFormat(" ({0})", m_symbolicId); } } if (!LocalizedText.IsNullOrEmpty(m_localizedText)) { buffer.AppendFormat(" '{0}'", m_localizedText); } if ((0x0000FFFF & Code) != 0) { buffer.AppendFormat(" [{0:X4}]", (0x0000FFFF & Code)); } return(buffer.ToString()); }
/// <summary> /// Translates the discovery URLs based on the client url and returns an updated ApplicationDescription. /// </summary> /// <param name="clientUrl">The client URL.</param> /// <param name="description">The application description.</param> /// <param name="baseAddresses">The base addresses.</param> /// <param name="applicationName">The localized application name.</param> /// <returns>A copy of the application description</returns> protected ApplicationDescription TranslateApplicationDescription( Uri clientUrl, ApplicationDescription description, IList <BaseAddress> baseAddresses, LocalizedText applicationName) { // get the discovery urls. StringCollection discoveryUrls = new StringCollection(); foreach (BaseAddress baseAddress in baseAddresses) { discoveryUrls.Add(GetBestDiscoveryUrl(clientUrl, baseAddress)); } // copy the description. ApplicationDescription copy = new ApplicationDescription(); copy.ApplicationName = description.ApplicationName; copy.ApplicationUri = description.ApplicationUri; copy.ApplicationType = description.ApplicationType; copy.ProductUri = description.ProductUri; copy.GatewayServerUri = description.DiscoveryProfileUri; copy.DiscoveryUrls = discoveryUrls; if (!LocalizedText.IsNullOrEmpty(applicationName)) { copy.ApplicationName = applicationName; } // return the copy. return(copy); }
/// <summary> /// Extracts an exception message from a Result object. /// </summary> private static string GetMessage(ServiceResult status) { if (status == null) { return(Strings.DefaultMessage); } if (!LocalizedText.IsNullOrEmpty(status.LocalizedText)) { return(status.LocalizedText.Text); } return(status.ToString()); }
/// <summary> /// Returns a mask which indicates which attributes have non-default value. /// </summary> /// <param name="context">The context for the system being accessed.</param> /// <returns>A mask the specifies the available attributes.</returns> public override AttributesToSave GetAttributesToSave(ISystemContext context) { AttributesToSave attributesToSave = base.GetAttributesToSave(context); if (!LocalizedText.IsNullOrEmpty(m_inverseName)) { attributesToSave |= AttributesToSave.InverseName; } if (m_symmetric) { attributesToSave |= AttributesToSave.Symmetric; } return(attributesToSave); }
/// <summary> /// Saves the attributes from the stream. /// </summary> /// <param name="context">The context for the system being accessed.</param> /// <param name="encoder">The encoder wrapping the stream to write.</param> public override void Save(ISystemContext context, XmlEncoder encoder) { base.Save(context, encoder); encoder.PushNamespace(Namespaces.OpcUaXsd); if (!LocalizedText.IsNullOrEmpty(m_inverseName)) { encoder.WriteLocalizedText("InverseName", m_inverseName); } if (m_symmetric) { encoder.WriteBoolean("Symmetric", m_symmetric); } encoder.PopNamespace(); }
/// <summary> /// Creates a fault message. /// </summary> /// <param name="request">The request.</param> /// <param name="exception">The exception.</param> /// <returns>A fault message.</returns> protected static Exception CreateSoapFault(IServiceRequest request, Exception exception) { ServiceFault fault = CreateFault(request, exception); // get the error from the header. ServiceResult error = fault.ResponseHeader.ServiceResult; if (error == null) { error = ServiceResult.Create(StatusCodes.BadUnexpectedError, "An unknown error occurred."); } // construct the fault code and fault reason. string codeName = StatusCodes.GetBrowseName(error.Code); FaultCode code = null; FaultReason reason = null; if (!LocalizedText.IsNullOrEmpty(error.LocalizedText)) { reason = new FaultReason(new FaultReasonText(Utils.Format("{0}", error.LocalizedText), CultureInfo.InvariantCulture)); } else { reason = new FaultReason(new FaultReasonText(codeName, CultureInfo.InvariantCulture)); } if (!String.IsNullOrEmpty(error.SymbolicId)) { FaultCode subcode = new FaultCode(error.SymbolicId, error.NamespaceUri); code = new FaultCode(codeName, Namespaces.OpcUa, subcode); } else { code = new FaultCode(codeName, Namespaces.OpcUa); } // throw the fault. return(new FaultException <ServiceFault>(fault, reason, code)); }
/// <summary> /// Returns a formatted string with the contents of exception. /// </summary> public string ToLongString() { StringBuilder buffer = new StringBuilder(); buffer.Append("Id: "); buffer.Append(StatusCodes.GetBrowseName(m_code)); if (!String.IsNullOrEmpty(m_symbolicId)) { buffer.AppendLine(); buffer.Append("SymbolicId: "); buffer.Append(m_symbolicId); } if (!LocalizedText.IsNullOrEmpty(m_localizedText)) { buffer.AppendLine(); buffer.Append("Description: "); buffer.Append(m_localizedText); } if (AdditionalInfo != null && AdditionalInfo.Length > 0) { buffer.AppendLine(); buffer.Append(AdditionalInfo); } ServiceResult innerResult = m_innerResult; if (innerResult != null) { buffer.AppendLine(); buffer.Append("==="); buffer.AppendLine(); buffer.Append(innerResult.ToLongString()); } return(buffer.ToString()); }
/// <summary> /// Updates the effective state for the condition. /// </summary> /// <param name="context">The context.</param> protected override void UpdateEffectiveState(ISystemContext context) { if (!this.EnabledState.Id.Value) { base.UpdateEffectiveState(context); return; } StringBuilder builder = new StringBuilder(); string locale = null; if (this.ActiveState.Value != null) { locale = this.ActiveState.Value.Locale; if (this.ActiveState.Id.Value) { if (this.ActiveState.EffectiveDisplayName != null && !LocalizedText.IsNullOrEmpty(this.ActiveState.EffectiveDisplayName.Value)) { builder.Append(this.ActiveState.EffectiveDisplayName.Value); } else { builder.Append(this.ActiveState.Value); } } else { builder.Append(this.ActiveState.Value); } } LocalizedText suppressedState = null; if (this.SuppressedState != null) { if (this.SuppressedState.Id.Value) { suppressedState = this.SuppressedState.Value; } } if (this.ShelvingState != null) { if (this.ShelvingState.CurrentState.Id.Value != ObjectIds.ShelvedStateMachineType_Unshelved) { suppressedState = this.ShelvingState.CurrentState.Value; } } if (suppressedState != null) { builder.Append(" | "); builder.Append(suppressedState); } LocalizedText ackState = null; if (ConfirmedState != null) { if (!this.ConfirmedState.Id.Value) { ackState = this.ConfirmedState.Value; } } if (AckedState != null) { if (!this.AckedState.Id.Value) { ackState = this.AckedState.Value; } } if (ackState != null) { builder.Append(" | "); builder.Append(ackState); } LocalizedText effectiveState = new LocalizedText(locale, builder.ToString()); SetEffectiveSubState(context, effectiveState, DateTime.MinValue); }
/// <summary> /// Adds a set of nodes to the table. /// </summary> /// <param name="nodeSet">The node set.</param> /// <param name="externalReferences">The external references.</param> /// <returns></returns> public List <Node> Import(NodeSet nodeSet, IDictionary <NodeId, IList <IReference> > externalReferences) { List <Node> importedNodes = new List <Node>(); if (nodeSet == null) { return(importedNodes); } // add the nodes. foreach (Node nodeToImport in nodeSet.Nodes) { // ignore empty nodes. if (nodeToImport == null || NodeId.IsNull(nodeToImport.NodeId)) { continue; } Node node = nodeSet.Copy(nodeToImport, m_namespaceUris, m_serverUris); // assign a browse name. if (QualifiedName.IsNull(node.BrowseName)) { node.BrowseName = new QualifiedName(node.NodeId.ToString(), 1); } // assign a display name. if (LocalizedText.IsNullOrEmpty(node.DisplayName)) { node.DisplayName = new LocalizedText(node.BrowseName.Name); } // index references (the node ids in the references were translated by the Copy() call above). foreach (ReferenceNode reference in node.References) { // ignore invalid references. if (NodeId.IsNull(reference.ReferenceTypeId) || NodeId.IsNull(reference.TargetId)) { continue; } // ignore missing targets. ExpandedNodeId targetId = reference.TargetId; if (NodeId.IsNull(targetId)) { continue; } // index reference. node.ReferenceTable.Add(reference.ReferenceTypeId, reference.IsInverse, targetId); // see if a remote node needs to be created. if (targetId.ServerIndex != 0) { RemoteNode remoteNode = Find(targetId) as RemoteNode; if (remoteNode == null) { remoteNode = new RemoteNode(this, targetId); InternalAdd(remoteNode); } remoteNode.AddRef(); } } // clear imported references. node.References.Clear(); // add the node. InternalAdd(node); importedNodes.Add(node); } // import the nodes. foreach (Node node in importedNodes) { // ignore invalid nodes. if (node == null || NodeId.IsNull(node.NodeId)) { continue; } // add reverse references. foreach (IReference reference in node.ReferenceTable) { Node targetNode = Find(reference.TargetId) as Node; if (targetNode == null) { if (reference.TargetId.ServerIndex != 0) { continue; } // return the reverse reference to a node outside the table. if (externalReferences != null) { NodeId targetId = ExpandedNodeId.ToNodeId(reference.TargetId, m_namespaceUris); if (targetId == null) { continue; } IList <IReference> referenceList = null; if (!externalReferences.TryGetValue(targetId, out referenceList)) { externalReferences[targetId] = referenceList = new List <IReference>(); } ReferenceNode reverseReference = new ReferenceNode(); reverseReference.ReferenceTypeId = reference.ReferenceTypeId; reverseReference.IsInverse = !reference.IsInverse; reverseReference.TargetId = node.NodeId; referenceList.Add(reverseReference); } continue; } // type definition and modelling rule references are one way. if (reference.ReferenceTypeId != ReferenceTypeIds.HasTypeDefinition && reference.ReferenceTypeId != ReferenceTypeIds.HasModellingRule) { targetNode.ReferenceTable.Add(reference.ReferenceTypeId, !reference.IsInverse, node.NodeId); } } // see if it is a type. if (m_typeTree != null) { m_typeTree.Add(node); } } return(importedNodes); }
/// <summary> /// Initializes the instance from an event notification. /// </summary> /// <param name="context">The context.</param> /// <param name="fields">The fields selected for the event notification.</param> /// <param name="e">The event notification.</param> /// <remarks> /// This method creates components based on the browse paths in the event field and sets /// the NodeId or Value based on values in the event notification. /// </remarks> public void Update( ISystemContext context, SimpleAttributeOperandCollection fields, EventFieldList e) { for (int ii = 0; ii < fields.Count; ii++) { SimpleAttributeOperand field = fields[ii]; object value = e.EventFields[ii].Value; // check if value provided. if (value == null) { continue; } // extract the NodeId for the event. if (field.BrowsePath.Count == 0) { if (field.AttributeId == Attributes.NodeId) { this.NodeId = value as NodeId; continue; } } // extract the type definition for the event. if (field.BrowsePath.Count == 1) { if (field.AttributeId == Attributes.Value) { if (field.BrowsePath[0] == BrowseNames.EventType) { m_typeDefinitionId = value as NodeId; continue; } } } // save value for child node. NodeState parent = this; for (int jj = 0; jj < field.BrowsePath.Count; jj++) { // find a predefined child identified by the browse name. BaseInstanceState child = parent.CreateChild(context, field.BrowsePath[jj]); // create a placeholder for unknown children. if (child == null) { if (field.AttributeId == Attributes.Value) { child = new BaseDataVariableState(parent); } else { child = new BaseObjectState(parent); } parent.AddChild(child); } // ensure the browse name is set. if (QualifiedName.IsNull(child.BrowseName)) { child.BrowseName = field.BrowsePath[jj]; } // ensure the display name is set. if (LocalizedText.IsNullOrEmpty(child.DisplayName)) { child.DisplayName = child.BrowseName.Name; } // process next element in path. if (jj < field.BrowsePath.Count - 1) { parent = child; continue; } // save the variable value. if (field.AttributeId == Attributes.Value) { BaseVariableState variable = child as BaseVariableState; if (variable != null && field.AttributeId == Attributes.Value) { try { variable.WrappedValue = e.EventFields[ii]; } catch (Exception) { variable.Value = null; } } break; } // save the node id. child.NodeId = value as NodeId; } } }