198 lines
6.1 KiB
C#
198 lines
6.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Threading;
|
|
using System.Xml;
|
|
using XYLEM.Base.XML;
|
|
|
|
namespace XYLEM.Device
|
|
{
|
|
[Serializable]
|
|
public class CalibrationPoints : IComparable<CalibrationPoints>, IEquatable<CalibrationPoints>
|
|
{
|
|
/// <summary>
|
|
/// calibrations to or from DUT
|
|
/// </summary>
|
|
public List<CalibrationPoint> calibrations { get; private set; }
|
|
|
|
public void SaveToFile(string filePath)
|
|
{
|
|
XMLWriteFuncClass.WriteToXmlFile(filePath, calibrations, false);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="filePath"></param>
|
|
/// <returns>Is a valid calibration for DUT</returns>
|
|
public bool LoadFromFile(string filePath)
|
|
{
|
|
var pointsSaved = XMLWriteFuncClass.ReadFromXmlFile<List<CalibrationPoint>>(filePath);
|
|
calibrations = pointsSaved;
|
|
return IsOkay();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Constructor for A empty list of calibrations, to use for loading calibrations from file
|
|
/// </summary>
|
|
public CalibrationPoints()
|
|
{
|
|
calibrations = new List<CalibrationPoint>();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="cal_points"></param>
|
|
/// <param name="maxTotalPoints">Maximum numbers of calibration able to write to device</param>
|
|
public CalibrationPoints(List<CalibrationPoint> cal_points, int maxTotalPoints)
|
|
{
|
|
if (cal_points == null)
|
|
{
|
|
throw new ArgumentNullException("Calibrations points can't be zero");
|
|
}
|
|
if (cal_points.Count > maxTotalPoints)
|
|
{
|
|
throw new Exception($"Has {cal_points.Count} calibrations points and only support {maxTotalPoints}");
|
|
}
|
|
// Pad with zero if not the full size. Done to make a working writing and comparing two calibrations.
|
|
if (cal_points.Count < maxTotalPoints)
|
|
{
|
|
var missing = maxTotalPoints-cal_points.Count;
|
|
cal_points.AddRange(Enumerable.Range(0, missing).Select(_ => new CalibrationPoint(0, 0)));
|
|
}
|
|
calibrations = cal_points;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Is a valid calibration to or from DUT
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public Boolean IsOkay()
|
|
{
|
|
if ((null == calibrations) || (0 > calibrations.Count))
|
|
{
|
|
return false; // Not okay
|
|
}
|
|
return true; // Is okay
|
|
}
|
|
|
|
/// <summary>
|
|
/// Number of calibrations to or from DUT
|
|
/// </summary>
|
|
/// <returns> is 0 or more if okay and -1 on error / not okay</returns>
|
|
public int Get_number_of_calibrations()
|
|
{
|
|
if (!IsOkay())
|
|
{
|
|
return -1;
|
|
}
|
|
return calibrations.Count;
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
if (!IsOkay())
|
|
{
|
|
return "No valide calibration to print out";
|
|
}
|
|
var ret_txt = "";
|
|
var idx = 0;
|
|
foreach (var cal in calibrations)
|
|
{
|
|
ret_txt += $"{idx++}: {cal}\n";
|
|
}
|
|
return ret_txt;
|
|
}
|
|
|
|
public int CompareTo(CalibrationPoints other)
|
|
{
|
|
// If null, it can't be the same
|
|
if (System.Object.ReferenceEquals(other, null))
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
// Any of them is not okay
|
|
if (!this.IsOkay() || !other.IsOkay())
|
|
{
|
|
return -1; // Not the same
|
|
}
|
|
// if number of items is not the same
|
|
if (this.Get_number_of_calibrations() != other.Get_number_of_calibrations())
|
|
{
|
|
return -1; // Not the same
|
|
}
|
|
var num_of_cals = this.calibrations.Count;
|
|
var my_cals = this.calibrations;
|
|
var others_cals = other.calibrations;
|
|
for (int i = 0; i < num_of_cals; i++)
|
|
{
|
|
if (my_cals[i] != others_cals[i])
|
|
{
|
|
return -1; // Not the same
|
|
}
|
|
}
|
|
return 0; // is the same
|
|
}
|
|
|
|
public bool Equals(CalibrationPoints other)
|
|
{
|
|
return this.CompareTo(other) == 0;
|
|
}
|
|
|
|
#region Operator == > < ! ..
|
|
public static bool operator ==(CalibrationPoints a, CalibrationPoints b)
|
|
{
|
|
// If both are null, or both are same instance, return true.
|
|
if (System.Object.ReferenceEquals(a, b))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
// If one is null, but not both, return false.
|
|
// from https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/how-to-define-value-equality-for-a-type?redirectedfrom=MSDN
|
|
if (a is null)
|
|
{
|
|
// null == null = true.
|
|
if (b is null)
|
|
{
|
|
return true;
|
|
}
|
|
// Only the left side is null.
|
|
return false;
|
|
}
|
|
|
|
// Return true if the fields match:
|
|
return (a.Equals(b));
|
|
}
|
|
|
|
public static bool operator !=(CalibrationPoints a, CalibrationPoints b)
|
|
{
|
|
return !(a == b);
|
|
}
|
|
|
|
public static bool operator <(CalibrationPoints a, CalibrationPoints b)
|
|
{
|
|
return a.CompareTo(b) < 0;
|
|
}
|
|
|
|
public static bool operator >(CalibrationPoints a, CalibrationPoints b)
|
|
{
|
|
return a.CompareTo(b) > 0;
|
|
}
|
|
|
|
public static bool operator >=(CalibrationPoints a, CalibrationPoints b)
|
|
{
|
|
return a.CompareTo(b) >= 0;
|
|
}
|
|
|
|
public static bool operator <=(CalibrationPoints a, CalibrationPoints b)
|
|
{
|
|
return a.CompareTo(b) <= 0;
|
|
}
|
|
#endregion
|
|
}
|
|
} |