using CMS.Extensions.Variable;
|
using CMS.Project.Abstractions;
|
using KissUtil.Extensions;
|
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.Logging;
|
using Volo.Abp.BackgroundWorkers;
|
using Volo.Abp.Threading;
|
|
namespace CMS.Plugin.PipeLineLems.Workers
|
{
|
/// <summary>
|
/// PipeLineLems后台工作者
|
/// </summary>
|
public class PipeLineLemsWorker : AsyncPeriodicBackgroundWorkerBase
|
{
|
private readonly ILogger<PipeLineLemsWorker> _logger;
|
private readonly VariableService _variableService;
|
|
/// <summary>
|
/// Initializes a new instance of the <see cref="PipeLineLemsWorker"/> class.
|
/// </summary>
|
/// <param name="timer">The timer.</param>
|
/// <param name="serviceScopeFactory">The service scope factory.</param>
|
public PipeLineLemsWorker(ILogger<PipeLineLemsWorker> logger, AbpAsyncTimer timer, VariableService variableService, IServiceScopeFactory serviceScopeFactory) : base(timer, serviceScopeFactory)
|
{
|
_logger = logger;
|
_variableService = variableService;
|
Timer.Period = 1 * 300 * 1000; // 每隔 300 秒 执行一次
|
Timer.RunOnStart = true;
|
}
|
|
/// <inheritdoc />
|
protected override async Task DoWorkAsync(PeriodicBackgroundWorkerContext workerContext)
|
{
|
var projectAccessor = workerContext.ServiceProvider.GetRequiredService<IProjectAccessor>();
|
var project = await projectAccessor.GetProjectAsync();
|
if (project?.Info == null)
|
{
|
return;
|
}
|
|
//_logger.LogInformation($"PipeLineLemsWorker is working for project {project.Info.Id}");
|
|
//// 计算OEE
|
//double oee = await CalculateOEEAsync();
|
|
//// 使用百分比格式 打印结果日志
|
//_logger.LogInformation($"OEE:{oee:P2}");
|
|
//// 写入变量
|
//await _variableService.WriteValueAsync(new Dictionary<string, object> {{ "OEE", oee } });
|
}
|
|
// 计算OEE=可用性×性能×质量
|
public async Task<double> CalculateOEEAsync()
|
{
|
// 读取OEE计算所需的值
|
double availability = await ReadDoubleValueAsync("Availability");
|
double performance = await ReadDoubleValueAsync("Performance");
|
double quality = await ReadDoubleValueAsync("Quality");
|
|
// 计算OEE
|
double oee = availability * performance * quality;
|
|
// 返回计算结果
|
return oee;
|
}
|
|
// 提供一个泛型方法,方便直接获取double类型的值
|
public async Task<double> ReadDoubleValueAsync(string variableName)
|
{
|
var variable = await _variableService.ReadValueAsync(variableName);
|
return (variable?.Content?.Value).SafeString().ToDoubleOrNull().GetValueOrDefault();
|
}
|
}
|
}
|