using System;
using System.Globalization;
using System.Xml.Linq;
namespace HC_APTBS.Models
{
///
/// Calibration parameters for an analogue sensor channel.
/// Maps a raw ADC counts value (from the bench CAN bus) to an
/// engineering-unit value using a voltage-range to value-range linear mapping,
/// then applies a gain and offset correction.
///
///
/// The bench ADC uses a 10-bit (1024-step) range over 0–5 V.
/// Conversion: V = (rawCanBusValue × 5000 / 1024) / 1000
/// Engineering value = V × ((MaxVal − MinVal) / (MaxVolt − MinVolt)) × Gain + Offset
///
///
public class SensorConfiguration
{
/// 1-based sensor channel number.
public short Number { get; set; } = 1;
/// Display name for the sensor (e.g. "Pressure").
public string SensorName { get; set; } = "Sensor";
/// Minimum input voltage at the sensor connector (V).
public double MinVolt { get; set; } = 0;
/// Maximum input voltage at the sensor connector (V).
public double MaxVolt { get; set; } = 5;
/// Engineering-unit value corresponding to .
public double MinVal { get; set; } = 0;
/// Engineering-unit value corresponding to .
public double MaxVal { get; set; } = 15;
/// Multiplicative gain correction applied after the range mapping.
public double Gain { get; set; } = 1;
/// Additive offset correction applied after the gain.
public double Offset { get; set; } = 0;
///
/// Static logging hook used by runtime guards and .
/// Wired at app startup to IAppLogger.Warning. Left null keeps the
/// Models layer DI-free and testable. Signature: (source, message).
///
public static Action? WarningLogger { get; set; }
/// Set once per instance after the first zero-range warning, to prevent hot-loop log spam.
private bool _warnedRangeZero;
///
/// Converts a raw CAN bus ADC count to a calibrated engineering-unit value.
/// Returns 0 and warns once if MaxVolt == MinVolt.
///
/// 10-bit ADC count from the CAN frame.
/// Calibrated value in engineering units.
public double GetValueFromRaw(double rawCanBusValue)
{
double range = MaxVolt - MinVolt;
if (range == 0.0)
{
if (!_warnedRangeZero)
{
WarningLogger?.Invoke(nameof(SensorConfiguration),
$"MaxVolt == MinVolt for sensor '{SensorName}' (channel {Number}). Returning 0.");
_warnedRangeZero = true;
}
return 0.0;
}
// Convert ADC counts → volts (10-bit ADC, 5 V reference)
double volts = rawCanBusValue * 5000.0 / 1024.0 / 1000.0;
// Map voltage range → engineering-unit range
double value = volts * ((MaxVal - MinVal) / range);
return value * Gain + Offset;
}
/// Serialises this sensor configuration to XML.
public XElement ToXml()
=> new XElement("sensor",
new XAttribute("num", Number),
new XAttribute("name", SensorName),
new XElement("Gain", Gain),
new XElement("Offset", Offset),
new XElement("MinVolt", MinVolt),
new XElement("MaxVolt", MaxVolt),
new XElement("MinVal", MinVal),
new XElement("MaxVal", MaxVal));
/// Deserialises a sensor configuration from XML.
public static SensorConfiguration FromXml(XElement element)
{
var sc = new SensorConfiguration();
TryParse(element.Attribute("num")?.Value, "num", v => sc.Number = short.Parse(v, CultureInfo.InvariantCulture));
TryParse(element.Attribute("name")?.Value, "name", v => sc.SensorName = v);
TryParse(element.Element("Gain")?.Value, "Gain", v => sc.Gain = ParseInvariant(v));
TryParse(element.Element("Offset")?.Value, "Offset", v => sc.Offset = ParseInvariant(v));
TryParse(element.Element("MinVolt")?.Value, "MinVolt", v => sc.MinVolt = ParseInvariant(v));
TryParse(element.Element("MaxVolt")?.Value, "MaxVolt", v => sc.MaxVolt = ParseInvariant(v));
TryParse(element.Element("MinVal")?.Value, "MinVal", v => sc.MinVal = ParseInvariant(v));
TryParse(element.Element("MaxVal")?.Value, "MaxVal", v => sc.MaxVal = ParseInvariant(v));
return sc;
}
/// Parses a decimal under , tolerating comma separators.
private static double ParseInvariant(string v)
=> double.Parse(v.Replace(',', '.'), CultureInfo.InvariantCulture);
/// Creates the default pressure sensor calibration for channel 1.
public static SensorConfiguration DefaultPressureSensor()
=> new SensorConfiguration
{
Number = 1,
SensorName = "Pressure",
Offset = -4.1,
MinVolt = 0.5,
MaxVolt = 3.2,
MinVal = 0,
MaxVal = 20
};
private static void TryParse(string? value, string fieldName, Action assign)
{
if (string.IsNullOrEmpty(value)) return;
try { assign(value!); }
catch (Exception ex)
{
WarningLogger?.Invoke(nameof(SensorConfiguration),
$"Skipped malformed '{fieldName}' value '{value}': {ex.Message}");
}
}
}
}