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 _sites; public List Sites { get { return _sites; } set { _sites = value; RaisePropertyChangedAuto(); } } private Site _selectedSite; public Site SelectedSite { get { return _selectedSite; } set { _selectedSite = value; RaisePropertyChangedAuto(); } } public SettingsViewVM() { RegisterForMessage(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() { try { using (NotificationProvider.PushTaskItem("Saving machine configuration...")) { EditingComposition.Machine.SiteGuid = SelectedSite.Guid; EditingComposition = await Services.MachineConfigurationService.SaveMachineEditingComposition(EditingComposition); await NotificationProvider.ShowSuccess("Machine configuration saved successfully."); } } catch (Exception ex) { LogManager.Log(ex, "Error saving the machine configuration."); await NotificationProvider.ShowError($"Error saving the machine configuration.\n{ex.FlattenMessage()}"); } } 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; try { 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); EditingComposition.ResetCounters(); await NotificationProvider.ShowSuccess("Machine counters deleted successfully."); } } catch (Exception ex) { LogManager.Log(ex, "Error resetting machine counters."); await NotificationProvider.ShowError($"Error resetting the machine counters.\n{ex.FlattenMessage()}"); } } 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; } } }