blob: 8a4d20b767cdabff57dbd22803ec842c5b5cf97c (
plain)
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
|
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 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;
}
#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)
{
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().WithSite(MachineProvider.Machine.SiteGuid).BuildListAsync();
}
var selectedRml = rmls.SingleOrDefault(x => x.Guid == Settings.LastPowerUpSelectedRmlGuid);
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 () =>
{
await NotificationProvider.ShowDialog<PowerUpViewVM>(vm);
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().WithSite(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.LastPowerUpSelectedRmlGuid = 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.");
}
});
});
}
#endregion
}
}
|