public string FormatProperty(ReferenceDescriptionViewModel vm) { var fcm = this.ide?.ActiveDocument?.ProjectItem?.FileCodeModel; var language = fcm?.Language ?? vsCMLanguageCSharp; switch (language) { case vsCMLanguageCSharp: if (vm.IsVariable) { if (vm.AccessLevel.HasFlag(AccessLevelFlags.CurrentWrite)) { return(FormatSnippet(this.ValueFormatCSharp, vm.DisplayName, vm.FullName, FormatTypeName(vm.DataType, language), FormatNodeId(vm.NodeId, language), FormatNodeId(vm.Parent.NodeId, language), vm.BrowseName.ToString())); } return(FormatSnippet(this.ReadOnlyValueFormatCSharp, vm.DisplayName, vm.FullName, FormatTypeName(vm.DataType, language), FormatNodeId(vm.NodeId, language), FormatNodeId(vm.Parent.NodeId, language), vm.BrowseName.ToString())); } if (vm.IsMethod) { return(FormatSnippet(this.MethodFormatCSharp, vm.DisplayName, vm.FullName, MethodDataTypeCS, FormatNodeId(vm.NodeId, language), FormatNodeId(vm.Parent.NodeId, language), vm.BrowseName.ToString())); } if (vm.IsEventNotifier) { return(FormatSnippet(this.EventFormatCSharp, vm.DisplayName, vm.FullName, EventDataTypeCS, FormatNodeId(vm.NodeId, language), FormatNodeId(vm.Parent.NodeId, language), vm.BrowseName.ToString())); } break; case vsCMLanguageVB: if (vm.IsVariable) { if (vm.AccessLevel.HasFlag(AccessLevelFlags.CurrentWrite)) { return(FormatSnippet(this.ValueFormatBasic, vm.DisplayName, vm.FullName, FormatTypeName(vm.DataType, language), FormatNodeId(vm.NodeId, language), FormatNodeId(vm.Parent.NodeId, language), vm.BrowseName.ToString())); } return(FormatSnippet(this.ReadOnlyValueFormatBasic, vm.DisplayName, vm.FullName, FormatTypeName(vm.DataType, language), FormatNodeId(vm.NodeId, language), FormatNodeId(vm.Parent.NodeId, language), vm.BrowseName.ToString())); } if (vm.IsMethod) { return(FormatSnippet(this.MethodFormatBasic, vm.DisplayName, vm.FullName, MethodDataTypeVB, FormatNodeId(vm.NodeId, language), FormatNodeId(vm.Parent.NodeId, language), vm.BrowseName.ToString())); } if (vm.IsEventNotifier) { return(FormatSnippet(this.EventFormatBasic, vm.DisplayName, vm.FullName, EventDataTypeVB, FormatNodeId(vm.NodeId, language), FormatNodeId(vm.Parent.NodeId, language), vm.BrowseName.ToString())); } break; } return(string.Empty); }
private string GetFullName() { var name = new StringBuilder(); for (ReferenceDescriptionViewModel current = this; current != null; current = current.Parent) { if (current.NodeId == ObjectsFolder || current.NodeId == TypesFolder || current.NodeId == ViewsFolder) { break; } name.Insert(0, current.BrowseName.Name); } return(SafeCharsRegex.Replace(name.ToString(), "_")); }
public MethodDescriptionViewModel(ReferenceDescription description, Parameter[] inArgs, Parameter[] outArgs, ReferenceDescriptionViewModel parent, Func <ReferenceDescriptionViewModel, Task> loadChildren) : base(description, parent, loadChildren) { this.inArgs = inArgs; this.outArgs = outArgs; }
public ReadonlyPropertyDescriptionViewModel(ReferenceDescription description, string propertyType, ReferenceDescriptionViewModel parent, Func <ReferenceDescriptionViewModel, Task> loadChildren) : base(description, parent, loadChildren) { this.propertyType = propertyType; }
public async Task RefreshAsync(string url, string userName, string password) { var endpointUrl = url; Trace.TraceInformation("BrowseAsync {0}", endpointUrl); try { this.BrowseStop(null); await [email protected](this.cts.Token); this.NotifyPropertyChanged("IsLoading"); try { this.NamespaceItems.Clear(); if (this.channel != null) { try { await this.channel.CloseAsync(); } catch (Exception) { } this.channel = null; } this.EndpointUrl = endpointUrl; this.userName = userName; this.password = password; if (string.IsNullOrEmpty(endpointUrl)) { return; } if (this.History.Count == 0 || this.History[0] != endpointUrl) { this.History.Insert(0, endpointUrl); } while (this.History.Count > 5) { this.History.RemoveAt(5); } var root = new ReferenceDescriptionViewModel(new ReferenceDescription { DisplayName = "Objects", NodeId = ExpandedNodeId.Parse(ObjectIds.ObjectsFolder), NodeClass = NodeClass.Object, TypeDefinition = ExpandedNodeId.Parse(ObjectTypeIds.FolderType) }, null, AccessLevelFlags.None, EventNotifierFlags.None, null, this.LoadChildrenAsync); this.NamespaceItems.Add(root); root.IsExpanded = true; } finally { [email protected](); this.NotifyPropertyChanged("IsLoading"); } } catch (OperationCanceledException) { } }
private async Task LoadChildrenAsync(ReferenceDescriptionViewModel parent) { try { var token = this.cts.Token; await [email protected](token); this.NotifyPropertyChanged("IsLoading"); try { parent.Children.Clear(); do { try { if (this.channel == null || this.channel.State != CommunicationState.Opened) { var getEndpointsRequest = new GetEndpointsRequest { EndpointUrl = this.endpointUrl, ProfileUris = new[] { TransportProfileUris.UaTcpTransport } }; var getEndpointsResponse = await UaTcpDiscoveryClient.GetEndpointsAsync(getEndpointsRequest); token.ThrowIfCancellationRequested(); var selectedEndpoint = getEndpointsResponse.Endpoints.OrderBy(e => e.SecurityLevel).First(); if (selectedEndpoint.UserIdentityTokens.Any(p => p.TokenType == UserTokenType.Anonymous)) { this.HideLoginPanel(); this.userIdentity = new AnonymousIdentity(); } else if (selectedEndpoint.UserIdentityTokens.Any(p => p.TokenType == UserTokenType.UserName)) { if (!this.showingLoginPanel) { this.ShowLoginPanel(); return; } else if (!this.ValidateLoginCredentials()) { return; } else { this.userIdentity = new UserNameIdentity(this.userName, this.password); } } else { throw new NotImplementedException("Browser supports only UserName and Anonymous identity, for now."); } dataTypeCache = new Dictionary <ExpandedNodeId, Type>(); this.channel = new UaTcpSessionChannel( this.localDescription, this.CertificateStore, this.userIdentity, selectedEndpoint); await this.channel.OpenAsync(); } token.ThrowIfCancellationRequested(); var rds = new List <ReferenceDescription>(); var browseRequest = new BrowseRequest { NodesToBrowse = new[] { new BrowseDescription { NodeId = ExpandedNodeId.ToNodeId(parent.NodeId, this.channel.NamespaceUris), ReferenceTypeId = NodeId.Parse(ReferenceTypeIds.HierarchicalReferences), ResultMask = (uint)BrowseResultMask.TargetInfo, NodeClassMask = (uint)NodeClass.Variable | (uint)NodeClass.Object | (uint)NodeClass.Method, BrowseDirection = BrowseDirection.Forward, IncludeSubtypes = true } } }; var browseResponse = await this.channel.BrowseAsync(browseRequest); rds.AddRange(browseResponse.Results.Where(result => result.References != null).SelectMany(result => result.References)); var continuationPoints = browseResponse.Results.Select(br => br.ContinuationPoint).Where(cp => cp != null).ToArray(); while (continuationPoints.Length > 0) { token.ThrowIfCancellationRequested(); var browseNextRequest = new BrowseNextRequest { ContinuationPoints = continuationPoints, ReleaseContinuationPoints = false }; var browseNextResponse = await this.channel.BrowseNextAsync(browseNextRequest); rds.AddRange(browseResponse.Results.Where(result => result.References != null).SelectMany(result => result.References)); } if (rds.Count == 0) { return; } foreach (var rd in rds) { token.ThrowIfCancellationRequested(); var n = ExpandedNodeId.ToNodeId(rd.NodeId, this.channel.NamespaceUris); Type dataType = null; EventNotifierFlags notifier = EventNotifierFlags.None; AccessLevelFlags accessLevel = AccessLevelFlags.None; if (rd.NodeClass == NodeClass.Variable) { var readRequest = new ReadRequest { NodesToRead = new ReadValueId[] { new ReadValueId { NodeId = n, AttributeId = AttributeIds.DataType }, new ReadValueId { NodeId = n, AttributeId = AttributeIds.ValueRank }, new ReadValueId { NodeId = n, AttributeId = AttributeIds.UserAccessLevel } } }; var readResponse = await this.channel.ReadAsync(readRequest); ExpandedNodeId dataTypeId, origDataTypeId; ReferenceDescription dataTypeRef; var dataTypeNode = readResponse.Results[0].GetValueOrDefault(NodeId.Null); if (dataTypeNode != NodeId.Null) { dataTypeId = origDataTypeId = NodeId.ToExpandedNodeId(dataTypeNode, this.channel.NamespaceUris); if (!this.dataTypeCache.TryGetValue(dataTypeId, out dataType)) { if (!UaTcpSecureChannel.DataTypeIdToTypeDictionary.TryGetValue(dataTypeId, out dataType)) { do { dataTypeNode = ExpandedNodeId.ToNodeId(dataTypeId, this.channel.NamespaceUris); var browseRequest2 = new BrowseRequest { NodesToBrowse = new[] { new BrowseDescription { NodeId = dataTypeNode, ReferenceTypeId = NodeId.Parse(ReferenceTypeIds.HasSubtype), ResultMask = (uint)BrowseResultMask.None, NodeClassMask = (uint)NodeClass.DataType, BrowseDirection = BrowseDirection.Inverse, IncludeSubtypes = false } } }; var browseResponse2 = await this.channel.BrowseAsync(browseRequest2); dataTypeRef = browseResponse2.Results[0].References?.FirstOrDefault(); dataTypeId = dataTypeRef?.NodeId; }while (dataTypeId != null && !UaTcpSecureChannel.DataTypeIdToTypeDictionary.TryGetValue(dataTypeId, out dataType)); if (dataTypeId == null) { dataType = typeof(object); } } this.dataTypeCache.Add(origDataTypeId, dataType); } var valueRank = readResponse.Results[1].GetValueOrDefault(-1); if (valueRank == 1) { dataType = dataType.MakeArrayType(); } if (valueRank > 1) { dataType = dataType.MakeArrayType(valueRank); } } else { dataType = typeof(object); } accessLevel = (AccessLevelFlags)Enum.ToObject(typeof(AccessLevelFlags), readResponse.Results[2].GetValueOrDefault <byte>()); } else if (rd.NodeClass == NodeClass.Object) { var readRequest = new ReadRequest { NodesToRead = new ReadValueId[] { new ReadValueId { NodeId = n, AttributeId = AttributeIds.EventNotifier }, } }; var readResponse = await this.channel.ReadAsync(readRequest); notifier = (EventNotifierFlags)Enum.ToObject(typeof(EventNotifierFlags), readResponse.Results[0].GetValueOrDefault <byte>()); } parent.Children.Add(new ReferenceDescriptionViewModel(rd, dataType, accessLevel, notifier, parent, this.LoadChildrenAsync)); await Task.Yield(); } break; // exit while; } catch (OperationCanceledException ex) { // exit while; } catch (ServiceResultException ex) { Console.WriteLine("ServiceResultException: {0}", ex); if (this.channel != null) { await this.channel.AbortAsync(token); this.channel = null; } if (ex.HResult == unchecked ((int)StatusCodes.BadSessionIdInvalid)) { continue; } } catch (Exception ex) { Console.WriteLine("Exception {0}", ex); if (this.channel != null) { await this.channel.AbortAsync(token); this.channel = null; } } try { await Task.Delay(5000, token); } catch (OperationCanceledException) { } }while (!token.IsCancellationRequested); } catch (Exception ex) { Console.WriteLine("Exception {0}", ex); } finally { [email protected](); this.NotifyPropertyChanged("IsLoading"); } } catch (OperationCanceledException) { // only get here if cancelled while waiting for lock } }
public ReferenceDescriptionViewModel(ReferenceDescription description, ReferenceDescriptionViewModel parent, Func <ReferenceDescriptionViewModel, Task> loadChildren) : base(parent, true) { this.description = description; this.loadChildren = loadChildren; }
public EventDescriptionViewModel(ReferenceDescription description, ReferenceDescriptionViewModel parent, Func <ReferenceDescriptionViewModel, Task> loadChildren) : base(description, parent, loadChildren) { }
public DataTypeDescriptionViewModel(ReferenceDescription description, ExpandedNodeId binaryEncodingId, XElement element, string targetNamespace, ReferenceDescriptionViewModel parent, Func <ReferenceDescriptionViewModel, Task> loadChildren) : base(description, parent, loadChildren) { this.binaryEncodingId = binaryEncodingId; this.element = element; this.targetNamespace = targetNamespace; this.baseType = parent.DisplayName; }