diff --git a/BenchtopParser/ProgramConfig.cs b/BenchtopParser/ProgramConfig.cs index 1927147..4705daf 100644 --- a/BenchtopParser/ProgramConfig.cs +++ b/BenchtopParser/ProgramConfig.cs @@ -7,31 +7,24 @@ using System.Threading.Tasks; namespace BenchtopParser { public class Program { - public String? ProgramNumber { get; set; } + public string? ProgramNumber { get; set; } // Config Groups. TODO: rename to w/e the heck they are. - public Dictionary A = new(); - public Dictionary I = new(); - public Dictionary M = new(); - public Dictionary O = new(); - public Dictionary P = new(); - public Dictionary R = new(); - public Dictionary S = new(); - public Dictionary T = new(); - public Dictionary U = new(); - public Dictionary X = new(); - - public string Name() { - if (M != null && M["Name"] != null && M["Name"].value != null) { - return M["Name"].value; - } - return ""; - } + public Dictionary A = new(); + public Dictionary I = new(); + public Dictionary M = new(); + public Dictionary O = new(); + public Dictionary P = new(); + public Dictionary R = new(); + public Dictionary S = new(); + public Dictionary T = new(); + public Dictionary U = new(); + public Dictionary X = new(); public void AddToGroup(String group, Configuration config) { // UGLY - but helpful later. // TODO: Find out what the config names are and MAKE A MAP - string name = config.name; + string name = config.Name; if (name == null) { throw new Exception("Name is null, adding before Config has set name"); } @@ -53,36 +46,36 @@ namespace BenchtopParser { } public class Configuration { - public String name { get; set; } - public String? value { get; set; } - public String? unit { get; set; } + public string Name { get; set; } + public string? Value { get; set; } + public string? Unit { get; set; } - public String? type { get; set; } //possible values are: a, c, f, i. Which I presume are Array, Characters, Float, Integer + public string? Type { get; set; } //possible values are: a, c, f, i. Which I presume are Array, Characters, Float, Integer public Configuration(string name, string? value, string? unit, string? type) { - this.name = name; - this.value = value; - this.unit = unit; - this.type = type; + Name = name; + Value = value; + Unit = unit; + Type = type; } - public Configuration(List config, String data) { - String[] nameValue = data.Split("="); - name = nameValue[0].Trim(); - value = nameValue[1].Trim(); + public Configuration(List config, string data) { + string[] nameValue = data.Split("="); + Name = nameValue[0].Trim(); + Value = nameValue[1].Trim(); - unit = config[1].Trim(); - type = config[2].Trim(); + Unit = config[1].Trim(); + Type = config[2].Trim(); } } public class ProgramConfig { - public Dictionary programs = new(); - public ProgramConfig(String program_config_value) { + public Dictionary Programs = new(); + public ProgramConfig(string programConfigValue) { Program? current = null; - foreach (string line in program_config_value.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)) { - List configLine = line.Split("\t").ToList(); + foreach (string line in programConfigValue.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)) { + List configLine = line.Split("\t").ToList(); if (configLine.Count < 2) { throw new Exception("Invalid Program Config, please provide an I28 Program Config file data"); } @@ -100,8 +93,8 @@ namespace BenchtopParser { } if (configLine[0].StartsWith("Stop,")) { // Short circuit early when stop comes. - if (current != null && current.ProgramNumber != null) { - programs.Add(current.ProgramNumber, current); + if (current?.ProgramNumber != null) { + Programs.Add(current.ProgramNumber, current); } current = null; // TODO: What if multiple configs how to handle start and stop? @@ -120,16 +113,14 @@ namespace BenchtopParser { string value = config[1]; - string[] programnumber_group_data = value.Split('\\'); // snake case as its three values - string data = programnumber_group_data[2]; + string[] programnumberGroupData = value.Split('\\'); // snake case as its three values + string data = programnumberGroupData[2]; if (data.StartsWith("=")) { continue; /* comment line */ } Configuration lineConfig = new(configLine, data); // Deal with this group logic - if (current != null) { - current.AddToGroup(programnumber_group_data[1], lineConfig); - } + current?.AddToGroup(programnumberGroupData[1], lineConfig); } // TODO: What if multiple configs how to handle start and stop? } diff --git a/BenchtopParser/TransducerVerify.cs b/BenchtopParser/TransducerVerify.cs index 84c07cb..adbea5a 100644 --- a/BenchtopParser/TransducerVerify.cs +++ b/BenchtopParser/TransducerVerify.cs @@ -7,32 +7,30 @@ using System.Threading.Tasks; namespace BenchtopParser { public class TransducerVerify { - public Dictionary transducers = new(); - public int indent = 26; // Default 26 from seeing files, overridden later in SetIndent for safety - + public Dictionary Transducers = new(); + public int Indent = 26; // Default 26 from seeing files, overridden later in SetIndent for safety /// Split the string, and clean up whitespace, returns the Name and Value - public String[] SplitLine(String line) { + public string[] SplitLine(string line) { return new[] { - line[..indent].TrimEnd(), - line[(indent + 1)..] + line[..Indent].TrimEnd(), + line[(Indent + 1)..] }; } - public void SetIndent(int id, String line) { + public void SetIndent(int id, string line) { int endOfCurrentTransducerKey = $"Transducer {id}".Length; - string restOfLine = line.Substring(endOfCurrentTransducerKey); + string restOfLine = line[endOfCurrentTransducerKey..]; int additionalSpaces = restOfLine.TakeWhile(c => c == ' ').Count(); // length 1 based, remove 1, then add additional spaces - this.indent = endOfCurrentTransducerKey - 1 + additionalSpaces; + Indent = endOfCurrentTransducerKey - 1 + additionalSpaces; } public TransducerVerify() { - return; } - public TransducerVerify(String transducer_verify_value) { - string separator = "==============================================================="; - List documentLines = transducer_verify_value.Split(new string[] { Environment.NewLine }, StringSplitOptions.TrimEntries).ToList(); + public TransducerVerify(string transducer_verify_value) { + const string separator = "==============================================================="; + List documentLines = transducer_verify_value.Split(new string[] { Environment.NewLine }, StringSplitOptions.TrimEntries).ToList(); // Check if proper document string title = documentLines[0]; @@ -41,52 +39,43 @@ namespace BenchtopParser { throw new Exception("Invalid Transducer Verify Report, please provide a Transducer Verify Report file data"); } - Dictionary countDict = documentLines.GroupBy(p => p).ToDictionary(p => p.Key, q => q.Count()); - int numTransducers = countDict[separator]; + Dictionary countDict = documentLines.GroupBy(p => p).ToDictionary(p => p.Key, q => q.Count()); int 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++) { - string line = documentLines[i]; + foreach (var line in documentLines) + { if (line == $"TRANSDUCER{currentTransducer + 1}") { currentTransducer++; continue; // Skip TITLE line and separator } - if (line == separator) { - continue; - } - - if (line == "") { + if (line is separator or "") { // end of current transducer continue; } // Add new Transducer if not present - if (transducers != null && !this.transducers.ContainsKey(currentTransducer)) { - transducers.Add(currentTransducer, new Transducer(currentTransducer)); + if (!Transducers.ContainsKey(currentTransducer)) { + Transducers.Add(currentTransducer, new Transducer(currentTransducer)); // Calculating indent SetIndent(currentTransducer, line); - transducers[currentTransducer].name = SplitLine(line)[1]; + Transducers[currentTransducer].Name = SplitLine(line)[1]; continue; } // Add transducer parameters - string[] _line = SplitLine(line); - if (transducers != null) { - transducers[currentTransducer].parameters[_line[0]] = _line[1]; - } else { - throw new Exception("Config list is null, something broke"); - } + string[] lineParts = SplitLine(line); + Transducers[currentTransducer].Parameters[lineParts[0]] = lineParts[1]; } } } public class Transducer { - public int id; - public String? name; - public Dictionary parameters = new(); + public int Id; + public string? Name; + public Dictionary Parameters = new(); public Transducer(int id) { - this.id = id; + Id = id; } } } diff --git a/BenchtopParserTests/BenchtopParserTests.csproj b/BenchtopParserTests/BenchtopParserTests.csproj index 5fc622b..870c287 100644 --- a/BenchtopParserTests/BenchtopParserTests.csproj +++ b/BenchtopParserTests/BenchtopParserTests.csproj @@ -9,6 +9,7 @@ + diff --git a/BenchtopParserTests/Program_Config_Tests.cs b/BenchtopParserTests/Program_Config_Tests.cs index ed2a6be..1334fa5 100644 --- a/BenchtopParserTests/Program_Config_Tests.cs +++ b/BenchtopParserTests/Program_Config_Tests.cs @@ -15,12 +15,12 @@ namespace BenchtopParserTests { Assert.IsNotNull(programConfigs); // Check that group collecting works - - Assert.That(programConfigs.programs["P1"].I["3"].value, Is.EqualTo("Unassigned")); - Assert.That(programConfigs.programs["P1"].O["3"].value, Is.EqualTo("Test Passed")); + Assert.That(programConfigs.Programs["P1"].I["3"].Value, Is.EqualTo("Unassigned")); + Assert.That(programConfigs.Programs["P1"].O["3"].Value, Is.EqualTo("Test Passed")); - Assert.That(programConfigs.programs["P1"].P["Minimum Pressure"].value, Is.EqualTo("95.000")); - Assert.That(programConfigs.programs["P1"].P["Minimum Pressure"].unit, Is.EqualTo("mbar")); - Assert.That(programConfigs.programs["P1"].P["Minimum Pressure"].type, Is.EqualTo("f")); + Assert.That(programConfigs.Programs["P1"].P["Minimum Pressure"].Value, Is.EqualTo("95.000")); + Assert.That(programConfigs.Programs["P1"].P["Minimum Pressure"].Unit, Is.EqualTo("mbar")); + Assert.That(programConfigs.Programs["P1"].P["Minimum Pressure"].Type, Is.EqualTo("f")); } [Test] diff --git a/BenchtopParserTests/Transducer_Verify_Tests.cs b/BenchtopParserTests/Transducer_Verify_Tests.cs index 09def04..ce43556 100644 --- a/BenchtopParserTests/Transducer_Verify_Tests.cs +++ b/BenchtopParserTests/Transducer_Verify_Tests.cs @@ -11,8 +11,8 @@ namespace BenchtopParserTests { [Test] public void Test_TransducerVerify() { BenchtopParser.TransducerVerify tv = new(transducer_verify_value); - Assert.That(tv.transducers[1].id, Is.EqualTo(1)); - Assert.That(tv.transducers[1].name, Is.EqualTo("CTS D34-442 115PSIA")); + Assert.That(tv.Transducers[1].Id, Is.EqualTo(1)); + Assert.That(tv.Transducers[1].Name, Is.EqualTo("CTS D34-442 115PSIA")); } [Test] @@ -25,13 +25,13 @@ namespace BenchtopParserTests { public void Test_TransducerVerify_SetIndent() { BenchtopParser.TransducerVerify tv = new(); tv.SetIndent(1, "Transducer 1 CTS D34-442 115PSIA"); - Assert.That(tv.indent, Is.EqualTo(26)); + Assert.That(tv.Indent, Is.EqualTo(26)); tv.SetIndent(1, "Transducer 1 CTS D34-442 115PSIA"); - Assert.That(tv.indent, Is.EqualTo(20)); + Assert.That(tv.Indent, Is.EqualTo(20)); tv.SetIndent(1, "Transducer 10CTS D34-442 115PSIA"); - Assert.That(tv.indent, Is.EqualTo(11)); + Assert.That(tv.Indent, Is.EqualTo(11)); } [Test]