benchtop-to-pdf/BTD-PDF/ConfigToPDF.cs

108 lines
3.4 KiB
C#

using BenchtopParser;
namespace BTD_PDF {
enum Parser {
ProgramConfig,
TransducerVerify,
HardwareCalibrationReport
}
public partial class ConfigToPDF : Form {
string? fileName = null;
Parser? parser = null;
private void setParseEnabled() {
if (fileName != null && fileName.Length > 0) {
if (parser != null) {
btnParse.Enabled = true;
}
}
}
void ConfigToPDF_DragEnter(object sender, DragEventArgs e) {
if (!e.Data.GetDataPresent(DataFormats.FileDrop)) {
return;
}
e.Effect = DragDropEffects.Copy;
}
void ConfigToPDF_DragDrop(object sender, DragEventArgs e) {
IDataObject? data = e.Data;
string[] files = (string[])data.GetData(DataFormats.FileDrop);
fileName = files[0];
if (fileName != null) {
lblFilename.Text = fileName;
parser = null;
comboParser.SelectedItem = null;
}
}
public ConfigToPDF() {
InitializeComponent();
this.AllowDrop = true;
this.DragEnter += new DragEventHandler(ConfigToPDF_DragEnter);
this.DragDrop += new DragEventHandler(ConfigToPDF_DragDrop);
}
private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e) {
}
private void btnParse_Click(object sender, EventArgs e) {
if (fileName == null) {
return;
}
var contents = File.ReadAllText(Path.Combine(fileName));
switch (parser) {
case Parser.ProgramConfig:
parseProgramConfig(contents);
break;
case Parser.HardwareCalibrationReport:
parseHardwareCalibrationReport(contents);
break;
case Parser.TransducerVerify:
parseTransducerVerify(contents);
break;
}
}
private void parseProgramConfig(string contents) {
ProgramConfig pc = new ProgramConfig(contents);
Console.WriteLine("Parsed PC");
lblContents.Text = pc.Programs["P1"].ProgramNumber;
}
private void parseHardwareCalibrationReport(string contents) {
HardwareCalibrationReport hr = new HardwareCalibrationReport(contents);
Console.WriteLine("Parsed HR");
lblContents.Text = hr.Instrument.Name;
}
private void parseTransducerVerify(string contents) {
TransducerVerify tv = new TransducerVerify(contents);
Console.WriteLine("Parsed TV");
lblContents.Text = tv.Transducers[1].Name;
}
private void comboParser_SelectedIndexChanged(object sender, EventArgs e) {
string value = comboParser.Text;
switch (value) {
case "Program Config":
parser = Parser.ProgramConfig;
break;
case "Hardware Calibration Report":
parser = Parser.HardwareCalibrationReport;
break;
case "Transducer Verify":
parser = Parser.TransducerVerify;
break;
}
setParseEnabled();
}
}
}