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

67 lines
2.1 KiB
C#

using AsbCloudDb.Model;
using System;
using System.Collections.Generic;
using System.Linq;
namespace AsbCloudInfrastructure.Services.DetectOperations.Detectors
{
internal class OperationDetector : IOperationDetector
{
private readonly int idOperation;
protected Func<DetectableTelemetry[], bool> isValid = (_) => true;
protected Func<DetectableTelemetry[], double> calcValue = (_) => 0d;
public OperationDetector(int idOperation)
{
this.idOperation = idOperation;
}
public bool TryDetect(int idTelemetry, DetectableTelemetry[] telemetryFragment, out IEnumerable<DetectedOperation> operations)
{
if(!telemetryFragment.Any() || !isValid(telemetryFragment))
{
operations = Enumerable.Empty<DetectedOperation>();
return false;
}
var operation = MakeOperation(idTelemetry, telemetryFragment);
operations = new List<DetectedOperation> { operation };
return true;
}
private DetectedOperation MakeOperation(int idTelemetry, DetectableTelemetry[] telemetryFragment)
{
var pBegin = telemetryFragment.First();
var pEnd = telemetryFragment.Last();
var operation = new DetectedOperation
{
IdTelemetry = idTelemetry,
IdCategory = idOperation,
IdUsersAtStart = pBegin.IdUser ?? -1,
DateStart = pBegin.DateTime,
DateEnd = pEnd.DateTime,
DepthStart = (double)pBegin.WellDepth,
DepthEnd = (double)pEnd.WellDepth,
Value = calcValue(telemetryFragment),
};
return operation;
}
public OperationDetector SetIsValidDelegate(Func<DetectableTelemetry[], bool> isValid)
{
this.isValid = isValid;
return this;
}
public OperationDetector SetCalcValueDelegate(Func<DetectableTelemetry[], double> calcValue)
{
this.calcValue = calcValue;
return this;
}
}
}