初步完成MQTT消息的发送

This commit is contained in:
2025-09-09 18:17:15 +08:00
parent ddfb124204
commit a9ca89b44a
3 changed files with 19 additions and 2 deletions

View File

@@ -4,6 +4,7 @@ using DMS.Application.Interfaces;
using DMS.Core.Interfaces; using DMS.Core.Interfaces;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using DMS.Application.DTOs.Events; using DMS.Application.DTOs.Events;
using DMS.Core.Models;
namespace DMS.Application.Services; namespace DMS.Application.Services;
@@ -85,6 +86,20 @@ public class DataLoaderService : IDataLoaderService
foreach (var variableMqttAliasDto in variableMqttAliasDtos) foreach (var variableMqttAliasDto in variableMqttAliasDtos)
{ {
_appDataStorageService.VariableMqttAliases.TryAdd(variableMqttAliasDto.Id, variableMqttAliasDto); _appDataStorageService.VariableMqttAliases.TryAdd(variableMqttAliasDto.Id, variableMqttAliasDto);
if (_appDataStorageService.Variables.TryGetValue(variableMqttAliasDto.VariableId, out var variable))
{
variableMqttAliasDto.Variable = _mapper.Map<Variable>(variable);
variable.MqttAliases?.Add(variableMqttAliasDto);
}
if (_appDataStorageService.MqttServers.TryGetValue(variableMqttAliasDto.MqttServerId, out var mqttServer))
{
variableMqttAliasDto.MqttServer = _mapper.Map<MqttServer>(mqttServer);
variableMqttAliasDto.MqttServerName = variableMqttAliasDto.MqttServer.ServerName;
mqttServer.VariableAliases?.Add(variableMqttAliasDto);
}
} }
} }

View File

@@ -238,9 +238,10 @@ namespace DMS.Infrastructure.Services
try try
{ {
var topic = context.MqttServer.PublishTopic; var topic = context.MqttServer.PublishTopic;
var payload = variableMqtt.Variable.DataValue?.ToString() ?? string.Empty;
await context.MqttService.PublishAsync(topic, payload); var sendMsg = $"{variableMqtt.Variable.Name}:{variableMqtt.Variable.DataValue}";
await context.MqttService.PublishAsync(topic, sendMsg);
_logger.LogDebug("成功向MQTT服务器 {ServerName} 发布变量 {VariableName} 的数据", _logger.LogDebug("成功向MQTT服务器 {ServerName} 发布变量 {VariableName} 的数据",
context.MqttServer.ServerName, variableMqtt.Variable.Name); context.MqttServer.ServerName, variableMqtt.Variable.Name);
} }

View File

@@ -24,6 +24,7 @@ namespace DMS.WPF.Profiles
.ReverseMap(); .ReverseMap();
CreateMap<MqttServerDto, MqttServerItemViewModel>().ReverseMap(); CreateMap<MqttServerDto, MqttServerItemViewModel>().ReverseMap();
CreateMap<MqttServer, MqttServerItemViewModel>().ReverseMap();
CreateMap<UserDto, UserItemViewModel>().ReverseMap(); CreateMap<UserDto, UserItemViewModel>().ReverseMap();
CreateMap<VariableHistoryDto, VariableHistoryItemViewModel>().ReverseMap(); CreateMap<VariableHistoryDto, VariableHistoryItemViewModel>().ReverseMap();
CreateMap<VariableDto, VariableItemViewModel>().ReverseMap(); CreateMap<VariableDto, VariableItemViewModel>().ReverseMap();