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 isValid = (_) => true; protected Func calcValue = (_) => 0d; public OperationDetector(int idOperation) { this.idOperation = idOperation; } public bool TryDetect(int idTelemetry, DetectableTelemetry[] telemetryFragment, out IEnumerable operations) { if(!telemetryFragment.Any() || !isValid(telemetryFragment)) { operations = Enumerable.Empty(); return false; } var operation = MakeOperation(idTelemetry, telemetryFragment); operations = new List { 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 isValid) { this.isValid = isValid; return this; } public OperationDetector SetCalcValueDelegate(Func calcValue) { this.calcValue = calcValue; return this; } } }