完成Mqtt服务器添加功能

This commit is contained in:
2025-09-06 12:03:39 +08:00
parent 08f9de137e
commit 52f76e2377
18 changed files with 417 additions and 271 deletions

View File

@@ -10,7 +10,7 @@ public class MqttServerDto
{
public int Id { get; set; }
public string ServerName { get; set; }
public string BrokerAddress { get; set; }
public string ServerUrl { get; set; }
public int Port { get; set; }
public string Username { get; set; }
public string Password { get; set; }

View File

@@ -7,7 +7,7 @@ public class MqttServer
{
public int Id { get; set; }
public string ServerName { get; set; }
public string BrokerAddress { get; set; } // Broker地址
public string ServerUrl { get; set; } // Broker地址
public int Port { get; set; } // 端口
public string Username { get; set; } // 用户名
public string Password { get; set; } // 密码

View File

@@ -97,7 +97,7 @@ namespace DMS.Infrastructure.UnitTests
{
var dbMqttServer = new Faker<DbMqttServer>()
.RuleFor(d => d.ServerName, f => f.Company.CompanyName())
.RuleFor(d => d.BrokerAddress, f => f.Internet.Ip())
.RuleFor(d => d.ServerUrl, f => f.Internet.Ip())
.RuleFor(d => d.Username, f => f.Internet.UserName())
.RuleFor(d => d.Password, f => f.Internet.Password())
.RuleFor(d => d.SubscribeTopic, f => "/topic/sub")

View File

@@ -19,9 +19,9 @@ public class DbMqttServer
public string ServerName { get; set; }
/// <summary>
/// MQTT代理地址
/// MQTT服务器URL
/// </summary>
public string BrokerAddress { get; set; }
public string ServerUrl { get; set; }
/// <summary>
/// 端口号
@@ -66,15 +66,18 @@ public class DbMqttServer
/// <summary>
/// 连接时间
/// </summary>
[SugarColumn(IsNullable = true)]
public DateTime? ConnectedAt { get; set; }
/// <summary>
/// 连接持续时间(秒)
/// </summary>
[SugarColumn(IsNullable = true)]
public long ConnectionDuration { get; set; }
/// <summary>
/// 消息格式
/// </summary>
[SugarColumn(IsNullable = true)]
public string MessageFormat { get; set; }
}

View File

@@ -266,7 +266,7 @@ public class MqttBackgroundService : BackgroundService
var options = new MqttClientOptionsBuilder()
.WithClientId(mqtt.ClientId)
.WithTcpServer(mqtt.BrokerAddress, mqtt.Port)
.WithTcpServer(mqtt.ServerUrl, mqtt.Port)
.WithCredentials(mqtt.Username, mqtt.Password)
.WithCleanSession()
.Build();

View File

@@ -1,4 +1,5 @@
using System.Windows;
using AutoMapper;
using AutoMapper.Internal;
using DMS.Application.Interfaces;
using DMS.Application.Services;
@@ -167,7 +168,14 @@ public partial class App : System.Windows.Application
services.AddSingleton<IDialogService, DialogService>();
// 注册WPF中的服务
services.AddSingleton<DataServices>();
services.AddSingleton<IMqttAppService, MqttAppService>();
services.AddSingleton<DataServices>(provider =>
new DataServices(
provider.GetRequiredService<IMapper>(),
provider.GetRequiredService<IDataCenterService>(),
provider.GetRequiredService<IMqttAppService>()
)
);
@@ -182,7 +190,18 @@ public partial class App : System.Windows.Application
services.AddSingleton<SettingViewModel>();
services.AddTransient<VariableTableViewModel>();
services.AddSingleton<DeviceDetailViewModel>();
services.AddSingleton<MqttsViewModel>();
services.AddSingleton<MqttsViewModel>(provider =>
new MqttsViewModel(
provider.GetRequiredService<ILogger<MqttsViewModel>>(),
provider.GetRequiredService<IDialogService>(),
provider.GetRequiredService<DataServices>(),
provider.GetRequiredService<IMqttAppService>(),
provider.GetRequiredService<IMapper>(),
provider.GetRequiredService<INavigationService>(),
provider.GetRequiredService<INotificationService>()
)
);
services.AddScoped<MqttServerDetailViewModel>();
// 注册对话框模型
services.AddTransient<ImportExcelDialogViewModel>();

View File

@@ -2,11 +2,13 @@ using System.Collections.ObjectModel;
using System.Windows;
using AutoMapper;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging;
using DMS.Application.DTOs;
using DMS.Core.Models;
using DMS.Application.Interfaces;
using DMS.Core.Enums;
using DMS.Core.Models;
using DMS.Message;
using DMS.WPF.ViewModels.Items;
namespace DMS.WPF.Services;
@@ -15,10 +17,11 @@ namespace DMS.WPF.Services;
/// 数据服务类,负责从数据库加载和管理各种数据,并提供数据变更通知。
/// 继承自ObservableRecipient可以接收消息实现IRecipient<LoadMessage>,处理加载消息。
/// </summary>
public partial class DataServices : ObservableObject, IDisposable
public partial class DataServices : ObservableObject, IRecipient<LoadMessage>, IDisposable
{
private readonly IMapper _mapper;
private readonly IDataCenterService _dataCenterService;
private readonly IMqttAppService _mqttAppService;
// 设备列表使用ObservableProperty特性当值改变时会自动触发属性变更通知。
@@ -42,8 +45,8 @@ public partial class DataServices : ObservableObject, IDisposable
private ObservableCollection<MenuItemViewModel> _menuTrees;
// MQTT配置列表。
// [ObservableProperty]
// private List<Mqtt> _mqtts;
[ObservableProperty]
private ObservableCollection<MqttServerItemViewModel> _mqttServers;
// 设备列表变更事件,当设备列表数据更新时触发。
@@ -53,7 +56,7 @@ public partial class DataServices : ObservableObject, IDisposable
public event Action<List<MenuBean>> OnMenuTreeListChanged;
// MQTT列表变更事件当MQTT配置数据更新时触发。
// public event Action<List<Mqtt>> OnMqttListChanged;
public event Action<List<MqttServerDto>> OnMqttListChanged;
/// <summary>
/// 处理变量值变更事件
@@ -81,19 +84,24 @@ public partial class DataServices : ObservableObject, IDisposable
/// </summary>
/// <param name="mapper">AutoMapper 实例。</param>
/// <param name="dataCenterService">数据服务中心实例。</param>
public DataServices(IMapper mapper, IDataCenterService dataCenterService)
/// <param name="mqttAppService">MQTT应用服务实例。</param>
public DataServices(IMapper mapper, IDataCenterService dataCenterService, IMqttAppService mqttAppService)
{
_mapper = mapper;
_dataCenterService = dataCenterService;
_mqttAppService = mqttAppService;
Devices = new ObservableCollection<DeviceItemViewModel>();
VariableTables = new ObservableCollection<VariableTableItemViewModel>();
Variables = new ObservableCollection<VariableItemViewModel>();
Menus = new ObservableCollection<MenuItemViewModel>();
MenuTrees = new ObservableCollection<MenuItemViewModel>();
// AllVariables = new ConcurrentDictionary<int, Variable>();
MqttServers = new ObservableCollection<MqttServerItemViewModel>();
// 监听变量值变更事件
_dataCenterService.VariableValueChanged += OnVariableValueChanged;
// 注册消息接收
// WeakReferenceMessenger.Register<LoadMessage>(this, (r, m) => r.Receive(m));
}
@@ -380,6 +388,84 @@ public partial class DataServices : ObservableObject, IDisposable
Variables.Remove(variableItem);
}
/// <summary>
/// 异步加载所有MQTT服务器数据
/// </summary>
public async Task LoadMqttServers(IMqttAppService mqttAppService)
{
try
{
var mqttServerDtos = await mqttAppService.GetAllMqttServersAsync();
MqttServers = _mapper.Map<ObservableCollection<MqttServerItemViewModel>>(mqttServerDtos);
OnMqttListChanged?.Invoke(mqttServerDtos);
}
catch (Exception ex)
{
// 记录异常或处理错误
Console.WriteLine($"加载MQTT服务器数据时发生错误: {ex.Message}");
}
}
/// <summary>
/// 添加MQTT服务器
/// </summary>
public async Task<MqttServerItemViewModel> AddMqttServer(IMqttAppService mqttAppService, MqttServerItemViewModel mqttServer)
{
var dto = _mapper.Map<MqttServerDto>(mqttServer);
var id = await mqttAppService.CreateMqttServerAsync(dto);
dto.Id = id;
var mqttServerItem = _mapper.Map<MqttServerItemViewModel>(dto);
MqttServers.Add(mqttServerItem);
return mqttServerItem;
}
/// <summary>
/// 更新MQTT服务器
/// </summary>
public async Task<bool> UpdateMqttServer(IMqttAppService mqttAppService, MqttServerItemViewModel mqttServer)
{
var dto = _mapper.Map<MqttServerDto>(mqttServer);
await mqttAppService.UpdateMqttServerAsync(dto);
return true;
}
/// <summary>
/// 删除MQTT服务器
/// </summary>
public async Task<bool> DeleteMqttServer(IMqttAppService mqttAppService, MqttServerItemViewModel mqttServer)
{
await mqttAppService.DeleteMqttServerAsync(mqttServer.Id);
MqttServers.Remove(mqttServer);
return true;
}
/// <summary>
/// 处理LoadMessage消息
/// </summary>
/// <param name="message">加载消息</param>
public void Receive(LoadMessage message)
{
switch (message.LoadType)
{
case LoadTypes.Devices:
// 设备数据已在IDataCenterService中处理
break;
case LoadTypes.Menu:
// 菜单数据已在IDataCenterService中处理
break;
case LoadTypes.Mqtts:
_ = Task.Run(async () => await LoadMqttServers(_mqttAppService));
break;
case LoadTypes.All:
// 加载所有数据
LoadAllDatas();
_ = Task.Run(async () => await LoadMqttServers(_mqttAppService));
break;
}
}
/// <summary>
/// 释放资源
/// </summary>

View File

@@ -23,7 +23,7 @@ namespace DMS.WPF.Services
{ typeof(VariableDialogViewModel), typeof(VariableDialog) },
{ typeof(PollLevelDialogViewModel), typeof(PollLevelDialog) },
{ typeof(IsActiveDialogViewModel), typeof(IsActiveDialog) },
// { typeof(MqttDialogViewModel), typeof(MqttDialog) }, // Add other mappings here
{ typeof(MqttDialogViewModel), typeof(MqttDialog) }, // Add other mappings here
// ... other dialogs
};

View File

@@ -1,4 +1,3 @@
using DMS.ViewModels;
using DMS.WPF.Interfaces;
using DMS.WPF.ViewModels;
using DMS.WPF.ViewModels.Items;

View File

@@ -3,7 +3,7 @@ using CommunityToolkit.Mvvm.Input;
namespace DMS.WPF.ViewModels.Dialogs;
public partial class MqttAliasDialogViewModel : ObservableObject
public partial class MqttAliasDialogViewModel : DialogViewModelBase<string>
{
[ObservableProperty]
private string _title = "设置MQTT别名";
@@ -31,4 +31,16 @@ public partial class MqttAliasDialogViewModel : ObservableObject
{
}
[RelayCommand]
private void Confirm()
{
Close(MqttAlias);
}
[RelayCommand]
private void Cancel()
{
Close(null);
}
}

View File

@@ -1,25 +1,56 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DMS.WPF.ViewModels.Items;
namespace DMS.WPF.ViewModels.Dialogs;
public partial class MqttDialogViewModel : ObservableObject
public partial class MqttDialogViewModel : DialogViewModelBase<MqttServerItemViewModel>
{
// [ObservableProperty]
// private Mqtt _mqtt;
//
// [ObservableProperty] private string title ;
// [ObservableProperty] private string primaryButContent ;
//
// public MqttDialogViewModel(Mqtt mqtt)
// {
// _mqtt = mqtt;
// }
//
//
// [RelayCommand]
// public void AddMqtt()
// {
//
// }
[ObservableProperty]
private MqttServerItemViewModel _mqttServer;
[ObservableProperty]
private string _title;
[ObservableProperty]
private string _primaryButText;
public MqttDialogViewModel()
{
MqttServer = new MqttServerItemViewModel();
}
public MqttDialogViewModel(MqttServerItemViewModel mqttServer)
{
MqttServer = new MqttServerItemViewModel
{
Id = mqttServer.Id,
ServerName = mqttServer.ServerName,
ServerUrl = mqttServer.ServerUrl,
Port = mqttServer.Port,
Username = mqttServer.Username,
Password = mqttServer.Password,
IsActive = mqttServer.IsActive,
SubscribeTopic = mqttServer.SubscribeTopic,
PublishTopic = mqttServer.PublishTopic,
ClientId = mqttServer.ClientId,
CreatedAt = mqttServer.CreatedAt,
ConnectedAt = mqttServer.ConnectedAt,
ConnectionDuration = mqttServer.ConnectionDuration,
MessageFormat = mqttServer.MessageFormat
};
}
[RelayCommand]
private void PrimaryButton()
{
Close(MqttServer);
}
[RelayCommand]
private void CancelButton()
{
Close(null);
}
}

View File

@@ -14,7 +14,7 @@ public partial class MqttServerItemViewModel : ObservableObject
private string _serverName;
[ObservableProperty]
private string _brokerAddress;
private string _serverUrl;
[ObservableProperty]
private int _port;

View File

@@ -9,7 +9,7 @@ using DMS.WPF.Services;
using DMS.WPF.ViewModels;
using DMS.WPF.ViewModels.Items;
namespace DMS.ViewModels
namespace DMS.WPF.ViewModels
{
/// <summary>
/// MQTT服务器详情视图模型。

View File

@@ -1,181 +1,184 @@
using System.Collections.ObjectModel;
using AutoMapper;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DMS.Application.DTOs;
using DMS.Application.Interfaces;
using DMS.Core.Enums;
using DMS.Services;
using DMS.WPF.Helper;
using DMS.WPF.Interfaces;
using DMS.WPF.Services;
using DMS.WPF.ViewModels.Dialogs;
using DMS.WPF.ViewModels.Items;
using Microsoft.Extensions.Logging;
using DMS.WPF.Views;
namespace DMS.WPF.ViewModels;
/// <summary>
/// MQTT服务器管理视图模型负责MQTT服务器的增删改查操作。
/// </summary>
public partial class MqttsViewModel : ViewModelBase
{
private readonly DataServices _dataServices;
private readonly IMqttAppService _mqttAppService;
private readonly IMapper _mapper;
private readonly IDialogService _dialogService;
private readonly ILogger<MqttsViewModel> _logger;
private readonly INavigationService _navigationService;
private readonly INotificationService _notificationService;
/// <summary>
/// MQTT服务器列表。
/// </summary>
[ObservableProperty]
private ObservableCollection<MqttServerItemViewModel> _mqtts;
// public ObservableCollection<Mqtt> Mqtts
// {
// get => _mqtts;
// set
// {
// if (_mqtts != null)
// {
// foreach (var mqtt in _mqtts)
// {
// mqtt.PropertyChanged -= Mqtt_PropertyChanged;
// }
// }
//
// SetProperty(ref _mqtts, value);
//
// if (_mqtts != null)
// {
// foreach (var mqtt in _mqtts)
// {
// mqtt.PropertyChanged += Mqtt_PropertyChanged;
// }
// }
// }
// }
/// <summary>
/// 当前选中的MQTT服务器。
/// </summary>
[ObservableProperty]
private MqttServerItemViewModel _selectedMqtt;
private readonly ILogger<MqttsViewModel> _logger;
public MqttsViewModel(
ILogger<MqttsViewModel> logger, IDialogService dialogService, DataServices dataServices
ILogger<MqttsViewModel> logger,
IDialogService dialogService,
DataServices dataServices,
IMqttAppService mqttAppService,
IMapper mapper,
INavigationService navigationService,
INotificationService notificationService
)
{
_logger = logger;
_dialogService = dialogService;
_dataServices = dataServices;
// if (_dataServices.Mqtts == null || _dataServices.Mqtts.Count == 0)
// {
// MessageHelper.SendLoadMessage(LoadTypes.Mqtts);
// }
// else
// {
// Mqtts = new ObservableCollection<Mqtt>(_dataServices.Mqtts);
// }
//
//
// _dataServices.OnMqttListChanged += ( mqtts) =>
// {
// Mqtts = new ObservableCollection<Mqtt>(mqtts);
// };
}
private async void Mqtt_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
// if (e.PropertyName == nameof(Mqtt.IsActive))
// {
// if (sender is Mqtt mqtt)
// {
// try
// {
// await _mqttRepository.UpdateAsync(mqtt);
// NotificationHelper.ShowSuccess($"MQTT: {mqtt.Name} 的启用状态已更新。");
// MessageHelper.SendLoadMessage(LoadTypes.Mqtts);
// }
// catch (Exception ex)
// {
// NotificationHelper.ShowError($"更新MQTT启用状态失败: {mqtt.Name} - {ex.Message}", ex);
// }
// }
// }
_mqttAppService = mqttAppService;
_mapper = mapper;
_navigationService = navigationService;
_notificationService = notificationService;
Mqtts = _dataServices.MqttServers;
}
/// <summary>
/// 添加MQTT服务器命令。
/// </summary>
[RelayCommand]
public async void AddMqtt()
public async Task AddMqtt()
{
// try
// {
// var mqtt = await _dialogService.ShowAddMqttDialog();
// if (mqtt == null)
// {
// _logger.LogInformation("用户取消了添加MQTT操作。");
// return;
// }
//
// await _mqttRepository.AddAsync(mqtt);
// MessageHelper.SendLoadMessage(LoadTypes.Mqtts);
// MessageHelper.SendLoadMessage(LoadTypes.Menu);
// }
// catch (Exception e)
// {
// NotificationHelper.ShowError($"添加MQTT的过程中发生错误{e.Message}", e);
// }
try
{
// 1. 显示添加MQTT服务器对话框
MqttServerItemViewModel mqtt = await _dialogService.ShowDialogAsync(new MqttDialogViewModel()
{
Title = "添加MQTT服务器",
PrimaryButText = "添加MQTT服务器"
});
// 如果用户取消或对话框未返回MQTT服务器则直接返回
if (mqtt == null)
{
return;
}
var mqttItem = await _dataServices.AddMqttServer(_mqttAppService, mqtt);
_notificationService.ShowSuccess($"MQTT服务器添加成功{mqttItem.ServerName}");
}
catch (Exception e)
{
_logger.LogError(e, "添加MQTT服务器的过程中发生错误");
_notificationService.ShowError($"添加MQTT服务器的过程中发生错误{e.Message}", e);
}
}
/// <summary>
/// 删除MQTT服务器命令。
/// </summary>
[RelayCommand]
public async void DeleteMqtt()
public async Task DeleteMqtt()
{
// try
// {
// if (SelectedMqtt == null)
// {
// NotificationHelper.ShowError("你没有选择任何MQTT请选择MQTT后再点击删除");
// return;
// }
//
// string msg = $"确认要删除MQTT名为:{SelectedMqtt.Name}";
// var isDel = await _dialogService.ShowConfrimeDialog("删除MQTT", msg, "删除MQTT");
// if (isDel)
// {
// await _mqttRepository.DeleteAsync(SelectedMqtt);
// MessageHelper.SendLoadMessage(LoadTypes.Mqtts);
// MessageHelper.SendLoadMessage(LoadTypes.Menu);
// NotificationHelper.ShowSuccess($"删除MQTT成功,MQTT名{SelectedMqtt.Name}");
// }
// }
// catch (Exception e)
// {
// NotificationHelper.ShowError($"删除MQTT的过程中发生错误{e.Message}", e);
// }
try
{
if (SelectedMqtt == null)
{
_notificationService.ShowError("你没有选择任何MQTT服务器请选择MQTT服务器后再点击删除");
return;
}
var viewModel = new ConfirmDialogViewModel("删除MQTT服务器", $"确认要删除MQTT服务器名为:{SelectedMqtt.ServerName}", "删除MQTT服务器");
if (await _dialogService.ShowDialogAsync(viewModel))
{
var mqttName = SelectedMqtt.ServerName;
await _dataServices.DeleteMqttServer(_mqttAppService, SelectedMqtt);
_notificationService.ShowSuccess($"删除MQTT服务器成功,服务器名:{mqttName}");
}
}
catch (Exception e)
{
_logger.LogError(e, "删除MQTT服务器的过程中发生错误");
_notificationService.ShowError($"删除MQTT服务器的过程中发生错误{e.Message}", e);
}
}
/// <summary>
/// 编辑MQTT服务器命令。
/// </summary>
[RelayCommand]
public async void EditMqtt()
public async Task EditMqtt()
{
// try
// {
// if (SelectedMqtt == null)
// {
// NotificationHelper.ShowError("你没有选择任何MQTT请选择MQTT后再点击编辑");
// return;
// }
//
// var editMqtt = await _dialogService.ShowEditMqttDialog(SelectedMqtt);
// if (editMqtt != null)
// {
// var res = await _mqttRepository.UpdateAsync(editMqtt);
// MessageHelper.SendLoadMessage(LoadTypes.Mqtts);
// }
// }
// catch (Exception e)
// {
// NotificationHelper.ShowError($"编辑MQTT的过程中发生错误{e.Message}", e);
// }
try
{
if (SelectedMqtt == null)
{
_notificationService.ShowError("你没有选择任何MQTT服务器请选择MQTT服务器后再点击编辑");
return;
}
MqttDialogViewModel mqttDialogViewModel = new MqttDialogViewModel(SelectedMqtt)
{
Title = "编辑MQTT服务器",
PrimaryButText = "保存修改"
};
// 1. 显示MQTT服务器对话框
MqttServerItemViewModel mqtt = await _dialogService.ShowDialogAsync(mqttDialogViewModel);
// 如果用户取消或对话框未返回MQTT服务器则直接返回
if (mqtt == null)
{
return;
}
await _dataServices.UpdateMqttServer(_mqttAppService, mqtt);
// 更新UI
_mapper.Map(mqtt, SelectedMqtt);
_notificationService.ShowSuccess($"编辑MQTT服务器成功{mqtt.ServerName}");
}
catch (Exception e)
{
_logger.LogError(e, "编辑MQTT服务器的过程中发生错误");
_notificationService.ShowError($"编辑MQTT服务器的过程中发生错误{e.Message}", e);
}
}
/// <summary>
/// 导航到MQTT服务器详情页面。
/// </summary>
[RelayCommand]
private void NavigateToMqttDetail()
private async Task NavigateToMqttDetail()
{
// if (SelectedMqtt == null)
// {
// NotificationHelper.ShowMessage("请选择一个MQTT服务器以查看详情。", NotificationType.Warning);
// return;
// }
// _navgatorServices.NavigateTo<MqttServerDetailView>(SelectedMqtt);
if (SelectedMqtt == null)
{
_notificationService.ShowWarn("请选择一个MQTT服务器以查看详情。");
return;
}
// 导航到MQTT服务器详情页
var menu = new MenuItemViewModel
{
TargetViewKey = "MqttServerDetailView",
TargetId = SelectedMqtt.Id
};
await _navigationService.NavigateToAsync(menu);
}
}

View File

@@ -4,119 +4,111 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ikw="http://schemas.inkore.net/lib/ui/wpf"
xmlns:hc="https://handyorg.github.io/handycontrol"
xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
xmlns:vmd="clr-namespace:DMS.WPF.ViewModels.Dialogs"
xmlns:ex="clr-namespace:DMS.Extensions"
xmlns:en="clr-namespace:DMS.Core.Enums"
xmlns:enums="clr-namespace:DMS.Core.Enums;assembly=DMS.Core"
xmlns:valueConverts="clr-namespace:DMS.WPF.ValueConverts"
Title="{Binding Title}"
CloseButtonText="取消"
DefaultButton="Primary"
PrimaryButtonText="{Binding PrimaryButText}"
Background="#fff"
PrimaryButtonCommand="{Binding PrimaryButtonCommand}"
CloseButtonCommand="{Binding CancelButtonCommand}"
d:DataContext="{d:DesignInstance vmd:MqttDialogViewModel}"
mc:Ignorable="d">
<ui:ContentDialog.Resources>
<ex:EnumBindingSource x:Key="mqttPlatform"
EnumType="{x:Type enums:MqttPlatform}" />
<valueConverts:EnumDescriptionConverter x:Key="EnumDescriptionConverter" />
<Style x:Key="LabelStyle" TargetType="TextBlock">
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="HorizontalAlignment" Value="Right" />
<Setter Property="Margin" Value="0,0,10,0" />
</Style>
</ui:ContentDialog.Resources>
<Grid Width="480"
Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<!-- 左边列 -->
<ikw:SimpleStackPanel Grid.Column="0"
Margin="10 10 20 10 "
Spacing="12">
<!-- MQTT名称 -->
<TextBlock Text="MQTT名称"
HorizontalAlignment="Left"
Style="{StaticResource TextBlockSubTitle}" />
<TextBox Text="{Binding Mqtt.Name, UpdateSourceTrigger=PropertyChanged}" />
<!-- MQTT IP地址 -->
<TextBlock Text="MQTT IP地址"
HorizontalAlignment="Left"
Style="{StaticResource TextBlockSubTitle}" />
<TextBox AcceptsReturn="True"
Text="{Binding Mqtt.Host, UpdateSourceTrigger=PropertyChanged}" />
<StackPanel Margin="16">
<!-- 基本信息 -->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="20" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<!-- Mqtt平台 -->
<TextBlock Text="Mqtt平台"
HorizontalAlignment="Left"
Style="{StaticResource TextBlockSubTitle}" />
<ComboBox SelectedItem="{Binding Mqtt.Platform}"
ItemsSource="{Binding Source={StaticResource mqttPlatform} }">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource EnumDescriptionConverter}}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<!-- Row 0 -->
<hc:TextBox
Grid.Row="0"
Grid.Column="0"
hc:InfoElement.Title="MQTT名称:"
Text="{Binding MqttServer.ServerName, UpdateSourceTrigger=PropertyChanged}" />
<hc:TextBox
Grid.Row="0"
Grid.Column="2"
hc:InfoElement.Title="客户端ID:"
Text="{Binding MqttServer.ClientId, UpdateSourceTrigger=PropertyChanged}" />
<!-- PublishTopic -->
<TextBlock Text="发布主题"
HorizontalAlignment="Left"
Style="{StaticResource TextBlockSubTitle}" />
<TextBox Text="{Binding Mqtt.PublishTopic, UpdateSourceTrigger=PropertyChanged}" />
<hc:TextBox
Grid.Row="1"
Grid.Column="0"
Margin="0,15,0,0"
Text="{Binding MqttServer.ServerUrl, UpdateSourceTrigger=PropertyChanged}" />
<!-- SubTopics -->
<TextBlock Text="订阅主题"
HorizontalAlignment="Left"
Style="{StaticResource TextBlockSubTitle}" />
<TextBox Text="{Binding Mqtt.SubTopic, UpdateSourceTrigger=PropertyChanged}" />
<CheckBox FontSize="16"
Content="是否设为默认"
Margin="0 30 0 0"
IsChecked="{Binding Mqtt.IsDefault}" />
</ikw:SimpleStackPanel>
<!-- 右边列 -->
<ikw:SimpleStackPanel Margin="10"
Grid.Column="1"
Spacing="12">
<hc:TextBox
Grid.Row="1"
Grid.Column="2"
Margin="0,15,0,0"
hc:InfoElement.Title="端口:"
Text="{Binding MqttServer.Port, UpdateSourceTrigger=PropertyChanged}" />
<!-- MQTT备注 -->
<TextBlock Text="MQTT备注"
HorizontalAlignment="Left"
Style="{StaticResource TextBlockSubTitle}" />
<TextBox Text="{Binding Mqtt.Remark, UpdateSourceTrigger=PropertyChanged}" />
<!-- MQTT端口 -->
<TextBlock Text="MQTT端口"
HorizontalAlignment="Left"
Style="{StaticResource TextBlockSubTitle}" />
<TextBox AcceptsReturn="True"
Text="{Binding Mqtt.Port, UpdateSourceTrigger=PropertyChanged}" />
<!-- Row 2 -->
<hc:TextBox
Grid.Row="2"
Grid.Column="0"
Margin="0,15,0,0"
hc:InfoElement.Title="用户名:"
Text="{Binding MqttServer.Username, UpdateSourceTrigger=PropertyChanged}" />
<!-- 用户名 -->
<TextBlock Text="用户名"
HorizontalAlignment="Left"
Style="{StaticResource TextBlockSubTitle}" />
<TextBox Text="{Binding Mqtt.UserName, UpdateSourceTrigger=PropertyChanged}" />
<hc:TextBox
Grid.Row="2"
Grid.Column="2"
Margin="0,15,0,0"
hc:InfoElement.Title="密码:"
Text="{Binding MqttServer.Password, UpdateSourceTrigger=PropertyChanged}" />
<!-- 密码 -->
<TextBlock Text="密码"
HorizontalAlignment="Left"
Style="{StaticResource TextBlockSubTitle}" />
<TextBox Text="{Binding Mqtt.PassWord, UpdateSourceTrigger=PropertyChanged}" />
<!-- Row 3 -->
<hc:TextBox
Grid.Row="3"
Grid.Column="0"
Margin="0,15,0,0"
hc:InfoElement.Title="发布主题:"
Text="{Binding MqttServer.PublishTopic, UpdateSourceTrigger=PropertyChanged}" />
<!-- ClientID -->
<TextBlock Text="ClientID"
HorizontalAlignment="Left"
Style="{StaticResource TextBlockSubTitle}" />
<TextBox Text="{Binding Mqtt.ClientID, UpdateSourceTrigger=PropertyChanged}" />
<CheckBox FontSize="16"
Content="是否启用"
Margin="0 30 0 0"
IsChecked="{Binding Mqtt.IsActive}" />
</ikw:SimpleStackPanel>
</Grid>
<hc:TextBox
Grid.Row="3"
Grid.Column="2"
Margin="0,15,0,0"
hc:InfoElement.Title="订阅主题:"
Text="{Binding MqttServer.SubscribeTopic, UpdateSourceTrigger=PropertyChanged}" />
<!-- Row 4 -->
<CheckBox
Grid.Row="4"
Grid.Column="0"
Margin="0,20,0,0"
Content="是否启用"
IsChecked="{Binding MqttServer.IsActive}" />
</Grid>
</StackPanel>
</ui:ContentDialog>

View File

@@ -5,9 +5,13 @@ namespace DMS.WPF.Views.Dialogs;
public partial class MqttDialog : ContentDialog
{
public MqttDialog(MqttDialogViewModel viewModel)
public MqttDialog()
{
InitializeComponent();
}
public MqttDialog(MqttDialogViewModel viewModel) : this()
{
DataContext = viewModel;
}
}

View File

@@ -40,22 +40,22 @@
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="名称:" Margin="0,0,5,0" VerticalAlignment="Center"/>
<TextBox Grid.Row="0" Grid.Column="1" Text="{Binding CurrentMqtt.Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="0,0,10,0"/>
<TextBox Grid.Row="0" Grid.Column="1" Text="{Binding CurrentMqtt.ServerName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="0,0,10,0"/>
<TextBlock Grid.Row="0" Grid.Column="2" Text="主机:" Margin="0,0,5,0" VerticalAlignment="Center"/>
<TextBox Grid.Row="0" Grid.Column="3" Text="{Binding CurrentMqtt.Host, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox Grid.Row="0" Grid.Column="3" Text="{Binding CurrentMqtt.ServerUrl, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock Grid.Row="1" Grid.Column="0" Text="端口:" Margin="0,0,5,0" VerticalAlignment="Center"/>
<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding CurrentMqtt.Port, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="0,0,10,0"/>
<TextBlock Grid.Row="1" Grid.Column="2" Text="客户端ID:" Margin="0,0,5,0" VerticalAlignment="Center"/>
<TextBox Grid.Row="1" Grid.Column="3" Text="{Binding CurrentMqtt.ClientID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox Grid.Row="1" Grid.Column="3" Text="{Binding CurrentMqtt.ClientId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock Grid.Row="2" Grid.Column="0" Text="用户名:" Margin="0,0,5,0" VerticalAlignment="Center"/>
<TextBox Grid.Row="2" Grid.Column="1" Text="{Binding CurrentMqtt.UserName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="0,0,10,0"/>
<TextBox Grid.Row="2" Grid.Column="1" Text="{Binding CurrentMqtt.Username, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="0,0,10,0"/>
<TextBlock Grid.Row="2" Grid.Column="2" Text="密码:" Margin="0,0,5,0" VerticalAlignment="Center"/>
<TextBox Grid.Row="2" Grid.Column="3" Text="{Binding CurrentMqtt.PassWord, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox Grid.Row="2" Grid.Column="3" Text="{Binding CurrentMqtt.Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock Grid.Row="3" Grid.Column="0" Text="发布主题:" Margin="0,0,5,0" VerticalAlignment="Center"/>
<TextBox Grid.Row="3" Grid.Column="1" Text="{Binding CurrentMqtt.PublishTopic, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="0,0,10,0"/>
@@ -88,13 +88,13 @@
IsReadOnly="True"
SelectionMode="Extended">
<DataGrid.Columns>
<DataGridTextColumn Header="变量名称" Binding="{Binding Variable.Name}"/>
<DataGridTextColumn Header="MQTT发送名称" Binding="{Binding MqttAlias}"/>
<DataGridTextColumn Header="变量名称" Binding="{Binding Variable.Name}"/>
<DataGridTextColumn Header="MQTT发送名称" Binding="{Binding MqttAlias}"/>
<DataGridTextColumn Header="地址" Binding="{Binding Variable.S7Address}"/>
<DataGridTextColumn Header="数据类型" Binding="{Binding Variable.SignalType}"/>
<DataGridTextColumn Header="当前值" Binding="{Binding Variable.DataValue}"/>
<DataGridTextColumn Header="显示值" Binding="{Binding Variable.DisplayValue}"/>
<DataGridTextColumn Header="更新时间" Binding="{Binding Variable.UpdateTime, StringFormat='yyyy-MM-dd HH:mm:ss'}"/>
<DataGridTextColumn Header="更新时间" Binding="{Binding Variable.UpdatedAt, StringFormat='yyyy-MM-dd HH:mm:ss'}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>

View File

@@ -6,9 +6,6 @@ MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DMS.Core", "DMS.Core\DMS.Core.csproj", "{CD16527A-A5F9-482F-ABF7-755DDE1B0406}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DMS.Application", "DMS.Application\DMS.Application.csproj", "{46E180A5-38CB-4229-915F-C9BC44534E77}"
ProjectSection(ProjectDependencies) = postProject
{66FB5295-C6B7-4767-948C-5986480F6733} = {66FB5295-C6B7-4767-948C-5986480F6733}
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DMS.Infrastructure", "DMS.Infrastructure\DMS.Infrastructure.csproj", "{66FB5295-C6B7-4767-948C-5986480F6733}"
ProjectSection(ProjectDependencies) = postProject