From da412f2cc5ac8448088f38caf9dd0fec6162da14 Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Tue, 26 Jul 2022 00:22:31 -0400 Subject: [PATCH] Add TransducerVerify --- BenchtopParser/I28.cs | 49 ++++++------ BenchtopParser/TransducerVerify.cs | 79 +++++++++++++++++++ .../Transducer_Verify_Tests.cs | 24 ++++++ 3 files changed, 128 insertions(+), 24 deletions(-) create mode 100644 BenchtopParserTests/Transducer_Verify_Tests.cs diff --git a/BenchtopParser/I28.cs b/BenchtopParser/I28.cs index 4aa22dd..c26cc2f 100644 --- a/BenchtopParser/I28.cs +++ b/BenchtopParser/I28.cs @@ -1,32 +1,34 @@ -namespace BenchtopParser { +using System; + +namespace BenchtopParser { public class I28 { public static Dictionary Load_ProgramConfig(String program_config_value) { - Dictionary program_configs = new Dictionary(); + Dictionary programConfigs = new Dictionary(); ProgramConfig? current = null; foreach (var line in program_config_value.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)) { - var config_line = line.Split("\t").ToList(); - if (config_line.Count() < 2) { + var configLine = line.Split("\t").ToList(); + if (configLine.Count() < 2) { throw new Exception("Invalid Program Config, please provide an I28 Program Config file data"); } - config_line.RemoveAt(0); // Remove random hex value - config_line.RemoveAt(0); // Remove L column - if (config_line[0].StartsWith("Start,")) { + configLine.RemoveAt(0); // Remove random hex value + configLine.RemoveAt(0); // Remove L column + if (configLine[0].StartsWith("Start,")) { // initialize programconfig current = new ProgramConfig(); continue; } - if (config_line[0].StartsWith("Stop,")) { + if (configLine[0].StartsWith("Stop,")) { // Short circuit early when stop comes. if (current != null && current.ProgramNumber != null) { - program_configs.Add(current.ProgramNumber, current); + programConfigs.Add(current.ProgramNumber, current); } current = null; // TODO: What if multiple configs how to handle start and stop? break; } - var config = config_line[0].Split(",", 2).ToList(); - if (config_line.Count() == 1) { + var config = configLine[0].Split(",", 2).ToList(); + if (configLine.Count() == 1) { // Set Program Number if (current == null) { throw new Exception("Setting Program Number Before Start. Error In Config"); @@ -40,17 +42,16 @@ // Skip numbers, keep value var value = config[1]; - - var programnumber_group_data = value.Split('\\'); + var programnumber_group_data = value.Split('\\'); // snake case as its three values var data = programnumber_group_data[2]; if (data.StartsWith("=")) { continue; /* comment line */ } var lineConfig = new Configuration(); - lineConfig.unit = config_line[1]; - lineConfig.type = config_line[2]; //a, c, f, i - var name_value = data.Split("="); - lineConfig.name = name_value[0]; - lineConfig.value = name_value[1]; + lineConfig.unit = configLine[1]; + lineConfig.type = configLine[2]; //a, c, f, i + var nameValue = data.Split("="); + lineConfig.name = nameValue[0]; + lineConfig.value = nameValue[1]; // Deal with this group logic var group = programnumber_group_data[1]; @@ -64,14 +65,14 @@ } // TODO: What if multiple configs how to handle start and stop? - return program_configs; + return programConfigs; } - public static Dictionary Load_TransducerVerify(String transducer_verify_value) { - foreach (var line in transducer_verify_value.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)) { - - } - return null; + public static TransducerVerify Load_TransducerVerify(String transducer_verify_value) { + TransducerVerify tv = new TransducerVerify(transducer_verify_value); + return tv; } } } + +// TODO: normalize snake or pascal case \ No newline at end of file diff --git a/BenchtopParser/TransducerVerify.cs b/BenchtopParser/TransducerVerify.cs index 9144a01..cc0b39d 100644 --- a/BenchtopParser/TransducerVerify.cs +++ b/BenchtopParser/TransducerVerify.cs @@ -6,5 +6,84 @@ using System.Threading.Tasks; namespace BenchtopParser { public class TransducerVerify { + + public Dictionary transducers = new Dictionary(); + 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 parameters = new Dictionary(); + public Transducer(int id) { + this.id = id; + } + } } diff --git a/BenchtopParserTests/Transducer_Verify_Tests.cs b/BenchtopParserTests/Transducer_Verify_Tests.cs new file mode 100644 index 0000000..2fc9388 --- /dev/null +++ b/BenchtopParserTests/Transducer_Verify_Tests.cs @@ -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(delegate { BenchtopParser.I28.Load_TransducerVerify("broken"); }); + Assert.That(ex.Message, Is.EqualTo("Invalid Transducer Verify Report, please provide a Transducer Verify Report file data")); + } + } +} \ No newline at end of file