laatzen/Common/CordoPlot/MainWindow.xaml.cs
2025-04-02 14:11:35 +02:00

366 lines
15 KiB
C#

using ScottPlot.WPF;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Newtonsoft.Json;
using Xylem.Common.CommonCore.Consts;
using Xylem.Common.Hardware.WaterMeter.Genesis.Protocols.StreamingProtocol;
using Xylem.Common.Logic.ProductionOrderCore.TestResults;
using Xylem.Common.Logic.SoftwareAccessHelper;
using Xylem.Common.Metrology.Measurements;
namespace CordoPlot
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window , INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private PlotableTestResults _testBenchresults;
public PlotableTestResults testBenchresults
{
get { return _testBenchresults; }
set
{
if (value != _testBenchresults)
{
_testBenchresults = value;
OnPropertyChanged("testBenchresults");
}
}
}
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(Object sender, RoutedEventArgs e)
{
try
{
var dialog = new Microsoft.Win32.OpenFileDialog();
dialog.FileName = "led"; // Default file name
dialog.DefaultExt = ".log"; // Default file extension
dialog.Filter = "Text documents (.log)|*.log"; // Filter files by extension
// Show open file dialog box
bool? result = dialog.ShowDialog();
// Process open file dialog box results
if (result == true)
{
//lade testresult from webservise
// Open document
string filename = dialog.FileName;
testBenchresults = new PlotableTestResults();
var AllLines = File.ReadAllLines(filename);
//LedRawData__Slot_7_SN24712962_PG2016079130.log
if (filename.Contains("_SN") && filename.Contains("_PG"))
{
var sn = filename.Substring(filename.LastIndexOf("_SN") + 3, filename.LastIndexOf("_PG") - filename.LastIndexOf("_SN") - 3);
var pg = filename.Substring(filename.LastIndexOf("_PG") + 3, filename.LastIndexOf(".log") - filename.LastIndexOf("_PG") - 3);
testBenchresults = JsonConvert.DeserializeObject<PlotableTestResults>(LocalWebRequest.GetRequest($"http://10.49.40.25/MeterProcessState/api/TestBench/GetTestResults?SerialNr={sn}&TestRunNr={pg}"));
testBenchresults.HasResults = true;
dataGrid.ItemsSource = testBenchresults.TestPointsMain;
dataGrid.Items.Refresh();
}
var listOfData = new List<List<IMeasurementRecord>>();
listOfData.Add(new List<IMeasurementRecord>());
listOfData.Add(new List<IMeasurementRecord>());
listOfData.Add(new List<IMeasurementRecord>());
listOfData.Add(new List<IMeasurementRecord>());
var decoder = new StreamingDecoder();
var currentSyncmark = SyncMarkRecord.DecodeEveryPackage;
//DecodeMsg
var linecounter = 0;
foreach (var line in AllLines)
{
linecounter = linecounter + 1;
decoder = new StreamingDecoder();
if (line.Contains("|"))
{
var t = line.Split('|');
DateTime recived = DateTime.Now;
DateTime.TryParse(t[0], out recived);
if (t[1].Contains("@"))
{
decoder.DecodeMsg(t[1].Trim());
IMeasurementRecord recordToAdd;
if (decoder.DataBendDetectTest != null && decoder.DataBendDetectTest.IsValid)
{
decoder.DataBendDetectTest.ReceivedTime = recived;
decoder.DataBendDetectTest.SyncMarkRecord = currentSyncmark;
recordToAdd = decoder.DataBendDetectTest;
}
else if (decoder.DataCalib != null && decoder.DataCalib.IsValid)
{
decoder.DataCalib.ReceivedTime = recived;
decoder.DataCalib.SyncMarkRecord = currentSyncmark;
var n = new PlotCalibrationRecord(decoder.DataCalib, linecounter);
listOfData[n.GetChannel()].Add(n);
}
else if (decoder.DataFlowTest != null && decoder.DataFlowTest.IsValid)
{
decoder.DataFlowTest.ReceivedTime = recived;
decoder.DataFlowTest.SyncMarkRecord = currentSyncmark;
var n = new PlotFlowTestRecord(decoder.DataFlowTest, linecounter);
listOfData[n.GetChannel()].Add(n);
}
else
{
//No valid data
continue;
}
}
else
{
if (line.Contains("Mark next incoming record as"))
{
if (line.Contains("Mark next incoming record as SyncStart"))
{
currentSyncmark = SyncMarkRecord.SyncStart;
}
else if (line.Contains("Mark next incoming record as DecodeIntermediate"))
{
currentSyncmark = SyncMarkRecord.DecodeIntermediate;
}
else if (line.Contains("Mark next incoming record as SkipDecoding"))
{
currentSyncmark = SyncMarkRecord.SkipDecoding;
}
else if (line.Contains("Mark next incoming record as SyncEnd"))
{
currentSyncmark = SyncMarkRecord.SyncEnd;
}
else
{
currentSyncmark = SyncMarkRecord.DecodeEveryPackage;
}
}
}
}
//FlowTestRecord
}
testBenchresults.MapDataPoints(listOfData);
List<List<double>> Lines = new List<List<double>>();
Lines.Add(new List<double>());
Lines.Add(new List<double>());
Lines.Add(new List<double>());
Lines.Add(new List<double>());
var ChnlHelper = new List<Tuple<int, WpfPlot, int?>>();
ChnlHelper.Add(new Tuple<int, WpfPlot, int?>(0, WpfPlot1, null));
ChnlHelper.Add(new Tuple<int, WpfPlot, int?>(1, WpfPlot1, null));
ChnlHelper.Add(new Tuple<int, WpfPlot, int?>(2, WpfPlot1, null));
ChnlHelper.Add(new Tuple<int, WpfPlot, int?>(3, WpfPlot1, null));
Plot(ChnlHelper);
//List<List<ushort>> Errors = new List<List<ushort>>();
//Errors.Add(new List<ushort>());
//Errors.Add(new List<ushort>());
//Errors.Add(new List<ushort>());
//Errors.Add(new List<ushort>());
//List<double> Syncmarks = new List<double>();
//List<double> Linescale = new List<double>();
//foreach (var dataItem in listOfData)
//{
// foreach (var item in dataItem)
// {
// if (item is FlowTestRecord flow)
// {
// Lines.First().Add(flow.GetVolumeCm());
// Errors.First().Add(0);
// Linescale.Add(flow.GetTimeS());
// if (item.GetDataSyncMark() == SyncMarkRecord.SyncStart || item.GetDataSyncMark() == SyncMarkRecord.SyncEnd)
// {
// Syncmarks.Add(flow.GetTimeS());
// }
// }
// if (item is CalibrationRecord cal)
// {
// if (Linescale.Any())
// {
// Lines[cal.Channel].Add(cal.GetVolumeCm());
// Errors[cal.Channel].Add(cal.Validation);
// if (item.GetDataSyncMark() == SyncMarkRecord.SyncStart || item.GetDataSyncMark() == SyncMarkRecord.SyncEnd)
// {
// Syncmarks.Add(cal.GetTimeS());
// }
// }
// }
// }
//}
//foreach (var syncm in Syncmarks)
//{
// WpfPlot1.Plot.Add.VerticalLine(syncm, 2, ScottPlot.Colors.DarkGray);
//}
//WpfPlot1.Plot.Axes.AutoScale();
//for (int i = 0; i <= 3; i++)
//{
// WpfPlot1.Plot.Add.Scatter(Lines[i].ToArray(), Linescale.ToArray(), colors[i].Item1);
// WpfPlot1.Plot.Add.Scatter(Errors[i].ToArray(), Linescale.ToArray(), colors[i].Item2);
//}
//WpfPlot1.Refresh();
}
}
catch (Exception ex)
{
}
}
private void Plot(List<Tuple<int, WpfPlot, int?>> ChnlHelper)
{
WpfPlot1.Reset();
WpfPlot2.Reset();
WpfPlot3.Reset();
List<Tuple<ScottPlot.Color, ScottPlot.Color>> colors = new List<Tuple<ScottPlot.Color, ScottPlot.Color>>();
colors.Add(new Tuple<ScottPlot.Color, ScottPlot.Color>(ScottPlot.Colors.Blue, ScottPlot.Colors.DarkBlue));
colors.Add(new Tuple<ScottPlot.Color, ScottPlot.Color>(ScottPlot.Colors.Brown, ScottPlot.Colors.DarkGoldenRod));
colors.Add(new Tuple<ScottPlot.Color, ScottPlot.Color>(ScottPlot.Colors.Yellow, ScottPlot.Colors.DarkKhaki));
colors.Add(new Tuple<ScottPlot.Color, ScottPlot.Color>(ScottPlot.Colors.Violet, ScottPlot.Colors.DarkViolet));
List<List<double>> FinLines = new List<List<double>>();
FinLines.Add(new List<double>());
FinLines.Add(new List<double>());
FinLines.Add(new List<double>());
FinLines.Add(new List<double>());
var Plotresults = new List<PlotResult>();
foreach (var item in ChnlHelper)
{
var volLines = testBenchresults.Plot(item.Item1, item.Item3);
volLines.Color = colors[item.Item1].Item1;
Plotresults.Add(volLines);
//foreach (var item in Errors[i])
//{
// if (item != 0)
// {
// WpfPlot1.Plot.Add.Marker(Linescale[cordHelp], FinLines[i][cordHelp], ScottPlot.MarkerShape.Cross, 10, colors[1].Item2);
// }
// cordHelp = cordHelp + 1;
//}
}
var maxScale = Plotresults.Max(p => p.ScaleLine.Count);
var AddScale = Plotresults.First(p => p.ScaleLine.Count == maxScale);
var srtExport = new System.Text.StringBuilder();
foreach (var item in ChnlHelper)
{
// Plotresults[item.Item1].ScaleUp(AddScale.ScaleLine);
WpfPlot1.Plot.Add.Scatter(Plotresults[item.Item1].ScaleLine.ToArray(), Plotresults[item.Item1].VolumeLine.ToArray(), Plotresults[item.Item1].Color);
if (Plotresults[item.Item1].TofLine.Any())
{
WpfPlot2.Plot.Add.Scatter(Plotresults[item.Item1].ScaleLine.ToArray(), Plotresults[item.Item1].TofLine.ToArray(), Plotresults[item.Item1].Color);
}
if (Plotresults[item.Item1].AmplitudeDownVLine.Any())
{
WpfPlot3.Plot.Add.Scatter(Plotresults[item.Item1].ScaleLine.ToArray(), Plotresults[item.Item1].AmplitudeDownVLine.ToArray(), Plotresults[item.Item1].Color);
WpfPlot3.Plot.Add.Scatter(Plotresults[item.Item1].ScaleLine.ToArray(), Plotresults[item.Item1].AmplitudeUpVLine.ToArray(), Plotresults[item.Item1].Color);
}
var exprLine = 0;
srtExport.AppendLine($"Chnl {item.Item1} ;Scale; Vol;");
foreach (var exprHelpLine in Plotresults[item.Item1].ScaleLine)
{
srtExport.AppendLine($"{exprHelpLine};{Plotresults[item.Item1].VolumeLine[exprLine]};");
exprLine = exprLine + 1;
}
}
WpfPlot1.Plot.Axes.AutoScale();
WpfPlot1.Refresh();
WpfPlot2.Plot.Axes.AutoScale();
WpfPlot2.Refresh();
WpfPlot3.Plot.Axes.AutoScale();
WpfPlot3.Refresh();
}
private void DataGrid_SelectedCellsChanged(Object sender, System.Windows.Controls.SelectedCellsChangedEventArgs e)
{
DataGridCellInfo di = e.AddedCells.First();
//Cast the DataGridCellInfo.Item to the source object type
//In this case the ItemsSource is a DataTable and individual items are DataRows
if (di.Item is TestPoint tp)
{
var ChnlHelper = new List<Tuple<int, WpfPlot, int?>>();
ChnlHelper.Add(new Tuple<int, WpfPlot, int?>(0, WpfPlot1, tp.TestPointNr -1 ));
ChnlHelper.Add(new Tuple<int, WpfPlot, int?>(1, WpfPlot1, tp.TestPointNr -1));
ChnlHelper.Add(new Tuple<int, WpfPlot, int?>(2, WpfPlot1, tp.TestPointNr -1 ));
ChnlHelper.Add(new Tuple<int, WpfPlot, int?>(3, WpfPlot1, tp.TestPointNr -1));
Plot(ChnlHelper);
}
}
}
}