init
This commit is contained in:
@@ -0,0 +1,367 @@
|
||||
using System.ComponentModel;
|
||||
using System.ComponentModel.Composition;
|
||||
using System.Threading.Tasks;
|
||||
using DTS.Common.Events;
|
||||
using Prism.Events;
|
||||
using Prism.Regions;
|
||||
using Unity;
|
||||
using DTS.Common.Interactivity;
|
||||
using DTS.Common.Interface.Sensors.SensorSettingsModule;
|
||||
using DTS.Common.Interface.Sensors;
|
||||
using DTS.Common.Storage;
|
||||
using System.Linq;
|
||||
using DTS.Common.Classes.Groups.ChannelSettings;
|
||||
using Prism.Ioc;
|
||||
using DTS.SensorDB;
|
||||
//using SensorSettingsModule.Model;
|
||||
|
||||
// ReSharper disable CheckNamespace
|
||||
// ReSharper disable MemberCanBePrivate.Global
|
||||
// ReSharper disable InconsistentNaming
|
||||
|
||||
namespace SensorSettingsModule
|
||||
{
|
||||
/// <summary>
|
||||
/// this class handles SensorSettingsViewModel functionality
|
||||
/// </summary>
|
||||
[PartCreationPolicy(CreationPolicy.Shared)]
|
||||
public class SensorSettingsViewModel : ISensorSettingsViewModel
|
||||
{
|
||||
/// <summary>
|
||||
/// The SensorSettings view
|
||||
/// </summary>
|
||||
public ISensorSettingsView View { get; set; }
|
||||
|
||||
public IDigitalInputDefaults DigitalInputDefaults { get; set; }
|
||||
public ISquibSettingDefaults SquibSettings { get; set; }
|
||||
public IDigitalOutDefaults DigitalOutSettings { get; set; }
|
||||
public IIEPESensorDefaults IEPESensorDefaults { get; set; }
|
||||
public ICalibrationPolicy SensorCalibrationDefaults { get; set; }
|
||||
public IAnalogDefaults AnalogDefaults { get; set; }
|
||||
public IUartSettingDefaults UartIODefaults { get; set; }
|
||||
public ICanSettingDefaults CanIODefaults { get; set; }
|
||||
public IStreamOutputSettingDefaults StreamOutputSettings { 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 SensorSettingsViewModel
|
||||
/// </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 SensorSettingsViewModel(ISensorSettingsView 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);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
public void OnSetActive()
|
||||
{
|
||||
SquibSettings = SquibSettingDefaults.GetSquibSettingsDefault(User);
|
||||
OnPropertyChanged("SquibSettings");
|
||||
DigitalOutSettings = DigitalOutputDefaults.GetDigitalOutDefault(User);
|
||||
OnPropertyChanged("DigitalOutSettings");
|
||||
IEPESensorDefaults = IEPESensorDefault.GetIEPESensorDefaults(User);
|
||||
OnPropertyChanged("IEPESensorDefaults");
|
||||
SensorCalibrationDefaults = CalibrationPolicy.GetCalibrationPolicy();
|
||||
OnPropertyChanged("SensorCalibrationDefaults");
|
||||
DigitalInputDefaults = DigitalInputSensorDefault.GetDigitalInputDefaults(User);
|
||||
OnPropertyChanged("DigitalInputDefaults");
|
||||
AnalogDefaults = AnalogSettingDefaults.GetAnalogDefaults();
|
||||
OnPropertyChanged("AnalogDefaults");
|
||||
UartIODefaults = UartSettingDefaults.GetUartSettingsDefault(UserID);
|
||||
OnPropertyChanged("UartIODefaults");
|
||||
StreamOutputSettings = StreamOutputSettingDefaults.GetStreamOutputSettingsDefault(UserID);
|
||||
OnPropertyChanged("StreamOutputSettings");
|
||||
CanIODefaults = CanSettingDefaults.GetCanSettingsDefault(UserID);
|
||||
OnPropertyChanged("CanIODefaults");
|
||||
}
|
||||
|
||||
public void Unset()
|
||||
{
|
||||
//FB 13120 needs to validate and save settings when change the tab
|
||||
ValidateAndSave();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public void Activated()
|
||||
{
|
||||
}
|
||||
|
||||
/// <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 RestoreOriginalSettings()
|
||||
{
|
||||
AnalogSettingDefaults.RestoreDefaults(AnalogDefaults);
|
||||
|
||||
IEPESensorDefault.RestoreDefaults(IEPESensorDefaults);
|
||||
IEPESensorDefault.Save(IEPESensorDefaults);
|
||||
|
||||
DigitalInputSensorDefault.RestoreDefaults(DigitalInputDefaults);
|
||||
DigitalInputSensorDefault.Save(DigitalInputDefaults);
|
||||
UartSettingDefaults.RestoreDefaults(UartIODefaults);
|
||||
UartSettingDefaults.CommitChange((UartSettingDefaults)UartIODefaults, UserID);
|
||||
OnPropertyChanged("UartIODefaults");
|
||||
StreamOutputSettingDefaults.RestoreDefaults(StreamOutputSettings);
|
||||
StreamOutputSettingDefaults.CommitChange((StreamOutputSettingDefaults)StreamOutputSettings, UserID);
|
||||
OnPropertyChanged("StreamOutputSettings");
|
||||
CanSettingDefaults.RestoreDefaults(CanIODefaults);
|
||||
CanSettingDefaults.CommitChange((CanSettingDefaults)CanIODefaults, UserID);
|
||||
OnPropertyChanged("CanIODefaults");
|
||||
CalibrationPolicy.RestoreDefaults();
|
||||
SensorCalibrationDefaults = CalibrationPolicy.GetCalibrationPolicy();
|
||||
OnPropertyChanged("SensorCalibrationDefaults");
|
||||
}
|
||||
|
||||
private void InvalidateAllTestSetups()
|
||||
{
|
||||
using (var sql = DbOperations.GetSQLCommand())
|
||||
{
|
||||
sql.CommandType = System.Data.CommandType.Text;
|
||||
sql.CommandText = "UPDATE [TestSetups] SET [Dirty]=1, [Complete]=0";
|
||||
sql.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
public bool ValidateAndSave()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(SquibSettings is SquibSettingDefaults squib)) { return false; }
|
||||
if (!(DigitalOutSettings is DigitalOutputDefaults dout)) { return false; }
|
||||
if (!(AnalogDefaults is AnalogSettingDefaults ad)) { return false; }
|
||||
if (!(UartIODefaults is UartSettingDefaults ud)) { return false; }
|
||||
if (!(StreamOutputSettings is StreamOutputSettingDefaults sout)) { return false; }
|
||||
if (!(CanIODefaults is CanSettingDefaults cd)) { return false; }
|
||||
|
||||
bool bValid = true;
|
||||
if (UartIODefaults.Validate())
|
||||
{
|
||||
DTS.SensorDB.UartSettingDefaults.CommitChange(ud, UserID);
|
||||
}
|
||||
else
|
||||
{
|
||||
bValid = false;
|
||||
}
|
||||
if (StreamOutputSettings.Validate())
|
||||
{
|
||||
DTS.SensorDB.StreamOutputSettingDefaults.CommitChange(sout, UserID);
|
||||
}
|
||||
else
|
||||
{
|
||||
bValid = false;
|
||||
}
|
||||
if (CanIODefaults.Validate())
|
||||
{
|
||||
CanSettingDefaults.CommitChange(cd, UserID);
|
||||
}
|
||||
else
|
||||
{
|
||||
bValid = false;
|
||||
}
|
||||
if (IEPESensorDefaults.Validate())
|
||||
{
|
||||
IEPESensorDefault.Save(IEPESensorDefaults);
|
||||
}
|
||||
else
|
||||
{
|
||||
bValid = false;
|
||||
}
|
||||
|
||||
if (DigitalInputDefaults.Validate())
|
||||
{
|
||||
DigitalInputSensorDefault.Save(DigitalInputDefaults);
|
||||
}
|
||||
else
|
||||
{
|
||||
bValid = false;
|
||||
}
|
||||
// FB 13120 Validate and save the default filter class settings
|
||||
if (AnalogDefaults.Validate())
|
||||
{
|
||||
AnalogDefaults.Save();
|
||||
}
|
||||
else
|
||||
{
|
||||
bValid = false;
|
||||
}
|
||||
|
||||
var original = CalibrationPolicy.GetCalibrationPolicy();
|
||||
if (original.SelectedCalPolicy != SensorCalibrationDefaults.SelectedCalPolicy
|
||||
|| original.WarningPeriod != SensorCalibrationDefaults.WarningPeriod
|
||||
|| original.UseSensorFirstUseDate != SensorCalibrationDefaults.UseSensorFirstUseDate
|
||||
|| original.DontAllowDataCollectionIfOverused != SensorCalibrationDefaults.DontAllowDataCollectionIfOverused
|
||||
|| original.UsageRemainingForWarning != SensorCalibrationDefaults.UsageRemainingForWarning
|
||||
|| original.DefaultMaxUsageAllowed != SensorCalibrationDefaults.DefaultMaxUsageAllowed
|
||||
|| original.AllowInspectBeforeUse != SensorCalibrationDefaults.AllowInspectBeforeUse)
|
||||
{
|
||||
CalibrationPolicy.WriteCalibrationPolicyToDb(SensorCalibrationDefaults);
|
||||
InvalidateAllTestSetups();
|
||||
}
|
||||
|
||||
if (SquibSettings.Validate())
|
||||
{
|
||||
var defaults = DbOperations.GetChannelSettingDefaults();
|
||||
var lookup = defaults.ToDictionary(d => d.SettingName);
|
||||
|
||||
lookup[ChannelSettingBase.SQMODE].IntValue = (int)squib.FireModeDefault;
|
||||
lookup[ChannelSettingBase.SQUIB_DELAY].DoubleValue = squib.FireDelayMS;
|
||||
lookup[ChannelSettingBase.SQUIB_DURATION].DoubleValue = squib.FireDurationMS;
|
||||
lookup[ChannelSettingBase.SQUIB_LIMIT_DURATION].BoolValue = squib.LimitDurationDefault;
|
||||
lookup[ChannelSettingBase.SQUIB_CURRENT].DoubleValue = squib.OutputCurrentDefault;
|
||||
SquibSettingDefaults.CommitChange(squib, User);
|
||||
DbOperations.SaveChannelSettingDefaults(new[] {
|
||||
lookup[ChannelSettingBase.SQMODE], lookup[ChannelSettingBase.SQUIB_DELAY],
|
||||
lookup[ChannelSettingBase.SQUIB_DURATION], lookup[ChannelSettingBase.SQUIB_LIMIT_DURATION],
|
||||
lookup[ChannelSettingBase.SQUIB_CURRENT]});
|
||||
|
||||
lookup[ChannelSettingBase.OUTPUT_MODE].IntValue = (int)dout.OutputMode;
|
||||
lookup[ChannelSettingBase.DIGITALOUT_DELAY].DoubleValue = dout.DelayMS;
|
||||
lookup[ChannelSettingBase.DIGITALOUT_DURATION].DoubleValue = dout.DurationMS;
|
||||
lookup[ChannelSettingBase.DIGITALOUT_LIMIT_DURATION].BoolValue = dout.LimitDuration;
|
||||
DbOperations.SaveChannelSettingDefaults(new[] {
|
||||
lookup[ChannelSettingBase.OUTPUT_MODE], lookup[ChannelSettingBase.DIGITALOUT_LIMIT_DURATION],
|
||||
lookup[ChannelSettingBase.DIGITALOUT_DURATION], lookup[ChannelSettingBase.DIGITALOUT_DELAY] });
|
||||
|
||||
DigitalOutputDefaults.CommitChange(dout, User);
|
||||
return bValid;
|
||||
}
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
var eventAggregator = ContainerLocator.Container.Resolve<IEventAggregator>();
|
||||
eventAggregator.GetEvent<PageErrorEvent>().Publish(new PageErrorArg(new[] { ex.Message }, null));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
public string User { get; set; }
|
||||
public int UserID { get; set; }
|
||||
public bool IsDirty => false;
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Properties
|
||||
|
||||
#region Commands
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user