diff options
| author | Mirta <mirta@twine-s.com> | 2020-11-23 16:13:53 +0200 |
|---|---|---|
| committer | Mirta <mirta@twine-s.com> | 2020-11-23 16:13:53 +0200 |
| commit | 91c007adced573e09b77ab4be4a5aba623a816cc (patch) | |
| tree | 250221fc2def7d59f1393be8394f766faf576656 /Software/Visual_Studio/FSE/Modules/Tango.FSE.MachineConfiguration/ViewModels/SettingsViewVM.cs | |
| parent | 4e9af2b852eb3b9eecfa09e9bc76869558e183cb (diff) | |
| parent | 50a3c0b857b4aa88a9e3970d69256f12b5b24eb8 (diff) | |
| download | Tango-91c007adced573e09b77ab4be4a5aba623a816cc.tar.gz Tango-91c007adced573e09b77ab4be4a5aba623a816cc.zip | |
Merge branch 'master' of https://twinetfs.visualstudio.com/Tango/_git/Tango
Diffstat (limited to 'Software/Visual_Studio/FSE/Modules/Tango.FSE.MachineConfiguration/ViewModels/SettingsViewVM.cs')
| -rw-r--r-- | Software/Visual_Studio/FSE/Modules/Tango.FSE.MachineConfiguration/ViewModels/SettingsViewVM.cs | 144 |
1 files changed, 144 insertions, 0 deletions
diff --git a/Software/Visual_Studio/FSE/Modules/Tango.FSE.MachineConfiguration/ViewModels/SettingsViewVM.cs b/Software/Visual_Studio/FSE/Modules/Tango.FSE.MachineConfiguration/ViewModels/SettingsViewVM.cs new file mode 100644 index 000000000..ed2cbd714 --- /dev/null +++ b/Software/Visual_Studio/FSE/Modules/Tango.FSE.MachineConfiguration/ViewModels/SettingsViewVM.cs @@ -0,0 +1,144 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.BL.Entities; +using Tango.Core.Commands; +using Tango.Core.DI; +using Tango.FSE.BL; +using Tango.FSE.Common; +using Tango.FSE.Common.AutoComplete; +using Tango.FSE.Common.SQL; +using Tango.FSE.MachineConfiguration.Messages; +using static Tango.FSE.BL.Services.MachineConfigurationService; + +namespace Tango.FSE.MachineConfiguration.ViewModels +{ + public class SettingsViewVM : FSEViewModel + { + [TangoInject] + private IRemoteSqlProvider RemoteSqlProvider { get; set; } + + private MachineEditingComposition _editingComposition; + public MachineEditingComposition EditingComposition + { + get { return _editingComposition; } + set { _editingComposition = value; RaisePropertyChangedAuto(); } + } + + public RelayCommand SaveCommand { get; set; } + public RelayCommand ResetCountersCommand { get; set; } + public RelayCommand ResetDeviceRegistrationCommand { get; set; } + + private List<Site> _sites; + public List<Site> Sites + { + get { return _sites; } + set { _sites = value; RaisePropertyChangedAuto(); } + } + + private Site _selectedSite; + public Site SelectedSite + { + get { return _selectedSite; } + set { _selectedSite = value; RaisePropertyChangedAuto(); } + } + + public SettingsViewVM() + { + RegisterForMessage<EditingCompositionLoadedMessage>(OnEditingCompositionLoaded); + SaveCommand = new RelayCommand(SaveConfiguration); + ResetCountersCommand = new RelayCommand(ResetCounters); + ResetDeviceRegistrationCommand = new RelayCommand(ResetDeviceRegistration); + } + + private void OnEditingCompositionLoaded(EditingCompositionLoadedMessage msg) + { + using (NotificationProvider.PushTaskItem("Loading machine configuration...")) + { + EditingComposition = msg.EditingComposition; + EditingComposition.Machine.ForceVersionUpdateChanged += (x, value) => { if (value) EditingComposition.Machine.SuspendVersionUpdate = false; }; + EditingComposition.Machine.SuspendVersionUpdateChanged += (x, value) => { if (value) EditingComposition.Machine.ForceVersionUpdate = false; }; + EditingComposition.Machine.OrganizationChanged -= OnMachine_OrganizationChanged; + EditingComposition.Machine.OrganizationChanged += OnMachine_OrganizationChanged; + OnMachine_OrganizationChanged(EditingComposition.Machine, EditingComposition.Machine.Organization); + } + } + + private void OnMachine_OrganizationChanged(object sender, Organization organization) + { + var sites = organization.Sites.ToList(); + sites.Insert(0, new Site() { Guid = null, Name = "NONE", ID = -1 }); + Sites = sites; + SelectedSite = Sites.FirstOrDefault(x => x.Guid == EditingComposition.Machine.SiteGuid); + + if (SelectedSite == null) + { + SelectedSite = Sites.First(); + } + } + + private async void SaveConfiguration() + { + using (NotificationProvider.PushTaskItem("Saving machine configuration...")) + { + EditingComposition.Machine.SiteGuid = SelectedSite.Guid; + EditingComposition = await Services.MachineConfigurationService.SaveMachineEditingComposition(EditingComposition); + await NotificationProvider.ShowSuccess("Machine configuration saved successfully."); + } + } + + private async void ResetCounters() + { + if (!CurrentUser.HasPermission(Tango.BL.Enumerations.Permissions.FSE_ResetMachineCounters)) + { + await NotificationProvider.ShowError("The current user profile does not allow resetting the machine counters.\nPlease contact your administrator."); + return; + } + + if (!MachineProvider.IsPPCAvailable) + { + await NotificationProvider.ShowError("Resetting the machine counters requires an active connection to this machine."); + return; + } + + if (!await NotificationProvider.ShowWarningQuestion("Resetting the machine counters will delete the entire job runs history. Are you sure?", "RESET COUNTERS", "CANCEL")) return; + + using (var task = NotificationProvider.PushTaskItem("Resetting machine counters...")) + { + task.UpdateProgress("Resetting local machine counters..."); + + var result = await RemoteSqlProvider.ExecuteSqlCommandAsync(new RemoteSqlCommand() + { + Mode = RemoteSqlCommandMode.Local, + Timeout = 30, + SQL = "DELETE FROM JOB_RUNS" + }); + + int localJobRuns = result.LocalAffectedRecords; + + task.UpdateProgress("Resetting global machine counters..."); + + int remoteJobRuns = await Services.MachineConfigurationService.ResetCounters(EditingComposition.Machine.Guid); + + await NotificationProvider.ShowSuccess("Machine counters deleted successfully."); + } + } + + private async void ResetDeviceRegistration() + { + if (!CurrentUser.HasPermission(Tango.BL.Enumerations.Permissions.FSE_ResetMachineDeviceRegistration)) + { + await NotificationProvider.ShowError("The current user profile does not allow resetting the device registration.\nPlease contact your administrator."); + return; + } + + if (!await NotificationProvider.ShowWarningQuestion("Resetting the machine's device registration will allow other panel PCs to perform a machine setup for this machine. Are you sure?", "RESET", "CANCEL")) return; + + EditingComposition.Machine.IsDeviceRegistered = false; + EditingComposition.Machine.DeviceId = null; + EditingComposition.Machine.DeviceName = null; + } + } +} |
