using System.ComponentModel; using System.ComponentModel.Composition; using System.Threading.Tasks; using DTS.Common.Events; using DTS.Common.Interface.Hardware.AddEditHardware; using DTS.Common.Interface.DataRecorders; using DTS.Common.Interface.DASFactory.Diagnostics; using System.Collections.Generic; using DTS.Common.Events.Hardware.HardwareList; using System.Linq; using System; using DTS.Common.Utilities.Logging; using DTS.Common.Interface.DASFactory.Diagnostics.HardwareList; using Prism.Events; using Prism.Regions; using Unity; using DTS.Common.Interactivity; // ReSharper disable CheckNamespace // ReSharper disable MemberCanBePrivate.Global // ReSharper disable InconsistentNaming namespace AddEditHardware { /// /// this class handles AddEditHardware functionality /// [PartCreationPolicy(CreationPolicy.Shared)] public class AddEditHardwareViewModel : IAddEditHardwareViewModel { /// /// Whether StandIn hardware is allowed or not /// datarecorders tile does not allow standin hardware, TestSetup and Group do /// public bool AllowStandin { get; set; } = true; /// /// The AddEditHardware View /// public IAddEditHardwareView View { get; set; } private IEventAggregator _eventAggregator { get; } private IRegionManager _regionManager; private IUnityContainer UnityContainer { get; } public InteractionRequest NotificationRequest { get; } public InteractionRequest ConfirmationRequest { get; } /// /// /// Occurs when a property value changes. /// public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } #region constructors and initializers /// /// Creates a new instance of the HardwareListViewModel /// /// /// The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed. /// The EventAggregator which allows different components to publish/subscribe to events without being coupled to each other. /// The unityContainer. public AddEditHardwareViewModel(IAddEditHardwareView view, IRegionManager regionManager, IEventAggregator eventAggregator, IUnityContainer unityContainer) { View = view; View.DataContext = this; NotificationRequest = new InteractionRequest(); ConfirmationRequest = new InteractionRequest(); _eventAggregator = eventAggregator; UnityContainer = unityContainer; _regionManager = regionManager; _eventAggregator.GetEvent().Subscribe(OnRaiseNotification); _eventAggregator.GetEvent() .Subscribe(OnBusyIndicatorNotification, ThreadOption.PublisherThread, true); } //required if you want to use the vm in the view xaml public AddEditHardwareViewModel() { } #endregion #region Methods /// /// used to pass in access to SLICE6TreeView module /// public void SetSLICE6TreeView(ISLICE6TreeView treeView, IHardwareListViewModel treeViewModel) { SLICE6TreeView = treeView; SLICE6TreeViewModel = treeViewModel; } public void Unset() { } public void Cleanup() { } public Task CleanupAsync() { return Task.CompletedTask; } public void Initialize() { } public void Initialize(object parameter) { } public void Initialize(object parameter, object model) { } public Task InitializeAsync() { return Task.CompletedTask; } public Task InitializeAsync(object parameter) { return Task.CompletedTask; } /// /// SLICE6TreeView interface /// can be null [not shown in test setups/groups currently] /// public ISLICE6TreeView SLICE6TreeView { get; private set; } /// /// SLICE6TreeViewModule, can be null /// public IHardwareListViewModel SLICE6TreeViewModel { get; private set; } public void Activated() { View?.Activated(); if (null != _hardware && !AllowStandin) { _hardware.StandIn = false; } } /// /// Private Event handler for RaiseNotification event. /// private void OnBusyIndicatorNotification(bool eventArg) { IsBusy = eventArg; } /// /// Private Event handler for RaiseNotification event. /// private void OnRaiseNotification(NotificationContentEventArgs eventArgsWithTitle) { // The NotificationRequest.Raise triggers the Invoke() method of the PopupWindowAction object to show the NotificationWindow window // Notification object expects a NotificationContentEventArgsWithoutTitle object and a Title string. var eventArgsWithoutTitle = new NotificationContentEventArgs(eventArgsWithTitle.Message, "", eventArgsWithTitle.Image, string.Empty); NotificationRequest.Raise(new Notification { Content = eventArgsWithoutTitle, Title = eventArgsWithTitle.Title }); } public void SetHardware(IDASHardware hw, IISOHardware isoHW) { NotificationsOn = false; if (null == hw) { Hardware = new Model.Hardware(); } else { Hardware = new Model.Hardware(hw, isoHW); } } public void NotifyModified() { if (!NotificationsOn) { return; } _eventAggregator.GetEvent() .Publish(new PageModifiedArg(PageModifiedArg.Status.Modified, null)); } public IISOHardware GetISOHardware() { var isoHW = Hardware.ToISOHardware(); if ((isoHW != null) && Hardware.StandIn && !Guid.TryParse(Hardware.SerialNumber, out var guid)) { guid = Guid.NewGuid(); Hardware.SerialNumber = guid.ToString(); isoHW.SerialNumber = guid.ToString(); isoHW.FirstUseDate = null; isoHW.IsFirstUseValid = true; isoHW.CalDate = DateTime.Today; } return isoHW; } public bool Validate(IISOHardware isoHW, ref List errors, ref List warnings, bool displayWindow, bool IsAdd) { var bValid = true; if (!isoHW.ValidateSerialNumber(ref errors)) { bValid = false; } //17639 Adding non-Stand-in DAS from Test Setups tab doesn't warn if DAS already exists if (IsAdd) { try { //17639 Adding non-Stand-in DAS from Test Setups tab doesn't warn if DAS already exists if (!Model.Hardware.CheckUniqueSN(isoHW.SerialNumber)) { errors.Add(Resources.StringResources.UniqueSNCheckFail); bValid = false; } } catch (Exception ex) { APILogger.Log(ex); bValid = true; errors.Add($"{Resources.StringResources.CheckUniqueSNError}{ex.Message}"); } } if (!isoHW.ValidateIPAddress(ref errors)) { bValid = false; } PublishPageError(displayWindow, errors, warnings); return bValid; } public void PublishPageError(bool displayWindow, List errors, List warnings) { if (displayWindow && (errors.Any() || warnings.Any())) { var newList = errors.ToList(); newList.AddRange(warnings.ToArray()); _eventAggregator.GetEvent().Publish(new PageErrorArg(newList.ToArray(), null)); } } /// /// Updates all SLICE6 associations related to a SLICE6DB /// public void SaveSLICE6Associations() { switch (Hardware.HardwareType) { case DTS.Common.Enums.Hardware.HardwareTypes.SLICE6DB: case DTS.Common.Enums.Hardware.HardwareTypes.SLICE6DB3: case DTS.Common.Enums.Hardware.HardwareTypes.SLICE6DB_InDummy: SLICE6TreeViewModel?.SaveSLICE6Associations(Hardware.SerialNumber); break; } } public void Save() { if (!(Hardware is Model.Hardware model)) { return; } Model.Hardware.Save(model, TestId, model.IsAdd); SaveSLICE6Associations(); _eventAggregator.GetEvent() .Publish(new Tuple(model.ISOHardware.DASId, model.SerialNumber)); SLICE6TreeViewModel?.LoadTreeView(model.SerialNumber); } #endregion #region Properties public int? TestId { get; set; } public bool NotificationsOn { get; set; } = false; private IAddEditHardwareHardware _hardware = new Model.Hardware(); /// /// hardware being operated on in viewmodel /// public IAddEditHardwareHardware Hardware { get => _hardware; set { _hardware = value; OnPropertyChanged("Hardware"); OnPropertyChanged("SerialNumber"); //In case modules were added OnPropertyChanged("FirmwareVersion"); OnPropertyChanged("IPAddress"); SLICE6TreeViewModel?.LoadTreeView(_hardware.SerialNumber); } } public bool IsDirty { get; private set; } private bool _isBusy; public bool IsBusy { get => _isBusy; set { _isBusy = value; OnPropertyChanged("IsBusy"); } } private bool _isMenuIncluded; public bool IsMenuIncluded { get => _isMenuIncluded; set { _isMenuIncluded = value; OnPropertyChanged("IsMenuIncluded"); } } private bool _isNavigationIncluded; public bool IsNavigationIncluded { get => _isNavigationIncluded; set { _isNavigationIncluded = value; OnPropertyChanged("IsNavigationIncluded"); } } //public string ListViewId => "HardwareListView"; #endregion Properties #region Commands #endregion } }