Files
DP44/DataPRO/Modules/Hardware/AddEditHardware/ViewModel/AddEditHardwareViewModel.cs
2026-04-17 14:55:32 -04:00

353 lines
12 KiB
C#

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
{
/// <summary>
/// this class handles AddEditHardware functionality
/// </summary>
[PartCreationPolicy(CreationPolicy.Shared)]
public class AddEditHardwareViewModel : IAddEditHardwareViewModel
{
/// <summary>
/// Whether StandIn hardware is allowed or not
/// datarecorders tile does not allow standin hardware, TestSetup and Group do
/// </summary>
public bool AllowStandin { get; set; } = true;
/// <summary>
/// The AddEditHardware View
/// </summary>
public IAddEditHardwareView View { get; set; }
private IEventAggregator _eventAggregator { get; }
private IRegionManager _regionManager;
private IUnityContainer UnityContainer { get; }
public InteractionRequest<Notification> NotificationRequest { get; }
public InteractionRequest<Confirmation> ConfirmationRequest { get; }
/// <inheritdoc />
/// <summary>
/// Occurs when a property value changes.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#region constructors and initializers
/// <summary>
/// Creates a new instance of the HardwareListViewModel
/// </summary>
/// <param name="view"></param>
/// <param name="regionManager">The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed.</param>
/// <param name="eventAggregator">The EventAggregator which allows different components to publish/subscribe to events without being coupled to each other.</param>
/// <param name="unityContainer">The unityContainer.</param>
public AddEditHardwareViewModel(IAddEditHardwareView view, IRegionManager regionManager,
IEventAggregator eventAggregator, IUnityContainer unityContainer)
{
View = view;
View.DataContext = this;
NotificationRequest = new InteractionRequest<Notification>();
ConfirmationRequest = new InteractionRequest<Confirmation>();
_eventAggregator = eventAggregator;
UnityContainer = unityContainer;
_regionManager = regionManager;
_eventAggregator.GetEvent<RaiseNotification>().Subscribe(OnRaiseNotification);
_eventAggregator.GetEvent<BusyIndicatorChangeNotification>()
.Subscribe(OnBusyIndicatorNotification, ThreadOption.PublisherThread, true);
}
//required if you want to use the vm in the view xaml
public AddEditHardwareViewModel()
{
}
#endregion
#region Methods
/// <summary>
/// used to pass in access to SLICE6TreeView module
/// </summary>
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;
}
/// <summary>
/// SLICE6TreeView interface
/// can be null [not shown in test setups/groups currently]
/// </summary>
public ISLICE6TreeView SLICE6TreeView { get; private set; }
/// <summary>
/// SLICE6TreeViewModule, can be null
/// </summary>
public IHardwareListViewModel SLICE6TreeViewModel { get; private set; }
public void Activated()
{
View?.Activated();
if (null != _hardware && !AllowStandin)
{
_hardware.StandIn = false;
}
}
/// <summary>
/// Private Event handler for RaiseNotification event.
/// </summary>
private void OnBusyIndicatorNotification(bool eventArg)
{
IsBusy = eventArg;
}
/// <summary>
/// Private Event handler for RaiseNotification event.
/// </summary>
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<PageModifiedEvent>()
.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<string> errors, ref List<string> 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<string> errors, List<string> warnings)
{
if (displayWindow && (errors.Any() || warnings.Any()))
{
var newList = errors.ToList();
newList.AddRange(warnings.ToArray());
_eventAggregator.GetEvent<PageErrorEvent>().Publish(new PageErrorArg(newList.ToArray(), null));
}
}
/// <summary>
/// Updates all SLICE6 associations related to a SLICE6DB
/// </summary>
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<HardwareSavedEvent>()
.Publish(new Tuple<int, string>(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();
/// <summary>
/// hardware being operated on in viewmodel
/// </summary>
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
}
}