202 lines
6.9 KiB
C#
202 lines
6.9 KiB
C#
using OmniPlus.Features;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace OmniPlus
|
|
{
|
|
internal class OmniPlusMeter
|
|
{
|
|
private readonly IDictionary<OmniPlusTestPoint, OmniPlusTest> testPoints;
|
|
|
|
private SystemParameters systemParameters;
|
|
private ConfigurationParameters configurationParameters;
|
|
private RegisterSettings registerSettings;
|
|
|
|
public OmniPlusMeter(bool connected = false)
|
|
{
|
|
this.Connected = connected;
|
|
this.systemParameters = new SystemParameters();
|
|
this.configurationParameters = new ConfigurationParameters();
|
|
this.testPoints = new Dictionary<OmniPlusTestPoint, OmniPlusTest>();
|
|
}
|
|
|
|
public bool Connected { get; set; }
|
|
|
|
public bool Initialized
|
|
=> this.RebootsCount != 0xFF
|
|
&& this.FirmwareInRange
|
|
&& this.ManifacturingTimeInRange
|
|
&& this.SystemTimeInRange
|
|
&& this.VoltsInRange
|
|
&& !this.FactorySealed;
|
|
|
|
public bool CanCalibrate { get; set; } = true;
|
|
|
|
public bool Calibrated { get; private set; }
|
|
|
|
public string BuildInfo
|
|
{
|
|
get => this.systemParameters.BuildInfo.Value;
|
|
set => this.systemParameters.BuildInfo.Value = value;
|
|
}
|
|
|
|
public uint ManifacturingTime
|
|
{
|
|
get => this.systemParameters.ManifacturingTime.SecondsSince;
|
|
set => this.systemParameters.ManifacturingTime.SecondsSince = value;
|
|
}
|
|
|
|
public bool ManifacturingTimeInRange { get; private set; }
|
|
|
|
public uint SystemTime
|
|
{
|
|
get => this.systemParameters.SystemTime.SecondsSince;
|
|
set => this.systemParameters.SystemTime.SecondsSince = value;
|
|
}
|
|
|
|
public int SystemTimeDrift { get; private set; }
|
|
|
|
public bool SystemTimeInRange { get; private set; }
|
|
|
|
public byte RebootsCount
|
|
{
|
|
get => this.systemParameters.RebootsCount;
|
|
set => this.systemParameters.RebootsCount = value;
|
|
}
|
|
|
|
public string Firmware
|
|
{
|
|
get => this.systemParameters.VersionAndType.Value;
|
|
set => this.systemParameters.VersionAndType.Value = value;
|
|
}
|
|
|
|
public bool FirmwareInRange { get; private set; }
|
|
|
|
public double Volts
|
|
{
|
|
get => Math.Max(this.systemParameters.BatteryVoltage.Volts, (ushort)1) / 1000.0;
|
|
set => this.systemParameters.BatteryVoltage.Volts = (ushort)(value * 1000);
|
|
}
|
|
|
|
public bool VoltsInRange => 3.19 <= this.Volts && this.Volts <= 3.75;
|
|
|
|
public double CorrectionFactor { get; private set; }
|
|
|
|
public sbyte FactoryCorrection
|
|
{
|
|
get => (sbyte)(this.CorrectionFactor * 10);
|
|
}
|
|
|
|
public bool FactorySealed
|
|
{
|
|
get => this.systemParameters.FactorySealed;
|
|
set => this.systemParameters.FactorySealed = value;
|
|
}
|
|
|
|
public ProgrammableID ProgrammableID
|
|
=> this.configurationParameters.ProgrammableID;
|
|
|
|
public FactoryID FactoryID
|
|
=> this.configurationParameters.FactoryID;
|
|
|
|
public sbyte ReadingDigits
|
|
=> this.configurationParameters?.ReadingDigits ?? 0;
|
|
|
|
public Readings Readings
|
|
=> this.configurationParameters.Readings;
|
|
|
|
public OptionalUniDirFields OptionalUniDirFields
|
|
=> this.configurationParameters.OptionalUniDirFields;
|
|
|
|
public byte AlartPersistenceDays
|
|
=> this.configurationParameters.AlarmPersistenceDays;
|
|
|
|
public ushort DataLogIntervalMins
|
|
=> this.configurationParameters.DataLogIntervalMins;
|
|
|
|
internal double AddCalibrationResult(OmniPlusTestPoint point, ChamberCalibrationResponse result)
|
|
{
|
|
this.testPoints[point] = new OmniPlusTest
|
|
{
|
|
Rotations = result.Rotations,
|
|
Duration = result.Duration,
|
|
MeterVolume = point.VolumeM3(result.Rotations, result.Duration)
|
|
};
|
|
|
|
return this.testPoints[point].MeterVolume;
|
|
}
|
|
|
|
internal double AddCalibrationVolume(OmniPlusTestPoint point, double volumeMessured)
|
|
{
|
|
this.testPoints[point].VolumeMessured = volumeMessured;
|
|
|
|
return this.testPoints[point].DifferencePercentage;
|
|
}
|
|
|
|
internal void AddConfigurationParameters(ConfigurationParameters configurationParameters)
|
|
=> this.configurationParameters = configurationParameters;
|
|
|
|
internal void AddRegisterSettings(RegisterSettings registerSettings)
|
|
=> this.registerSettings = registerSettings;
|
|
|
|
internal void AddSystemParameters(SystemParameters systemParameters)
|
|
{
|
|
if (systemParameters != null)
|
|
{
|
|
this.systemParameters = systemParameters;
|
|
|
|
this.SystemTimeDrift = (DateTime.UtcNow - this.systemParameters.SystemTime.DateTime).Seconds;
|
|
this.SystemTimeInRange = Math.Abs(this.SystemTimeDrift) <= 700;
|
|
this.ManifacturingTimeInRange = Math.Abs((DateTime.UtcNow - this.systemParameters.ManifacturingTime.DateTime).Days) <= 365;
|
|
|
|
var firmwareTokens = this.Firmware
|
|
?.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)
|
|
?? new string[2];
|
|
|
|
var versionString = string.Empty;
|
|
|
|
if (firmwareTokens?.Length > 0 && firmwareTokens[0]?.Length > 0)
|
|
{
|
|
versionString = firmwareTokens[0].Substring(1);
|
|
}
|
|
|
|
var versionParsed = double.TryParse(versionString, out var version);
|
|
var typeString = string.Empty;
|
|
|
|
if (firmwareTokens?.Length > 1 && firmwareTokens[1]?.Length > 0)
|
|
{
|
|
typeString = firmwareTokens[1].ToUpper();
|
|
}
|
|
|
|
var isOmni = typeString == "OMNI-01" || typeString == "OMNI-02";
|
|
|
|
this.FirmwareInRange = versionParsed && version >= 1.23 && isOmni;
|
|
}
|
|
}
|
|
|
|
internal bool Compare(RegisterSettings registerSettingsAfter)
|
|
=> $"{this.registerSettings}" == $"{registerSettingsAfter}";
|
|
|
|
internal bool Compare(SystemParameters systemParametersAfter)
|
|
=> $"{this.systemParameters}" == $"{systemParametersAfter}";
|
|
|
|
internal bool Compare(ConfigurationParameters configurationParametersAfter)
|
|
=> configurationParametersAfter.Equals(this.configurationParameters);
|
|
|
|
internal double GetCorrectionFactor()
|
|
{
|
|
this.CorrectionFactor = this.testPoints
|
|
.OrderBy(x => x.Key.GalPerMin)
|
|
.Select(x => x.Value.DifferencePercentage)
|
|
.ToArray()
|
|
.GetCorrectionFactor();
|
|
|
|
this.CanCalibrate = this.CorrectionFactor != double.MaxValue;
|
|
|
|
return this.CorrectionFactor;
|
|
}
|
|
}
|
|
}
|