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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.AutoComplete.Editors;
using Tango.BL;
using Tango.BL.ActionLogs;
using Tango.BL.Builders;
using Tango.BL.DTO;
using Tango.BL.Entities;
using Tango.BL.Enumerations;
using Tango.Core;
using Tango.Core.Commands;
using Tango.Logging;
using Tango.MachineStudio.Common;
using Tango.MachineStudio.Common.Notifications;
using Tango.MachineStudio.ThreadExtensions.Models;
using Tango.Settings;
using Tango.SharedUI;
namespace Tango.MachineStudio.ThreadExtensions.ViewModels
{
public class TestResultsViewVM : ViewModel
{
private INotificationProvider _notification;
private IActionLogManager _actionLogManager;
private ObservablesContext _active_context;
public event EventHandler SaveTestResults;
#region Properties
private SynchronizedObservableCollection<RmlExtensionTestResult> _selectedTestResults;
public SynchronizedObservableCollection<RmlExtensionTestResult> SelectedTestResults
{
get { return _selectedTestResults; }
set { _selectedTestResults = value; }
}
private ObservableCollection<TestResultViewVM> _resultTabs;
public ObservableCollection<TestResultViewVM> ResultTabs
{
get { return _resultTabs; }
set { _resultTabs = value; }
}
private TestResultViewVM _selectedTab;
/// <summary>
/// Gets or sets the selected tab.
/// </summary>
public TestResultViewVM SelectedTab
{
get { return _selectedTab; }
set
{
_selectedTab = value;
RaisePropertyChangedAuto();
foreach (var tab in ResultTabs.Where(x => x != _selectedTab))
{
tab.IsSelected = false;
}
if (_selectedTab != null)
{
_selectedTab.IsSelected = true;
}
//EnableRenderingForSelectedTabGraphs();
}
}
protected string _selectedMachineGuid;
/// <summary>
/// Gets or sets the selected machine.
/// </summary>
public String SelectedMachineGUID
{
get { return _selectedMachineGuid; }
set
{
if (value != null && _selectedMachineGuid != value)
{
_selectedMachineGuid = value;
SelectedMachineChanged();
RaisePropertyChangedAuto();
InvalidateRelayCommands();
}
}
}
private string _RMLExtentionGUID;
public string RMLExtemtionGUID
{
get { return _RMLExtentionGUID; }
set { _RMLExtentionGUID = value;
OnRMLExtemtionGUIDChanged();
}
}
private string _threadName;
public string ThreadName
{
get { return _threadName; }
set { _threadName = value;
RaisePropertyChangedAuto(); }
}
private string _RMLGUID;
public string RMLGUID
{
get { return _RMLGUID; }
set
{
_RMLGUID = value;
}
}
#endregion
#region Commands
/// <summary>
/// Gets or sets the add tab command.
/// </summary>
public RelayCommand AddTabCommand { get; set; }
/// <summary>
/// Gets or sets the remove tab command.
/// </summary>
public RelayCommand<TestResultViewVM> RemoveTabCommand { get; set; }
/// <summary>
/// Gets or sets the rename tab command.
/// </summary>
public RelayCommand RenameTabCommand { get; set; }
public RelayCommand ApplyToProcessParametersCommand { get; set; }
public RelayCommand SaveCommand { get; set; }
#endregion
public TestResultsViewVM(INotificationProvider notification, IActionLogManager actionLogManager)
{
_notification = notification;
_actionLogManager = actionLogManager;
ResultTabs = new ObservableCollection<TestResultViewVM>();
AddTabCommand = new RelayCommand(() => AddNewTab());
RemoveTabCommand = new RelayCommand<TestResultViewVM>(RemoveTab);
RenameTabCommand = new RelayCommand(RenameTab);
ApplyToProcessParametersCommand = new RelayCommand(ApplyToProcessParameters);
SaveCommand = new RelayCommand(Save, () => IsFree);
}
#region Tabs modification
/// <summary>
/// Removes the specified tab.
/// </summary>
/// <param name="tab">The tab.</param>
private void RemoveTab(TestResultViewVM tab)
{
if (ResultTabs.Count == 1)
return;
if (_notification.ShowQuestion("Are you sure you want to delete the selected tab?"))
{
_active_context.RubbingResults.RemoveRange(tab.TestResult.RubbingResults);
tab.TestResult.RubbingResults = null;
_active_context.TensileResults.RemoveRange(tab.TestResult.TensileResults);
tab.TestResult.TensileResults = null;
_active_context.RmlExtensionTestResults.Remove(tab.TestResult);
ResultTabs.Remove(tab);
SelectedTab = ResultTabs.LastOrDefault();
_active_context.SaveChanges();
}
}
/// <summary>
/// Adds a new tab.
/// </summary>
private bool AddNewTab(String name = null)
{
if (ResultTabs.Count > 7)
{
//_notification.ShowError("Cannot exceed the maximum number of 8 tabs. You can remove a tab, or create a new project.");
return false;
}
if (name == null)
{
name = _notification.ShowTextInput("Enter tab name", "Tab Name", "Test");
}
if (!String.IsNullOrWhiteSpace(name))
{
var tab = CreateNewTestResultVM(name, (ResultTabs.Count + 1));
ResultTabs.Add(tab);
_active_context.RmlExtensionTestResults.Add(tab.TestResult);
SelectedTab = tab;
return true;
}
else
{
return false;
}
}
/// <summary>
/// Renames the tab.
/// </summary>
/// <param name="tab">The tab.</param>
private void RenameTab()
{
if (SelectedTab != null)
{
var name = _notification.ShowTextInput("Enter tab name", "Tab Name", SelectedTab.TestResult.Name);
if (!String.IsNullOrWhiteSpace(name))
{
SelectedTab.TestResult.Name = name;
}
}
}
#endregion
#region Loading test results
private void OnRMLExtemtionGUIDChanged()
{
ResultTabs.Clear();
SelectedTab = null;
}
private void SelectedMachineChanged()
{
ResultTabs.Clear();
SelectedTab = null;
LoadTestResults();
}
public async void LoadTestResults()
{
if (String.IsNullOrEmpty(SelectedMachineGUID))
{
_notification.ShowWarning(LogManager.Log($" Please, select machine.", LogCategory.Warning));
return;
}
try
{
IsFree = false;
if (_active_context != null)
{
_active_context.Dispose();
}
_active_context = ObservablesContext.CreateDefault();
ResultTabs.Clear();
LogManager.Log("Loading selected test results...");
using (_notification.PushTaskItem("Loading Test Results Parameters ..."))
{
var testResults = await new RMLExtensionTestResultsCollectionBuilder(_active_context).SetAll().ForRMLExtension(RMLExtemtionGUID).ForMachine(SelectedMachineGUID).WithRubbingAndTensileResults().BuildAsync();
SelectedTestResults = testResults.OrderBy(x => x.ResultIndex).ToSynchronizedObservableCollection();
foreach (var result in SelectedTestResults)
{
ResultTabs.Add(new TestResultViewVM(_notification, _actionLogManager) { TestResult = result, ThreadName = ThreadName });
if (result.ResultIndex == 1)
{
SelectedTab = ResultTabs[ResultTabs.Count - 1];
}
}
if (ResultTabs.Count == 0)
{
SelectedTab = CreateNewTestResultVM("Untitled", 1);
ResultTabs.Add(SelectedTab);
_active_context.RmlExtensionTestResults.Add(SelectedTab.TestResult);
await _active_context.SaveChangesAsync();
}
}
IsFree = true;
}
catch (Exception ex)
{
LogManager.Log(ex, $"Error loading TestResults.\n{ex.FlattenMessage()}");
}
finally
{
IsFree = true;
}
}
private TestResultViewVM CreateNewTestResultVM(string name, int index)
{
TestResultViewVM newtab = new TestResultViewVM(_notification, _actionLogManager) { ThreadName = ThreadName };
newtab.TestResult = new RmlExtensionTestResult() { RmlsExtensionsGuid = RMLExtemtionGUID, MachineGuid = SelectedMachineGUID, ResultIndex = index, Name = name, BtsrMax = 0.0, BtsrMin = 0.0, DryerTemperature = 0, TunnelTemperature = 0, TunnelFlow = 0.0, TunnelAvgTemperature = 0.0, TensionHeadMax = 0.0,
TensionHeadMin = 0.0, TensioinAfterDryerMax = 0.0, TensionAfterDryerMin = 0.0, TensionWinderMax = 0.0, TensionWinderMin = 0.0, PullerTensionMax = 0.0, PullerTensionMin = 0.0, ExitTensionMax = 0.0, ExitTensionMin = 0.0, SeverityZone1Max = 0.0, SeverityZone1Min = 0.0, SeverityZone2Max = 0.0, SeverityZone2Min = 0.0,
RefLubVersion="", RefCof = 0.0, RefLub = 0.0, ThreadLubVersion = "", ThreadCof = 0.0, ThreadLub = 0.0, Conclusions="", Comment=""};
var rubbingresults = new SynchronizedObservableCollection<RubbingResult>();
rubbingresults.Add(new RubbingResult() { RmlExtensionTestResultsGuid = newtab.TestResult.Guid, TestResultColor = TestResultColors.CYAN });
rubbingresults.Add(new RubbingResult() { RmlExtensionTestResultsGuid = newtab.TestResult.Guid, TestResultColor = TestResultColors.MAGENTA });
rubbingresults.Add(new RubbingResult() { RmlExtensionTestResultsGuid = newtab.TestResult.Guid, TestResultColor = TestResultColors.YELLOW });
rubbingresults.Add(new RubbingResult() { RmlExtensionTestResultsGuid = newtab.TestResult.Guid, TestResultColor = TestResultColors.BLACK });
newtab.TestResult.RubbingResults = rubbingresults;
var tensileresults = new SynchronizedObservableCollection<TensileResult>();
tensileresults.Add(new TensileResult() { RmlExtensionTestResultsGuid = newtab.TestResult.Guid, TestResultColor = TestResultColors.CYAN, ColorPercent = 100 });
tensileresults.Add(new TensileResult() { RmlExtensionTestResultsGuid = newtab.TestResult.Guid, TestResultColor = TestResultColors.BLACK, ColorPercent = 100 });
tensileresults.Add(new TensileResult() { RmlExtensionTestResultsGuid = newtab.TestResult.Guid, TestResultColor = TestResultColors.CYAN, ColorPercent = 200 });
tensileresults.Add(new TensileResult() { RmlExtensionTestResultsGuid = newtab.TestResult.Guid, TestResultColor = TestResultColors.BLACK, ColorPercent = 200 });
tensileresults.Add(new TensileResult() { RmlExtensionTestResultsGuid = newtab.TestResult.Guid, TestResultColor = TestResultColors.WHITE, ColorPercent = null });
newtab.TestResult.TensileResults = tensileresults;
return newtab;
}
#endregion
#region Save
public async void ApplyToProcessParameters()
{
try {
if (RMLGUID == null)
return;
using (var context = ObservablesContext.CreateDefault())
{
var rml = await new RmlBuilder(context)
.Set(RMLGUID)
.WithActiveParametersGroup()
.BuildAsync();
if (rml == null || rml.ProcessParametersTablesGroups.ToList().Count == 0)
{
_notification.ShowError("Could not save process parameters an RML with no process group.");
return;
}
var activeProcessParametersGroup = rml.ProcessParametersTablesGroups.ToList().SingleOrDefault(x => x.Active);
if(activeProcessParametersGroup != null)
{
var processParametersTables = activeProcessParametersGroup.ProcessParametersTables.OrderBy(x => x.TableIndex).ToSynchronizedObservableCollection().FirstOrDefault();
processParametersTables.DryerZone1Temp = SelectedTab.TestResult.DryerTemperature == null? 0 : (double)SelectedTab.TestResult.DryerTemperature;
processParametersTables.RBlowerFlow = SelectedTab.TestResult.TunnelFlow == null ? 0 : (double)SelectedTab.TestResult.TunnelFlow;
processParametersTables.RBlowerTemp = SelectedTab.TestResult.TunnelFlow == null ? 0 : (double)SelectedTab.TestResult.TunnelFlow; ;
processParametersTables.LBlowerFlow = SelectedTab.TestResult.TunnelTemperature == null ? 0 : (double)SelectedTab.TestResult.TunnelTemperature; ;
processParametersTables.LBlowerTemp = SelectedTab.TestResult.TunnelTemperature == null ? 0 : (double)SelectedTab.TestResult.TunnelTemperature; ;
await context.SaveChangesAsync();
}
}
}
catch(Exception ex)
{
LogManager.Log(ex, "Could not save process parameters.");
_notification.ShowError($"An error occurred while trying to save process parameters.\n{ex.Message}");
}
}
public async void Save()
{
if (String.IsNullOrEmpty(SelectedMachineGUID))
{
_notification.ShowWarning(LogManager.Log($"Could not save Test Results. Please, select machine.", LogCategory.Warning));
return;
}
try
{
IsFree = false;
await Task.Factory.StartNew(() =>
{
foreach (var tab in ResultTabs)
{
tab.TestResult.LastUpdated = DateTime.UtcNow;
}
_active_context.SaveChanges();
});
LoadTestResults();
}
catch (Exception ex)
{
LogManager.Log(ex, "Could not save test results.");
_notification.ShowError($"An error occurred while trying to save test results.\n{ex.Message}");
}
finally
{
IsFree = true;
EventHandler handler = SaveTestResults;
handler?.Invoke(this, new EventArgs());
}
}
#endregion
}
}
|