using DTS.Common.Interface.Sensors; using System; namespace DTS.Common.Classes.Sensors { /// /// the scaler is a bit different than an ordinary scaler, so the name here is inaccurate, however the idea is /// that we allow the user to transform collected data, primarly by allowing them to define the 0,1 value of the digital output /// public class DigitalInputScaleMultiplier : IDigitalInputScaleMultiplier { public Forms Form { get; set; } = Forms.ArbitraryLowAndHigh; /// /// for arbirary low/high, this is the low value, the value 0 should be displayed as (OFF) /// public double DefaultValue { get; set; } /// /// for arbitrary low/high, this is the high value, the value 1 should be displayed as (ON) /// public double ActiveValue { get; set; } = 1D; public bool SimpleEquals(IDigitalInputScaleMultiplier rhs) { return Form == rhs.Form && DefaultValue == rhs.DefaultValue && ActiveValue == rhs.ActiveValue; } public override bool Equals(object obj) { if (obj is DigitalInputScaleMultiplier) { var b = obj as DigitalInputScaleMultiplier; return b.Form == Form && b.ActiveValue == ActiveValue && b.DefaultValue == DefaultValue; } else { return false; } } public override int GetHashCode() { //the idea here is to use two primes to avoid collisions, it's not perfect but should work in general and we can predict when it won't if (ActiveValue == 31 || DefaultValue == 31 || DefaultValue == 79 || ActiveValue == 79) { return (int)Form + Convert.ToInt32(ActiveValue * 127) + Convert.ToInt32(DefaultValue * 23); } else { return (int)Form + Convert.ToInt32(ActiveValue * 31) + Convert.ToInt32(DefaultValue * 79); } } /// /// constructor and copy constructor /// public DigitalInputScaleMultiplier() { DefaultValue = 0D; } public DigitalInputScaleMultiplier(DigitalInputScaleMultiplier copy) { Form = copy.Form; DefaultValue = copy.DefaultValue; ActiveValue = copy.ActiveValue; } /// /// serializes scaler to a string /// /// public string ToSerializeDbString() { switch (Form) { case Forms.ArbitraryLowAndHigh: return ToSerializeDbStringLowAndHigh(); default: throw new NotSupportedException("DigitalScaleMultiplier::ToSerializeDbString unsupported form: " + Form); } } /// /// serializes an ArbitraryLowHigh to a string /// /// private string ToSerializeDbStringLowAndHigh() { return string.Format("{1}{0}{2}{0}{3}", System.Globalization.CultureInfo.InvariantCulture.TextInfo.ListSeparator, Form, DefaultValue.ToString(System.Globalization.CultureInfo.InvariantCulture), ActiveValue.ToString(System.Globalization.CultureInfo.InvariantCulture)); } /// /// deserializes an arbitrary low/high from a string /// /// private void FromDbSerializeStringLowAndHigh(string[] tokens) { if (tokens.Length < 3) { throw new NotSupportedException("DigitalInputScaleMultiplier::FromDbSerializeStringLowAndHigh invalid format for scale multiplier"); } if (double.TryParse(tokens[1], System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out double d)) { DefaultValue = d; } else { throw new NotSupportedException("DigitalInputScaleMultiplier::FromDbSerializeStringLowAndHigh invalid format for low value: " + tokens[1]); } if (double.TryParse(tokens[2], System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out d)) { ActiveValue = d; } else { throw new NotSupportedException("DigitalInputScaleMultiplier::FromDbSerializeStringLowAndHigh invalid format for high value: " + tokens[2]); } } /// /// deserializes a scaler from a string, regardless of format /// /// public void FromDbSerializeString(string s) { if (null == s) { Utilities.Logging.APILogger.Log("Unable to serialize Db. String is null."); //FIXME is this the right thing to do? return; //throw new NotSupportedException("DigitalINputScaleMultiplier::FromDbSerializeString nothing to parse"); } var tokens = s.Split(new[] { System.Globalization.CultureInfo.InvariantCulture.TextInfo.ListSeparator }, StringSplitOptions.None); if (Enum.TryParse(tokens[0], out Forms form)) { Form = form; switch (form) { case Forms.ArbitraryLowAndHigh: FromDbSerializeStringLowAndHigh(tokens); break; default: throw new NotSupportedException("DigitalInputScaleMultiplier::FromDbSerializeString unsupported form " + form); } } else { throw new NotSupportedException("DigitalINputScaleMultiplier::FromDbSerializeString unsupported format: " + s); } } } }