/// <summary> /// Creates a default display for a given Element Template and Element instance. /// </summary> /// <param name="elementTemplate">The element template on which the display will be based</param> /// <param name="initialContextElementPath">The element which will be the display's initial context</param> /// <param name="includedCategories">Optional list of categories to include. By default, all categories are included, as is the set of attributes not in a category. If specifying categories, the name @None will explicitly include uncategorized attributes.</param> /// <returns>A reference to the created display</returns> public Task <DisplayRevision> CreateDefaultDisplayAsync(AFElementTemplateSlim elementTemplate, string initialContextElementPath, ICollection <string> includedCategories = null) { //If the path is not escaped, do so here. TODO does this need to be URL encoded? if (!initialContextElementPath.Contains(@"\\\\")) { initialContextElementPath = initialContextElementPath.Replace(@"\", @"\\"); } string name = string.Format("{0} default display", elementTemplate.Name); //TODO check collisions CoresightDisplayBuilder displayBuilder = new CoresightDisplayBuilder(CoresightBaseUri, name); IEnumerable <AFAttributeTemplateSlim> includedAttributes = elementTemplate.AllAttributes.Where(a => !a.IsHidden && ValueTypeHelper.TypeIsSupported(a.TypeName)); Dictionary <string, IEnumerable <AFAttributeTemplateSlim> > attributesByCategory = AFAttributeTemplateSlim.GroupByCategory(includedAttributes, includedCategories); //If an explicit collection of included categories is specified, preserve its order bool explicitCategoriesSpecified = includedCategories != null && includedCategories.Count > 0; var attributesByCategoryOrdered = !explicitCategoriesSpecified? attributesByCategory.AsEnumerable() : includedCategories.Select(c => new KeyValuePair <string, IEnumerable <AFAttributeTemplateSlim> >(c, attributesByCategory[c])); foreach (var category in attributesByCategoryOrdered) { string titleText = AFAttributeTemplateSlim.NullCategory.Equals(category.Key, StringComparison.OrdinalIgnoreCase) ? "Uncategorized" : category.Key; titleText = attributesByCategory.Count < 2 ? null : titleText; AddAttributeGroup(displayBuilder, category.Value, initialContextElementPath, titleText); } return(displayBuilder.SaveAsync()); }
public static void Main(string[] args) { Console.WriteLine("Create an example Coresight display"); Console.WriteLine("Paste in your PI WebAPI URL:"); Uri webApiBaseUri = new Uri(Console.ReadLine().TrimEnd('/') + "/"); Console.WriteLine("Paste in your Coresight URL:"); string coresightUri = Console.ReadLine(); Console.WriteLine("Paste in the path to an AF Element whose template will form the default display"); string elementPath = Console.ReadLine(); Console.WriteLine("Optionally, provide a semicolon-delimited list of attribute categories to be included. By default, all are included."); string attributeCategoryList = Console.ReadLine(); List <string> includedCategories = attributeCategoryList.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList(); PIWebAPIClient piWebApiClient = new PIWebAPIClient(webApiBaseUri); AFElementDTO elementDTO = piWebApiClient.GetElementDTOByPathAsync(elementPath).GetAwaiter().GetResult(); AFElementTemplateSlim elementTemplate = piWebApiClient.GetElementTemplateSlimAsync(elementDTO).GetAwaiter().GetResult(); CoresightDefaultDisplayFactory displayFactory = new CoresightDefaultDisplayFactory(coresightUri); DisplayRevision defaultDisplay = displayFactory.CreateDefaultDisplayAsync(elementTemplate, elementPath, includedCategories).GetAwaiter().GetResult(); Uri displayUri = defaultDisplay.GetUri(displayFactory.CoresightBaseUri); Console.WriteLine("New display created at {0}", displayUri); Console.ReadKey(); Console.ReadKey(); }
public static void Main(string[] args) { Console.WriteLine("Create an example Coresight display"); Console.WriteLine("Paste in your Coresight URL:"); string coresightUri = Console.ReadLine(); Console.WriteLine("Paste in the path to an AF Element whose template will form the default display"); string elementPath = Console.ReadLine(); AFElement element = AFObjectHelper.Resolve <AFElement>(elementPath); if (element.Template == null) { throw new InvalidOperationException("Element must be based on a template"); } Console.WriteLine("Optionally, provide a semicolon-delimited list of attribute categories to be included. By default, all are included."); string attributeCategoryList = Console.ReadLine(); List <string> includedCategories = attributeCategoryList.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList(); AFElementTemplateSlim elementTemplateSlim = element.Template.ToSlim(element); CoresightDefaultDisplayFactory displayFactory = new CoresightDefaultDisplayFactory(coresightUri); DisplayRevision defaultDisplay = displayFactory.CreateDefaultDisplayAsync(elementTemplateSlim, elementPath, includedCategories).GetAwaiter().GetResult(); Uri displayUri = defaultDisplay.GetUri(displayFactory.CoresightBaseUri); Console.WriteLine("New display created at {0}", displayUri); SetClipboardText(displayUri.ToString()); Console.WriteLine("This URL has been copied to the clipboard"); Console.ReadKey(); }
/// <summary> /// Slims an AF SDK AFElementTemplate /// </summary> /// <param name="fat">The template to be slimmed</param> /// <param name="instance">Optional element instance to provide data reference information if not set on the template</param> /// <returns></returns> public static AFElementTemplateSlim ToSlim(this AFElementTemplate fat, AFElement instance = null) { //Populate element template AFElementTemplateSlim slim = new AFElementTemplateSlim() { Id = fat.ID, Name = fat.Name, Description = fat.Description, InstanceTypeName = fat.InstanceType.Name, Path = fat.GetPath().Replace(@"\", @"\\") //escape the backslashes }; //Populate attribute templates IEnumerable <AFAttributeTemplate> allAttributes = fat.GetAllAttributeTemplatesAndChildren(); slim.AllAttributes = allAttributes.Select(a => a.ToSlim(instance?.Attributes[a.GetPath(a.ElementTemplate)])).ToList(); return(slim); }
/// <summary> /// Slims an AF SDK AFElementTemplate /// </summary> /// <param name="elementTemplate">The template to be slimmed</param> /// <param name="attributeTemplates">The template's attributes</param> /// <param name="attributeInstances">Optional attribute instances to provide data reference information if not set on the template</param> /// <returns></returns> public static AFElementTemplateSlim ToSlim(this AFElementTemplateDTO elementTemplate, IEnumerable <AFAttributeTemplateDTO> attributeTemplates, IEnumerable <AFAttributeDTO> attributeInstances = null) { //Populate element template AFElementTemplateSlim slim = new AFElementTemplateSlim() { Id = elementTemplate.Id, Name = elementTemplate.Name, Description = elementTemplate.Description, InstanceTypeName = elementTemplate.InstanceType, Path = elementTemplate.Path }; //Populate attribute templates var attributeInstancesByFullName = attributeInstances.ToDictionary(a => a.GetPathRelativeToElement()); var attributeTemplatesByFullName = attributeTemplates.ToDictionary(a => a.GetPathRelativeToElement()); slim.AllAttributes = attributeTemplatesByFullName.Select(kvp => kvp.Value.ToSlim(attributeInstancesByFullName[kvp.Key])).ToList(); return(slim); }