Skip to content

Instantly share code, notes, and snippets.

@bradmartin333
Created March 7, 2022 12:14
Show Gist options
  • Save bradmartin333/17643a145a4626df44d28087081ca622 to your computer and use it in GitHub Desktop.
Save bradmartin333/17643a145a4626df44d28087081ca622 to your computer and use it in GitHub Desktop.
Parse uTP.txt files by recipe and date
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace PrintLogParser
{
public partial class FormMain : Form
{
List<Print> Prints = new List<Print>();
public FormMain()
{
InitializeComponent();
}
private void BtnOpenFile_Click(object sender, EventArgs e)
{
string path = OpenFile("Select uTP Log", "txt file (*.txt)|*.txt");
if (path == null) return;
LabelPath.Text = path;
Prints.Clear();
ComboBoxRecipe.Items.Clear();
ComboBoxDate.Items.Clear();
RichTextBox.Text = "Minutes\tCycles\n";
FindPrints();
ComboBoxRecipe.Items.AddRange(Prints.Select(x => x.Recipe).Distinct().ToArray());
}
private string OpenFile(string title, string filter)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.RestoreDirectory = true;
openFileDialog.Title = title;
openFileDialog.Filter = filter;
if (openFileDialog.ShowDialog() == DialogResult.OK)
return openFileDialog.FileName;
}
return null;
}
private void FindPrints()
{
using (StreamReader reader = new StreamReader(LabelPath.Text))
{
_ = reader.ReadLine(); // Discard header
while (reader.Peek() != -1)
{
var line = reader.ReadLine();
_ = reader.Peek();
string[] data = line.Split(',');
if (data.Length == 2)
{
if (line.Contains("STARTED")) Prints.Add(new Print() { Start = DateTime.Parse(data[0]) });
else
{
Prints.Last().Stop = DateTime.Parse(data[0]);
if (line.Contains("FINISHED"))
Prints.Last().Finished = true;
}
}
else
{
if (Prints.Last().Recipe == string.Empty) Prints.Last().Recipe = data[19].Split('\\').Last();
Prints.Last().Cycles++;
}
}
}
}
private void ComboBoxRecipe_SelectedIndexChanged(object sender, EventArgs e)
{
if (ComboBoxRecipe.SelectedIndex == -1) return;
ComboBoxDate.Items.Clear();
ComboBoxDate.Text = "";
RichTextBox.Text = "Minutes\tCycles\n";
ComboBoxDate.Items.AddRange(Prints.Where(x => x.Recipe == ComboBoxRecipe.Text).
Select(y => y.Start.ToShortDateString()).Distinct().ToArray());
}
private void ComboBoxDate_SelectedIndexChanged(object sender, EventArgs e)
{
RichTextBox.Text = "Minutes\tCycles\n";
foreach (Print print in Prints.Where(x => x.Recipe == ComboBoxRecipe.Text &&
x.Start.ToShortDateString() == ComboBoxDate.Text))
{
RichTextBox.Text += print.ToString();
}
}
}
public class Print
{
public DateTime Start { get; set; }
public DateTime Stop { get; set; }
public bool Finished { get; set; }
public string Recipe { get; set; } = string.Empty;
public int Cycles { get; set; }
public Print() { }
public override string ToString()
{
return $"{(Stop - Start).TotalMinutes:f3}\t{Cycles}\n";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment