diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-12-30 15:23:34 +0200 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-12-30 15:23:34 +0200 |
| commit | 721e03d7e6926055111d4edbbdf5f700a11ed58a (patch) | |
| tree | 8721b9b55923f3b9c23fbab5b22a770f5d169a01 /Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.MachineDesigner/ViewModels/MainViewVM.cs | |
| parent | cc3480d4aac4175b1072060dd2d6d398d3451ae8 (diff) | |
| download | Tango-721e03d7e6926055111d4edbbdf5f700a11ed58a.tar.gz Tango-721e03d7e6926055111d4edbbdf5f700a11ed58a.zip | |
Working on new Machine Designer.
Diffstat (limited to 'Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.MachineDesigner/ViewModels/MainViewVM.cs')
| -rw-r--r-- | Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.MachineDesigner/ViewModels/MainViewVM.cs | 546 |
1 files changed, 233 insertions, 313 deletions
diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.MachineDesigner/ViewModels/MainViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.MachineDesigner/ViewModels/MainViewVM.cs index e2924a185..b0b97d7d4 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.MachineDesigner/ViewModels/MainViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.MachineDesigner/ViewModels/MainViewVM.cs @@ -26,30 +26,21 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels public class MainViewVM : StudioViewModel<IMainView> { private INotificationProvider _notification; - private ObservablesContext _dbContext; - private ObservablesContext _machineContext; - private ObservablesContext _configContext; #region Properties - private ObservableCollection<Machine> _machines; - /// <summary> - /// Gets or sets the machines. - /// </summary> - public ObservableCollection<Machine> Machines + private ObservablesStaticCollections _machinesAdapter; + public ObservablesStaticCollections MachinesAdapter { - get { return _machines; } - set { _machines = value; RaisePropertyChangedAuto(); } + get { return _machinesAdapter; } + set { _machinesAdapter = value; RaisePropertyChangedAuto(); } } - private ObservableCollection<Dispenser> _dispensers; - /// <summary> - /// Gets or sets the dispensers. - /// </summary> - public ObservableCollection<Dispenser> Dispensers + private ObservablesStaticCollections _activeMachineAdapter; + public ObservablesStaticCollections ActiveMachineAdapter { - get { return _dispensers; } - set { _dispensers = value; RaisePropertyChangedAuto(); } + get { return _activeMachineAdapter; } + set { _activeMachineAdapter = value; RaisePropertyChangedAuto(); } } private Machine _selectedMachine; @@ -80,17 +71,6 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels set { _activeMachine = value; RaisePropertyChangedAuto(); } } - - private Configuration _configuration; - /// <summary> - /// Gets or sets the editable machine configuration. - /// </summary> - public Configuration Configuration - { - get { return _configuration; } - set { _configuration = value; RaisePropertyChangedAuto(); } - } - private IdsPack _selectedIds; /// <summary> /// Gets or sets the selected ids pack. @@ -155,6 +135,11 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels /// </summary> public RelayCommand EditMachineCommand { get; set; } + /// <summary> + /// Gets or sets the back to machines command. + /// </summary> + public RelayCommand BackToMachinesCommand { get; set; } + #endregion #region Constructors @@ -169,32 +154,17 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels /// </summary> public MainViewVM(INotificationProvider notification) { - _dbContext = ObservablesContext.CreateDefault(); + MachinesAdapter = new ObservablesStaticCollections(ObservablesContext.CreateDefault()); _notification = notification; //SaveCommand = new RelayCommand(Save); - AddIdsCommand = new RelayCommand(AddIds, (x) => Configuration != null && Configuration.IdsPacks.Count < 8); + AddIdsCommand = new RelayCommand(AddIds, (x) => ActiveMachine != null && ActiveMachine.Configuration != null && ActiveMachine.Configuration.IdsPacks.Count < 8); RemoveIdsCommand = new RelayCommand(RemoveIds, (x) => SelectedIds != null); - EditMachineCommand = new RelayCommand(EditSelectedMachine, () => SelectedMachine != null); - } - - private async void EditSelectedMachine() - { - using (_notification.PushTaskItem("Loading machine details...")) - { - IsFree = false; - - if (_machineContext != null) - { - _machineContext.Dispose(); - } - - _machineContext = ObservablesContext.CreateDefault(); - ActiveMachine = (await new MachineBuilder(_machineContext).Set(SelectedMachine).WithOrganization().WithConfiguration().BuildAsync()); - View.NavigateTo(MachineDesignerNavigationView.MachineDetailsView); - - IsFree = true; - } + EditMachineCommand = new RelayCommand(() => LoadSelectedMachine(), () => SelectedMachine != null); + BackToMachinesCommand = new RelayCommand(() => View.NavigateTo(MachineDesignerNavigationView.MachinesView)); + SaveCommand = new RelayCommand(SaveMachine); + AddMachineCommand = new RelayCommand(AddNewMachine); + RemoveMachineCommand = new RelayCommand(RemoveSelectedMachine,() => SelectedMachine != null); } #endregion @@ -208,59 +178,16 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels #endregion - //private Task InitCollections() - //{ - // return Task.Factory.StartNew(() => - // { - // CanWork = false; - - // _dbContext = ObservablesContext.CreateDefault(); - - // Adapter = new ObservablesStaticCollections(); - // Adapter.ApplicationDisplayPanelVersions = _dbContext.ApplicationDisplayPanelVersions.ToObservableCollection(); - // Adapter.ApplicationFirmwareVersions = _dbContext.ApplicationFirmwareVersions.ToObservableCollection(); - // Adapter.ApplicationOsVersions = _dbContext.ApplicationOsVersions.ToObservableCollection(); - // Adapter.EmbeddedFirmwareVersions = _dbContext.EmbeddedFirmwareVersions.ToObservableCollection(); - // Adapter.DispenserTypes = _dbContext.DispenserTypes.ToObservableCollection(); - // Adapter.LiquidTypes = _dbContext.LiquidTypes.ToObservableCollection(); - // Adapter.MidTankTypes = _dbContext.MidTankTypes.ToObservableCollection(); - // Adapter.CartridgeTypes = _dbContext.CartridgeTypes.ToObservableCollection(); - // Adapter.IdsPackFormulas = _dbContext.IdsPackFormulas.ToObservableCollection(); - // Adapter.HardwareVersions = _dbContext.HardwareVersions.ToObservableCollection(); - // Adapter.MachineVersions = _dbContext.MachineVersions.ToObservableCollection(); - // Adapter.Organizations = _dbContext.Organizations.ToObservableCollection(); - - // Adapter.InitCollectionSources(); - - // CanWork = true; - // }); - //} - - private void Reset() - { - //using (_notification.PushTaskItem("Resetting designer...")) - //{ - // SelectedMachine = null; - // Machine = new Machine(); - // Configuration = new Configuration(); - // InitCollections(); - //} - } - private async void OnDispensersFilterChanged() { if (!String.IsNullOrWhiteSpace(DispensersFilter)) { IsFree = false; - Dispensers = (await _configContext.Dispensers.Where(x => x.SerialNumber.ToLower().StartsWith(DispensersFilter.ToLower())).ToListAsync()).ToObservableCollection(); + ActiveMachineAdapter.Dispensers = (await ActiveMachineAdapter.Context.Dispensers.Where(x => x.SerialNumber.ToLower().StartsWith(DispensersFilter.ToLower())).ToListAsync()).ToObservableCollection(); IsFree = true; } } - #region Virtual Methods - - #endregion - #region Drag Drop Handlers /// <summary> @@ -270,7 +197,7 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels /// <param name="idsPack2">The ids pack2.</param> public void DropIdsPack(IdsPack idsPack1, IdsPack idsPack2) { - Configuration.IdsPacks.Swap(idsPack1, idsPack2); + ActiveMachine.Configuration.IdsPacks.Swap(idsPack1, idsPack2); } /// <summary> @@ -279,8 +206,8 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels /// <param name="applicationDisplayPanelVersion">The application display panel version.</param> public void DropTouchPanel(ApplicationDisplayPanelVersion applicationDisplayPanelVersion) { - Configuration.ApplicationDisplayPanelVersion = applicationDisplayPanelVersion; - Configuration.ApplicationDisplayPanelVersionGuid = applicationDisplayPanelVersion.Guid; + ActiveMachine.Configuration.ApplicationDisplayPanelVersion = applicationDisplayPanelVersion; + ActiveMachine.Configuration.ApplicationDisplayPanelVersionGuid = applicationDisplayPanelVersion.Guid; } /// <summary> @@ -289,8 +216,8 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels /// <param name="applicationFirmwareVersion">The application firmware version.</param> public void DropApplicationFirmwareVersion(ApplicationFirmwareVersion applicationFirmwareVersion) { - Configuration.ApplicationFirmwareVersion = applicationFirmwareVersion; - Configuration.ApplicationFirmwareVersionGuid = applicationFirmwareVersion.Guid; + ActiveMachine.Configuration.ApplicationFirmwareVersion = applicationFirmwareVersion; + ActiveMachine.Configuration.ApplicationFirmwareVersionGuid = applicationFirmwareVersion.Guid; } /// <summary> @@ -299,8 +226,8 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels /// <param name="hardwareVersion">The hardware version.</param> public void DropHardwareVersion(HardwareVersion hardwareVersion) { - Configuration.HardwareVersion = hardwareVersion; - Configuration.HardwareVersionGuid = hardwareVersion.Guid; + ActiveMachine.Configuration.HardwareVersion = hardwareVersion; + ActiveMachine.Configuration.HardwareVersionGuid = hardwareVersion.Guid; } /// <summary> @@ -309,8 +236,8 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels /// <param name="embeddedFirmwareVersion">The embedded firmware version.</param> public void DropEmbeddedFirmware(EmbeddedFirmwareVersion embeddedFirmwareVersion) { - Configuration.EmbeddedFirmwareVersion = embeddedFirmwareVersion; - Configuration.EmbeddedFirmwareVersionGuid = embeddedFirmwareVersion.Guid; + ActiveMachine.Configuration.EmbeddedFirmwareVersion = embeddedFirmwareVersion; + ActiveMachine.Configuration.EmbeddedFirmwareVersionGuid = embeddedFirmwareVersion.Guid; } /// <summary> @@ -319,8 +246,8 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels /// <param name="applicationOsVersion">The application os version.</param> public void DropApplicationOsVersion(ApplicationOsVersion applicationOsVersion) { - Configuration.ApplicationOsVersion = applicationOsVersion; - Configuration.ApplicationOsVersionGuid = applicationOsVersion.Guid; + ActiveMachine.Configuration.ApplicationOsVersion = applicationOsVersion; + ActiveMachine.Configuration.ApplicationOsVersionGuid = applicationOsVersion.Guid; } /// <summary> @@ -388,7 +315,7 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels /// </summary> private void RemoveIds() { - _dbContext.IdsPacks.Remove(SelectedIds); + ActiveMachineAdapter.Context.IdsPacks.Remove(SelectedIds); SelectedIds = null; } @@ -397,249 +324,242 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels /// </summary> private void AddIds() { - _dbContext.IdsPacks.Add(new IdsPack() { Configuration = Configuration }); + ActiveMachineAdapter.Context.IdsPacks.Add(new IdsPack() { Configuration = ActiveMachine.Configuration }); InvalidateRelayCommands(); } - ///// <summary> - ///// Saves the current machine configuration. - ///// </summary> - //private async void Save() - //{ - // foreach (var ids in Configuration.IdsPacks) - // { - // ids.PackIndex = Configuration.IdsPacks.IndexOf(ids); - // ids.Configuration = Configuration; - // ids.ConfigurationGuid = Configuration.Guid; - // } - - // //Validate - - // List<String> errors = new List<string>(); - - // if (Machine.MachineVersion == null) - // { - // errors.Add("Machine version is required."); - // } - - // if (Machine.Name.IsNullOrWhiteSpace()) - // { - // errors.Add("Machine name is required."); - // } - - // if (Machine.Organization == null) - // { - // errors.Add("Machine organization is required."); - // } - - // if (Machine.SerialNumber.IsNullOrWhiteSpace()) - // { - // errors.Add("Machine serial number is required."); - // } - - // if (Configuration.Name.IsNullOrWhiteSpace()) - // { - // errors.Add("Configuration name is required."); - // } + private async void LoadSelectedMachine(bool newMachine = false) + { + using (_notification.PushTaskItem("Loading machine details...")) + { + try + { + IsFree = false; - // if (Configuration.ApplicationDisplayPanelVersion == null) - // { - // errors.Add("Touch Panel is required."); - // } + if (ActiveMachineAdapter != null) + { + ActiveMachineAdapter.Context.Dispose(); + } - // if (Configuration.ApplicationFirmwareVersion == null) - // { - // errors.Add("Application firmware is required."); - // } + ActiveMachineAdapter = new ObservablesStaticCollections(ObservablesContext.CreateDefault()); + ActiveMachineAdapter.Organizations = (await ActiveMachineAdapter.Context.Organizations.ToListAsync()).ToObservableCollection(); + ActiveMachineAdapter.MachineVersions = (await ActiveMachineAdapter.Context.MachineVersions.ToListAsync()).ToObservableCollection(); + ActiveMachineAdapter.Rmls = (await ActiveMachineAdapter.Context.Rmls.ToListAsync()).ToObservableCollection(); + ActiveMachineAdapter.ColorSpaces = (await ActiveMachineAdapter.Context.ColorSpaces.ToListAsync()).ToObservableCollection(); + ActiveMachineAdapter.SpoolTypes = (await ActiveMachineAdapter.Context.SpoolTypes.ToListAsync()).ToObservableCollection(); - // if (Configuration.ApplicationOsVersion == null) - // { - // errors.Add("Application operation system is required."); - // } + ActiveMachineAdapter.ApplicationDisplayPanelVersions = (await ActiveMachineAdapter.Context.ApplicationDisplayPanelVersions.ToListAsync()).ToObservableCollection(); + ActiveMachineAdapter.ApplicationFirmwareVersions = (await ActiveMachineAdapter.Context.ApplicationFirmwareVersions.ToListAsync()).ToObservableCollection(); + ActiveMachineAdapter.ApplicationOsVersions = (await ActiveMachineAdapter.Context.ApplicationOsVersions.ToListAsync()).ToObservableCollection(); + ActiveMachineAdapter.EmbeddedFirmwareVersions = (await ActiveMachineAdapter.Context.EmbeddedFirmwareVersions.ToListAsync()).ToObservableCollection(); + ActiveMachineAdapter.DispenserTypes = (await ActiveMachineAdapter.Context.DispenserTypes.ToListAsync()).ToObservableCollection(); + ActiveMachineAdapter.LiquidTypes = (await ActiveMachineAdapter.Context.LiquidTypes.ToListAsync()).ToObservableCollection(); + ActiveMachineAdapter.MidTankTypes = (await ActiveMachineAdapter.Context.MidTankTypes.ToListAsync()).ToObservableCollection(); + ActiveMachineAdapter.CartridgeTypes = (await ActiveMachineAdapter.Context.CartridgeTypes.ToListAsync()).ToObservableCollection(); + ActiveMachineAdapter.IdsPackFormulas = (await ActiveMachineAdapter.Context.IdsPackFormulas.ToListAsync()).ToObservableCollection(); + ActiveMachineAdapter.HardwareVersions = (await ActiveMachineAdapter.Context.HardwareVersions.ToListAsync()).ToObservableCollection(); + ActiveMachineAdapter.MachineVersions = (await ActiveMachineAdapter.Context.MachineVersions.ToListAsync()).ToObservableCollection(); + ActiveMachineAdapter.Organizations = (await ActiveMachineAdapter.Context.Organizations.ToListAsync()).ToObservableCollection(); - // if (Configuration.EmbeddedFirmwareVersion == null) - // { - // errors.Add("Embedded firmware is required."); - // } + if (!newMachine) + { + ActiveMachine = (await new MachineBuilder(ActiveMachineAdapter.Context).Set(SelectedMachine.Guid).WithOrganization().WithConfiguration().WithSpools().BuildAsync()); + } + else + { + ActiveMachine = new Machine(); + ActiveMachine.Configuration = new Configuration(); + ActiveMachineAdapter.Context.Machines.Add(ActiveMachine); + } - // if (Configuration.HardwareVersion == null) - // { - // errors.Add("Hardware version is required."); - // } + View.NavigateTo(MachineDesignerNavigationView.MachineDetailsView); + } + catch (Exception ex) + { + LogManager.Log(ex, $"Error loading machine details for serial number {SelectedMachine.SerialNumber}"); + _notification.ShowError($"An error occurred while trying to load the selected machine details.\n{ex.Message}"); + } + finally + { + IsFree = true; + InvalidateRelayCommands(); + } + } + } - // foreach (var pack in Configuration.IdsPacks) - // { - // if (pack.LiquidType != null || pack.CartridgeType != null || pack.Dispenser != null || pack.IdsPackFormula != null || pack.MidTankType != null) - // { - // if (pack.CartridgeType == null) - // { - // errors.Add(String.Format("Cartridge type is required on IDS pack '{0}'.", pack.PackIndex)); - // } - // if (pack.Dispenser == null) - // { - // errors.Add(String.Format("Dispenser is required on IDS pack '{0}'.", pack.PackIndex)); - // } - // if (pack.LiquidType == null) - // { - // errors.Add(String.Format("Liquid type is required on IDS pack '{0}'.", pack.PackIndex)); - // } - // if (pack.MidTankType == null) - // { - // errors.Add(String.Format("Mid Tank type is required on IDS pack '{0}'.", pack.PackIndex)); - // } - // if (pack.IdsPackFormula == null) - // { - // errors.Add(String.Format("Formula type is required on IDS pack '{0}'.", pack.PackIndex)); - // } + private async void SaveMachine() + { + foreach (var ids in ActiveMachine.Configuration.IdsPacks) + { + ids.PackIndex = ActiveMachine.Configuration.IdsPacks.IndexOf(ids); + ids.Configuration = ActiveMachine.Configuration; + ids.ConfigurationGuid = ActiveMachine.Configuration.Guid; + } - // pack.IsEmpty = false; - // } - // else - // { - // pack.IsEmpty = true; - // } - // } + List<String> errors = new List<string>(); - // if (errors.Count > 0) - // { - // String errorsString = "Please fix the following validation errors before trying to save." + Environment.NewLine + Environment.NewLine; - // errorsString += String.Join(Environment.NewLine, errors); - // _notification.ShowError(errorsString); - // return; - // } + if (ActiveMachine.MachineVersion == null) + { + errors.Add("Machine version is required."); + } - // //Validate + if (ActiveMachine.Name.IsNullOrWhiteSpace()) + { + errors.Add("Machine name is required."); + } - // try - // { - // CanWork = false; + if (ActiveMachine.Organization == null) + { + errors.Add("Machine organization is required."); + } - // using (_notification.PushTaskItem("Saving Machine Configuration...")) - // { - // if (!_dbContext.Machines.Any(x => x.SerialNumber.ToLower() == Machine.SerialNumber.ToLower())) - // { - // if (!_notification.ShowQuestion("The specified machine serial number does not exist. Do you wish to create a new machine?")) - // { - // CanWork = true; - // return; - // } - // else - // { - // Machine.Configuration = Configuration; - // Configuration.CreationDate = DateTime.UtcNow; - // Machine.ProductionDate = DateTime.UtcNow; + if (ActiveMachine.SerialNumber.IsNullOrWhiteSpace()) + { + errors.Add("Machine serial number is required."); + } - // _dbContext.Machines.Add(Machine); - // } - // } + if (ActiveMachine.Configuration.ApplicationDisplayPanelVersion == null) + { + errors.Add("Touch Panel is required."); + } - // await _dbContext.SaveChangesAsync(); - // OnSelectedMachineChanged(); + if (ActiveMachine.Configuration.ApplicationFirmwareVersion == null) + { + errors.Add("Application firmware is required."); + } - // } - // } - // catch (Exception ex) - // { - // _notification.ShowError("An error occurred while trying to save the configuration" + Environment.NewLine + ex.Message); - // } - // finally - // { - // CanWork = true; - // InvalidateRelayCommands(); - // } - //} + if (ActiveMachine.Configuration.ApplicationOsVersion == null) + { + errors.Add("Application operation system is required."); + } - ///// <summary> - ///// Sets the current configuration to the selected machine version default configuration. - ///// </summary> - //private async void SetVersionConfiguration() - //{ - // if (Machine.MachineVersion != null) - // { - // using (_notification.PushTaskItem("Applying default configuration...")) - // { - // CanWork = false; + if (ActiveMachine.Configuration.EmbeddedFirmwareVersion == null) + { + errors.Add("Embedded firmware is required."); + } - // await Task.Factory.StartNew(() => - // { - // var version = _dbContext.MachineVersions.Where(x => x.Guid == Machine.MachineVersion.Guid).Include(x => x.DefaultConfiguration).FirstOrDefault(); - // var version_config = _dbContext.Adapter.GetConfiguration(x => x.Guid == version.DefaultConfiguration.Guid); - // Configuration = version_config.Clone(); - // Machine.Configuration = Configuration; - // }); + if (ActiveMachine.Configuration.HardwareVersion == null) + { + errors.Add("Hardware version is required."); + } - // CanWork = true; - // } - // } - // else - // { - // _notification.ShowError("No machine version selected."); - // } - //} + foreach (var pack in ActiveMachine.Configuration.IdsPacks) + { + if (pack.LiquidType != null || pack.CartridgeType != null || pack.Dispenser != null || pack.IdsPackFormula != null || pack.MidTankType != null) + { + if (pack.CartridgeType == null) + { + errors.Add(String.Format("Cartridge type is required on IDS pack '{0}'.", pack.PackIndex)); + } + if (pack.Dispenser == null) + { + errors.Add(String.Format("Dispenser is required on IDS pack '{0}'.", pack.PackIndex)); + } + if (pack.LiquidType == null) + { + errors.Add(String.Format("Liquid type is required on IDS pack '{0}'.", pack.PackIndex)); + } + if (pack.MidTankType == null) + { + errors.Add(String.Format("Mid Tank type is required on IDS pack '{0}'.", pack.PackIndex)); + } + if (pack.IdsPackFormula == null) + { + errors.Add(String.Format("Formula type is required on IDS pack '{0}'.", pack.PackIndex)); + } - ///// <summary> - ///// Sets the current configuration as a default machine version configuration. - ///// </summary> - //private void SetAsDefaultConfiguration() - //{ - // _notification.ShowModalDialog<MachineVersionDialogVM>(async (vm) => - // { - // CanWork = false; + pack.IsEmpty = false; + } + else + { + pack.IsEmpty = true; + } + } - // try - // { - // using (_notification.PushTaskItem("Saving Default Configuration...")) - // { - // if (vm.SelectedVersion != null) - // { - // var version = _dbContext.MachineVersions.Where(x => x.Guid == vm.SelectedVersion.Guid).Include(x => x.DefaultConfiguration).SingleOrDefault(x => x.Guid == vm.SelectedVersion.Guid); + foreach (var pack in ActiveMachine.Configuration.IdsPacks.Where(x => x.Dispenser != null)) + { + if (ActiveMachine.Configuration.IdsPacks.Where(x => x.Dispenser == pack.Dispenser).Count() > 1) + { + errors.Add($"Dispenser '{pack.Dispenser.SerialNumber}' is installed on multiple IDS packs."); + } - // _dbContext.Configurations.Remove(version.DefaultConfiguration); + if (ActiveMachineAdapter.Context.IdsPacks.Count(x => x.ConfigurationGuid != pack.ConfigurationGuid && x.DispenserGuid == pack.DispenserGuid) > 0) + { + errors.Add($"Dispenser '{pack.Dispenser.SerialNumber}' is already installed on a different machine."); + } + } - // var cloned = Configuration.Clone(); - // _dbContext.Configurations.Add(cloned); - // version.DefaultConfiguration = cloned; + if (errors.Count > 0) + { + String errorsString = "Please fix the following validation errors before trying to save." + Environment.NewLine + Environment.NewLine; + errorsString += String.Join(Environment.NewLine, errors.Distinct()); + _notification.ShowError(errorsString); + return; + } - // await _dbContext.SaveChangesAsync(); - // } - // else - // { - // MachineVersion newVersion = new MachineVersion(); - // newVersion.Version = vm.Version; - // newVersion.Name = vm.VersionName; + try + { + IsFree = false; - // var cloned = Configuration.Clone(); + using (_notification.PushTaskItem("Saving Machine Details...")) + { - // _dbContext.Configurations.Add(cloned); - // newVersion.DefaultConfiguration = cloned; - // _dbContext.MachineVersions.Add(newVersion); - // await _dbContext.SaveChangesAsync(); - // } - // } - // } - // catch (Exception ex) - // { - // _notification.ShowError(ex.Message); - // } - // finally - // { - // CanWork = true; - // } + ActiveMachine.ConfigurationGuid = ActiveMachine.Configuration.Guid; + ActiveMachine.LastUpdated = DateTime.UtcNow; + ActiveMachine.ProductionDate = DateTime.UtcNow; + await ActiveMachineAdapter.Context.SaveChangesAsync(); + } + } + catch (Exception ex) + { + LogManager.Log(ex, "Error saving machine details."); + _notification.ShowError("An error occurred while trying to save the machine details" + Environment.NewLine + ex.Message); + } + finally + { + IsFree = true; + InvalidateRelayCommands(); + } + } - // }, () => - // { + private void AddNewMachine() + { + LoadSelectedMachine(true); + } - // }); - //} + private async void RemoveSelectedMachine() + { + if (_notification.ShowQuestion("Are you sure you want to delete the selected machine?")) + { + using (_notification.PushTaskItem("Removing machine...")) + { + try + { + IsFree = false; + await SelectedMachine.DeleteCascadeAsync(MachinesAdapter.Context); + MachinesAdapter.Context.Machines.Remove(SelectedMachine); + } + catch (Exception ex) + { + LogManager.Log(ex, $"Error removing machine {SelectedMachine.SerialNumber}."); + _notification.ShowError($"An error occurred while trying to delete the selected machine.\n{ex.Message}"); + } + finally + { + IsFree = true; + } + } + } + } #endregion private async void OnFilterChanged() { - if (!String.IsNullOrWhiteSpace(Filter)) + if (Filter != null) { IsFree = false; - Machines = (await _dbContext.Machines.Where(x => x.SerialNumber.StartsWith(Filter)).ToListAsync()).ToObservableCollection(); + MachinesAdapter.Machines = (await MachinesAdapter.Context.Machines.Where(x => x.SerialNumber.StartsWith(Filter)).Include(x => x.Organization).Include(x => x.MachineVersion).ToListAsync()).ToObservableCollection(); IsFree = true; } } |
