Ejemplo n.º 1
0
        private void PortOnDataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            _buffer += port.ReadExisting().Replace("\r", "");
            var off = 0;

            while ((off = _buffer.IndexOf('\n')) != -1)
            {
                var line = _buffer.Substring(0, off);
                _buffer = _buffer.Substring(off + 1);

                var now  = DateTimeOffset.Now.ToUnixTimeMilliseconds() - startTime;
                var text = now + ":nogasm:" + line;
                Console.WriteLine(text);
                w?.WriteLine(text);
                var m = nogasmRegex.Match(line);
                if (m.Success)
                {
                    average.Add(Convert.ToDouble(m.Groups[5].Value, new NumberFormatInfo()));
                    presure.Add(Convert.ToDouble(m.Groups[3].Value, new NumberFormatInfo()));
                    vibe.Add(Convert.ToDouble(m.Groups[1].Value, new NumberFormatInfo()));
                    time.Add(now);
                    OnNogasmDataPoint?.Invoke(this, new NogasmDataPointArgs(now,
                                                                            Convert.ToDouble(m.Groups[3].Value, new NumberFormatInfo()),
                                                                            Convert.ToDouble(m.Groups[5].Value, new NumberFormatInfo()),
                                                                            Convert.ToDouble(m.Groups[1].Value, new NumberFormatInfo())));

                    if (average.Count > 10000)
                    {
                        var range = average.Count - 1000;
                        average.RemoveRange(0, range);
                        presure.RemoveRange(0, range);
                        vibe.RemoveRange(0, range);
                        time.RemoveRange(0, range);
                    }
                }
            }

            if (DateTimeOffset.Now.Subtract(_last_input).TotalMilliseconds > 100)
            {
                _last_input = DateTimeOffset.Now;
                Dispatcher?.Invoke(() =>
                {
                    AverageGraph.Plot(time, average);
                    PressureGraph.Plot(time, presure);
                    MototGraph.Plot(time, vibe);
                });
            }
        }
Ejemplo n.º 2
0
        private void MenuFileNew_Click(object sender, RoutedEventArgs e)
        {
            if (port != null)
            {
                StartStop_Click(sender, e);
            }

            logFile = null;
            average = new List <double>();
            presure = new List <double>();
            vibe    = new List <double>();
            time    = new List <double>();

            output  = new List <double>();
            outtime = new List <double>();

            var oGraphs = new List <LineGraph>();

            foreach (var graph in Lines.Children)
            {
                if (graph is LineGraph g && g.Description.Contains("O****m"))
                {
                    oGraphs.Add(g);
                }
            }

            foreach (var g in oGraphs)
            {
                Lines.Children.Remove(g);
            }

            _last_input = DateTimeOffset.Now;
            Dispatcher?.Invoke(() =>
            {
                AverageGraph.Plot(time, average);
                PressureGraph.Plot(time, presure);
                MototGraph.Plot(time, vibe);
                OutputGraph.Plot(outtime, output);
            });
        }
Ejemplo n.º 3
0
        private void MenuFileOpen_Click(object sender, RoutedEventArgs e)
        {
            //ToDo: Dirty check

            var open = new Microsoft.Win32.OpenFileDialog
            {
                DefaultExt = ".log",
                Filter     = "Logs (.log)|*.log"
            };

            // Process save file dialog box results
            if (open.ShowDialog() == true)
            {
                if (port != null)
                {
                    StartStop_Click(sender, e);
                }

                logFile = null;
                average = new List <double>();
                presure = new List <double>();
                vibe    = new List <double>();
                time    = new List <double>();

                output  = new List <double>();
                outtime = new List <double>();

                var oGraphs = new List <LineGraph>();
                foreach (var graph in Lines.Children)
                {
                    if (graph is LineGraph g && g.Description.Contains("O****m"))
                    {
                        oGraphs.Add(g);
                    }
                }

                foreach (var g in oGraphs)
                {
                    Lines.Children.Remove(g);
                }

                _last_input = DateTimeOffset.Now;
                Dispatcher?.Invoke(() =>
                {
                    AverageGraph.Plot(time, average);
                    PressureGraph.Plot(time, presure);
                    MototGraph.Plot(time, vibe);
                    OutputGraph.Plot(outtime, output);
                });

                //ToDo: Background this?
                StreamReader stream = null;
                try
                {
                    stream = new StreamReader(File.OpenRead(open.FileName));
                }
                catch (ArgumentException ex)
                {
                    MessageBox.Show($"Error on opening file: {ex.Message}", "Error Opening File", MessageBoxButton.OK,
                                    MessageBoxImage.Error);
                }

                try
                {
                    string line;
                    while ((line = stream?.ReadLine()) != null)
                    {
                        //ToDo: Abstract log event consumer
                        var m = logRegex.Match(line.ToLower(CultureInfo.CurrentCulture));
                        if (m.Success)
                        {
                            if (!long.TryParse(m.Groups[1].Value, out var t))
                            {
                                continue;
                            }

                            switch (m.Groups[2].Value)
                            {
                            case "nogasm:":
                                var m2 = nogasmRegex.Match(m.Groups[3].Value);
                                if (m2.Success)
                                {
                                    average.Add(Convert.ToDouble(m2.Groups[5].Value, new NumberFormatInfo()));
                                    presure.Add(Convert.ToDouble(m2.Groups[3].Value, new NumberFormatInfo()));
                                    vibe.Add(Convert.ToDouble(m2.Groups[1].Value, new NumberFormatInfo()));
                                    time.Add(t);

                                    if (time.Count > 2 &&
                                        Math.Abs(average[average.Count - 2] - average[average.Count - 1]) < 0.001 &&
                                        Math.Abs(presure[presure.Count - 2] - presure[presure.Count - 1]) < 0.001 &&
                                        Math.Abs(vibe[vibe.Count - 2] - vibe[vibe.Count - 1]) < 0.001)
                                    {
                                        time.RemoveAt(time.Count - 1);
                                        average.RemoveAt(time.Count - 1);
                                        presure.RemoveAt(time.Count - 1);
                                        vibe.RemoveAt(time.Count - 1);
                                    }
                                }

                                break;

                            case "user:o****m":
                                var oGraph = new LineGraph();
                                oGraph.Description = "O****m";
                                Dispatcher?.Invoke(() =>
                                {
                                    Lines.Children.Add(oGraph);
                                    oGraph.Plot(new double[] { t, t }, new double[] { 0, 4000 });
                                });
                                break;

                            case "output:":
                                if (double.TryParse(m.Groups[3].Value, out var val))
                                {
                                    output.Add(val * 1000);
                                    outtime.Add(t);
                                }
                                break;
                            }
                        }
                    }

                    Dispatcher?.Invoke(() =>
                    {
                        AverageGraph.Plot(time, average);
                        PressureGraph.Plot(time, presure);
                        MototGraph.Plot(time, vibe);
                        OutputGraph.Plot(outtime, output);
                    });
                }
                catch (IOException ex)
                {
                    MessageBox.Show($"Error parsing file: {ex.Message}", "Error Opening File", MessageBoxButton.OK,
                                    MessageBoxImage.Error);
                }
                catch (OutOfMemoryException ex)
                {
                    MessageBox.Show($"Error parsing file: {ex.Message}", "Error Opening File", MessageBoxButton.OK,
                                    MessageBoxImage.Error);
                }
                catch (ArgumentNullException ex)
                {
                    MessageBox.Show($"Error parsing file: {ex.Message}", "Error Opening File", MessageBoxButton.OK,
                                    MessageBoxImage.Error);
                }
                catch (RegexMatchTimeoutException ex)
                {
                    MessageBox.Show($"Error parsing file: {ex.Message}", "Error Opening File", MessageBoxButton.OK,
                                    MessageBoxImage.Error);
                }

                stream?.Close();
            }
        }