Files
2026-04-17 14:55:32 -04:00

211 lines
8.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DatabaseExport
{
/// <summary>
/// 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
/// </summary>
public class InitialOffset
{
/// <summary>
/// copy constructor
/// </summary>
/// <param name="copy"></param>
public InitialOffset(InitialOffset copy)
{
if (null == copy) { return; }
_eu = copy.EU;
_mv = copy.MV;
_form = copy.Form;
}
/// <summary>
/// default constructor
/// </summary>
public InitialOffset()
{
_form = Forms.None;
_eu = 0D;
_mv = 0D;
}
// /// <summary>
// /// constructor for the old format Initial EU (a single double represting offset in EU)
// /// </summary>
// /// <param name="d"></param>
// public InitialOffset(double d)
// {
// _form = Forms.EU;
// _eu = d;
// _mv = 0D;
// }
/// <summary>
/// serializes to a db safe string
/// </summary>
/// <returns></returns>
public string ToDbSerializeString()
{
var s = new List<string>();
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());
}
// /// <summary>
// /// deserializes from a string suitable for db storage
// /// </summary>
// /// <param name="input"></param>
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]); }
}
/// <summary>
/// the format Initial Offset is in
/// </summary>
public enum Forms
{
None = 0,
EU = 1,
EUAtMV = 2
}
// /// <summary>
// /// the format this intial offset instance is in
// /// </summary>
private Forms _form;
public Forms Form
{
get => _form;
set => _form = value;
}
/// <summary>
/// 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
/// </summary>
private double _eu = 0;
public double EU
{
get => _eu;
set => _eu = value;
}
/// <summary>
/// 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
/// </summary>
private double _mv = 0;
public double MV
{
get => _mv;
set => _mv = value;
}
// private enum Fields
// {
// Form,
// EU,
// MV
// }
// /// <summary>
// /// Displays initial offset structure to string
// /// created for FB5429
// /// </summary>
// /// <param name="NONEFormatString">string resource similar to "None"</param>
// /// <param name="EUFormatString">string resource similar to "EU"</param>
// /// <param name="mVFormatString">string resource similar to "mV"</param>
// /// <returns></returns>
// 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();
// }
// /// <summary>
// /// Compares attributes to another InitialOffset object
// /// created for FB5429
// /// </summary>
// /// <param name="obj">an InitialOffset object</param>
// /// <returns>if contents are equal</returns>
// public override bool Equals(object obj)
// {
// if (obj is InitialOffset)
// {
// InitialOffset io = obj as InitialOffset;
// Fields[] fields = Enum.GetValues(typeof(Fields)).Cast<Fields>().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);
// }
}
}