/// <summary> /// Initializes a new instance of the MainViewModel class. /// </summary> public MainViewModel(IProcessManager processManager, DialogHandler dialogHandler) { IsEnable = false; Mouse.SetCursor(Cursors.Wait); _processManager = processManager; _dialogHandler = dialogHandler; if (Application.Current != null) { Application.Current.DispatcherUnhandledException += DispatcherUnhandledException; } if (Properties.Settings.Default.UpdateSettings) { Properties.Settings.Default.Upgrade(); Properties.Settings.Default.UpdateSettings = false; Properties.Settings.Default.Save(); } Messenger.Default.Register <StatusMessage>(this, sm => { Messages += $"{sm.Timestamp.ToString("yy.MM.dd. HH:mm:ss")} -- {sm.Message}{Environment.NewLine}"; }); Messenger.Default.Register <UpdateMessage>(this, message => { switch (message.UpdateType) { case UpdateType.NewProcessCreated: var currentProcess = (Process)message.Parameter; currentProcess.Start = currentProcess.Start.ToLocalTime(); currentProcess.End = currentProcess.End.ToLocalTime(); DispatcherHelper.CheckBeginInvokeOnUI(() => ActiveProcessesList.Add(currentProcess)); break; case UpdateType.OpenNewTab: var newTab = (HeaderedItemViewModel)message.Parameter; Tabs.Add(newTab); SelectedTab = newTab; break; case UpdateType.DatasetRename: var oldName = message.Parameter.ToString(); var tabsToClose = Tabs.Where(t => t.Header.ToString() == oldName + " -Data").ToList(); foreach (var tab in tabsToClose) { Tabs.Remove(tab); } break; } }); IsInSettingsMode = false; ChangeSettingsModeCommand = new RelayCommand(() => IsInSettingsMode = !IsInSettingsMode); RefreshCommand = new RelayCommand(async() => { Log.Info(LogMessages.MainRefreshCommand); await((ViewModelLocator)App.Current.Resources["Locator"]).EndpointUpdate(); }); AboutCommand = new RelayCommand(async() => { var version = Assembly.GetExecutingAssembly().GetName().Version; var sdkVersion = Assembly.LoadFrom("Slamby.SDK.Net.dll").GetName().Version; var apiVersion = Version.Parse(SimpleIoc.Default.GetInstance <ResourcesMonitorViewModel>().EndPointStatus.Status.ApiVersion); await _dialogHandler.Show(new CommonDialog { DataContext = new CommonDialogViewModel { Header = "About", Content = $"Tau version: {version.Major}.{version.Minor}.{version.Build}{Environment.NewLine}SDK version: {sdkVersion.Major}.{sdkVersion.Minor}.{sdkVersion.Build}{Environment.NewLine}Api version: {apiVersion.Major}.{apiVersion.Minor}.{apiVersion.Build}", Buttons = ButtonsEnum.Ok } }, "RootDialog"); }); HelpCommand = new RelayCommand(() => { System.Diagnostics.Process.Start("http://developers.slamby.com"); }); SizeChangedCommand = new RelayCommand(() => { if (LogWindowIsOpen) { LogWindowIsOpen = false; LogWindowIsOpen = true; } }); DoubleClickCommand = new RelayCommand(() => { object content = null; switch (SelectedMenuItem.Name.ToLower()) { case "datasets": content = new ManageDataSet(); break; case "services": content = new ManageService(); break; case "processes": content = new ManageProcess(); break; case "resourcesmonitor": content = new ResourcesMonitor(); break; } Messenger.Default.Send(new UpdateMessage(UpdateType.OpenNewTab, new HeaderedItemViewModel(SelectedMenuItem.Name, content, true))); }); ExpandStatusCommand = new RelayCommand(() => Messenger.Default.Send(new UpdateMessage(UpdateType.OpenNewTab, new HeaderedItemViewModel("ResourcesMonitor", new ResourcesMonitor(), true)))); RefreshProcessCommand = new RelayCommand <string>(async id => { if (string.IsNullOrEmpty(id)) { return; } try { var processResponse = await _processManager.GetProcessAsync(id); if (ResponseValidator.Validate(processResponse)) { var selectedItem = ActiveProcessesList.FirstOrDefault(p => p.Id == id); if (selectedItem != null) { ActiveProcessesList[ActiveProcessesList.IndexOf(selectedItem)] = processResponse.ResponseObject; ActiveProcessesList = new ObservableCollection <Process>(ActiveProcessesList); if (processResponse.ResponseObject.Status != ProcessStatusEnum.InProgress) { Task.Run(async() => { await Task.Delay(20000); DispatcherHelper.CheckBeginInvokeOnUI(() => { var itemToRemove = ActiveProcessesList.FirstOrDefault(p => p.Id == id); if (itemToRemove != null) { ActiveProcessesList.Remove(itemToRemove); } }); }); } } } } catch (Exception exception) { Messenger.Default.Send(exception); } }); CancelProcessCommand = new RelayCommand <Process>(async process => { var processResponse = await _processManager.CancelProcessAsync(process.Id); if (ResponseValidator.Validate(processResponse)) { var selectedItem = ActiveProcessesList.FirstOrDefault(p => p.Id == process.Id); if (selectedItem != null) { selectedItem.Status = ProcessStatusEnum.Cancelled; ActiveProcessesList = new ObservableCollection <Process>(ActiveProcessesList); Task.Run(async() => { await Task.Delay(20000); DispatcherHelper.CheckBeginInvokeOnUI(() => { var itemToRemove = ActiveProcessesList.FirstOrDefault(p => p.Id == process.Id); if (itemToRemove != null) { ActiveProcessesList.Remove(itemToRemove); } }); }); } } }); SelectionChangedCommand = new RelayCommand(() => { Mouse.SetCursor(Cursors.Wait); }); InitData(); Tabs = new ObservableCollection <HeaderedItemViewModel> { new HeaderedItemViewModel("DataSet", new ManageDataSet(), true) }; SelectedTab = Tabs.First(); }
/// <summary> /// Initializes a new instance of the ManageProcessViewModel class. /// </summary> public ManageProcessViewModel(IProcessManager processManager, DialogHandler dialogHandler) { _processManager = processManager; _dialogHandler = dialogHandler; Messenger.Default.Register <UpdateMessage>(this, message => { switch (message.UpdateType) { case UpdateType.NewProcessCreated: var processes = Processes.ToList(); var currentProcess = (Process)message.Parameter; currentProcess.Start = currentProcess.Start.ToLocalTime(); currentProcess.End = currentProcess.End.ToLocalTime(); processes.Add(currentProcess); DispatcherHelper.CheckBeginInvokeOnUI(() => Processes = new ObservableCollection <Process>(processes.OrderByDescending(p => p.Start))); break; } }); LoadedCommand = new RelayCommand(async() => { try { Mouse.SetCursor(Cursors.Arrow); SetGridSettings(); if (_loadedFirst && _processManager != null) { _loadedFirst = false; await _dialogHandler.ShowProgress(null, async() => { DispatcherHelper.CheckBeginInvokeOnUI(() => Processes.Clear()); Log.Info(LogMessages.ManageProcessLoadProcesses); var response = await _processManager.GetProcessesAsync(true); if (ResponseValidator.Validate(response, false)) { DispatcherHelper.CheckBeginInvokeOnUI(() => { if (response.ResponseObject.Any()) { var ordered = response.ResponseObject.OrderByDescending(p => p.Start); Processes = new ObservableCollection <Process>(ordered.Select(p => { p.Start = p.Start.ToLocalTime(); p.End = p.End.ToLocalTime(); return(p); })); RaisePropertyChanged("Processes"); } }); } }); } } catch (Exception exception) { Messenger.Default.Send(exception); } }); RefreshProcessCommand = new RelayCommand <string>(async id => { try { var processResponse = await _processManager.GetProcessAsync(id); if (ResponseValidator.Validate(processResponse, false)) { var selectedItem = Processes.FirstOrDefault(p => p.Id == id); if (selectedItem != null) { Processes[Processes.IndexOf(selectedItem)] = processResponse.ResponseObject; Processes = new ObservableCollection <Process>(Processes); } } } catch (Exception exception) { Messenger.Default.Send(exception); } }); CancelProcessCommand = new RelayCommand <Process>(async process => { try { var processResponse = await _processManager.CancelProcessAsync(process.Id); if (ResponseValidator.Validate(processResponse, false)) { var selectedItem = Processes.FirstOrDefault(p => p.Id == process.Id); if (selectedItem != null) { selectedItem.Status = ProcessStatusEnum.Cancelled; Processes = new ObservableCollection <Process>(Processes); } } } catch (Exception exception) { Messenger.Default.Send(exception); } }); ShowProcessDetailsCommand = new RelayCommand <Process>(async process => { if (process == null) { return; } var context = new CommonDialogViewModel { Header = "Process details", Buttons = ButtonsEnum.Ok, Content = new JContent(process) }; var view = new CommonDialog { DataContext = context }; await _dialogHandler.Show(view); }); }