diff --git a/AsbCloudInfrastructure/Services/Subsystems/DepthInterpolation.cs b/AsbCloudInfrastructure/Services/Subsystems/DepthInterpolation.cs index def5886a..edb41678 100644 --- a/AsbCloudInfrastructure/Services/Subsystems/DepthInterpolation.cs +++ b/AsbCloudInfrastructure/Services/Subsystems/DepthInterpolation.cs @@ -8,43 +8,41 @@ namespace AsbCloudInfrastructure.Services.Subsystems.Utils internal class DepthInterpolation { private IEnumerator<(DateTimeOffset dateMin, float depthMin, DateTimeOffset dateMax, float depthMax)> enumerator; - (DateTimeOffset x, float? y) lastValue = default; + (DateTimeOffset x, float y) lastValue = default; private DateTimeOffset dateMin; public DepthInterpolation(IOrderedEnumerable<(DateTimeOffset dateMin, float depthMin, DateTimeOffset dateMax, float depthMax)> collection) { enumerator = collection.GetEnumerator(); enumerator.MoveNext(); - dateMin = enumerator.Current.dateMin; + dateMin = enumerator.Current.dateMin; } public float GetDepth(DateTimeOffset date) - { + { + var isNotEnd = true; + while (isOnInterval(date) && isNotEnd) { - while (isLess(date)) - { - lastValue.x = date; - lastValue.y = CalcValue(enumerator.Current, (date - dateMin).TotalSeconds); - enumerator.MoveNext(); - } + lastValue.x = date; + lastValue.y = CalcValue(enumerator.Current, (date- enumerator.Current.dateMin).TotalSeconds); + isNotEnd = enumerator.MoveNext(); } - return (float)lastValue.y; + return lastValue.y; + } - private bool isLess(DateTimeOffset date) - { - if (date >= enumerator.Current.dateMin) - return true; - return false; - } + private bool isOnInterval(DateTimeOffset date) + => date >= enumerator.Current.dateMin && date <= enumerator.Current.dateMax; private float CalcValue((DateTimeOffset dateMin, float depthMin, DateTimeOffset dateMax, float depthMax) item, double timestamp) { - var a = (item.depthMax - item.depthMin) / (item.dateMax - item.dateMin).TotalSeconds; - var b = item.depthMin * (item.dateMax.Second - (item.depthMax - item.depthMin) / (item.dateMax.Second - item.dateMin.Second)); - var resultDepth = a * timestamp + b; - return (float)resultDepth; + if (item.depthMin == item.depthMax) + { + return item.depthMin; + } + var w = timestamp / (item.dateMax - item.dateMin).TotalSeconds; + var d = ((item.depthMax - item.depthMin) * w)+ item.depthMin; + return (float)d; } - } #nullable disable } diff --git a/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs b/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs index def85fa4..62e9e927 100644 --- a/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs +++ b/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs @@ -298,7 +298,7 @@ namespace AsbCloudInfrastructure.Services.Subsystems ); dataDepthFromSaub.Add(dateRowItem); } - var interp = new DepthInterpolation(dataDepthFromSaub.OrderBy(t => t.depthMin)); + var interp = new DepthInterpolation(dataDepthFromSaub.OrderBy(t => t.dateMin)); if (dataSpinList.Any()) { var mode = dataSpinList[0].Mode;