Files
DP44/Common/DTS.CommonCore/.svn/pristine/b1/b145f8ac01873966fe0a213e47270ad9648286e0.svn-base

132 lines
5.8 KiB
Plaintext
Raw Normal View History

2026-04-17 14:55:32 -04:00
using DTS.Common.Interface.Sensors;
using System;
namespace DTS.Common.Classes.Sensors
{
/// <summary>
/// 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
/// </summary>
public class DigitalInputScaleMultiplier : IDigitalInputScaleMultiplier
{
public Forms Form { get; set; } = Forms.ArbitraryLowAndHigh;
/// <summary>
/// for arbirary low/high, this is the low value, the value 0 should be displayed as (OFF)
/// </summary>
public double DefaultValue { get; set; }
/// <summary>
/// for arbitrary low/high, this is the high value, the value 1 should be displayed as (ON)
/// </summary>
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);
}
}
/// <summary>
/// constructor and copy constructor
/// </summary>
public DigitalInputScaleMultiplier()
{
DefaultValue = 0D;
}
public DigitalInputScaleMultiplier(DigitalInputScaleMultiplier copy)
{
Form = copy.Form;
DefaultValue = copy.DefaultValue;
ActiveValue = copy.ActiveValue;
}
/// <summary>
/// serializes scaler to a string
/// </summary>
/// <returns></returns>
public string ToSerializeDbString()
{
switch (Form)
{
case Forms.ArbitraryLowAndHigh: return ToSerializeDbStringLowAndHigh();
default: throw new NotSupportedException("DigitalScaleMultiplier::ToSerializeDbString unsupported form: " + Form);
}
}
/// <summary>
/// serializes an ArbitraryLowHigh to a string
/// </summary>
/// <returns></returns>
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)); }
/// <summary>
/// deserializes an arbitrary low/high from a string
/// </summary>
/// <param name="tokens"></param>
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]); }
}
/// <summary>
/// deserializes a scaler from a string, regardless of format
/// </summary>
/// <param name="s"></param>
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); }
}
}
}