From aea7a21d578c501906f8dd1837860617e4197e03 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Thu, 17 Jul 2025 17:27:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0MQTT=E5=85=B3=E8=81=94?= =?UTF-8?q?=E5=8F=98=E9=87=8F=E7=9A=84=E5=88=AB=E5=90=8D=E9=97=AE=E9=A2=98?= =?UTF-8?q?=EF=BC=8C=E5=AE=9E=E7=8E=B0=E4=BA=86=EF=BC=8C=E5=90=8C=E4=B8=AA?= =?UTF-8?q?=E5=8F=98=E9=87=8F=E5=8F=91=E7=BB=99=E4=B8=8D=E5=90=8C=E7=9A=84?= =?UTF-8?q?MQTT=E6=9C=8D=E5=8A=A1=E5=99=A8=E7=9A=84=E5=90=8D=E7=A7=B0?= =?UTF-8?q?=E4=B8=8D=E5=90=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Data/Entities/DbVariableMqtt.cs | 54 ++++++++ .../VariableMqttAliasRepository.cs | 123 ++++++++++++++++++ Models/VariableMqtt.cs | 84 ++++++++++++ .../MqttAliasBatchEditDialogViewModel.cs | 34 +++++ .../Dialogs/MqttAliasDialogViewModel.cs | 34 +++++ Views/Dialogs/MqttAliasBatchEditDialog.xaml | 38 ++++++ .../Dialogs/MqttAliasBatchEditDialog.xaml.cs | 13 ++ Views/Dialogs/MqttAliasDialog.xaml | 23 ++++ Views/Dialogs/MqttAliasDialog.xaml.cs | 13 ++ 9 files changed, 416 insertions(+) create mode 100644 Data/Entities/DbVariableMqtt.cs create mode 100644 Data/Repositories/VariableMqttAliasRepository.cs create mode 100644 Models/VariableMqtt.cs create mode 100644 ViewModels/Dialogs/MqttAliasBatchEditDialogViewModel.cs create mode 100644 ViewModels/Dialogs/MqttAliasDialogViewModel.cs create mode 100644 Views/Dialogs/MqttAliasBatchEditDialog.xaml create mode 100644 Views/Dialogs/MqttAliasBatchEditDialog.xaml.cs create mode 100644 Views/Dialogs/MqttAliasDialog.xaml create mode 100644 Views/Dialogs/MqttAliasDialog.xaml.cs diff --git a/Data/Entities/DbVariableMqtt.cs b/Data/Entities/DbVariableMqtt.cs new file mode 100644 index 0000000..486647b --- /dev/null +++ b/Data/Entities/DbVariableMqtt.cs @@ -0,0 +1,54 @@ +using System; +using SqlSugar; + +namespace PMSWPF.Data.Entities; + +/// +/// 表示变量数据与MQTT服务器之间的关联实体,包含MQTT别名。 +/// +[SugarTable("VariableMqtt")] +public class DbVariableMqtt +{ + /// + /// 关联的唯一标识符。 + /// + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + + /// + /// 关联的变量数据ID。 + /// + public int VariableDataId { get; set; } + + /// + /// 关联的MQTT服务器ID。 + /// + public int MqttId { get; set; } + + /// + /// 变量在该MQTT服务器上的别名。 + /// + public string MqttAlias { get; set; } = string.Empty; + + /// + /// 创建时间。 + /// + public DateTime CreateTime { get; set; } = DateTime.Now; + + /// + /// 更新时间。 + /// + public DateTime UpdateTime { get; set; } = DateTime.Now; + + /// + /// 导航属性:关联的变量数据。 + /// + [Navigate(NavigateType.ManyToOne, nameof(VariableDataId))] + public DbVariableData? VariableData { get; set; } + + /// + /// 导航属性:关联的MQTT服务器。 + /// + [Navigate(NavigateType.ManyToOne, nameof(MqttId))] + public DbMqtt? Mqtt { get; set; } +} diff --git a/Data/Repositories/VariableMqttAliasRepository.cs b/Data/Repositories/VariableMqttAliasRepository.cs new file mode 100644 index 0000000..004bf50 --- /dev/null +++ b/Data/Repositories/VariableMqttAliasRepository.cs @@ -0,0 +1,123 @@ +using PMSWPF.Data.Entities; +using SqlSugar; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace PMSWPF.Data.Repositories; + +/// +/// 变量与MQTT服务器别名关联的数据仓库。 +/// +public class VariableMqttAliasRepository +{ + /// + /// 根据变量ID和MQTT服务器ID获取别名。 + /// + /// 变量数据ID。 + /// MQTT服务器ID。 + /// DbVariableMqtt实体,如果不存在则为null。 + public async Task GetAliasByVariableAndMqtt(int variableDataId, int mqttId) + { + using (var db = DbContext.GetInstance()) + { + return await GetAliasByVariableAndMqtt(variableDataId, mqttId, db); + } + } + + /// + /// 根据变量ID和MQTT服务器ID获取别名。 + /// + /// 变量数据ID。 + /// MQTT服务器ID。 + /// SqlSugarClient实例。 + /// DbVariableMqtt实体,如果不存在则为null。 + public async Task GetAliasByVariableAndMqtt(int variableDataId, int mqttId, SqlSugarClient db) + { + return await db.Queryable() + .Where(it => it.VariableDataId == variableDataId && it.MqttId == mqttId) + .FirstAsync(); + } + + /// + /// 批量添加变量与MQTT服务器的关联。 + /// + /// 要添加的DbVariableMqtt实体列表。 + /// 成功添加的数量。 + public async Task AddManyAsync(IEnumerable entities) + { + using (var db = DbContext.GetInstance()) + { + return await AddManyAsync(entities, db); + } + } + + /// + /// 批量添加变量与MQTT服务器的关联。 + /// + /// 要添加的DbVariableMqtt实体列表。 + /// SqlSugarClient实例。 + /// 成功添加的数量。 + public async Task AddManyAsync(IEnumerable entities, SqlSugarClient db) + { + return await db.Insertable(entities).ExecuteCommandAsync(); + } + + /// + /// 更新变量与MQTT服务器的别名。 + /// + /// 变量数据ID。 + /// MQTT服务器ID。 + /// 新的别名。 + /// 受影响的行数。 + public async Task UpdateAliasAsync(int variableDataId, int mqttId, string newAlias) + { + using (var db = DbContext.GetInstance()) + { + return await UpdateAliasAsync(variableDataId, mqttId, newAlias, db); + } + } + + /// + /// 更新变量与MQTT服务器的别名。 + /// + /// 变量数据ID。 + /// MQTT服务器ID。 + /// 新的别名。 + /// SqlSugarClient实例。 + /// 受影响的行数。 + public async Task UpdateAliasAsync(int variableDataId, int mqttId, string newAlias, SqlSugarClient db) + { + return await db.Updateable() + .SetColumns(it => it.MqttAlias == newAlias) + .Where(it => it.VariableDataId == variableDataId && it.MqttId == mqttId) + .ExecuteCommandAsync(); + } + + /// + /// 删除变量与MQTT服务器的关联。 + /// + /// 变量数据ID。 + /// MQTT服务器ID。 + /// 受影响的行数。 + public async Task DeleteAsync(int variableDataId, int mqttId) + { + using (var db = DbContext.GetInstance()) + { + return await DeleteAsync(variableDataId, mqttId, db); + } + } + + /// + /// 删除变量与MQTT服务器的关联。 + /// + /// 变量数据ID。 + /// MQTT服务器ID。 + /// SqlSugarClient实例。 + /// 受影响的行数。 + public async Task DeleteAsync(int variableDataId, int mqttId, SqlSugarClient db) + { + return await db.Deleteable() + .Where(it => it.VariableDataId == variableDataId && it.MqttId == mqttId) + .ExecuteCommandAsync(); + } +} diff --git a/Models/VariableMqtt.cs b/Models/VariableMqtt.cs new file mode 100644 index 0000000..0133b2f --- /dev/null +++ b/Models/VariableMqtt.cs @@ -0,0 +1,84 @@ +using System; +using CommunityToolkit.Mvvm.ComponentModel; +using PMSWPF.Data.Entities; +using PMSWPF.Enums; + +namespace PMSWPF.Models; + +/// +/// 表示变量数据与MQTT服务器之间的关联模型,包含MQTT别名。 +/// +public partial class VariableMqtt : ObservableObject +{ + public VariableMqtt() + { + } + + public VariableMqtt(VariableData? variableData, Mqtt? mqtt) + { + VariableData = variableData; + Mqtt = mqtt; + MqttAlias = MqttAlias != String.Empty ? MqttAlias : variableData.Name; + } + + /// + /// 关联的唯一标识符。 + /// + public int Id { get; set; } + + /// + /// 关联的变量数据ID。 + /// + public int VariableDataId { get; set; } + + /// + /// 关联的MQTT服务器ID。 + /// + public int MqttId { get; set; } + + /// + /// 变量在该MQTT服务器上的别名。 + /// + [ObservableProperty] + private string _mqttAlias = string.Empty; + + /// + /// 变量的唯一标识符(S7地址或OPC UA NodeId)。 + /// + public string Identifier + { + get + { + if (VariableData.ProtocolType == ProtocolType.S7) + { + return VariableData.S7Address; + } + else if (VariableData.ProtocolType == ProtocolType.OpcUA) + { + return VariableData.OpcUaNodeId; + } + + return string.Empty; + } + } + + /// + /// 创建时间。 + /// + public DateTime CreateTime { get; set; } = DateTime.Now; + + /// + /// 更新时间。 + /// + public DateTime UpdateTime { get; set; } = DateTime.Now; + + /// + /// 导航属性:关联的变量数据。 + /// + public VariableData? VariableData { get; set; } + + /// + /// 导航属性:关联的MQTT服务器。 + /// + public Mqtt? Mqtt { get; set; } +} \ No newline at end of file diff --git a/ViewModels/Dialogs/MqttAliasBatchEditDialogViewModel.cs b/ViewModels/Dialogs/MqttAliasBatchEditDialogViewModel.cs new file mode 100644 index 0000000..30ca763 --- /dev/null +++ b/ViewModels/Dialogs/MqttAliasBatchEditDialogViewModel.cs @@ -0,0 +1,34 @@ +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; +using PMSWPF.Models; +using System.Collections.ObjectModel; +using System.Collections.Generic; +using System.Linq; + +namespace PMSWPF.ViewModels.Dialogs; + +public partial class MqttAliasBatchEditDialogViewModel : ObservableObject +{ + [ObservableProperty] + private string _title = "批量设置MQTT别名"; + + [ObservableProperty] + private ObservableCollection _variablesToEdit; + + public Mqtt SelectedMqtt { get; private set; } + + public MqttAliasBatchEditDialogViewModel(List selectedVariables, Mqtt selectedMqtt) + { + SelectedMqtt = selectedMqtt; + Title=$"设置:{SelectedMqtt.Name}-MQTT服务器关联变量的别名"; + VariablesToEdit = new ObservableCollection( + selectedVariables.Select(v => new VariableMqtt(v, selectedMqtt)) + ); + } + + public MqttAliasBatchEditDialogViewModel() + { + // For design time + VariablesToEdit = new ObservableCollection(); + } +} \ No newline at end of file diff --git a/ViewModels/Dialogs/MqttAliasDialogViewModel.cs b/ViewModels/Dialogs/MqttAliasDialogViewModel.cs new file mode 100644 index 0000000..453ab06 --- /dev/null +++ b/ViewModels/Dialogs/MqttAliasDialogViewModel.cs @@ -0,0 +1,34 @@ +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; + +namespace PMSWPF.ViewModels.Dialogs; + +public partial class MqttAliasDialogViewModel : ObservableObject +{ + [ObservableProperty] + private string _title = "设置MQTT别名"; + + [ObservableProperty] + private string _message = "请输入变量在该MQTT服务器上的别名:"; + + [ObservableProperty] + private string _variableName = string.Empty; + + [ObservableProperty] + private string _mqttServerName = string.Empty; + + [ObservableProperty] + private string _mqttAlias = string.Empty; + + public MqttAliasDialogViewModel(string variableName, string mqttServerName) + { + VariableName = variableName; + MqttServerName = mqttServerName; + Message = $"请输入变量 '{VariableName}' 在MQTT服务器 '{MqttServerName}' 上的别名:"; + } + + public MqttAliasDialogViewModel() + { + + } +} \ No newline at end of file diff --git a/Views/Dialogs/MqttAliasBatchEditDialog.xaml b/Views/Dialogs/MqttAliasBatchEditDialog.xaml new file mode 100644 index 0000000..8a92e30 --- /dev/null +++ b/Views/Dialogs/MqttAliasBatchEditDialog.xaml @@ -0,0 +1,38 @@ + + + 设置的别名,当变量向MQTT服务器发送数据时就会按照设置设置好的别名发送,请在MQTT服务端,按照设置的名称接受。 + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Views/Dialogs/MqttAliasBatchEditDialog.xaml.cs b/Views/Dialogs/MqttAliasBatchEditDialog.xaml.cs new file mode 100644 index 0000000..7e14369 --- /dev/null +++ b/Views/Dialogs/MqttAliasBatchEditDialog.xaml.cs @@ -0,0 +1,13 @@ +using iNKORE.UI.WPF.Modern.Controls; +using PMSWPF.ViewModels.Dialogs; + +namespace PMSWPF.Views.Dialogs; + +public partial class MqttAliasBatchEditDialog : ContentDialog +{ + public MqttAliasBatchEditDialog(MqttAliasBatchEditDialogViewModel viewModel) + { + InitializeComponent(); + DataContext = viewModel; + } +} \ No newline at end of file diff --git a/Views/Dialogs/MqttAliasDialog.xaml b/Views/Dialogs/MqttAliasDialog.xaml new file mode 100644 index 0000000..ea400da --- /dev/null +++ b/Views/Dialogs/MqttAliasDialog.xaml @@ -0,0 +1,23 @@ + + + + + + + + \ No newline at end of file diff --git a/Views/Dialogs/MqttAliasDialog.xaml.cs b/Views/Dialogs/MqttAliasDialog.xaml.cs new file mode 100644 index 0000000..11b5317 --- /dev/null +++ b/Views/Dialogs/MqttAliasDialog.xaml.cs @@ -0,0 +1,13 @@ +using iNKORE.UI.WPF.Modern.Controls; +using PMSWPF.ViewModels.Dialogs; + +namespace PMSWPF.Views.Dialogs; + +public partial class MqttAliasDialog : ContentDialog +{ + public MqttAliasDialog(MqttAliasDialogViewModel viewModel) + { + InitializeComponent(); + DataContext = viewModel; + } +} \ No newline at end of file