DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/DetectOperations/Detectors/DetectorAbstract.cs

98 lines
3.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using AsbCloudDb.Model;
namespace AsbCloudInfrastructure.Services.DetectOperations
{
#nullable enable
abstract class DetectorAbstract
{
public int StepLength { get; set; } = 3;
public int FragmentLength { get; set; } = 6;
public int IdCategory { get; }
// TODO: assert MaxDurationSeconds and MinDurationSeconds
public double MaxDurationSeconds { get; } = 24 * 60 * 60;
public double MinDurationSeconds { get; } = 3;
/// <summary>
/// Конструктор детектора.
/// Словарь с IdCategory в дефолтных данных AsbCloudDbContext для таблицы WellOperationCategory
/// </summary>
/// <param name="IdCategory">ключ названия/описания операции из таблицы WellOperationCategory</param>
public DetectorAbstract(int IdCategory)
{
this.IdCategory = IdCategory;
}
public virtual DetectedOperation? DetectOrDefault(DetectableTelemetry[] telemetry, ref int positionStart)
{
if ((telemetry.Length > positionStart + FragmentLength + StepLength) && DetectStart(telemetry, positionStart))
{
var skip = positionStart + StepLength;
while (telemetry.Length > skip + FragmentLength)
{
if (DetectEnd(telemetry, skip))
{
var dateStart = telemetry[positionStart].DateTime;
var dateEnd = telemetry[skip].DateTime;
var durationSec = (dateEnd - dateStart).TotalSeconds;
if (durationSec < MinDurationSeconds || durationSec > MaxDurationSeconds)
return null;
var result = new DetectedOperation
{
IdCategory = IdCategory,
IdUsersAtStart = telemetry[positionStart].IdUser ?? -1,
DateStart = dateStart,
DateEnd = dateEnd,
DepthStart = telemetry[positionStart].WellDepth ?? -1d,
DepthEnd = telemetry[skip].WellDepth ?? -1d,
Value = CalcValue(telemetry, positionStart, skip),
};
if (!IsValid(result))
return null;
positionStart = skip;// + FragmentLength;
return result;
}
skip = skip + StepLength;
}
}
return null;
}
/// <summary>
/// Расчет ключевого параметра
/// </summary>
/// <param name="result"></param>
protected abstract double CalcValue(DetectableTelemetry[] telemetry, int positionBegin, int positionEnd);
/// <summary>
/// Определение начала операции
/// </summary>
/// <param name="telemetry"></param>
/// <param name="position"></param>
/// <returns></returns>
protected abstract bool DetectStart(DetectableTelemetry[] telemetry, int position);
/// <summary>
/// Определение окончания операции
/// </summary>
/// <param name="telemetry"></param>
/// <param name="position"></param>
/// <returns></returns>
protected abstract bool DetectEnd(DetectableTelemetry[] telemetry, int position);
/// <summary>
/// Валидация операции
/// </summary>
/// <param name="result"></param>
/// <returns></returns>
protected abstract bool IsValid(DetectedOperation result);
}
#nullable disable
}