blob: 7d2d8b401b09103366f7ce10e51ed60222dc8053 (
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
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
|
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Tango.Core.Commands;
using Tango.Integration.Operation;
using Tango.Integration.Upgrade;
using Tango.MachineStudio.Common.Notifications;
using Tango.SharedUI;
using Tango.SharedUI.Helpers;
namespace Tango.MachineStudio.UI.ViewModels
{
public class FirmwareUpgradeViewVM : DialogViewVM
{
private IMachineOperator _operator;
private INotificationProvider _notification;
private FileStream _stream;
private FirmwareUpgradeHandler _handler;
public FirmwareUpgradeHandler Handler
{
get { return _handler; }
set { _handler = value; RaisePropertyChangedAuto(); }
}
private String _selectedFile;
public String SelectedFile
{
get { return _selectedFile; }
set { _selectedFile = value; RaisePropertyChangedAuto(); }
}
private int _currentPage;
public int CurrentPage
{
get { return _currentPage; }
set { _currentPage = value; RaisePropertyChangedAuto(); }
}
private String upgradeError;
public String UpgradeError
{
get { return upgradeError; }
set { upgradeError = value; RaisePropertyChangedAuto(); }
}
private bool _dfu;
public bool DFU
{
get { return _dfu; }
set
{
_dfu = value; RaisePropertyChangedAuto();
if (_dfu)
{
UploadTFP = false;
DFUAndTFP = false;
}
}
}
private bool _uploadTFP;
public bool UploadTFP
{
get { return _uploadTFP; }
set
{
_uploadTFP = value; RaisePropertyChangedAuto();
if (_uploadTFP)
{
DFU = false;
DFUAndTFP = false;
}
}
}
private bool _dfuAndTFP;
public bool DFUAndTFP
{
get { return _dfuAndTFP; }
set
{
_dfuAndTFP = value; RaisePropertyChangedAuto();
if (_dfuAndTFP)
{
DFU = false;
UploadTFP = false;
}
}
}
public RelayCommand SelectCommand { get; set; }
public RelayCommand UpgradeCommand { get; set; }
public RelayCommand AbortCommand { get; set; }
public FirmwareUpgradeViewVM(IMachineOperator machineOperator, INotificationProvider notificationProvider) : base()
{
DFU = true;
_notification = notificationProvider;
_operator = machineOperator;
SelectCommand = new RelayCommand(BrowseForFile);
UpgradeCommand = new RelayCommand(StartUpgrade, () => SelectedFile != null);
AbortCommand = new RelayCommand(AbortUpgrade, () => Handler != null && Handler.Status != FirmwareUpgradeStatus.Validating && Handler.Status != FirmwareUpgradeStatus.Activating);
}
private async void BrowseForFile()
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "Select tango firmware package file";
dlg.Filter = "Tango Firmware Package|*.tfp";
if (dlg.ShowDialog().Value)
{
try
{
using (FileStream fs = new FileStream(dlg.FileName, FileMode.Open))
{
var info = await _operator.GetFirmwarePackageInfo(fs);
}
}
catch (Exception ex)
{
LogManager.Log(ex);
_notification.ShowError("The selected package seems to be invalid.");
return;
}
SelectedFile = dlg.FileName;
InvalidateRelayCommands();
}
}
private async void StartUpgrade()
{
CanClose = false;
CurrentPage = 1;
try
{
IsFree = false;
if (DFU)
{
_operator.FirmwareUpgradeMode = FirmwareUpgradeModes.DFU;
}
else if (UploadTFP)
{
_operator.FirmwareUpgradeMode = FirmwareUpgradeModes.TFP_PACKAGE;
}
else if (DFUAndTFP)
{
_operator.FirmwareUpgradeMode = FirmwareUpgradeModes.DFU | FirmwareUpgradeModes.TFP_PACKAGE;
}
_stream = new FileStream(SelectedFile, FileMode.Open);
Handler = await _operator.UpgradeFirmware(_stream);
Handler.Progress += (_, e) =>
{
InvokeUI(() =>
{
AbortCommand.RaiseCanExecuteChanged();
});
UIHelper.DoEvents();
};
Handler.Completed += (_, __) =>
{
CanClose = true;
_stream.Dispose();
CurrentPage = 2;
IsFree = true;
};
Handler.Canceled += (_, __) =>
{
CanClose = true;
_stream.Dispose();
CurrentPage = 0;
IsFree = true;
};
Handler.Failed += (_, ex) =>
{
UpgradeError = ex.FlattenMessage();
CanClose = true;
_stream.Dispose();
CurrentPage = 3;
IsFree = true;
};
}
catch (Exception ex)
{
IsFree = true;
CanClose = true;
_stream?.Dispose();
UpgradeError = ex.FlattenMessage();
CurrentPage = 3;
}
}
protected override bool CanOK()
{
return base.CanOK() && CanClose;
}
private async void AbortUpgrade()
{
CanClose = true;
await Handler.Cancel();
CurrentPage = 0;
}
}
}
|