aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Utilities/Tango.DispenserAnalyzer.UI/ViewModels/MainWindowVM.cs
blob: 25e1bfbd07200b4b3c3df1a5c9ae3583affcd480 (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
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
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;
using System.Windows;
using System.Windows.Threading;
using OxyPlot;

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

        private string _testName = "";
        public string TestName
        {
            get { return _testName.ToUpper() + " TEST"; }
            set { _testName = value; RaisePropertyChangedAuto(); }
        }
        
        private IList<DataPoint> _points;
        public IList<DataPoint> Points
        {
            get { return _points; }
            set
            {
                _points = value;
                RaisePropertyChangedAuto();
            }
        }
        private int _step;
        public int XStep
        {
            get { return _step; }
            set { _step = 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();
            }
        }
        public bool ResetAllAxes { get; set; }

        private bool _isRunning;
        /// <summary>
        /// Gets or sets a value indicating whether this instance is running.
        /// </summary>
        public bool IsRunning
        {
            get { return _isRunning; }
            set { _isRunning = value; RaisePropertyChangedAuto(); }
        }
        private ObservableCollection<IAnalyzerResult> _analyzerResults;
        public ObservableCollection<IAnalyzerResult> AnalyzerResults
        {
            get { return _analyzerResults; }
            set { _analyzerResults = 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);
            
            YFormatter = value => value.ToString();
            _from = 0;
            _to = 1;
            XStep = 1;
            AnalyzerResults = new ObservableCollection<IAnalyzerResult>();
            _isRunning = false;
            ResetAllAxes = false;

            this.Points = new List<DataPoint>();

        }
        #region Read File
        private void OpenCSVFile()
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "CSV Files|*.csv";
            if (dlg.ShowDialog().Value)
            {
                try
                {
                    OpenFilePath = dlg.FileName;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("An error occurred while trying to import the CSV file. " + ex.Message,
                                          "Warning",
                                          MessageBoxButton.OK,
                                          MessageBoxImage.Warning);
                }
            }
        }
        protected virtual bool IsFileLocked(string filePath)
        {
            FileStream stream = null;
            try
            {
                stream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.None);
            }
            catch (IOException ex)
            {
                MessageBox.Show("Warning: " + ex.Message, "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
                return true;
            }
            finally
            {
                if (stream != null)
                    stream.Close();
            }
            return false;
        }
        #endregion
        #region Generate
        public bool CanGenerate()
        {
            return (OpenFilePath!= null && OpenFilePath.Length != 0 && File.Exists(OpenFilePath));
        }
        private async void Generate()
        {
            if (IsFileLocked(OpenFilePath))
                return;
            ResetSettings();

            IAnalyzer analyzer = AnalysisService.GetAnalyzer(OpenFilePath);
            if (analyzer == null)
                return;

            IsRunning = true;
            AnalyzerAttribute attribute = (AnalyzerAttribute)analyzer.GetType().GetCustomAttributes(typeof(AnalyzerAttribute),true).FirstOrDefault();
            
            TestName = attribute.Name;
            List<DispenserCsvRow>  data = CsvFile.Read<DispenserCsvRow>(new CsvSource(OpenFilePath)).ToList();
            List<DispenserSample> samples = new List<DispenserSample>();

            To = 0;
            From = 0;
            int index = 0;
            bool  in_test_range = false;
            
            foreach (var item in data)
            {
                double pressure = 0;
                if(item.Label == "Label")
                {
                    item.Pressure = "0";
                    item.Command = "Label";
                }
                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 )
                    {
                        if (TestName.Contains( "SEAL") && Points.Count ==0)
                        {
                            _from = pressure;
                        }
                        else
                            _from = Math.Min(pressure, _from);
                        _to = Math.Max(pressure, _to);
                        Points.Add(new DataPoint(index, pressure));
                    }
                    index++;
                }
            }
            data.Clear();
            _to += 100;
            RaisePropertyChanged("To");
            if(_from != 0)
                _from -= 100;
            RaisePropertyChanged("From");
            XStep = (int)(Points.Count / 5);
            
            List<IAnalyzerResult> res = await analyzer.Process(samples);
            AnalyzerResults = new ObservableCollection<IAnalyzerResult>(res);
            
            IsRunning = false;
        }
        #endregion

        private void ResetSettings()
        {
            Points.Clear();
            ResetAllAxes = true;
            AnalyzerResults.Clear();
            TestName = "";
        }
        
    }
}