diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-08-20 19:43:15 +0300 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-08-20 19:43:15 +0300 |
| commit | 51afc4f6a17383e91a72c2ce060e82604d43c3a8 (patch) | |
| tree | c0aa029d9864fc8f03b69716a42eda5efe65ccd5 /Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Developer/ViewModels/MainViewVM.cs | |
| parent | 2ea6653199844f5607d17a8912eb7a99e2471610 (diff) | |
| download | Tango-51afc4f6a17383e91a72c2ce060e82604d43c3a8.tar.gz Tango-51afc4f6a17383e91a72c2ce060e82604d43c3a8.zip | |
Working on new Machine Studio DB.
Diffstat (limited to 'Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Developer/ViewModels/MainViewVM.cs')
| -rw-r--r-- | Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Developer/ViewModels/MainViewVM.cs | 418 |
1 files changed, 273 insertions, 145 deletions
diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Developer/ViewModels/MainViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Developer/ViewModels/MainViewVM.cs index dace2ceac..6ce1a6108 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Developer/ViewModels/MainViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Developer/ViewModels/MainViewVM.cs @@ -41,6 +41,9 @@ using Tango.MachineStudio.Common; using Tango.BL.ColorConversion; using Tango.MachineStudio.Logging.ViewModels; using Tango.MachineStudio.Logging.Views; +using Tango.AutoComplete.Editors; +using System.Data.Entity; +using System.Runtime.ExceptionServices; namespace Tango.MachineStudio.Developer.ViewModels { @@ -48,6 +51,7 @@ namespace Tango.MachineStudio.Developer.ViewModels /// Represents the developer module main view, view model. /// </summary> /// <seealso cref="Tango.SharedUI.ViewModel" /> + [TangoCreateWhenRegistered] public class MainViewVM : StudioViewModel<DeveloperModule> { private static object _syncLock = new object(); @@ -71,6 +75,7 @@ namespace Tango.MachineStudio.Developer.ViewModels private bool _hiveOpened; private bool _color_changed_from_hive; private bool _dialog_shown; + private bool _disable_gamut_check; #region Properties @@ -104,16 +109,6 @@ namespace Tango.MachineStudio.Developer.ViewModels set { _runningJobStatus = value; RaisePropertyChangedAuto(); } } - private ObservableCollection<Machine> _machines; - /// <summary> - /// Gets or sets the machines. - /// </summary> - public ObservableCollection<Machine> Machines - { - get { return _machines; } - set { _machines = value; RaisePropertyChangedAuto(); } - } - private ObservableCollection<ColorSpace> _colorSpaces; /// <summary> /// Gets or sets the color spaces. @@ -173,19 +168,32 @@ namespace Tango.MachineStudio.Developer.ViewModels get { return _selectedMachine; } set { - _selectedMachine = value; - OnSelectedMachineChanged(); - RaisePropertyChangedAuto(); - InvalidateRelayCommands(); - - if (_selectedMachine != null) + if (value != null && _selectedMachine != value) { - _selectedMachine.Modified -= SelectedMachine_Modified; - _selectedMachine.Modified += SelectedMachine_Modified; + _selectedMachine = value; + OnSelectedMachineChanged(); + RaisePropertyChangedAuto(); + InvalidateRelayCommands(); + + if (_selectedMachine != null) + { + _selectedMachine.Modified -= SelectedMachine_Modified; + _selectedMachine.Modified += SelectedMachine_Modified; + } } } } + private bool _isLoading; + /// <summary> + /// Gets or sets a value indicating whether this instance is loading machine. + /// </summary> + public bool IsLoading + { + get { return _isLoading; } + set { _isLoading = value; RaisePropertyChangedAuto(); } + } + private List<LiquidTypesRml> _liquidTypesRmls; /// <summary> /// Gets or sets the liquid types RMLS. @@ -248,7 +256,6 @@ namespace Tango.MachineStudio.Developer.ViewModels { _activeJob = value; RaisePropertyChangedAuto(); - OnActiveJobChanged(); } } @@ -322,7 +329,7 @@ namespace Tango.MachineStudio.Developer.ViewModels set { _selectedRML = value; - InvalidateLiquidFactorsAndProcessTables(); + OnSelectedRMLChanged(); RaisePropertyChangedAuto(); InvalidateRelayCommands(); } @@ -504,6 +511,11 @@ namespace Tango.MachineStudio.Developer.ViewModels set { _selectedJobEvent = value; RaisePropertyChangedAuto(); OnSelectedJobEventChanged(); } } + /// <summary> + /// Gets or sets the machines providers. + /// </summary> + public ISuggestionProvider MachinesProvider { get; set; } + #endregion #region Commands @@ -643,29 +655,6 @@ namespace Tango.MachineStudio.Developer.ViewModels /// <param name="notificationProvider">The notification provider.</param> public MainViewVM(IStudioApplicationManager applicationManager, INotificationProvider notificationProvider, IDiagnosticsFrameProvider diagnosticsFrameProvider, IVideoCaptureProvider videoCaptureProvider, DeveloperNavigationManager navigation, IAuthenticationProvider authentication, IEventLogger eventLogger, ISpeechProvider speech) { - _settings = SettingsManager.Default.GetOrCreate<DeveloperModuleSettings>(); - - SelectedJobs = new ObservableCollection<Job>(); - - JobEvents = new ObservableCollection<MachinesEvent>(); - - LogManager.Log("Initializing machine Db context..."); - _machineDbContext = ObservablesContext.CreateDefault(); - - Machines = _machineDbContext.Machines.ToObservableCollection(); - - if (_settings.LastSelectedMachineGuid != null) - { - LogManager.Log("Setting last selected machine from settings..."); - SelectedMachine = _machineDbContext.Machines.SingleOrDefault(x => x.Guid == _settings.LastSelectedMachineGuid); - } - - if (_settings.LastSelectedJobGuid != null && SelectedMachine != null) - { - LogManager.Log("Setting last selected job from settings..."); - SelectedMachineJob = SelectedMachine.Jobs.SingleOrDefault(x => x.Guid == _settings.LastSelectedJobGuid); - } - _authentication = authentication; _notification = notificationProvider; @@ -690,8 +679,8 @@ namespace Tango.MachineStudio.Developer.ViewModels SaveLiquidFactorsCommand = new RelayCommand(SaveLiquidFactors, () => SelectedRML != null); AddSegmentCommand = new RelayCommand(AddSegment, () => ActiveJob != null); RemoveSegmentCommand = new RelayCommand(RemoveSelectedSegments, () => SelectedSegment != null); - AddJobCommand = new RelayCommand(AddJob, () => SelectedMachine != null); - RemoveJobCommand = new RelayCommand(RemoveSelectedJobs, () => SelectedMachineJob != null); + AddJobCommand = new RelayCommand(AddJob, () => SelectedMachine != null && !IsLoading); + RemoveJobCommand = new RelayCommand(RemoveSelectedJobs, () => SelectedMachineJob != null && !IsLoading); AddBrushStopCommand = new RelayCommand(AddBrushStop, () => SelectedSegment != null); RemoveBrushStopCommand = new RelayCommand(RemoveSelectedBrushStops, () => SelectedBrushStop != null); SaveJobCommand = new RelayCommand(SaveActiveJob, () => SelectedMachine != null); @@ -700,20 +689,63 @@ namespace Tango.MachineStudio.Developer.ViewModels StartJobAndRecordCommand = new RelayCommand(StartJobAndRecord, () => _dataCaptureVM != null && !_dataCaptureVM.Recorder.IsRecording && !_dataCaptureVM.Player.IsPlaying && ActiveJob != null && !IsJobRunning && MachineOperator != null && !MachineOperator.MachineEventsStateProvider.Events.ToList().Exists(x => x.ActionTypes.Contains(BL.Enumerations.ActionTypes.PreventJobExecution))); StopJobCommand = new RelayCommand(StopJob, () => IsJobRunning); CloseJobCompletionStatusCommand = new RelayCommand(CloseJobCompletionStatusBar); - LoadJobCommand = new RelayCommand(LoadSelectedJob, () => SelectedMachineJob != null); - DuplicateJobCommand = new RelayCommand(DuplicateSelectedJobs, () => SelectedMachineJob != null); + LoadJobCommand = new RelayCommand(LoadSelectedJob, () => SelectedMachineJob != null && !IsLoading); + DuplicateJobCommand = new RelayCommand(DuplicateSelectedJobs, () => SelectedMachineJob != null && !IsLoading); DuplicateSegmentCommand = new RelayCommand(DuplicateSelectedSegments, () => SelectedSegment != null); DuplicateBrushStopCommand = new RelayCommand(DuplicateSelectedBrushStops, () => SelectedBrushStop != null); SaveProcessParametersCommand = new RelayCommand(SaveProcessParameters, () => SelectedRML != null && SelectedRML.ProcessParametersTablesGroups.Count > 0); PushProcessParametersCommand = new RelayCommand(PushProcessParameters, () => SelectedRML != null && SelectedRML.ProcessParametersTablesGroups.Count > 0 && SelectedProcessParametersTable != null && MachineOperator != null); - ImportEmbroideryFileCommand = new RelayCommand(ImportEmbroideryFile, () => SelectedMachine != null); + ImportEmbroideryFileCommand = new RelayCommand(ImportEmbroideryFile, () => SelectedMachine != null && !IsLoading); DisplayJobEmbroideryFileCommand = new RelayCommand<Job>(DisplayJobEmbroideryFile); - ReloadMachinesCommand = new RelayCommand(ReloadMachine); + ReloadMachinesCommand = new RelayCommand(() => LoadMachine()); ApplicationManager.ConnectedMachineChanged += ApplicationManager_ConnectedMachineChanged; _eventLogger.NewLog += _eventLogger_NewLog; + + MachinesProvider = new SuggestionProvider((filter) => + { + try + { + return _machineDbContext.Machines.Where(x => x.SerialNumber.StartsWith(filter)).ToList(); + } + catch + { + return null; + } + }); + } + + #endregion + + #region Application Ready + + public override void OnApplicationReady() + { + base.OnApplicationReady(); + + _settings = SettingsManager.Default.GetOrCreate<DeveloperModuleSettings>(); + + SelectedJobs = new ObservableCollection<Job>(); + JobEvents = new ObservableCollection<MachinesEvent>(); + + LogManager.Log("Initializing machine Db context..."); + _machineDbContext = ObservablesContext.CreateDefault(); + _machineDbContext.Configuration.LazyLoadingEnabled = false; + + if (_settings.LastSelectedMachineGuid != null) + { + LogManager.Log("Setting last selected machine from settings..."); + SelectedMachine = _machineDbContext.Machines.SingleOrDefault(x => x.Guid == _settings.LastSelectedMachineGuid); + } + + if (_settings.LastSelectedJobGuid != null && SelectedMachine != null) + { + LogManager.Log("Setting last selected job from settings..."); + SelectedMachineJob = SelectedMachine.Jobs.SingleOrDefault(x => x.Guid == _settings.LastSelectedJobGuid); + } + _colorConversionThread = new Thread(ColorConversionThreadMethod); _colorConversionThread.IsBackground = true; _colorConversionThread.Start(); @@ -723,37 +755,42 @@ namespace Tango.MachineStudio.Developer.ViewModels #region Color Conversion + [HandleProcessCorruptedStateExceptions] private void ColorConversionThreadMethod() { while (true) { - if (IsVisible && ActiveJob != null && ActiveJob.Segments != null) + if (!_disable_gamut_check && IsVisible && ActiveJob != null && ActiveJob.Segments != null) { - var stops = ActiveJob.Segments.SelectMany(x => x.BrushStops).ToList(); - - foreach (var stop in stops) + try { - if (stop.ColorSpace.Code == BL.Enumerations.ColorSpaces.Volume.ToInt32() && !stop.IsLiquidVolumesOutOfRange) + var stops = ActiveJob.Segments.SelectMany(x => x.BrushStops).ToList(); + + foreach (var stop in stops) { - try + if (stop.ColorSpace.Code == BL.Enumerations.ColorSpaces.Volume.ToInt32() && !stop.IsLiquidVolumesOutOfRange) { - var output = TangoColorConverter.GetSuggestions(stop); + try + { + var output = TangoColorConverter.GetSuggestions(stop); - stop.Red = output.SingleCoordinates.Red; - stop.Green = output.SingleCoordinates.Green; - stop.Blue = output.SingleCoordinates.Blue; + stop.Red = output.SingleCoordinates.Red; + stop.Green = output.SingleCoordinates.Green; + stop.Blue = output.SingleCoordinates.Blue; + } + catch { } } - catch { } - } - else if (stop.ColorSpace.Code == BL.Enumerations.ColorSpaces.RGB.ToInt32() && !stop.Corrected) - { - try + else if (stop.ColorSpace.Code == BL.Enumerations.ColorSpaces.RGB.ToInt32() && !stop.Corrected) { - stop.IsOutOfGamut = TangoColorConverter.IsOutOfGamut(stop); + try + { + stop.IsOutOfGamut = TangoColorConverter.IsOutOfGamut(stop); + } + catch { } } - catch { } } } + catch { } } Thread.Sleep(500); @@ -950,9 +987,12 @@ namespace Tango.MachineStudio.Developer.ViewModels if (RmlProcessParametersTableGroup != null && RmlProcessParametersTableGroup.ProcessParametersTables.Count > 0) { LogManager.Log("Process parameters group changed..."); - SelectedProcessParametersTable = RmlProcessParametersTableGroup.ProcessParametersTables.OrderBy(x => x.TableIndex).FirstOrDefault(); - UpdateEstimatedDuration(); + InvokeUI(() => + { + SelectedProcessParametersTable = RmlProcessParametersTableGroup.ProcessParametersTables.OrderBy(x => x.TableIndex).FirstOrDefault(); + UpdateEstimatedDuration(); + }); } } @@ -992,9 +1032,7 @@ namespace Tango.MachineStudio.Developer.ViewModels if (SelectedMachine != null) { LogManager.Log(String.Format("Machine {0} changed.", SelectedMachine.SerialNumber)); - ReloadMachine(); - JobsCollectionView = CollectionViewSource.GetDefaultView(SelectedMachine.Jobs); - JobsCollectionView.SortDescriptions.Add(new SortDescription(nameof(Job.LastUpdated), ListSortDirection.Descending)); + LoadMachine(); } } @@ -1018,19 +1056,6 @@ namespace Tango.MachineStudio.Developer.ViewModels }; } - /// <summary> - /// Called when the active job has changed. - /// </summary> - protected virtual void OnActiveJobChanged() - { - if (ActiveJob != null) - { - LogManager.Log(String.Format("Active job {0} changed.", ActiveJob.Name)); - SegmentsCollectionView = CollectionViewSource.GetDefaultView(ActiveJob.Segments); - SegmentsCollectionView.SortDescriptions.Add(new SortDescription(nameof(Segment.SegmentIndex), ListSortDirection.Ascending)); - } - } - #endregion #region Drag & Drop @@ -1301,7 +1326,12 @@ namespace Tango.MachineStudio.Developer.ViewModels { LogManager.Log(String.Format("Saving liquid factors for RML {0}...", SelectedRML.Name)); await SelectedRML.SaveAsync(_activeJobDbContext); - InvalidateLiquidFactorsAndProcessTables(); + LiquidTypesRmls = ActiveJob.Machine.Configuration.NoneEmptyIdsPacks.Where(x => !x.IsEmpty).OrderBy(x => x.PackIndex).Select(x => x.LiquidType).SelectMany(x => x.LiquidTypesRmls).Where(x => x.Rml.Guid == SelectedRML.Guid).ToList(); + + if (SelectedSegment != null) + { + SetSegmentBrushStopsLiquidVolumes(SelectedSegment); + } } } } @@ -1323,7 +1353,10 @@ namespace Tango.MachineStudio.Developer.ViewModels if (SelectedRML != null && SelectedMachine != null) { LogManager.Log("Invalidating liquid factors, process parameters and process group history..."); - LiquidTypesRmls = SelectedMachine.Configuration.NoneEmptyIdsPacks.Where(x => !x.IsEmpty).OrderBy(x => x.PackIndex).Select(x => x.LiquidType).SelectMany(x => x.LiquidTypesRmls).Where(x => x.Rml.Guid == SelectedRML.Guid).ToList(); + LiquidTypesRmls = ActiveJob.Machine.Configuration.NoneEmptyIdsPacks.Where(x => !x.IsEmpty).OrderBy(x => x.PackIndex).Select(x => x.LiquidType).SelectMany(x => x.LiquidTypesRmls).Where(x => x.Rml.Guid == SelectedRML.Guid).ToList(); + + _activeJobDbContext.ProcessParametersTablesGroups.Where(x => x.RmlGuid == SelectedRML.Guid).Include(x => x.ProcessParametersTables).ToList(); + RmlProcessParametersTableGroup = SelectedRML.ProcessParametersTablesGroups.ToList().SingleOrDefault(x => x.Active); var selectedHistory = RmlProcessParametersTableGroup; @@ -1334,11 +1367,26 @@ namespace Tango.MachineStudio.Developer.ViewModels RmlProcessParametersTableGroup.ProcessParametersTables = RmlProcessParametersTableGroup.ProcessParametersTables.OrderBy(x => x.TableIndex).ToObservableCollection(); } - GroupsHistory = SelectedRML.ProcessParametersTablesGroups.OrderByDescending(x => x.SaveDate).OrderBy(x => !x.Active).ToObservableCollection(); + GroupsHistory = SelectedRML.ProcessParametersTablesGroups.OrderByDescending(x => x.SaveDate).OrderBy(x => !x.Active).ToObservableCollection(); _selectedGroupHistory = selectedHistory; - RaisePropertyChangedAuto(nameof(SelectedGroupHistory)); + InvokeUI(() => + { + RaisePropertyChanged(nameof(SelectedGroupHistory)); + RaisePropertyChanged(nameof(RmlProcessParametersTableGroup)); + }); + } + } + + private async void OnSelectedRMLChanged() + { + using (_notification.PushTaskItem("Loading RML...")) + { + await Task.Factory.StartNew(() => + { + InvalidateLiquidFactorsAndProcessTables(); + }); } } @@ -1417,62 +1465,86 @@ namespace Tango.MachineStudio.Developer.ViewModels /// <summary> /// Loads the selected job. /// </summary> - private void LoadSelectedJob() + private async void LoadSelectedJob() { if (SelectedMachineJob != null) { using (_notification.PushTaskItem("Loading job details...")) { - LogManager.Log(String.Format("Loading job {0}...", SelectedMachineJob.Name)); - SelectedSegments = new ObservableCollection<Segment>(); - SelectedBrushStops = new ObservableCollection<BrushStop>(); - SelectedRML = null; - SelectedSegment = null; - SelectedGroupHistory = null; - SelectedBrushStop = null; - SelectedProcessParametersTable = null; - RmlProcessParametersTableGroup = null; + await Task.Factory.StartNew(() => + { + _disable_gamut_check = true; + LogManager.Log(String.Format("Loading job {0}...", SelectedMachineJob.Name)); + SelectedSegments = new ObservableCollection<Segment>(); + SelectedBrushStops = new ObservableCollection<BrushStop>(); + SelectedRML = null; + SelectedSegment = null; + SelectedGroupHistory = null; + SelectedBrushStop = null; + SelectedProcessParametersTable = null; + RmlProcessParametersTableGroup = null; - _blockInvalidateCommands = false; + _blockInvalidateCommands = false; - LogManager.Log("Creating active job DB context..."); - _activeJobDbContext = ObservablesContext.CreateDefault(); - _activeJobDbContext.Configuration.LazyLoadingEnabled = true; + LogManager.Log("Creating active job DB context..."); + _activeJobDbContext = ObservablesContext.CreateDefault(); + _activeJobDbContext.Configuration.LazyLoadingEnabled = false; - LogManager.Log("Initializing available color spaces, RMLs & Winding methods..."); - var processParamsGroups = _activeJobDbContext.ProcessParametersTablesGroups.ToList(); - var processParams = _activeJobDbContext.ProcessParametersTables.ToList(); - ColorSpaces = _activeJobDbContext.ColorSpaces.ToObservableCollection(); - Rmls = _activeJobDbContext.Rmls.ToObservableCollection(); - WindingMethods = _activeJobDbContext.WindingMethods.ToObservableCollection(); - SpoolTypes = _activeJobDbContext.SpoolTypes.ToObservableCollection(); + LogManager.Log("Initializing available color spaces, RMLs & Winding methods..."); + //var processParamsGroups = _activeJobDbContext.ProcessParametersTablesGroups.ToList(); + //var processParams = _activeJobDbContext.ProcessParametersTables.ToList(); - LogManager.Log("Setting active job..."); - _activeJob = _activeJobDbContext.Jobs.SingleOrDefault(x => x.Guid == SelectedMachineJob.Guid); + ColorSpaces = _activeJobDbContext.ColorSpaces.ToObservableCollection(); + Rmls = _activeJobDbContext.Rmls.ToObservableCollection(); + WindingMethods = _activeJobDbContext.WindingMethods.ToObservableCollection(); + SpoolTypes = _activeJobDbContext.SpoolTypes.ToObservableCollection(); - _selectedRML = ActiveJob.Rml; + LogManager.Log("Setting active job..."); + ActiveJob = _activeJobDbContext.Jobs.Include(x => x.Rml).Include(x => x.Segments).Include(x => x.Segments.Select(y => y.BrushStops)).SingleOrDefault(x => x.Guid == SelectedMachineJob.Guid); - LogManager.Log("Setting selected segment..."); - _selectedSegment = ActiveJob.Segments.FirstOrDefault(); + _activeJobDbContext.Ccts.Where(x => x.RmlGuid == ActiveJob.RmlGuid).ToList(); + _activeJobDbContext.Cats.Where(x => x.RmlGuid == ActiveJob.RmlGuid).ToList(); + _activeJobDbContext.Machines.SingleOrDefault(x => x.Guid == ActiveJob.MachineGuid); + _activeJobDbContext.Configurations.SingleOrDefault(x => x.Guid == ActiveJob.Machine.ConfigurationGuid); - ActiveJob.LengthChanged -= ActiveJob_LengthChanged; - ActiveJob.LengthChanged += ActiveJob_LengthChanged; + _activeJobDbContext.LiquidTypesRmls.ToList(); - ActiveJob = _activeJob; + _activeJobDbContext.IdsPackFormulas.ToList(); + _activeJobDbContext.LiquidTypes.ToList(); + _activeJobDbContext.MidTankTypes.ToList(); + _activeJobDbContext.DispenserTypes.ToList(); - SelectedRML = _selectedRML; - SelectedSegment = _selectedSegment; + _activeJobDbContext.IdsPacks.Where(x => x.ConfigurationGuid == ActiveJob.Machine.ConfigurationGuid).ToList(); - UpdateEstimatedDuration(); - _blockInvalidateCommands = false; - InvalidateRelayCommands(); + + LogManager.Log("Setting selected segment..."); + _selectedSegment = ActiveJob.Segments.FirstOrDefault(); + + ActiveJob.LengthChanged -= ActiveJob_LengthChanged; + ActiveJob.LengthChanged += ActiveJob_LengthChanged; + + _selectedRML = ActiveJob.Rml; + InvalidateLiquidFactorsAndProcessTables(); + RaisePropertyChanged(nameof(SelectedRML)); + + UpdateEstimatedDuration(); + + _blockInvalidateCommands = false; + InvalidateRelayCommands(); + + _disable_gamut_check = false; + }); + + SegmentsCollectionView = CollectionViewSource.GetDefaultView(ActiveJob.Segments); + SegmentsCollectionView.SortDescriptions.Add(new SortDescription(nameof(Segment.SegmentIndex), ListSortDirection.Ascending)); + + SelectedSegment = _selectedSegment; UIHelper.DoEvents(); _navigation.NavigateTo(DeveloperNavigationView.JobView); - } } } @@ -1480,20 +1552,43 @@ namespace Tango.MachineStudio.Developer.ViewModels /// <summary> /// Saves the active job. /// </summary> - private void SaveActiveJob() + private async void SaveActiveJob() { if (ActiveJob != null) { using (_notification.PushTaskItem("Saving job details...")) { - LogManager.Log(String.Format("Saving the active job {0}...", ActiveJob.Name)); - ActiveJob.LastUpdated = DateTime.UtcNow; - ActiveJob.Rml = SelectedRML; - ActiveJob.EstimatedDurationMili = (int)EstimatedDuration.TotalMilliseconds; + await Task.Factory.StartNew(() => + { + LogManager.Log(String.Format("Saving the active job {0}...", ActiveJob.Name)); + ActiveJob.LastUpdated = DateTime.UtcNow; + ActiveJob.Rml = SelectedRML; + ActiveJob.EstimatedDurationMili = (int)EstimatedDuration.TotalMilliseconds; + _activeJobDbContext.SaveChanges(); + + _machineDbContext.Entry(SelectedMachineJob).Reload(); + - _activeJobDbContext.SaveChanges(); - ReloadMachine(); - SelectedMachineJob = SelectedMachine.Jobs.SingleOrDefault(x => x.Guid == ActiveJob.Guid); + _machineDbContext.Entry(SelectedMachineJob).Collection(x => x.Segments).Load(); + + foreach (var segment in SelectedMachineJob.Segments.ToList()) + { + _machineDbContext.Entry(segment).Collection(x => x.BrushStops).Load(); + + foreach (var brushStop in segment.BrushStops.ToList()) + { + _machineDbContext.Entry(brushStop).Reload(); + } + + _machineDbContext.Entry(segment).Reload(); + } + + InvokeUI(() => + { + SelectedMachineJob.Segments = SelectedMachineJob.Segments; + }); + + }); } } } @@ -1527,19 +1622,51 @@ namespace Tango.MachineStudio.Developer.ViewModels #region Private Methods - private void ReloadMachine() + private async void LoadMachine() { - LogManager.Log("Reloading selected machine..."); - _machineDbContext.Dispose(); - _machineDbContext = ObservablesContext.CreateDefault(); - _machineDbContext.Configuration.LazyLoadingEnabled = true; - String machineGuid = _selectedMachine.Guid; - Machines = _machineDbContext.Machines.ToObservableCollection(); - _selectedMachine = Machines.SingleOrDefault(x => x.Guid == machineGuid); - RaisePropertyChanged(nameof(SelectedMachine)); + try + { + LogManager.Log("Loading selected machine..."); + + IsLoading = true; + + using (_notification.PushTaskItem("Loading selected machine...")) + { + await _machineDbContext.Jobs.Where(x => x.MachineGuid == SelectedMachine.Guid).Include(x => x.User).Include(x => x.User.Contact).LoadAsync(); + await _machineDbContext.Configurations.SingleOrDefaultAsync(x => x.Guid == SelectedMachine.ConfigurationGuid); + + await _machineDbContext.ColorSpaces.LoadAsync(); + + SelectedMachine.Configuration.IdsPacks.EnableCrossThreadOperations(); + + await _machineDbContext.IdsPacks.OrderBy(x => x.PackIndex).Where(x => x.ConfigurationGuid == SelectedMachine.ConfigurationGuid). + Include(x => x.CartridgeType). + Include(x => x.DispenserType). + Include(x => x.LiquidType). + Include(x => x.IdsPackFormula). + Include(x => x.MidTankType).LoadAsync(); + + RaisePropertyChanged(nameof(SelectedMachine)); - JobsCollectionView = CollectionViewSource.GetDefaultView(SelectedMachine.Jobs); - JobsCollectionView.SortDescriptions.Add(new SortDescription(nameof(Job.LastUpdated), ListSortDirection.Descending)); + JobsCollectionView = CollectionViewSource.GetDefaultView(SelectedMachine.Jobs); + JobsCollectionView.SortDescriptions.Add(new SortDescription(nameof(Job.LastUpdated), ListSortDirection.Descending)); + } + + IsLoading = false; + + foreach (var job in SelectedMachine.Jobs.OrderByDescending(x => x.LastUpdated)) + { + if (IsLoading) break; + job.Segments.EnableCrossThreadOperations(); + await _machineDbContext.Segments.Where(x => x.JobGuid == job.Guid).Include(x => x.BrushStops).OrderBy(x => x.SegmentIndex).LoadAsync(); + } + } + catch (Exception ex) + { + LogManager.Log(ex); + _notification.ShowError("An error occurred while trying to load the selected machine."); + IsLoading = false; + } } private void UpdateEstimatedDuration() @@ -1557,7 +1684,7 @@ namespace Tango.MachineStudio.Developer.ViewModels LogManager.Log("Setting segment brush stops liquid volumes..."); foreach (var stop in segment.BrushStops) { - stop.SetLiquidVolumes(SelectedMachine.Configuration, SelectedRML, SelectedProcessParametersTable); + stop.SetLiquidVolumes(ActiveJob.Machine.Configuration, SelectedRML, SelectedProcessParametersTable); } } } @@ -1713,14 +1840,15 @@ namespace Tango.MachineStudio.Developer.ViewModels newJob.Rml = _machineDbContext.Rmls.FirstOrDefault(); newJob.WindingMethod = _machineDbContext.WindingMethods.FirstOrDefault(); newJob.SpoolType = _machineDbContext.SpoolTypes.FirstOrDefault(); + newJob.ColorSpace = _machineDbContext.ColorSpaces.FirstOrDefault(); newJob.Machine = SelectedMachine; SelectedMachine.Jobs.Add(newJob); + newJob.AddSolidSegment(); LogManager.Log("Saving selected machine to database..."); await SelectedMachine.SaveAsync(_machineDbContext); SelectedMachineJob = newJob; LoadSelectedJob(); - AddSegment(); } } } @@ -1769,7 +1897,7 @@ namespace Tango.MachineStudio.Developer.ViewModels stop.OffsetPercent = 100; stop.Segment = SelectedSegment; - stop.ColorSpace = _activeJobDbContext.ColorSpaces.FirstOrDefault(); + stop.ColorSpace = ColorSpaces.FirstOrDefault(); stop.Color = Colors.Black; stop.SetLiquidVolumes(SelectedMachine.Configuration, SelectedRML, SelectedProcessParametersTable); SelectedSegment.BrushStops.Add(stop); |
