aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Utilities/Tango.DispenserAnalyzer.UI/ViewModels/MainWindowVM.cs
blob: d14587fd81b13796f3a267e52ce8dff165b0506a (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;
using Tango.Core.Commands;
using Tango.CSV;
using Tango.DispenserAnalyzer.UI.Models;
using LiveCharts;
using LiveCharts.Wpf;
using Tango.SharedUI;
using System.Collections.ObjectModel;
using System.IO;
using System.Windows.Input;
using Tango.DispenserAnalyzer.UI.Analysis;
using System.Reflection;
using Tango.DispenserAnalyzer.UI.Analyzers;

namespace Tango.DispenserAnalyzer.UI.ViewModels
{
    public class MainWindowVM: ViewModel
    {
        private string _openFilePath;
        public string OpenFilePath
        {
            get { return _openFilePath; }
            set
            {
                if(value != null && _openFilePath != value)
                {
                    PressedValuesCollection.Clear();
                    AnalyzerResult = null;
                    _openFilePath = value;
                    RaisePropertyChangedAuto();
                    GenerateCommand.RaiseCanExecuteChanged();
                }
            }
        }

        private string _testName;
        public string TestName
        {
            get { return _testName; }
            set { _testName = value; RaisePropertyChangedAuto(); }
        }

        private ObservableCollection<String> _labels;
        public ObservableCollection<String> Labels
        {
            get { return _labels; }
            set { _labels = value; RaisePropertyChangedAuto(); }
        }

        private int _step;
        public int XStep
        {
            get { return _step; }
            set { _step = value; RaisePropertyChangedAuto(); }
        }
        
        private SeriesCollection _pressedValuesCollection;
        public SeriesCollection PressedValuesCollection
        {
            get { return _pressedValuesCollection; }
            set
            {
                _pressedValuesCollection = value;
                RaisePropertyChangedAuto();
            }
        }

        private double _from;
        public double From
        {
            get { return _from; }
            set
            {
                _from = value; RaisePropertyChangedAuto();
            }
        }

        private double _to;
        public double To
        {
            get { return _to; }
            set
            {
                _to = value; RaisePropertyChangedAuto();
            }
        }

        private IAnalyzerResult analyzerResult;
        public IAnalyzerResult AnalyzerResult
        {
            get { return analyzerResult; }
            set { analyzerResult = value; RaisePropertyChangedAuto(); }
        }
        
        public Func<double, string> YFormatter { get; set; }

        public RelayCommand OpenCSVFileCommand { get; set; }
        public RelayCommand GenerateCommand { get; set; }

        public MainWindowVM()
        {
            OpenCSVFileCommand = new RelayCommand(OpenCSVFile);
            GenerateCommand = new RelayCommand(Generate, CanGenerate);
            PressedValuesCollection = new SeriesCollection();
            
            Labels = new ObservableCollection<string>() {};
            YFormatter = value => value.ToString();
            _from = 0;
            _to = 1;
            XStep = 1;
            AnalyzerResult = null;
        }

        private void OpenCSVFile()
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "CSV Files|*.csv";
            if (dlg.ShowDialog().Value)
            {
                try
                {
                    OpenFilePath = dlg.FileName;
                }
                catch (Exception ex)
                {
                    //_notification.ShowError($"An error occurred while trying to import the benchmark file.\n{ex.FlattenMessage()}");
                }
            }
        }
        public bool CanGenerate()
        {
            return (OpenFilePath!= null && OpenFilePath.Length != 0 && File.Exists(OpenFilePath));
        }
        private async void Generate()
        {
            IAnalyzer analyzer = AnalysisService.GetAnalyzer(OpenFilePath);
            if (analyzer == null)
                return;

            AnalyzerAttribute attribute = (AnalyzerAttribute)analyzer.GetType().GetCustomAttributes(typeof(AnalyzerAttribute),true).FirstOrDefault();
            Series pressed_values = new LineSeries()
            {
                Title = attribute.Name,
                Values = new ChartValues<double>(),
                PointGeometry = null,
            };
            TestName = attribute.Name;
            Labels = new ObservableCollection<string>();
            List<DispenserCsvRow>  data = CsvFile.Read<DispenserCsvRow>(new CsvSource(OpenFilePath)).ToList();
            List<DispenserSample> samples = new List<DispenserSample>();

            To = 0;
            From = 0;
            int index = 0;
            List<object> listValues = new List<object>();
            List<string> listLabel = new List<string>();
            bool  in_test_range = false;
            
            foreach (var item in data)
            {
                double pressure = 0;
                if (double.TryParse(item.Pressure, out pressure) || !String.IsNullOrWhiteSpace(item.Command))
                {
                    samples.Add(new DispenserSample(){
                        Pressure = pressure,
                        Command = String.IsNullOrWhiteSpace(item.Command) ? null : item.Command,
                        Index = index
                    });
                   
                    if(!String.IsNullOrWhiteSpace(item.Command))
                    {
                        in_test_range = !in_test_range;
                    }
                    if( pressure != 0 )//&& listValues.Count< 10000)
                    {
                        if (listValues.Count == 0)
                        {
                            From = pressure - 100;//????
                        }
                        listLabel.Add(index.ToString());
                        listValues.Add(pressure);
                        _to = Math.Max(pressure, _to);
                    }
                    index++;
                }
            }
            
            _to += 100;
            RaisePropertyChanged("To");

            Labels = listLabel.ToObservableCollection<string>();
            XStep = (int)(Labels.Count / 5);

            pressed_values.Values.AddRange(listValues.AsEnumerable());
            PressedValuesCollection.Clear();
            PressedValuesCollection.Add(pressed_values);

            AnalyzerResult = await analyzer.Process(samples);
        }
    }
}