АПД разбивается на ротор и слайд

This commit is contained in:
Olga Nemt 2023-08-23 15:35:39 +05:00
parent 23fd77eb9d
commit e28ec2cd0a
2 changed files with 85 additions and 35 deletions

View File

@ -15,7 +15,7 @@ using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services.Subsystems
{
internal static class SubsystemOperationTimeCalcWorkFactory
{
private const string workId = "Subsystem operation time calc";
@ -24,6 +24,8 @@ namespace AsbCloudInfrastructure.Services.Subsystems
private const int idSubsytemTorqueMaster = 65537;
private const int idSubsytemSpinMaster = 65536;
private const int idSubsytemAkb = 1;
private const int idSubsytemAkbRototor = 11;
private const int idSubsytemAkbSlide = 12;
private const int idSubsytemMse = 2;
public static WorkPeriodic MakeWork()
@ -85,8 +87,8 @@ namespace AsbCloudInfrastructure.Services.Subsystems
{
var connection = db.Database.GetDbConnection();
if (
connection?.State is null ||
connection.State == ConnectionState.Broken ||
connection?.State is null ||
connection.State == ConnectionState.Broken ||
connection.State == ConnectionState.Closed)
{
await db.Database.OpenConnectionAsync(token);
@ -101,11 +103,20 @@ namespace AsbCloudInfrastructure.Services.Subsystems
private static async Task<IEnumerable<SubsystemOperationTime>> OperationTimeSaubAsync(int idTelemetry, DateTimeOffset begin, IAsbCloudDbContext db, CancellationToken token)
{
static bool isSubsytemAkb(short? mode)
static bool isSubsytemAkbRotor(short? mode)
{
if (mode is null)
return false;
if (mode == 1 | mode == 3)
if (mode == 1)
return true;
return false;
}
static bool isSubsytemAkbSlide(short? mode)
{
if (mode is null)
return false;
if (mode == 3)
return true;
return false;
}
@ -138,8 +149,9 @@ namespace AsbCloudInfrastructure.Services.Subsystems
using var result = await ExecuteReaderAsync(db, query, token);
var subsystemsOperationTimes = new List<SubsystemOperationTime>();
(bool isEnable, DateTimeOffset date, float depth) akbPre = default;
(bool isEnable, DateTimeOffset date, float depth) akbPreRotor = default;
(bool isEnable, DateTimeOffset date, float depth) akbPreSlide = default;
(bool isEnable, DateTimeOffset date, float depth) msePre = default;
while (result.Read())
@ -147,29 +159,50 @@ namespace AsbCloudInfrastructure.Services.Subsystems
var mode = result.GetFieldValue<short?>(1);
var state = result.GetFieldValue<short?>(3);
var isAkbEnable = isSubsytemAkb(mode);
var isAkbRotorEnable = isSubsytemAkbRotor(mode);
var isAkbSlideEnable = isSubsytemAkbSlide(mode);
var isMseEnable = IsSubsystemMse(state);
var date = result.GetFieldValue<DateTimeOffset>(0);
var depth = result.GetFieldValue<float>(2);
var depth = result.GetFieldValue<float>(2);
if (!akbPre.isEnable && isAkbEnable)
if (!akbPreRotor.isEnable && isAkbRotorEnable)
{
akbPre = (true, date, depth);
akbPreRotor = (true, date, depth);
}
else if (akbPre.isEnable && !isAkbEnable)
else if (akbPreRotor.isEnable && !isAkbRotorEnable)
{
var subsystemOperationTime = new SubsystemOperationTime
{
IdTelemetry = idTelemetry,
IdSubsystem = idSubsytemAkb,
DateStart = akbPre.date,
IdSubsystem = idSubsytemAkbRototor,
DateStart = akbPreRotor.date,
DateEnd = date,
DepthStart = akbPre.depth,
DepthStart = akbPreRotor.depth,
DepthEnd = depth,
};
if (IsValid(subsystemOperationTime))
subsystemsOperationTimes.Add(subsystemOperationTime);
akbPre.isEnable = false;
akbPreRotor.isEnable = false;
}
if (!akbPreSlide.isEnable && isAkbSlideEnable)
{
akbPreSlide = (true, date, depth);
}
else if (akbPreSlide.isEnable && !isAkbSlideEnable)
{
var subsystemOperationTime = new SubsystemOperationTime
{
IdTelemetry = idTelemetry,
IdSubsystem = idSubsytemAkbSlide,
DateStart = akbPreSlide.date,
DateEnd = date,
DepthStart = akbPreSlide.depth,
DepthEnd = depth,
};
if (IsValid(subsystemOperationTime))
subsystemsOperationTimes.Add(subsystemOperationTime);
akbPreSlide.isEnable = false;
}
if (!msePre.isEnable && isMseEnable)
@ -182,21 +215,22 @@ namespace AsbCloudInfrastructure.Services.Subsystems
{
IdTelemetry = idTelemetry,
IdSubsystem = idSubsytemMse,
DateStart = akbPre.date,
DateStart = akbPreRotor.date < akbPreSlide.date ? akbPreRotor.date : akbPreSlide.date,
DateEnd = date,
DepthStart = akbPre.depth,
DepthStart = akbPreRotor.depth < akbPreSlide.depth ? akbPreRotor.depth : akbPreSlide.depth,
DepthEnd = depth,
};
if (IsValid(subsystemOperationTime))
subsystemsOperationTimes.Add(subsystemOperationTime);
msePre.isEnable = false;
}
}
return subsystemsOperationTimes;
}
private static async Task<IEnumerable<SubsystemOperationTime>> OperationTimeSpinAsync(int idTelemetry, DateTimeOffset begin, IAsbCloudDbContext db, CancellationToken token)
{
static int? GetSubsytemId(short? mode, int? state)
@ -215,7 +249,7 @@ namespace AsbCloudInfrastructure.Services.Subsystems
$"select " +
$" tspin.date, " +
$" tspin.mode, " +
$" tspin.state " +
$" tspin.state " +
$"from ( " +
$" select " +
$" date, " +
@ -226,7 +260,7 @@ namespace AsbCloudInfrastructure.Services.Subsystems
$" lag(state, 1) over (order by date) as state_lag " +
$" from t_telemetry_data_spin " +
$" where id_telemetry = {idTelemetry} and date >= '{begin:u}'" +
$" order by date ) as tspin " +
$" order by date ) as tspin " +
$"where mode_lag is null or state_lag is null or (mode != mode_lag and mode_lead != mode_lag) or state != state_lag " +
$"order by date;";
@ -239,12 +273,12 @@ namespace AsbCloudInfrastructure.Services.Subsystems
var mode = resultSpin.GetFieldValue<short?>(1);
var state = resultSpin.GetFieldValue<short?>(2);
var idSubsystem = GetSubsytemId(mode, state);
if(idSubsystemLast != idSubsystem)
if (idSubsystemLast != idSubsystem)
{
idSubsystemLast = idSubsystem;
var date = resultSpin.GetFieldValue<DateTimeOffset>(0);
rows.Add((idSubsystem, date));
}
}
}
await resultSpin.DisposeAsync();
}
@ -260,7 +294,7 @@ namespace AsbCloudInfrastructure.Services.Subsystems
return Enumerable.Empty<SubsystemOperationTime>();
var subsystemsOperationTimes = new List<SubsystemOperationTime>(32);
for (int i = 1; i < rows.Count; i++)
{
var r0 = rows[i - 1];
@ -276,12 +310,12 @@ namespace AsbCloudInfrastructure.Services.Subsystems
DepthStart = depthInterpolation.GetDepth(r0.Date),
DepthEnd = depthInterpolation.GetDepth(r1.Date),
};
if (IsValid(subsystemOperationTime))
subsystemsOperationTimes.Add(subsystemOperationTime);
}
}
return subsystemsOperationTimes;
}
@ -336,7 +370,7 @@ namespace AsbCloudInfrastructure.Services.Subsystems
if (!dataDepthFromSaub.Any())
return null;
var depthInterpolation = new DepthInterpolation(dataDepthFromSaub.Select(i=>(i.DateMin, i.DepthMin)));
var depthInterpolation = new DepthInterpolation(dataDepthFromSaub.Select(i => (i.DateMin, i.DepthMin)));
return depthInterpolation;
}
}

View File

@ -146,28 +146,44 @@ namespace AsbCloudInfrastructure.Services.Subsystems
private IEnumerable<SubsystemStatDto> CalcStat(IEnumerable<SubsystemOperationTimeDto> dtos, (double depthIntervalRotor, double depthIntervalSlide) depthInterval)
{
var groupedDataSubsystems = dtos
.OrderBy(o => o.Id)
.GroupBy(o => o.IdSubsystem);
var periodGroupTotal = dtos.Sum(o => (o.DateEnd - o.DateStart).TotalHours);
var result = groupedDataSubsystems.Select(g =>
{
var depthIntervalSubsystem = GetDepthIntervalSubsystem(g.Key, depthInterval);
var periodGroup = g.Sum(o => (o.DateEnd - o.DateStart).TotalHours);
var periodGroupDepth = g.Sum(o => o.DepthEnd - o.DepthStart);
var periodGroupDepth = g.Sum(o => o.DepthEnd - o.DepthStart);
var subsystemStat = new SubsystemStatDto()
{
IdSubsystem = g.Key,
SubsystemName = subsystemService.GetOrDefault(g.Key)?.Name ?? "unknown",
UsedTimeHours = periodGroup,
UsedTimeHours = periodGroup,
KUsage = periodGroupDepth / depthIntervalSubsystem,
SumDepthInterval = periodGroupDepth,
OperationCount = g.Count()
OperationCount = g.Count()
};
if(subsystemStat.KUsage > 1)
if (subsystemStat.KUsage > 1)
subsystemStat.KUsage = 1;
return subsystemStat;
});
var apdParts = result.Where(x => x.IdSubsystem == 11 || x.IdSubsystem == 12);
if (apdParts.Any())
{
var apdSum = new SubsystemStatDto()
{
IdSubsystem = 1,
SubsystemName = "АПД",
UsedTimeHours = apdParts.Sum(part => part.UsedTimeHours),
KUsage = apdParts.Sum(part => part.KUsage) / apdParts.Count(),
SumDepthInterval = apdParts.Sum(part => part.SumDepthInterval),
OperationCount = apdParts.Sum(part => part.OperationCount),
};
result = result.Append(apdSum).OrderBy(m => m.IdSubsystem);
}
return result;
}
private static (double depthIntervalRotor, double depthIntervalSlide) GetDepthInterval (IEnumerable<DetectedOperationDto> detectedOperations)
@ -348,8 +364,8 @@ namespace AsbCloudInfrastructure.Services.Subsystems
if (request?.Take > 0)
query = query.Take((int)request.Take);
else
query = query.Take(3000);
//else
//query = query.Take(3000);
return query;
}