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
{
///
/// PipeLineLems后台工作者
///
public class PipeLineLemsWorker : AsyncPeriodicBackgroundWorkerBase
{
private readonly ILogger _logger;
private readonly VariableService _variableService;
///
/// Initializes a new instance of the class.
///
/// The timer.
/// The service scope factory.
public PipeLineLemsWorker(ILogger logger, AbpAsyncTimer timer, VariableService variableService, IServiceScopeFactory serviceScopeFactory) : base(timer, serviceScopeFactory)
{
_logger = logger;
_variableService = variableService;
Timer.Period = 1 * 300 * 1000; // 每隔 300 秒 执行一次
Timer.RunOnStart = true;
}
///
protected override async Task DoWorkAsync(PeriodicBackgroundWorkerContext workerContext)
{
var projectAccessor = workerContext.ServiceProvider.GetRequiredService();
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 {{ "OEE", oee } });
}
// 计算OEE=可用性×性能×质量
public async Task 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 ReadDoubleValueAsync(string variableName)
{
var variable = await _variableService.ReadValueAsync(variableName);
return (variable?.Content?.Value).SafeString().ToDoubleOrNull().GetValueOrDefault();
}
}
}