using AsbCloudDb.Model.Subsystems; using System; namespace AsbCloudInfrastructure.Services.Subsystems { public class SubsystemDetector { private readonly int idTelemetry; private readonly int idSubsystem; private readonly Func isEnable; private readonly Func isValid; (bool isEnable, DateTimeOffset date, float depth) pre = default; public SubsystemDetector( int idTelemetry, int idSubsystem, Func isEnable, Func isValid) { this.idTelemetry = idTelemetry; this.idSubsystem = idSubsystem; this.isEnable = isEnable; this.isValid = isValid; } public bool TryDetect(short? mode, DateTimeOffset date, float depth, out SubsystemOperationTime? subsystemOperationTime) { var isEnable = this.isEnable(mode); if (!pre.isEnable && isEnable) { pre = (true, date, depth); } else if (pre.isEnable && !isEnable) { var detected = new SubsystemOperationTime { IdTelemetry = idTelemetry, IdSubsystem = idSubsystem, DateStart = pre.date, DateEnd = date, DepthStart = pre.depth, DepthEnd = depth, }; pre.isEnable = false; if (isValid(detected)) { subsystemOperationTime = detected; return true; } } subsystemOperationTime = null; return false; } } }