From a9ca89b44a69af16f6c669b6af4c8ac5a5e3089c Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Tue, 9 Sep 2025 18:17:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E5=AE=8C=E6=88=90MQTT?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E7=9A=84=E5=8F=91=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DMS.Application/Services/DataLoaderService.cs | 15 +++++++++++++++ DMS.Infrastructure/Services/MqttServiceManager.cs | 5 +++-- DMS.WPF/Profiles/MappingProfile.cs | 1 + 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/DMS.Application/Services/DataLoaderService.cs b/DMS.Application/Services/DataLoaderService.cs index 464e25b..890f61c 100644 --- a/DMS.Application/Services/DataLoaderService.cs +++ b/DMS.Application/Services/DataLoaderService.cs @@ -4,6 +4,7 @@ using DMS.Application.Interfaces; using DMS.Core.Interfaces; using System.Collections.Concurrent; using DMS.Application.DTOs.Events; +using DMS.Core.Models; namespace DMS.Application.Services; @@ -85,6 +86,20 @@ public class DataLoaderService : IDataLoaderService foreach (var variableMqttAliasDto in variableMqttAliasDtos) { _appDataStorageService.VariableMqttAliases.TryAdd(variableMqttAliasDto.Id, variableMqttAliasDto); + if (_appDataStorageService.Variables.TryGetValue(variableMqttAliasDto.VariableId, out var variable)) + { + variableMqttAliasDto.Variable = _mapper.Map(variable); + variable.MqttAliases?.Add(variableMqttAliasDto); + } + + if (_appDataStorageService.MqttServers.TryGetValue(variableMqttAliasDto.MqttServerId, out var mqttServer)) + { + variableMqttAliasDto.MqttServer = _mapper.Map(mqttServer); + variableMqttAliasDto.MqttServerName = variableMqttAliasDto.MqttServer.ServerName; + mqttServer.VariableAliases?.Add(variableMqttAliasDto); + } + + } } diff --git a/DMS.Infrastructure/Services/MqttServiceManager.cs b/DMS.Infrastructure/Services/MqttServiceManager.cs index d4ad501..8cdea04 100644 --- a/DMS.Infrastructure/Services/MqttServiceManager.cs +++ b/DMS.Infrastructure/Services/MqttServiceManager.cs @@ -238,9 +238,10 @@ namespace DMS.Infrastructure.Services try { 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} 的数据", context.MqttServer.ServerName, variableMqtt.Variable.Name); } diff --git a/DMS.WPF/Profiles/MappingProfile.cs b/DMS.WPF/Profiles/MappingProfile.cs index 24ab14a..274b24c 100644 --- a/DMS.WPF/Profiles/MappingProfile.cs +++ b/DMS.WPF/Profiles/MappingProfile.cs @@ -24,6 +24,7 @@ namespace DMS.WPF.Profiles .ReverseMap(); CreateMap().ReverseMap(); + CreateMap().ReverseMap(); CreateMap().ReverseMap(); CreateMap().ReverseMap(); CreateMap().ReverseMap();