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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
using Tango.BL;
using Tango.BL.Builders;
using Tango.BL.Entities;
using Tango.Core.DI;
using Tango.Integration.ExternalBridge;
using Tango.Integration.Operation;
using Tango.PPC.Common;
using Tango.PPC.Common.Application;
using Tango.PPC.Common.Authentication;
using Tango.PPC.Common.ExternalBridge;
using Tango.PPC.Common.Modules;
using Tango.PPC.Common.Navigation;
using Tango.PPC.Common.Notifications;
using Tango.PPC.Common.WatchDog;
using Tango.PPC.UI.Dialogs;
using Tango.SharedUI;
using System.Data.Entity;
namespace Tango.PPC.UI.ViewModels
{
/// <summary>
/// Represents the PPC main view model.
/// </summary>
/// <seealso cref="Tango.PPC.Common.PPCViewModel" />
public class MainViewVM : PPCViewModel
{
private DispatcherTimer _date_timer;
private bool _isPowerUpDialogShown;
private DateTime _currentDateTime;
/// <summary>
/// Gets or sets the current date time.
/// </summary>
public DateTime CurrentDateTime
{
get { return _currentDateTime; }
set { _currentDateTime = value; RaisePropertyChangedAuto(); }
}
public MainViewVM()
{
_date_timer = new DispatcherTimer();
_date_timer.Interval = TimeSpan.FromSeconds(1);
_date_timer.Tick += _date_timer_Tick;
_date_timer.Start();
}
/// <summary>
/// Called when the application has been started.
/// </summary>
public override void OnApplicationStarted()
{
}
public override void OnApplicationReady()
{
base.OnApplicationReady();
MachineProvider.MachineOperator.CartridgeValidationRequestReceived += MachineOperator_CartridgeValidationRequestReceived;
MachineProvider.MachineOperator.PowerUpStarted += MachineOperator_PowerUpStarted;
MachineProvider.MachineOperator.ThreadLoadingConfirmationRequired += MachineOperator_ThreadLoadingConfirmationRequired;
}
#region Event Handlers
/// <summary>
/// Handles the Tick event of the _date_timer.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
private void _date_timer_Tick(object sender, EventArgs e)
{
CurrentDateTime = DateTime.Now;
}
private void MachineOperator_CartridgeValidationRequestReceived(object sender, CartridgeValidationEventArgs e)
{
InvokeUI(async () =>
{
var vm = await NotificationProvider.ShowDialog<CartridgeValidationViewVM>(new CartridgeValidationViewVM()
{
IDSPacks = MachineProvider.Machine.Configuration.NoneEmptyIdsPacks.ToList(),
});
if (vm.DialogResult)
{
e.Approve(vm.SelectedIDSPack.PackIndex);
}
else
{
e.Decline();
}
});
}
private async void MachineOperator_PowerUpStarted(object sender, EventArgs e)
{
if (_isPowerUpDialogShown)
{
LogManager.Log("Power up detected but power up dialog is already shown. Skipping...");
return;
}
LogManager.Log("Power up detected, showing power up screen...");
if (!Settings.DisplayPowerUpScreen)
{
LogManager.Log("Power up screen disabled. skipping...");
return;
}
PowerUpViewVM vm;
try
{
LogManager.Log("Loading site rmls...");
List<Rml> rmls = new List<Rml>();
using (ObservablesContext db = ObservablesContext.CreateDefault())
{
rmls = await new RmlsCollectionBuilder(db).SetAll().ForSite(MachineProvider.Machine.SiteGuid).BuildListAsync();
}
var selectedRml = rmls.SingleOrDefault(x => x.Guid == Settings.LoadedRmlGuid);
vm = new PowerUpViewVM();
vm.Rmls = rmls;
vm.SelectedRml = selectedRml != null ? selectedRml : rmls.FirstOrDefault();
vm.IsSelectedRml = selectedRml != null;
}
catch (Exception ex)
{
LogManager.Log(ex, "Error initializing power up screen.");
return;
}
InvokeUI(async () =>
{
_isPowerUpDialogShown = true;
await NotificationProvider.ShowDialog<PowerUpViewVM>(vm);
_isPowerUpDialogShown = false;
await Task.Factory.StartNew(() =>
{
LogManager.Log("Power up screen closed.");
try
{
using (ObservablesContext db = ObservablesContext.CreateDefault())
{
List<ProcessParametersTable> processTables = new List<ProcessParametersTable>();
if (vm.IsSelectedRml)
{
LogManager.Log($"Selected rml '{vm.SelectedRml.Name}'...");
processTables = new RmlBuilder(db).Set(vm.SelectedRml.Guid).WithActiveParametersGroup().Build().GetActiveProcessGroup().ProcessParametersTables.ToList();
}
else
{
LogManager.Log("Selected minimal temperature...");
var rmlsToAvg = new RmlsCollectionBuilder(db).SetAll().ForSite(MachineProvider.Machine.SiteGuid).WithActiveParametersGroup().Build();
processTables = rmlsToAvg.Select(x => x.GetActiveProcessGroup()).SelectMany(x => x.ProcessParametersTables).ToList();
}
var processToLoad = processTables.OrderBy(x => x.GetAverageTemperature()).First();
LogManager.Log($"Selected process parameters:\nRML: {processToLoad.ProcessParametersTablesGroup.Rml.Name}\nGroup: {processToLoad.ProcessParametersTablesGroup.Name}\nProcess Table: {processToLoad.Name}");
LogManager.Log("Uploading process parameters...");
var r = MachineProvider.MachineOperator.UploadProcessParameters(processToLoad).Result;
Settings.LoadedRmlGuid = vm.IsSelectedRml ? vm.SelectedRml.Guid : null;
Settings.Save();
}
}
catch (Exception ex)
{
LogManager.Log(ex, "Error occurred while trying to get and upload the proper process parameters after power screen closed.");
}
});
});
}
private async void MachineOperator_ThreadLoadingConfirmationRequired(object sender, ThreadLoadingConfirmationRequiredEventArgs e)
{
LogManager.Log("Thread loading confirmation detected, showing thread loading screen...");
if (!Settings.DisplayAutomaticThreadLoadingScreen)
{
LogManager.Log("Thread loading screen disabled. skipping...");
return;
}
ThreadLoadingViewVM vm;
try
{
LogManager.Log("Loading site rmls...");
List<Rml> rmls = new List<Rml>();
using (ObservablesContext db = ObservablesContext.CreateDefault())
{
rmls = await new RmlsCollectionBuilder(db).SetAll().ForSite(MachineProvider.Machine.SiteGuid).WithActiveParametersGroup().BuildListAsync();
}
var selectedRml = rmls.SingleOrDefault(x => x.Guid == Settings.LoadedRmlGuid);
vm = new ThreadLoadingViewVM(MachineProvider, e);
vm.Rmls = rmls;
vm.SelectedRml = selectedRml != null ? selectedRml : rmls.FirstOrDefault();
}
catch (Exception ex)
{
LogManager.Log(ex, "Error initializing thread loading screen.");
return;
}
InvokeUI(async () =>
{
await NotificationProvider.ShowDialog<ThreadLoadingViewVM>(vm);
LogManager.Log("Thread loading screen closed.");
if (!vm.DialogResult)
{
LogManager.Log("Thread loading screen aborted by user. No operation was performed.");
return;
}
try
{
if (vm.Result.IsCompleted)
{
await NotificationProvider.ShowSuccess("Thread loading completed successfully.");
}
else
{
await NotificationProvider.ShowError($"Thread loading failed due to the following reason:\n{vm.Result.FailedException.FlattenException()}");
}
if (vm.SelectedRml != null)
{
Settings.LoadedRmlGuid = vm.SelectedRml.Guid;
Settings.Save();
}
}
catch (Exception ex)
{
LogManager.Log(ex, "Error occurred after thread loading screen closed.");
}
});
}
#endregion
}
}
|