Files
WCS/Plugins/Driver/Cowain.Driver/DeviceHostedService.cs

102 lines
3.9 KiB
C#
Raw Normal View History

using Cowain.Base.Helpers;
using Plugin.Cowain.Driver.ViewModels;
using Plugin.Cowain.Driver.Abstractions;
using Plugin.Cowain.Driver.IServices;
using Plugin.Cowain.Driver.Models.Enum;
using Microsoft.Extensions.Logging;
namespace Plugin.Cowain.Driver;
public class DeviceHostedService : BackgroundHostedService
{
private readonly IDeviceMonitor _deviceMonitor;
private readonly IDeviceService _deviceService;
private readonly IDriverPluginService _driverPlugin;
private readonly IActionPluginService _actionPlugin;
private readonly ITagService _tagService;
private readonly IActionService _actionService;
private readonly ILogger<DeviceHostedService> _logger;
public DeviceHostedService(IDeviceMonitor deviceMonitor, IDeviceService deviceService, IDriverPluginService driverPlugin, IActionPluginService actionPlugin, ITagService tagService, IActionService actionService, ILogger<DeviceHostedService> logger)
{
_deviceMonitor = deviceMonitor;
_deviceService = deviceService;
_driverPlugin = driverPlugin;
_actionPlugin = actionPlugin;
_tagService = tagService;
_actionService = actionService;
_logger = logger;
}
public override async Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("DeviceHostedService StopAsync 开始执行");
if (ExecuteTask != null)
{
var stoppingCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
stoppingCts.Cancel();
try
{
// 正确使用异步操作并传递取消令牌
var stopTasks = _deviceMonitor.DeviceThreads
.Select(hostTask => hostTask.StopReadAsync(stoppingCts.Token))
.ToList();
await Task.WhenAll(stopTasks);
_logger.LogInformation("DeviceHostedService 所有设备线程已停止");
}
finally
{
await ExecuteTask.WaitAsync(stoppingCts.Token).ConfigureAwait(ConfigureAwaitOptions.SuppressThrowing);
_logger.LogInformation("DeviceHostedService StopAsync 执行完成");
}
}
}
[LogAndSwallow]
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
var devices = await _deviceService.GetAllAsync();
foreach (var device in devices)
{
var driver = _driverPlugin.GetDriver(device.DriverName);
if (driver != null)
{
driver.DeviceName = device.DeviceName;
//需要获取变量表
var tags = await _tagService.GetDeviceTagsAsync(device.Id);
var variables = tags.Select(tag => new VariableViewModel
{
Id = tag.Id,
DeviceId = tag.DeviceId,
Name = tag.Name,
Address = tag.Address,
Desc = tag.Desc,
DataType = Enum.Parse<DataTypeEnum>(tag.DataType),
OperMode = Enum.Parse<OperModeEnum>(tag.OperMode),
AlarmEnable = tag.AlarmEnable,
AlarmValue = tag.AlarmValue,
AlarmMsg = tag.AlarmMsg,
AlarmGroup = tag.AlarmGroup,
AlarmLevel = tag.AlarmLevel,
Json = tag.Json,
ArrayCount = tag.ArrayCount
}).ToList();
device.Variables = new(variables);
var actions = await _actionService.GetDeviceActionsAsync(device.Id);
device.VarActions = new(actions);
_deviceMonitor.AddDevice(driver, _actionPlugin, device);
}
}
// 正确使用异步操作并传递取消令牌
var startTasks = _deviceMonitor.DeviceThreads
.Select(hostTask => hostTask.StartReadAsync(stoppingToken))
.ToList();
}
}