108 lines
3.4 KiB
C#
108 lines
3.4 KiB
C#
using System;
|
|
using Xylem.Common.Hardware.WaterMeter.Genesis.GenesisConfig.Const;
|
|
|
|
namespace Xylem.Common.Hardware.WaterMeter.Genesis.GenesisCore
|
|
{
|
|
/// <summary>
|
|
/// Calibration content
|
|
/// </summary>
|
|
public class CalibFactor
|
|
{
|
|
/// <summary>
|
|
/// has been calibrated
|
|
/// </summary>
|
|
public Boolean IsCalculated;
|
|
|
|
/// <summary>
|
|
/// Channel for calibration
|
|
/// </summary>
|
|
public Int32 Channel { get; }
|
|
|
|
/// <summary>
|
|
/// Register to store the calibration
|
|
/// </summary>
|
|
public String RegisterToStoreCalibFactor { get; }
|
|
|
|
/// <summary>
|
|
/// Ctor for calibration factor
|
|
/// </summary>
|
|
/// <param name="channel"></param>
|
|
/// <param name="registerToStoreCalibFactor"></param>
|
|
public CalibFactor(Int32 channel, String registerToStoreCalibFactor)
|
|
{
|
|
Channel = channel;
|
|
RegisterToStoreCalibFactor = registerToStoreCalibFactor;
|
|
IsCalculated = false;
|
|
}
|
|
|
|
private UInt16 _meterRawCalibFactor = MeterConfig.CalibrationDefault;
|
|
|
|
/// <summary>
|
|
/// set the user readable relative (around 1.0) calibration factor
|
|
/// and convert it to the raw calibration factor for the meter
|
|
/// </summary>
|
|
/// <param name="relativeCalibFactor"></param>
|
|
/// <exception cref="Exception"></exception>
|
|
public void BuildMeterCalibFactor(Double relativeCalibFactor)
|
|
{
|
|
try
|
|
{
|
|
|
|
var tmp = Math.Round(relativeCalibFactor * GetMeterRawCalibFactor(), 0);
|
|
|
|
_meterRawCalibFactor = Convert.ToUInt16(tmp);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception("Unable to set calibration factor", ex);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Read the relative (around 1.0) calibration factor calculated from the actual
|
|
/// meter calibration factor divided by the default calibration factor
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public Double GetRelativeCalibFactor()
|
|
{
|
|
Double calibFactor = GetMeterRawCalibFactor();
|
|
var calibDivider = GetMeterRawCalibFactorDefault();
|
|
if (0 != calibDivider)
|
|
{
|
|
calibFactor /= calibDivider;
|
|
}
|
|
else
|
|
{
|
|
throw new ApplicationException("Default calibration factor row value is 0");
|
|
}
|
|
return calibFactor;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Genesis internal raw default value (around 15625)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public UInt16 GetMeterRawCalibFactorDefault()
|
|
{
|
|
return MeterConfig.CalibrationDefault;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set the meter raw calibration factor (around 15625)
|
|
/// </summary>
|
|
/// <param name="meterRawCalibFactor"></param>
|
|
public void SetMeterRawCalibFactor(UInt16 meterRawCalibFactor)
|
|
{
|
|
_meterRawCalibFactor = meterRawCalibFactor;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the meter raw calibration factor (around 15625)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public UInt16 GetMeterRawCalibFactor()
|
|
{
|
|
return _meterRawCalibFactor;
|
|
}
|
|
}
|
|
}
|