Add TransducerVerify

This commit is contained in:
Tyrel Souza 2022-07-26 00:22:31 -04:00
parent f71dc856fb
commit da412f2cc5
3 changed files with 128 additions and 24 deletions

View File

@ -1,32 +1,34 @@
namespace BenchtopParser { using System;
namespace BenchtopParser {
public class I28 { public class I28 {
public static Dictionary<String, ProgramConfig> Load_ProgramConfig(String program_config_value) { public static Dictionary<String, ProgramConfig> Load_ProgramConfig(String program_config_value) {
Dictionary<String, ProgramConfig> program_configs = new Dictionary<String, ProgramConfig>(); Dictionary<String, ProgramConfig> programConfigs = new Dictionary<String, ProgramConfig>();
ProgramConfig? current = null; ProgramConfig? current = null;
foreach (var line in program_config_value.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)) { foreach (var line in program_config_value.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)) {
var config_line = line.Split("\t").ToList(); var configLine = line.Split("\t").ToList();
if (config_line.Count() < 2) { if (configLine.Count() < 2) {
throw new Exception("Invalid Program Config, please provide an I28 Program Config file data"); throw new Exception("Invalid Program Config, please provide an I28 Program Config file data");
} }
config_line.RemoveAt(0); // Remove random hex value configLine.RemoveAt(0); // Remove random hex value
config_line.RemoveAt(0); // Remove L column configLine.RemoveAt(0); // Remove L column
if (config_line[0].StartsWith("Start,")) { if (configLine[0].StartsWith("Start,")) {
// initialize programconfig // initialize programconfig
current = new ProgramConfig(); current = new ProgramConfig();
continue; continue;
} }
if (config_line[0].StartsWith("Stop,")) { if (configLine[0].StartsWith("Stop,")) {
// Short circuit early when stop comes. // Short circuit early when stop comes.
if (current != null && current.ProgramNumber != null) { if (current != null && current.ProgramNumber != null) {
program_configs.Add(current.ProgramNumber, current); programConfigs.Add(current.ProgramNumber, current);
} }
current = null; current = null;
// TODO: What if multiple configs how to handle start and stop? // TODO: What if multiple configs how to handle start and stop?
break; break;
} }
var config = config_line[0].Split(",", 2).ToList(); var config = configLine[0].Split(",", 2).ToList();
if (config_line.Count() == 1) { if (configLine.Count() == 1) {
// Set Program Number // Set Program Number
if (current == null) { if (current == null) {
throw new Exception("Setting Program Number Before Start. Error In Config"); throw new Exception("Setting Program Number Before Start. Error In Config");
@ -40,17 +42,16 @@
// Skip numbers, keep value // Skip numbers, keep value
var value = config[1]; var value = config[1];
var programnumber_group_data = value.Split('\\'); // snake case as its three values
var programnumber_group_data = value.Split('\\');
var data = programnumber_group_data[2]; var data = programnumber_group_data[2];
if (data.StartsWith("=")) { continue; /* comment line */ } if (data.StartsWith("=")) { continue; /* comment line */ }
var lineConfig = new Configuration(); var lineConfig = new Configuration();
lineConfig.unit = config_line[1]; lineConfig.unit = configLine[1];
lineConfig.type = config_line[2]; //a, c, f, i lineConfig.type = configLine[2]; //a, c, f, i
var name_value = data.Split("="); var nameValue = data.Split("=");
lineConfig.name = name_value[0]; lineConfig.name = nameValue[0];
lineConfig.value = name_value[1]; lineConfig.value = nameValue[1];
// Deal with this group logic // Deal with this group logic
var group = programnumber_group_data[1]; var group = programnumber_group_data[1];
@ -64,14 +65,14 @@
} }
// TODO: What if multiple configs how to handle start and stop? // TODO: What if multiple configs how to handle start and stop?
return program_configs; return programConfigs;
} }
public static Dictionary<String, TransducerVerify> Load_TransducerVerify(String transducer_verify_value) { public static TransducerVerify Load_TransducerVerify(String transducer_verify_value) {
foreach (var line in transducer_verify_value.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)) { TransducerVerify tv = new TransducerVerify(transducer_verify_value);
return tv;
}
}
}
} // TODO: normalize snake or pascal case
return null;
}
}
}

View File

@ -6,5 +6,84 @@ using System.Threading.Tasks;
namespace BenchtopParser { namespace BenchtopParser {
public class TransducerVerify { public class TransducerVerify {
public Dictionary<int, Transducer> transducers = new Dictionary<int, Transducer>();
public int indent = 26;
/// Split the string, and clean up whitespace, returns the Name and Value
public String[] SplitLine(String line) {
return new[] {
line[..indent].TrimEnd(),
line[(indent + 1)..]
};
}
public void SetIndent(int id, String line) {
var endOfCurrentTransducerKey = $"Transducer {id}".Length;
var restOfLine = line.Substring(endOfCurrentTransducerKey);
var additionalSpaces = restOfLine.TakeWhile(c => c == ' ').Count();
// length 1 based, remove 1, then add additional spaces
this.indent = endOfCurrentTransducerKey - 1 + additionalSpaces;
}
public TransducerVerify(String transducer_verify_value) {
var separator = "===============================================================";
var documentLines = transducer_verify_value.Split(new string[] { Environment.NewLine }, StringSplitOptions.TrimEntries).ToList();
// Check if proper document
var title = documentLines[0];
documentLines.RemoveAt(0);
if (title != "|| Transducer Verify Report ||") {
throw new Exception("Invalid Transducer Verify Report, please provide a Transducer Verify Report file data");
}
var countDict = documentLines.GroupBy(p => p).ToDictionary(p => p.Key, q => q.Count());
var numTransducers = countDict[separator];
var currentTransducer = 0; // ONE BASED - will add 1 later at check step, but first transducer is 1.
// Find the the indexes of each transducer start and stop
for (int i = 0; i < documentLines.Count(); i++) {
var line = documentLines[i];
if (line == $"TRANSDUCER{currentTransducer + 1}") {
currentTransducer++;
continue; // Skip TITLE line and separator
}
if (line == separator) {
continue;
}
if (line == "") {
// end of current transducer
continue;
}
// Add new Transducer if not present
if (transducers != null && !this.transducers.ContainsKey(currentTransducer)) {
transducers.Add(currentTransducer, new Transducer(currentTransducer));
// Calculating indent
SetIndent(currentTransducer, line);
transducers[currentTransducer].name = SplitLine(line)[1];
continue;
}
// Add transducer parameters
var _line = SplitLine(line);
if (transducers != null) {
transducers[currentTransducer].parameters[_line[0]] = _line[1];
} else {
throw new Exception("Config list is null, something broke");
}
}
}
}
public class Transducer {
public int id;
public String? name;
public Dictionary<String, String> parameters = new Dictionary<String, String>();
public Transducer(int id) {
this.id = id;
}
} }
} }

View File

@ -0,0 +1,24 @@
namespace BenchtopParserTests {
public class Transducer_Verify_Tests {
[SetUp]
public void Setup() {
}
[Test]
public void Test_TransducerVerify() {
String transducer_verify = File.ReadAllText(
Path.Combine(TestContext.CurrentContext.TestDirectory, @"TestFiles\Transducer Verify.txt")
);
BenchtopParser.TransducerVerify tv = BenchtopParser.I28.Load_TransducerVerify(transducer_verify);
Assert.That(tv.transducers[1].id, Is.EqualTo(1));
Assert.That(tv.transducers[1].name, Is.EqualTo("CTS D34-442 115PSIA"));
}
[Test]
public void Test_TransducerVerify_Throw_InvalidProgram() {
Exception ex = Assert.Throws<Exception>(delegate { BenchtopParser.I28.Load_TransducerVerify("broken"); });
Assert.That(ex.Message, Is.EqualTo("Invalid Transducer Verify Report, please provide a Transducer Verify Report file data"));
}
}
}