diff options
| author | Avi Levkovich <avi@twine-s.com> | 2019-01-09 13:45:40 +0200 |
|---|---|---|
| committer | Avi Levkovich <avi@twine-s.com> | 2019-01-09 13:45:40 +0200 |
| commit | c2019e2a94afc7f25258ae38478ae74434414a1f (patch) | |
| tree | ee9cf870d5738004ebd92cf841c1bdbd26793984 /Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.MachineDesigner/ViewModels/MainViewVM.cs | |
| parent | c84c8852f2889235a181ec74f0d2fdf8584f2a7d (diff) | |
| parent | d50d729a2b7d45ca4b22df7ff0d8823825c479b6 (diff) | |
| download | Tango-c2019e2a94afc7f25258ae38478ae74434414a1f.tar.gz Tango-c2019e2a94afc7f25258ae38478ae74434414a1f.zip | |
Merge branch 'master' of https://twinetfs.visualstudio.com/_git/Tango
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 | 699 |
1 files changed, 290 insertions, 409 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 5a5df0d0f..71b2e0bd5 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 @@ -18,45 +18,33 @@ using Tango.MachineStudio.Common; using Tango.BL; using Tango.AutoComplete.Editors; using System.Data.Entity; +using Tango.BL.Builders; +using Tango.MachineStudio.MachineDesigner.Contracts; +using System.Windows.Threading; +using Tango.Core.Threading; namespace Tango.MachineStudio.MachineDesigner.ViewModels { - public class MainViewVM : StudioViewModel + public class MainViewVM : StudioViewModel<IMainView> { private INotificationProvider _notification; - private ObservablesContext _db; - private Configuration _original_configuration; + private ActionTimer _machines_action_timer; + private ActionTimer _dispensers_action_timer; #region Properties - private ObservablesStaticCollections _adapter; - /// <summary> - /// Gets or sets the db static adapter. - /// </summary> - public ObservablesStaticCollections Adapter + private ObservablesStaticCollections _machinesAdapter; + public ObservablesStaticCollections MachinesAdapter { - get { return _adapter; } - set { _adapter = value; RaisePropertyChangedAuto(); } + get { return _machinesAdapter; } + set { _machinesAdapter = value; RaisePropertyChangedAuto(); } } - private bool _canWork; - /// <summary> - /// Gets or sets a value indicating whether this instance can work. - /// </summary> - public bool CanWork + private ObservablesStaticCollections _activeMachineAdapter; + public ObservablesStaticCollections ActiveMachineAdapter { - get { return _canWork; } - set { _canWork = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } - } - - private Machine _machine; - /// <summary> - /// Gets or sets the current editable machine. - /// </summary> - public Machine Machine - { - get { return _machine; } - set { _machine = value; RaisePropertyChangedAuto(); } + get { return _activeMachineAdapter; } + set { _activeMachineAdapter = value; RaisePropertyChangedAuto(); } } private Machine _selectedMachine; @@ -70,19 +58,21 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels { if (_selectedMachine != value) { - _selectedMachine = value; RaisePropertyChangedAuto(); OnSelectedMachineChanged(); + _selectedMachine = value; + RaisePropertyChangedAuto(); + InvalidateRelayCommands(); } } } - private Configuration _configuration; + private Machine _activeMachine; /// <summary> - /// Gets or sets the editable machine configuration. + /// Gets or sets the active machine. /// </summary> - public Configuration Configuration + public Machine ActiveMachine { - get { return _configuration; } - set { _configuration = value; RaisePropertyChangedAuto(); } + get { return _activeMachine; } + set { _activeMachine = value; RaisePropertyChangedAuto(); } } private IdsPack _selectedIds; @@ -95,29 +85,19 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels set { _selectedIds = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } } - private ObservableCollection<Configuration> _history; + private String _dispensersFilter; /// <summary> - /// Gets or sets the machine configuration history. + /// Gets or sets the dispensers filter. /// </summary> - public ObservableCollection<Configuration> History + public String DispensersFilter { - get { return _history; } - set { _history = value; RaisePropertyChangedAuto(); } - } - - private Configuration _selectedHistoryConfiguration; - /// <summary> - /// Gets or sets the machine selected configuration from history. - /// </summary> - public Configuration SelectedHistoryConfiguration - { - get { return _selectedHistoryConfiguration; } - set { _selectedHistoryConfiguration = value; RaisePropertyChangedAuto(); OnHistoryConfigurationSelected(); } + get { return _dispensersFilter; } + set { _dispensersFilter = value; RaisePropertyChangedAuto(); OnDispensersFilterChanged(); } } private String _filter; /// <summary> - /// Gets or sets the configuration components filter. + /// Gets or sets the machines filter. /// </summary> public String Filter { @@ -125,10 +105,13 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels set { _filter = value; RaisePropertyChangedAuto(); OnFilterChanged(); } } - /// <summary> - /// Gets or sets the machines provider. - /// </summary> - public ISuggestionProvider MachinesProvider { get; set; } + private Spool _selectedSpool; + public Spool SelectedSpool + { + get { return _selectedSpool; } + set { _selectedSpool = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } + } + #endregion @@ -150,20 +133,36 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels public RelayCommand RemoveIdsCommand { get; set; } /// <summary> - /// Gets or sets the set version configuration command. + /// Gets or sets the remove machine command. /// </summary> - public RelayCommand SetVersionConfigurationCommand { get; set; } + public RelayCommand RemoveMachineCommand { get; set; } /// <summary> - /// Gets or sets the set as default command. + /// Gets or sets the add machine command. /// </summary> - public RelayCommand SetAsDefaultCommand { get; set; } + public RelayCommand AddMachineCommand { get; set; } /// <summary> - /// Gets or sets the reset command. + /// Gets or sets the edit machine command. /// </summary> - public RelayCommand ResetCommand { get; set; } + public RelayCommand EditMachineCommand { get; set; } + /// <summary> + /// Gets or sets the back to machines command. + /// </summary> + public RelayCommand BackToMachinesCommand { get; set; } + + /// <summary> + /// Gets or sets the add spool command. + /// </summary> + public RelayCommand AddSpoolCommand { get; set; } + + /// <summary> + /// Gets or sets the remove spool command. + /// </summary> + public RelayCommand RemoveSpoolCommand { get; set; } + + public RelayCommand CloneMachineCommand { get; set; } #endregion #region Constructors @@ -178,198 +177,48 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels /// </summary> public MainViewVM(INotificationProvider notification) { - CanWork = true; - + MachinesAdapter = new ObservablesStaticCollections(ObservablesContext.CreateDefault()); _notification = notification; - Configuration = new Configuration(); - Configuration.Name = "Untitled"; - Machine = new Machine(); - Machine.Configuration = Configuration; - - SaveCommand = new RelayCommand(Save, (x) => CanWork); - AddIdsCommand = new RelayCommand(AddIds, (x) => CanWork && Configuration.IdsPacks.Count < 8); - RemoveIdsCommand = new RelayCommand(RemoveIds, (x) => CanWork && SelectedIds != null); - SetVersionConfigurationCommand = new RelayCommand(SetVersionConfiguration, (x) => CanWork); - SetAsDefaultCommand = new RelayCommand(SetAsDefaultConfiguration, (x) => CanWork); - ResetCommand = new RelayCommand(Reset, () => CanWork); + _machines_action_timer = new ActionTimer(TimeSpan.FromMilliseconds(200)); + _dispensers_action_timer = new ActionTimer(TimeSpan.FromMilliseconds(200)); - MachinesProvider = new SuggestionProvider((filter) => - { - try - { - return _db.Machines.Where(x => x.SerialNumber.StartsWith(filter)).ToList(); - } - catch - { - return new List<Machine>(); - } - }); + 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(() => LoadSelectedMachine(), () => SelectedMachine != null); + BackToMachinesCommand = new RelayCommand(() => View.NavigateTo(MachineDesignerNavigationView.MachinesView)); + SaveCommand = new RelayCommand(SaveMachine); + AddMachineCommand = new RelayCommand(AddNewMachine); + RemoveMachineCommand = new RelayCommand(RemoveSelectedMachine, () => SelectedMachine != null); + AddSpoolCommand = new RelayCommand(AddNewSpool); + RemoveSpoolCommand = new RelayCommand(RemoveSpool, () => SelectedSpool != null); + CloneMachineCommand = new RelayCommand(CloneMachine, () => SelectedMachine != null); } - #endregion #region Application Ready - public async override void OnApplicationReady() + public override async void OnApplicationReady() { - await InitCollections(); + MachinesAdapter.MachineVersions = (await MachinesAdapter.Context.MachineVersions.ToListAsync()).ToObservableCollection(); } #endregion - private Task InitCollections() - { - return Task.Factory.StartNew(() => - { - CanWork = false; - - _db = ObservablesContext.CreateDefault(); - - Adapter = new ObservablesStaticCollections(); - Adapter.ApplicationDisplayPanelVersions = _db.ApplicationDisplayPanelVersions.ToObservableCollection(); - Adapter.ApplicationFirmwareVersions = _db.ApplicationFirmwareVersions.ToObservableCollection(); - Adapter.ApplicationOsVersions = _db.ApplicationOsVersions.ToObservableCollection(); - Adapter.EmbeddedFirmwareVersions = _db.EmbeddedFirmwareVersions.ToObservableCollection(); - Adapter.DispenserTypes = _db.DispenserTypes.ToObservableCollection(); - Adapter.LiquidTypes = _db.LiquidTypes.ToObservableCollection(); - Adapter.MidTankTypes = _db.MidTankTypes.ToObservableCollection(); - Adapter.CartridgeTypes = _db.CartridgeTypes.ToObservableCollection(); - Adapter.IdsPackFormulas = _db.IdsPackFormulas.ToObservableCollection(); - Adapter.HardwareVersions = _db.HardwareVersions.ToObservableCollection(); - Adapter.MachineVersions = _db.MachineVersions.ToObservableCollection(); - Adapter.Organizations = _db.Organizations.ToObservableCollection(); - - Adapter.InitCollectionSources(); - - CanWork = true; - }); - } - - private void Reset() - { - using (_notification.PushTaskItem("Resetting designer...")) - { - SelectedMachine = null; - Machine = new Machine(); - Configuration = new Configuration(); - History = new ObservableCollection<Configuration>(); - SelectedHistoryConfiguration = null; - Filter = String.Empty; - InitCollections(); - } - } - - #region Virtual Methods - - /// <summary> - /// Called when the selected machine has changed. - /// </summary> - protected virtual void OnSelectedMachineChanged() - { - if (SelectedMachine != null) - { - CanWork = false; - - using (_notification.PushTaskItem("Loading machine configuration...")) - { - Task.Factory.StartNew(() => - { - InitCollections().Wait(); - Machine = _db.Machines.Where(x => x.Guid == SelectedMachine.Guid).Include(x => x.Organization).SingleOrDefault(x => x.Guid == SelectedMachine.Guid); - Configuration = _db.Adapter.GetConfiguration(x => x.Guid == Machine.ConfigurationGuid); - - SetHistory(); - - _original_configuration = Configuration.Clone(); - }); - } - - CanWork = true; - } - else - { - History = new ObservableCollection<Configuration>(); - } - } - - /// <summary> - /// Called when the history configuration has been selected - /// </summary> - protected virtual void OnHistoryConfigurationSelected() + private void OnDispensersFilterChanged() { - if (SelectedHistoryConfiguration != null && CanWork) + if (!String.IsNullOrWhiteSpace(DispensersFilter)) { - using (_notification.PushTaskItem("Loading Configuration...")) + _dispensers_action_timer.ResetReplace(() => { Task.Factory.StartNew(() => { - CanWork = false; - - SelectedHistoryConfiguration = _db.Adapter.GetConfiguration(x => x.Guid == SelectedHistoryConfiguration.Guid); - Configuration = SelectedHistoryConfiguration; - Machine.Configuration = Configuration; - - CanWork = true; + ActiveMachineAdapter.Dispensers = ActiveMachineAdapter.Context.Dispensers.Where(x => x.SerialNumber.ToLower().StartsWith(DispensersFilter.ToLower())).ToSynchronizedObservableCollection(); }); - } - } - } - - /// <summary> - /// Called when the filter has changed - /// </summary> - protected virtual void OnFilterChanged() - { - List<ICollectionView> collections = new List<ICollectionView>(); - collections.Add(Adapter.ApplicationFirmwareVersionsViewSource); - collections.Add(Adapter.ApplicationDisplayPanelVersionsViewSource); - collections.Add(Adapter.ApplicationOsVersionsViewSource); - collections.Add(Adapter.EmbeddedFirmwareVersionsViewSource); - collections.Add(Adapter.DispenserTypesViewSource); - collections.Add(Adapter.CartridgeTypesViewSource); - collections.Add(Adapter.LiquidTypesViewSource); - collections.Add(Adapter.MidTankTypesViewSource); - collections.Add(Adapter.HardwareVersionsViewSource); - - foreach (var collection in collections) - { - collection.Filter = (x) => - { - foreach (var prop in x.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(y => y.PropertyType == typeof(String))) - { - String value = prop.GetValue(x).ToStringSafe(); - - if (value != null && Filter != null) - { - if (value.ToLower().Contains(Filter.ToLower())) - { - return true; - } - } - } - - return false; - }; + }); } } - #endregion - - #region Public Methods - - /// <summary> - /// Sets the selected machine without any further processing. - /// </summary> - /// <param name="machine">The machine.</param> - public void SetSelectedMachine(Machine machine) - { - _machine = machine; - RaisePropertyChanged(nameof(Machine)); - } - - #endregion - #region Drag Drop Handlers /// <summary> @@ -379,7 +228,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> @@ -388,8 +237,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> @@ -398,8 +247,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> @@ -408,8 +257,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> @@ -418,8 +267,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> @@ -428,30 +277,30 @@ 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> - /// Drops the type of the cartridge. + /// Drops the dispenser. /// </summary> - /// <param name="cartridgeType">Type of the cartridge.</param> + /// <param name="dispenser">The dispenser.</param> /// <param name="idsPack">The ids pack.</param> - public void DropCartridgeType(CartridgeType cartridgeType, IdsPack idsPack) + public void DropDispenser(Dispenser dispenser, IdsPack idsPack) { - idsPack.CartridgeType = cartridgeType; - idsPack.CartridgeTypeGuid = cartridgeType.Guid; + idsPack.Dispenser = dispenser; + idsPack.DispenserGuid = dispenser.Guid; } /// <summary> - /// Drops the dispenser. + /// Drops the type of the cartridge. /// </summary> - /// <param name="dispenserType">The dispenser.</param> + /// <param name="cartridgeType">Type of the cartridge.</param> /// <param name="idsPack">The ids pack.</param> - public void DropDispenserType(DispenserType dispenserType, IdsPack idsPack) + public void DropCartridgeType(CartridgeType cartridgeType, IdsPack idsPack) { - idsPack.DispenserType = dispenserType; - idsPack.DispenserTypeGuid = dispenserType.Guid; + idsPack.CartridgeType = cartridgeType; + idsPack.CartridgeTypeGuid = cartridgeType.Guid; } /// <summary> @@ -497,7 +346,7 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels /// </summary> private void RemoveIds() { - _db.IdsPacks.Remove(SelectedIds); + ActiveMachineAdapter.Context.IdsPacks.Remove(SelectedIds); SelectedIds = null; } @@ -506,104 +355,164 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels /// </summary> private void AddIds() { - _db.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() + private async void LoadSelectedMachine(bool newMachine = false, bool clone = false, MachineVersion selectedVersion = null) { - foreach (var ids in Configuration.IdsPacks) + using (_notification.PushTaskItem("Loading machine details...")) { - ids.PackIndex = Configuration.IdsPacks.IndexOf(ids); - ids.Configuration = Configuration; - ids.ConfigurationGuid = Configuration.Guid; + try + { + IsFree = false; + + if (ActiveMachineAdapter != null) + { + ActiveMachineAdapter.Context.Dispose(); + } + + 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(); + + 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 (!newMachine) + { + ActiveMachine = (await new MachineBuilder(ActiveMachineAdapter.Context).Set(SelectedMachine.Guid).WithOrganization().WithConfiguration().WithSpools().BuildAsync()); + + if (clone) + { + ActiveMachine = ActiveMachine.Clone(); + ActiveMachine.Name = ""; + ActiveMachine.SerialNumber = ""; + ActiveMachineAdapter.Context.Machines.Add(ActiveMachine); + } + } + else + { + if (selectedVersion == null) + { + ActiveMachine = new Machine(); + ActiveMachine.Configuration = new Configuration(); + ActiveMachineAdapter.Context.Machines.Add(ActiveMachine); + } + else + { + ActiveMachine = selectedVersion.CreatePrototypeMachine(ActiveMachineAdapter.Context); + ActiveMachineAdapter.Context.Machines.Add(ActiveMachine); + } + } + + 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(); + } } + } - //Validate + 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; + } List<String> errors = new List<string>(); - if (Machine.MachineVersion == null) + if (ActiveMachine.MachineVersion == null) { errors.Add("Machine version is required."); } - if (Machine.Name.IsNullOrWhiteSpace()) + if (ActiveMachine.Name.IsNullOrWhiteSpace()) { errors.Add("Machine name is required."); } - if (Machine.Organization == null) + if (ActiveMachine.Organization == null) { errors.Add("Machine organization is required."); } - if (Machine.SerialNumber.IsNullOrWhiteSpace()) + if (ActiveMachine.SerialNumber.IsNullOrWhiteSpace()) { errors.Add("Machine serial number is required."); } - if (Configuration.Name.IsNullOrWhiteSpace()) - { - errors.Add("Configuration name is required."); - } - - if (Configuration.ApplicationDisplayPanelVersion == null) + if (ActiveMachine.Configuration.ApplicationDisplayPanelVersion == null) { errors.Add("Touch Panel is required."); } - if (Configuration.ApplicationFirmwareVersion == null) + if (ActiveMachine.Configuration.ApplicationFirmwareVersion == null) { errors.Add("Application firmware is required."); } - if (Configuration.ApplicationOsVersion == null) + if (ActiveMachine.Configuration.ApplicationOsVersion == null) { errors.Add("Application operation system is required."); } - if (Configuration.EmbeddedFirmwareVersion == null) + if (ActiveMachine.Configuration.EmbeddedFirmwareVersion == null) { errors.Add("Embedded firmware is required."); } - if (Configuration.HardwareVersion == null) + if (ActiveMachine.Configuration.HardwareVersion == null) { errors.Add("Hardware version is required."); } - foreach (var pack in Configuration.IdsPacks) + foreach (var pack in ActiveMachine.Configuration.IdsPacks) { - if (pack.LiquidType != null || pack.CartridgeType != null || pack.DispenserType != null || pack.IdsPackFormula != null || pack.MidTankType != null) + if (pack.LiquidType != null || pack.CartridgeType != null || pack.Dispenser != null || pack.IdsPackFormula != null || pack.MidTankType != null) { - if (pack.Name.IsNullOrWhiteSpace()) - { - errors.Add(String.Format("Name is required on IDS pack number '{0}'.", Configuration.IdsPacks.IndexOf(pack) + 1)); - continue; - } if (pack.CartridgeType == null) { - errors.Add(String.Format("Cartridge type is required on IDS pack '{0}'.", pack.Name)); + errors.Add(String.Format("Cartridge type is required on IDS pack '{0}'.", pack.PackIndex)); } - if (pack.DispenserType == null) + if (pack.Dispenser == null) { - errors.Add(String.Format("Dispenser type is required on IDS pack '{0}'.", pack.Name)); + 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.Name)); + 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.Name)); + 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.Name)); + errors.Add(String.Format("Formula type is required on IDS pack '{0}'.", pack.PackIndex)); } pack.IsEmpty = false; @@ -611,172 +520,144 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels else { pack.IsEmpty = true; - pack.Name = String.Empty; } } + 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."); + } + + 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."); + } + } + + if (ActiveMachine.Spools.GroupBy(x => x.SpoolType).Any(x => x.Count() > 1)) + { + errors.Add($"Same spool type is registered multiple times."); + } + 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); + errorsString += String.Join(Environment.NewLine, errors.Distinct()); _notification.ShowError(errorsString); return; } - //Validate - try { - CanWork = false; + IsFree = false; - using (_notification.PushTaskItem("Saving Machine Configuration...")) + using (_notification.PushTaskItem("Saving Machine Details...")) { - if (!_db.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; - _db.Machines.Add(Machine); - _db.MachinesConfigurations.Add(new MachinesConfiguration() - { - Configuration = Configuration, - Machine = Machine, - }); - } - } - else - { - bool add_history = History.Count == 0 || _original_configuration.Name != Configuration.Name; - - if (add_history) - { - _db.Configurations.Add(_original_configuration); + ActiveMachine.ConfigurationGuid = ActiveMachine.Configuration.Guid; + ActiveMachine.LastUpdated = DateTime.UtcNow; + ActiveMachine.ProductionDate = DateTime.UtcNow; + await ActiveMachineAdapter.Context.SaveChangesAsync(); - _db.MachinesConfigurations.Add(new MachinesConfiguration() - { - Configuration = _original_configuration, - Machine = Machine - }); - } + if (SelectedMachine != null) + { + await SelectedMachine.Reload(MachinesAdapter.Context); } - - await _db.SaveChangesAsync(); - OnSelectedMachineChanged(); - } } catch (Exception ex) { - _notification.ShowError("An error occurred while trying to save the configuration" + Environment.NewLine + ex.Message); + LogManager.Log(ex, "Error saving machine details."); + _notification.ShowError("An error occurred while trying to save the machine details" + Environment.NewLine + ex.Message); } finally { - CanWork = true; + IsFree = true; InvalidateRelayCommands(); } } - /// <summary> - /// Sets the specified machine history. - /// </summary> - /// <param name="machine">The machine.</param> - private void SetHistory() + private void AddNewMachine() { - History = _db.MachinesConfigurations.Where(x => x.MachineGuid == Machine.Guid).Select(x => x.Configuration).ToObservableCollection(); - //History.Insert(0, Machine.Configuration); + MachineCreationDialogVM vm = new MachineCreationDialogVM(); + vm.MachineVersions = MachinesAdapter.MachineVersions.ToList(); + _notification.ShowModalDialog<MachineCreationDialogVM, Views.MachineCreationDialog>(vm, (x) => + { + if (String.IsNullOrWhiteSpace(vm.SelectedMachineVersion.PrototypeMachineData)) + { + _notification.ShowError("The selected version does not contain any prototype machine data."); + return; + } + + LoadSelectedMachine(true, false, vm.SelectedMachineVersion); + }, () => { }); } - /// <summary> - /// Sets the current configuration to the selected machine version default configuration. - /// </summary> - private async void SetVersionConfiguration() + private async void RemoveSelectedMachine() { - if (Machine.MachineVersion != null) + if (_notification.ShowQuestion("Are you sure you want to delete the selected machine?")) { - using (_notification.PushTaskItem("Applying default configuration...")) + using (_notification.PushTaskItem("Removing machine...")) { - CanWork = false; - - await Task.Factory.StartNew(() => + try { - var version = _db.MachineVersions.Where(x => x.Guid == Machine.MachineVersion.Guid).Include(x => x.DefaultConfiguration).FirstOrDefault(); - var version_config = _db.Adapter.GetConfiguration(x => x.Guid == version.DefaultConfiguration.Guid); - Configuration = version_config.Clone(); - Machine.Configuration = Configuration; - }); - - CanWork = true; + 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; + } } } - else + } + + #endregion + + private void CloneMachine() + { + LoadSelectedMachine(false, true); + } + + private void AddNewSpool() + { + _activeMachineAdapter.Context.Spools.Add(new Spool() { - _notification.ShowError("No machine version selected."); - } + Machine = ActiveMachine, + }); } - /// <summary> - /// Sets the current configuration as a default machine version configuration. - /// </summary> - private void SetAsDefaultConfiguration() + private void RemoveSpool() { - _notification.ShowModalDialog<MachineVersionDialogVM>(async (vm) => + if (SelectedSpool != null) { - CanWork = false; + _activeMachineAdapter.Context.Spools.Remove(SelectedSpool); + ActiveMachine.Spools.Remove(SelectedSpool); + } + } - try + private void OnFilterChanged() + { + if (Filter != null) + { + _machines_action_timer.ResetReplace(() => { - using (_notification.PushTaskItem("Saving Default Configuration...")) + Task.Factory.StartNew(() => { - if (vm.SelectedVersion != null) - { - var version = _db.MachineVersions.Where(x => x.Guid == vm.SelectedVersion.Guid).Include(x => x.DefaultConfiguration).SingleOrDefault(x => x.Guid == vm.SelectedVersion.Guid); - - _db.Configurations.Remove(version.DefaultConfiguration); - - var cloned = Configuration.Clone(); - _db.Configurations.Add(cloned); - version.DefaultConfiguration = cloned; - - await _db.SaveChangesAsync(); - } - else - { - MachineVersion newVersion = new MachineVersion(); - newVersion.Version = vm.Version; - newVersion.Name = vm.VersionName; - - var cloned = Configuration.Clone(); - - _db.Configurations.Add(cloned); - newVersion.DefaultConfiguration = cloned; - _db.MachineVersions.Add(newVersion); - await _db.SaveChangesAsync(); - } - } - } - catch (Exception ex) - { - _notification.ShowError(ex.Message); - } - finally - { - CanWork = true; - } - - }, () => - { - - }); + IsFree = false; + MachinesAdapter.Machines = MachinesAdapter.Context.Machines.Where(x => x.SerialNumber.StartsWith(Filter)).Include(x => x.Organization).Include(x => x.MachineVersion).ToSynchronizedObservableCollection(); + IsFree = true; + }); + }); + } } - - #endregion } } |
