using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DatabaseExport { /// /// InitialOffset is the replacement for InitialEU /// it encompasses the old InitialOffset specified in EU with a method of specifying it in mV @EU /// Initial EU is a post data collection adjustment to engineering units recorded /// public class InitialOffset { /// /// copy constructor /// /// public InitialOffset(InitialOffset copy) { if (null == copy) { return; } _eu = copy.EU; _mv = copy.MV; _form = copy.Form; } /// /// default constructor /// public InitialOffset() { _form = Forms.None; _eu = 0D; _mv = 0D; } // /// // /// constructor for the old format Initial EU (a single double represting offset in EU) // /// // /// // public InitialOffset(double d) // { // _form = Forms.EU; // _eu = d; // _mv = 0D; // } /// /// serializes to a db safe string /// /// public string ToDbSerializeString() { var s = new List(); s.Add(Form.ToString()); s.Add(EU.ToString(System.Globalization.CultureInfo.InvariantCulture)); s.Add(MV.ToString(System.Globalization.CultureInfo.InvariantCulture)); return string.Join(System.Globalization.CultureInfo.InvariantCulture.TextInfo.ListSeparator, s.ToArray()); } // /// // /// deserializes from a string suitable for db storage // /// // /// public void FromDbSerializeString(string input) { if (string.IsNullOrWhiteSpace(input)) { Form = Forms.None; EU = 0; MV = 0; return; } var tokens = input.Split(new string[] { System.Globalization.CultureInfo.InvariantCulture.TextInfo.ListSeparator }, StringSplitOptions.None); Forms form; if (Enum.TryParse(tokens[0], out form)) { _form = form; double d; if (tokens.Length < 3) { throw new System.IO.InvalidDataException("Invalid InitialOffset number of parameters: " + input); } else { if (double.TryParse(tokens[1], System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out d)) { _eu = d; } else { throw new FormatException("Invalid InitialOffset EU format: " + tokens[1]); } if (double.TryParse(tokens[2], System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out d)) { _mv = d; } else { throw new FormatException("Invalid InitialOffset MV format: " + tokens[2]); } } } else { throw new System.IO.InvalidDataException("Invalid InitialOffset form: " + tokens[0]); } } /// /// the format Initial Offset is in /// public enum Forms { None = 0, EU = 1, EUAtMV = 2 } // /// // /// the format this intial offset instance is in // /// private Forms _form; public Forms Form { get => _form; set => _form = value; } /// /// EU value. In the case of Form == EU, this is the offset in EU /// In. the form of EU@mV, this is the EU@mV value, and offset in EU still needs to be calculated /// GetInitialEUValue calculates the offset in eu /// this value is not used for InitialOffset format None /// private double _eu = 0; public double EU { get => _eu; set => _eu = value; } /// /// mV value, only applies for the format EU@mV /// this is the value in mV that The value in EU is observed at by a calibrated instrument /// private double _mv = 0; public double MV { get => _mv; set => _mv = value; } // private enum Fields // { // Form, // EU, // MV // } // /// // /// Displays initial offset structure to string // /// created for FB5429 // /// // /// string resource similar to "None" // /// string resource similar to "EU" // /// string resource similar to "mV" // /// // public string ToDisplayString(string NONEFormatString, string EUFormatString, string mVFormatString) // { // StringBuilder sb = new StringBuilder(); // switch (Form) // { // case Forms.EU: // sb.AppendFormat("{0} {1}", EU, EUFormatString); // break; // case Forms.EUAtMV: // sb.AppendFormat("{0} {1} @ {2} {3}", EU, EUFormatString, MV, mVFormatString); // break; // case Forms.None: // sb.AppendFormat("{0}", NONEFormatString); // break; // default: // break; // } // return sb.ToString(); // } // /// // /// Compares attributes to another InitialOffset object // /// created for FB5429 // /// // /// an InitialOffset object // /// if contents are equal // public override bool Equals(object obj) // { // if (obj is InitialOffset) // { // InitialOffset io = obj as InitialOffset; // Fields[] fields = Enum.GetValues(typeof(Fields)).Cast().ToArray(); // foreach (var field in fields) // { // switch (field) // { // case Fields.Form: if (io.Form != Form) { return false; } break; // case Fields.EU: if (io.EU != EU) { return false; } break; // case Fields.MV: if (io.MV != MV) { return false; } break; // default: // throw new NotSupportedException("InitialOffset::Equals Unknown field " + field.ToString()); // } // } // return true; // } // return base.Equals(obj); // } } }