-
Notifications
You must be signed in to change notification settings - Fork 3
/
ViewModel.cs
319 lines (273 loc) · 10 KB
/
ViewModel.cs
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
using OxyPlot;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Threading;
namespace DesktopApp
{
//ViewModel class
public class ViewModel : INotifyPropertyChanged
{
private readonly AnomalyData _data; //AnomalyData Model.
private readonly Panel _panel; //Panel Model.
private string _trainPath; //the train csv file path.
private string _testPath; //the test csv file path.
private string _trainFile; //the entire train file
private string _testFile; //the entire test file
private Thread _th;
//the Title above the DLL's graphs.
public string VM_DllTitle => _data._DllTitle;
//the DLL's PlotModel.
public PlotModel VM_DllPlotModel => _data._DllPlotModel;
//list of all the XML features.
public List<string> VM_AllFeatures => _data._AllFeatures;
//the current selected column.
public string VM_CurrColumn
{
get => _data._CurrColumn;
set => _data._CurrColumn = value;
}
//the current correlated column.
public string VM_CorrelatedColumn
{
get => _data._CorrelatedColumn;
set => _data._CorrelatedColumn = value;
}
//list of the current column Points.
public List<DataPoint> VM_CurrentDataPoints => new List<DataPoint>(_data._CurrentDataPoints);
//list of the most correlated column Points.
public List<DataPoint> VM_CorrelatedDataPoints => new List<DataPoint>(_data._CorrelatedDataPoints);
//Stop sending
public void CloseAll()
{
_panel.Close();
}
public float VM_Throttle
{
get => _data._Throttle;
set => _data._Throttle = value;
}
public float VM_Rudder
{
get => _data._Rudder;
set => _data._Rudder = value;
}
public float VM_Elevator
{
get => ConvertToJoy(_data._Elevator);
set => _data._Elevator = value;
}
public float VM_Aileron
{
get => ConvertToJoy(_data._Aileron);
set => _data._Aileron = value;
}
public float VM_AirSpeed
{
get => _data._AirSpeed;
set => _data._AirSpeed = value;
}
public float VM_Roll
{
get => _data._Roll;
set => _data._Roll = value;
}
public float VM_Pitch
{
get => _data._Pitch;
set => _data._Pitch = value;
}
public float VM_Alimeter
{
get => _data._Alimeter;
set => _data._Alimeter = value;
}
public float VM_Yaw
{
get => _data._Yaw;
set => _data._Yaw = value;
}
public float VM_Direction
{
get => _data._Direction;
set => _data._Direction = value;
}
//list of the current column and the most correlative column Points.
public List<DataPoint> VM_MultiDataPoints => new List<DataPoint>(_data._MultiDataPoints);
//list of the 10 last current column and the most correlative column Points.
public List<DataPoint> VM_LastMultiDataPoints => new List<DataPoint>(_data._LastMultiDataPoints);
//list of 2 points declaring the regression line.
public List<DataPoint> VM_LinearReg => new List<DataPoint>(_data._LinearReg);
public event PropertyChangedEventHandler PropertyChanged;
//the line number that currently being sent.
public double VM_NumLine
{
get => _panel._NumLine;
set => _panel._NumLine = (int) (value + 1);
}
//number of rows.
public double VM_LinesN
{
get => _panel._LinesN;
set => _panel._LinesN = (int) value;
}
//Constructor
public ViewModel(Panel p, AnomalyData data)
{
_data = data;
_panel = p;
_panel.PropertyChanged +=
delegate(object sender, PropertyChangedEventArgs e) { NotifyPropertyChanged("VM" + e.PropertyName); };
_panel.PropertyChanged +=
delegate(object sender, PropertyChangedEventArgs e) { NotifyNumLineChanged("VM" + e.PropertyName); };
_data.PropertyChanged +=
delegate(object sender, PropertyChangedEventArgs e) { NotifyPropertyChanged("VM" + e.PropertyName); };
VM_Aileron = 100;
VM_Elevator = 100;
VM_Rudder = -1;
VM_Throttle = 0;
}
private void NotifyPropertyChanged(string propName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
private void NotifyNumLineChanged(string propName)
{
if (PropertyChanged == null) return;
if (propName == "VM_Num_line")
{
_data._NumLine = _panel._NumLine;
}
}
//open file dialog to upload train csv.
public bool ClickCsvTrain()
{
var openFileDialog1 = new Microsoft.Win32.OpenFileDialog {Filter = "CSV (*.csv)|*.csv|All Files (*.*)|*.*"};
if (openFileDialog1.ShowDialog() != true) return false;
_trainPath = openFileDialog1.FileName;
return true;
}
//open file dialog to upload test csv.
public bool ClickCsvTest()
{
var openFileDialog1 = new Microsoft.Win32.OpenFileDialog {Filter = "CSV (*.csv)|*.csv|All Files (*.*)|*.*"};
if (openFileDialog1.ShowDialog() != true) return false;
_testPath = openFileDialog1.FileName;
if (_th != null)
_panel.SetPath(_testPath);
else
{
_th = new Thread(() => _panel.CSV_Sender(_testPath));
_th.Start();
}
return true;
}
//upload the files to the Model, and learn the test.
public void LearnProcess()
{
var file = File.ReadAllText(_trainPath);
var features = "";
for (var i = 0; i < VM_AllFeatures.Count; i++)
{
var col = VM_AllFeatures[i];
features += col;
if (i != VM_AllFeatures.Count - 1)
{
features += ",";
}
}
features += "\r\n";
_trainFile = features + file;
_data.UploadTrain(_trainFile); // send AnomalyData string of the file
file = File.ReadAllText(_testPath);
_testFile = features + file;
_data.UploadTest(_testFile);
_data.Learn();
}
//open file dialog to upload the xml file.
public void ClickXml()
{
var openFileDialog1 = new Microsoft.Win32.OpenFileDialog {Filter = "XML (*.xml)|*.xml|All Files (*.*)|*.*"};
if (openFileDialog1.ShowDialog() != true) return;
var path = openFileDialog1.FileName;
_data.AnalyzeXml(path);
}
//the Panel controls buttons
public void PlaybackControl(MediaEventArgs m)
{
var description = m.GetDescription();
switch (description)
{
case "submit":
_panel.SetSpeed(m.GetValue());
break;
case "skip back":
VM_NumLine = 0;
break;
case "back" when VM_NumLine >= 75:
VM_NumLine -= 75;
break;
case "back":
VM_NumLine = 0;
break;
case "pause":
_panel.Pause();
break;
case "stop":
VM_NumLine = 0;
Thread.Sleep(10);
_panel.Pause();
break;
case "play":
_panel.Play();
break;
case "forward" when VM_NumLine < VM_LinesN - 75:
VM_NumLine += 75;
break;
case "forward":
case "skip end":
VM_NumLine = VM_LinesN - 1;
break;
}
}
//open file dialog to upload the DLL.
private void ClickDll()
{
var openFileDialog1 = new Microsoft.Win32.OpenFileDialog {Filter = "DLL (*.dll)|*.dll|All Files (*.*)|*.*"};
if (openFileDialog1.ShowDialog() != true) return;
var path = openFileDialog1.FileName;
_data.PathSetter(path);
_data.Start();
_data.Draw();
}
//list of anomalies.
public List<string> VM_DllAnomaliesList => _data._DllAnomaliesList;
//convert anomalies string to their index.
private void SelectedAnomaly(string v)
{
VM_NumLine = _data.GetTimeStepFromString(v);
}
//denormalize the Joystick value
private static int ConvertToJoy(float value)
{
const int oldRange = 2;
const int newRange = 90;
var newValue = (int) (((value - (-1)) * newRange) / oldRange) + 60;
return newValue;
}
public void GraphControls(GraphEventArgs m)
{
var description = m.GetDescription();
switch (description)
{
case "ClickDLL":
ClickDll();
break;
case "SelectedAnomaly":
SelectedAnomaly(m.GetValue());
break;
}
}
}
}