using Hi.Common; using Hi.Common.XmlUtils; using Hi.Licenses; using Hi.MachiningSteps; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; namespace Sample.Common; /// /// User Service. /// /// /// Sample-owned copy, kept so the step-present /// demo still compiles. Each GUI/APP keeps its own version; the core engine /// package (HiNc) no longer carries this GUI service type. /// public class UserService : IDisposable { /// /// Gets or sets the application configuration. /// public UserConfig UserConfig { get; set; } = new UserConfig(); /// /// Gets or sets the path to the application configuration file. /// public string UserConfigPath { get; set; } /// /// Gets whether physics features are enabled based on configuration and license. /// public bool EnablePhysics => UserConfig.ShowPhysicsOptions && IsPhysicsLicensed; /// /// Gets whether advanced physics features are licensed. /// public bool IsPhysicsLicensed => License.IsLoggedIn(AuthFeature.AdvancedPhysics); //runtime properties are not in the category of canonical IO. #region Runtime Property private readonly ILogger _logger; LooseRunner SaveConfigLooseRunner; /// /// Gets or sets the currently selected item in the application. /// public object SelectedItem { get; set; } private bool disposedValue; #endregion /// /// Initializes a new instance of the class. /// /// The logger instance. public UserService(ILogger logger) { _logger = logger; SaveConfigLooseRunner = new LooseRunner(logger); } /// /// Initializes a new instance of the class with the specified configuration. /// /// The application configuration. /// The logger instance. public UserService(UserConfig appConfig, ILogger logger) : this(logger) { UserConfig = appConfig; } /// /// Saves the user configuration to the file specified by . /// public void SaveUserConfig() { try { if (UserConfigPath != null) UserConfig.MakeXmlSourceToFile(UserConfigPath); } catch (Exception ex) { _logger?.LogError(ex, "{Message}", ex.Message); } } /// /// Schedules a loose save of the user configuration using a LooseRunner. /// public void LooseSaveUserConfig() { SaveConfigLooseRunner.TryRun(token => SaveUserConfig()); } #region step present /// /// Gets or sets additional step presentation access configurations. /// public Dictionary AdditionalStepPresentAccess { get; set; } = new Dictionary(); /// /// StepPresentAccessDictionary. /// Read only. /// public Dictionary StepPresentAccessDictionary { get { var originalPresentAccessDictionary = typeof(MachiningStep).GetProperties() .Where(p => p.GetCustomAttribute() != null) .ToDictionary(prop => prop.Name, prop => new PresentAccess(prop.GetCustomAttribute(), step => prop.GetValue(step))); var presentAccessDictionary = new Dictionary( AdditionalStepPresentAccess.Concat(originalPresentAccessDictionary)); return presentAccessDictionary; } } /// /// Candidate Step Present Key List for display. /// Read only. /// public List CandidateStepPresentKeyList => StepPresentAccessDictionary.Keys.ToList(); /// /// StepPresentAccessList for display. /// Read only. /// public List> DisplayedStepPresentAccessList { get { var displayedStepPresentKeyList = UserConfig.DisplayedStepPresentKeyList; List> dst = new List>( displayedStepPresentKeyList.Count); var stepPresentAccessDictionary = StepPresentAccessDictionary; foreach (var key in displayedStepPresentKeyList) { if (stepPresentAccessDictionary.TryGetValue( key, out var access)) { dst.Add(new KeyValuePair(key, access)); } } return dst; } } #endregion /// protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { SaveConfigLooseRunner.Dispose(); } disposedValue = true; } } /// public void Dispose() { // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method Dispose(disposing: true); GC.SuppressFinalize(this); } }