make groups hardcoded, ugly for now until we get map

This commit is contained in:
Tyrel Souza 2022-07-26 01:17:53 -04:00
parent 0ca165c6bb
commit a6ffcd9585
2 changed files with 106 additions and 85 deletions

View File

@ -5,97 +5,118 @@ using System.Text;
using System.Threading.Tasks;
namespace BenchtopParser {
public class Program {
public String? ProgramNumber { get; set; }
public Dictionary<String, Dictionary<String, Configuration>> Group { get; set; }
public class Program {
public String? ProgramNumber { get; set; }
public Program() {
this.Group = new Dictionary<String, Dictionary<String, Configuration>>();
// Config Groups. TODO: rename to w/e the heck they are.
public Dictionary<String, Configuration> A = new Dictionary<String, Configuration>();
public Dictionary<String, Configuration> I = new Dictionary<String, Configuration>();
public Dictionary<String, Configuration> M = new Dictionary<String, Configuration>();
public Dictionary<String, Configuration> O = new Dictionary<String, Configuration>();
public Dictionary<String, Configuration> P = new Dictionary<String, Configuration>();
public Dictionary<String, Configuration> R = new Dictionary<String, Configuration>();
public Dictionary<String, Configuration> S = new Dictionary<String, Configuration>();
public Dictionary<String, Configuration> T = new Dictionary<String, Configuration>();
public Dictionary<String, Configuration> U = new Dictionary<String, Configuration>();
public Dictionary<String, Configuration> X = new Dictionary<String, Configuration>();
public void AddToGroup(String group, Configuration config) {
// UGLY - but helpful later.
// TODO: Find out what the config names are and MAKE A MAP
var name = config.name;
if (name == null) {
throw new Exception("Name is null, adding before Config has set name");
}
switch (group.ToUpper()) {
case "A": this.A.Add(name, config); break;
case "I": this.I.Add(name, config); break;
case "M": this.M.Add(name, config); break;
case "O": this.O.Add(name, config); break;
case "P": this.P.Add(name, config); break;
case "R": this.R.Add(name, config); break;
case "S": this.S.Add(name, config); break;
case "T": this.T.Add(name, config); break;
case "U": this.U.Add(name, config); break;
case "X": this.X.Add(name, config); break;
}
}
public class Configuration {
public String? name { get; set; }
public String? value { get; set; }
public String? type { get; set; }
public String? unit { get; set; }
}
public Configuration(List<String> config, String data) {
String[] nameValue = data.Split("=");
name = nameValue[0].Trim();
value = nameValue[1].Trim();
public class Configuration {
public String? name { get; set; }
public String? value { get; set; }
public String? type { get; set; }
public String? unit { get; set; }
unit = config[1].Trim();
type = config[2].Trim(); //a, c, f, i
}
public Configuration(List<String> config, String data) {
String[] nameValue = data.Split("=");
name = nameValue[0].Trim();
value = nameValue[1].Trim();
unit = config[1].Trim();
type = config[2].Trim(); //a, c, f, i
}
}
public class ProgramConfig {
public Dictionary<String, Program> programs = new Dictionary<String, Program>();
public ProgramConfig (String program_config_value) {
Program? current = null;
public class ProgramConfig {
public Dictionary<String, Program> programs = new Dictionary<String, Program>();
public ProgramConfig (String program_config_value) {
Program? current = null;
foreach (var line in program_config_value.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)) {
List<String> configLine = line.Split("\t").ToList();
if (configLine.Count < 2) {
throw new Exception("Invalid Program Config, please provide an I28 Program Config file data");
}
// Remove the first two items because they seem to be a checksum and an unneeded type.
// Then we can start at 0 like normal, not have the first important bit be at 2.
configLine.RemoveAt(0); // Remove random hex value
configLine.RemoveAt(0); // Remove L column
/* LOOP CONTROL */
if (configLine[0].StartsWith("Start,")) {
// initialize programconfig
current = new Program();
continue;
}
if (configLine[0].StartsWith("Stop,")) {
// Short circuit early when stop comes.
if (current != null && current.ProgramNumber != null) {
programs.Add(current.ProgramNumber, current);
}
current = null;
// TODO: What if multiple configs how to handle start and stop?
break;
}
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");
}
current.ProgramNumber = config[1];
continue;
}
/* CREATE A CONFIGURATION */
var value = config[1];
var programnumber_group_data = value.Split('\\'); // snake case as its three values
var data = programnumber_group_data[2];
if (data.StartsWith("=")) { continue; /* comment line */ }
Configuration lineConfig = new Configuration(configLine, data);
// Deal with this group logic
var group = programnumber_group_data[1];
if (current != null && !current.Group.ContainsKey(group)) {
current.Group.Add(group, new Dictionary<String, Configuration> {
{ lineConfig.name, lineConfig }
});
} else if (current != null) {
current.Group[group].Add(lineConfig.name, lineConfig);
} else {
throw new Exception("Config list is null, something broke");
}
foreach (var line in program_config_value.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)) {
List<String> configLine = line.Split("\t").ToList();
if (configLine.Count < 2) {
throw new Exception("Invalid Program Config, please provide an I28 Program Config file data");
}
// TODO: What if multiple configs how to handle start and stop?
}
// Remove the first two items because they seem to be a checksum and an unneeded type.
// Then we can start at 0 like normal, not have the first important bit be at 2.
configLine.RemoveAt(0); // Remove random hex value
configLine.RemoveAt(0); // Remove L column
/* LOOP CONTROL */
if (configLine[0].StartsWith("Start,")) {
// initialize programconfig
current = new Program();
continue;
}
if (configLine[0].StartsWith("Stop,")) {
// Short circuit early when stop comes.
if (current != null && current.ProgramNumber != null) {
programs.Add(current.ProgramNumber, current);
}
current = null;
// TODO: What if multiple configs how to handle start and stop?
break;
}
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");
}
current.ProgramNumber = config[1];
continue;
}
/* CREATE A CONFIGURATION */
var value = config[1];
var programnumber_group_data = value.Split('\\'); // snake case as its three values
var data = programnumber_group_data[2];
if (data.StartsWith("=")) { continue; /* comment line */ }
Configuration lineConfig = new Configuration(configLine, data);
// Deal with this group logic
if (current != null) {
current.AddToGroup(programnumber_group_data[1], lineConfig);
}
}
// TODO: What if multiple configs how to handle start and stop?
}
}
}

View File

@ -13,12 +13,12 @@ namespace BenchtopParserTests {
Assert.IsNotNull(programConfigs);
// Check that group collecting works -
Assert.That(programConfigs.programs["P1"].Group["I"]["3"].value, Is.EqualTo("Unassigned"));
Assert.That(programConfigs.programs["P1"].Group["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"].Group["P"]["Minimum Pressure"].value, Is.EqualTo("95.000"));
Assert.That(programConfigs.programs["P1"].Group["P"]["Minimum Pressure"].unit, Is.EqualTo("mbar"));
Assert.That(programConfigs.programs["P1"].Group["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]