1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
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()
{
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;
}
}
}
|