2025-09-04 13:40:07 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2025-09-07 08:51:18 +08:00
|
|
|
|
using AutoMapper;
|
2025-09-04 13:40:07 +08:00
|
|
|
|
using DMS.Application.Interfaces;
|
|
|
|
|
|
using DMS.Application.Models;
|
2025-09-07 08:51:18 +08:00
|
|
|
|
using DMS.Core.Interfaces.Services;
|
2025-09-04 13:40:07 +08:00
|
|
|
|
using DMS.Core.Models;
|
|
|
|
|
|
|
|
|
|
|
|
namespace DMS.Application.Services.Processors;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 负责将变量数据发布到MQTT的处理器。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class MqttPublishProcessor : IVariableProcessor
|
|
|
|
|
|
{
|
2025-09-07 08:51:18 +08:00
|
|
|
|
private readonly IMapper _mapper;
|
|
|
|
|
|
private readonly IMqttServiceManager _mqttServiceManager;
|
2025-09-06 15:19:04 +08:00
|
|
|
|
|
2025-09-07 08:51:18 +08:00
|
|
|
|
public MqttPublishProcessor(IMapper mapper, IMqttServiceManager mqttServiceManager)
|
|
|
|
|
|
{
|
|
|
|
|
|
_mapper = mapper;
|
|
|
|
|
|
_mqttServiceManager = mqttServiceManager;
|
|
|
|
|
|
}
|
2025-09-04 13:40:07 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 处理单个变量上下文,如果有关联的MQTT配置,则将其推送到发送队列。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="context">包含变量及其元数据的上下文对象。</param>
|
|
|
|
|
|
public async Task ProcessAsync(VariableContext context)
|
|
|
|
|
|
{
|
2025-09-07 08:51:18 +08:00
|
|
|
|
var variable = context.Data;
|
|
|
|
|
|
if (variable?.MqttAliases == null || variable.MqttAliases.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return; // 没有关联的MQTT配置,直接返回
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 遍历所有关联的MQTT配置,并将其推入发送队列
|
2025-09-10 18:15:31 +08:00
|
|
|
|
foreach (var variableMqttAliasDto in variable.MqttAliases)
|
2025-09-07 08:51:18 +08:00
|
|
|
|
{
|
2025-09-11 11:04:07 +08:00
|
|
|
|
if (!variableMqttAliasDto.MqttServer.IsActive)
|
|
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-07 08:51:18 +08:00
|
|
|
|
// 发布变量数据到MQTT服务器
|
2025-09-10 18:15:31 +08:00
|
|
|
|
var variableMqttAlias = _mapper.Map<VariableMqttAlias>(variableMqttAliasDto);
|
2025-10-05 19:57:58 +08:00
|
|
|
|
variableMqttAlias.Variable.DisplayValue=variable.DisplayValue;
|
2025-09-10 18:15:31 +08:00
|
|
|
|
await _mqttServiceManager.PublishVariableDataAsync(variableMqttAlias);
|
2025-09-07 08:51:18 +08:00
|
|
|
|
}
|
2025-09-04 13:40:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|