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;
using System.Text;
using System.Data;
using System.Runtime.Remoting.Messaging;
using ScottPlot;
namespace CordoPlot
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public DataTable dspData { get; set; }
public DataTable chnData1 { get; set; }
public DataTable chnData2 { get; set; }
public DataTable chnData3 { get; set; }
public String CSerialnr { get { return _CSerialnr; } set { _CSerialnr = value; OnPropertyChanged("CSerialnr"); } }
public String CTestRunNr { get { return _CTestRunNr; } set { _CTestRunNr = value; OnPropertyChanged("CTestRunNr"); } }
public String CTestBenchNr { get { return _CTestBenchNr; } set { _CTestBenchNr = value; OnPropertyChanged("CTestBenchNr"); } }
public String CDate { get { return _CDate; } set { _CDate = value; OnPropertyChanged("CDate"); } }
public String CMeterSize { get { return _CMeterSize; } set { _CMeterSize = value; OnPropertyChanged("CMeterSize"); } }
public String CAirTempC { get { return _CAirTempC; } set { _CAirTempC = value; OnPropertyChanged("CAirTempC"); } }
public String CAirPressure { get { return _CAirPressure; } set { _CAirPressure = value; OnPropertyChanged("CAirPressure"); } }
public String CAirHumanity { get { return _CAirHumanity; } set { _CAirHumanity = value; OnPropertyChanged("CAirHumanity"); } }
public String CWaterTempC { get { return _CWaterTempC; } set { _CWaterTempC = value; OnPropertyChanged("CWaterTempC"); } }
public String CTotalMeters { get { return _CTotalMeters; } set { _CTotalMeters = value; OnPropertyChanged("CTotalMeters"); } }
public String CRemark { get { return _CRemark; } set { _CRemark = value; OnPropertyChanged("CRemark"); } }
private String _CSerialnr { get; set; }
private String _CTestRunNr { get; set; }
private String _CTestBenchNr { get; set; }
private String _CDate { get; set; }
private String _CMeterSize { get; set; }
private String _CAirTempC { get; set; }
private String _CAirPressure { get; set; }
private String _CAirHumanity { get; set; }
private String _CWaterTempC { get; set; }
private String _CTotalMeters { get; set; }
private String _CRemark { get; set; }
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"))
{
try
{
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(LocalWebRequest.GetRequest($"http://localhost:50075/api/TestBench/GetTestResults?SerialNr={sn}&TestRunNr={pg}", 10000));
testBenchresults.HasResults = true;
dataGridTestPoints.ItemsSource = testBenchresults.TestPointsMain;
dataGridTestPoints.Items.Refresh();
DataTable MainInfoDt = new DataTable("DisplayData");
MainInfoDt.Columns.Add("Prop", typeof(string));
MainInfoDt.Columns.Add("Val", typeof(string));
MainInfoDt.Rows.Add("Serialnr", testBenchresults.Serialnr.ToString());
MainInfoDt.Rows.Add("TestRunNr", testBenchresults.TestRunNr.ToString());
MainInfoDt.Rows.Add("TestBenchNr", testBenchresults.TestBenchNr.ToString());
MainInfoDt.Rows.Add("Date", testBenchresults.Date.ToString());
MainInfoDt.Rows.Add("MeterSize", testBenchresults.MeterSize.ToString());
MainInfoDt.Rows.Add("AirTempC", testBenchresults.AirTempC.ToString());
MainInfoDt.Rows.Add("AirPressure", testBenchresults.AirPressure.ToString());
MainInfoDt.Rows.Add("AirHumanity", testBenchresults.AirHumanity.ToString());
MainInfoDt.Rows.Add("WaterTempC", testBenchresults.WaterTempC.ToString());
MainInfoDt.Rows.Add("TotalMeters", testBenchresults.TotalMeters.ToString());
MainInfoDt.Rows.Add("Remark", testBenchresults.Remark.ToString());
dataGridInfo.DataContext = MainInfoDt.DefaultView;
}
catch (Exception ex)
{
}
}
var listOfData = new List>();
listOfData.Add(new List());
listOfData.Add(new List());
listOfData.Add(new List());
listOfData.Add(new List());
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);
StringBuilder sb;
StringBuilder sbDetails;
DataTable dspDataRef;
DataTable chnData1Ref;
DataTable chnData2Ref;
DataTable chnData3Ref;
testBenchresults.PrepExprtDataPoints();
//dspData = dspDataRef;
//chnData1 = chnData1Ref;
//chnData2 = chnData2Ref;
//chnData3 = chnData3Ref;
dataGridDispl.DataContext = dspData.DefaultView;
dataGridchl1.DataContext = chnData1.DefaultView;
dataGridchl2.DataContext = chnData2.DefaultView;
dataGridchl3.DataContext = chnData3.DefaultView;
//File.WriteAllText(filename + "Display.csv", sb.ToString());
//File.WriteAllText(filename + "Details.csv", sbDetails.ToString());
List> Lines = new List>();
Lines.Add(new List());
Lines.Add(new List());
Lines.Add(new List());
Lines.Add(new List());
var ChnlHelper = new List>();
ChnlHelper.Add(new Tuple(0, WpfPlot1, null));
ChnlHelper.Add(new Tuple(1, WpfPlot1, null));
ChnlHelper.Add(new Tuple(2, WpfPlot1, null));
ChnlHelper.Add(new Tuple(3, WpfPlot1, null));
Plot(ChnlHelper);
//List> Errors = new List>();
//Errors.Add(new List());
//Errors.Add(new List());
//Errors.Add(new List());
//Errors.Add(new List());
//List Syncmarks = new List();
//List Linescale = new List();
//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> ChnlHelper)
{
WpfPlot1.Reset();
WpfPlot2.Reset();
WpfPlot3.Reset();
List> colors = new List>();
colors.Add(new Tuple(ScottPlot.Colors.Blue, ScottPlot.Colors.DarkBlue));
colors.Add(new Tuple(ScottPlot.Colors.Brown, ScottPlot.Colors.DarkGoldenRod));
colors.Add(new Tuple(ScottPlot.Colors.Yellow, ScottPlot.Colors.DarkKhaki));
colors.Add(new Tuple(ScottPlot.Colors.Violet, ScottPlot.Colors.DarkViolet));
List> FinLines = new List>();
FinLines.Add(new List());
FinLines.Add(new List());
FinLines.Add(new List());
FinLines.Add(new List());
var Plotresults = new List();
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)
{
dataGridDispl.DataContext = dspData.AsEnumerable()
.Where(row => row.Field("TestNr") == tp.TestPointNr)
.CopyToDataTable();
dataGridchl1.DataContext = chnData1.AsEnumerable()
.Where(row => row.Field("TestNr") == tp.TestPointNr)
.CopyToDataTable();
dataGridchl2.DataContext = chnData2.AsEnumerable()
.Where(row => row.Field("TestNr") == tp.TestPointNr)
.CopyToDataTable();
dataGridchl3.DataContext = chnData3.AsEnumerable()
.Where(row => row.Field("TestNr") == tp.TestPointNr)
.CopyToDataTable();
var ChnlHelper = new List>();
ChnlHelper.Add(new Tuple(0, WpfPlot1, tp.TestPointNr - 1));
ChnlHelper.Add(new Tuple(1, WpfPlot1, tp.TestPointNr - 1));
ChnlHelper.Add(new Tuple(2, WpfPlot1, tp.TestPointNr - 1));
ChnlHelper.Add(new Tuple(3, WpfPlot1, tp.TestPointNr - 1));
Plot(ChnlHelper);
}
}
string lastTitle = "";
List lastListOfIndex = new List();
List