实现MQTT关联变量的别名问题,实现了,同个变量发给不同的MQTT服务器的名称不同
This commit is contained in:
@@ -126,6 +126,7 @@ public partial class App : Application
|
||||
services.AddSingleton<UserRepository>();
|
||||
services.AddSingleton<VarDataRepository>();
|
||||
services.AddSingleton<VarTableRepository>();
|
||||
services.AddSingleton<VariableMqttAliasRepository>();
|
||||
// 注册视图模型
|
||||
services.AddSingleton<MainViewModel>();
|
||||
services.AddSingleton<HomeViewModel>();
|
||||
@@ -251,7 +252,8 @@ public partial class App : Application
|
||||
_db.CodeFirst.InitTables<DbVariableS7Data>();
|
||||
_db.CodeFirst.InitTables<DbUser>();
|
||||
_db.CodeFirst.InitTables<DbMqtt>();
|
||||
_db.CodeFirst.InitTables<DbVariableDataMqtt>();
|
||||
_db.CodeFirst.InitTables<DbVariableMqtt>();
|
||||
// _db.CodeFirst.InitTables<DbVariableDataMqtt>();
|
||||
_db.CodeFirst.InitTables<DbMenu>();
|
||||
}
|
||||
}
|
||||
@@ -90,6 +90,6 @@ public class DbMqtt
|
||||
/// <summary>
|
||||
/// 关联的变量数据列表。
|
||||
/// </summary>
|
||||
[Navigate(typeof(DbVariableDataMqtt), "MqttId", "VariableDataId")]
|
||||
public List<DbVariableData>? VariableDatas { get; set; }
|
||||
[Navigate(NavigateType.OneToMany, nameof(DbVariableMqtt.MqttId))]
|
||||
public List<DbVariableMqtt>? VariableMqtts { get; set; }
|
||||
}
|
||||
@@ -161,8 +161,8 @@ public class DbVariableData
|
||||
/// <summary>
|
||||
/// 关联的MQTT配置列表。
|
||||
/// </summary>
|
||||
[Navigate(typeof(DbVariableDataMqtt), "VariableDataId", "MqttId")]
|
||||
public List<DbMqtt>? Mqtts { get; set; }
|
||||
[Navigate(NavigateType.OneToMany, nameof(DbVariableMqtt.VariableDataId))]
|
||||
public List<DbVariableMqtt>? VariableMqtts { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 关联的历史记录列表。
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
using SqlSugar;
|
||||
|
||||
namespace PMSWPF.Data.Entities;
|
||||
|
||||
[SugarTable("VariableDataMqtt")]
|
||||
public class DbVariableDataMqtt
|
||||
{
|
||||
[SugarColumn(IsPrimaryKey = true)]
|
||||
public int VariableDataId { get; set; }
|
||||
|
||||
[SugarColumn(IsPrimaryKey = true)]
|
||||
public int MqttId { get; set; }
|
||||
|
||||
[Navigate(NavigateType.ManyToOne, nameof(VariableDataId))]
|
||||
public DbVariableData VariableData { get; set; }
|
||||
|
||||
[Navigate(NavigateType.ManyToOne, nameof(MqttId))]
|
||||
public DbMqtt Mqtt { get; set; }
|
||||
}
|
||||
@@ -52,7 +52,9 @@ public class MqttRepository
|
||||
stopwatch.Start();
|
||||
using (var _db = DbContext.GetInstance())
|
||||
{
|
||||
var result = await _db.Queryable<DbMqtt>().Includes(m=>m.VariableDatas)
|
||||
var result = await _db.Queryable<DbMqtt>()
|
||||
.Includes(m => m.VariableMqtts, vm => vm.VariableData)
|
||||
.Includes(m => m.VariableMqtts, vm => vm.Mqtt)
|
||||
.ToListAsync();
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"获取所有Mqtt配置耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
@@ -95,7 +97,7 @@ public class MqttRepository
|
||||
catch (Exception ex)
|
||||
{
|
||||
await db.RollbackTranAsync();
|
||||
NlogHelper.Error( $"添加MQTT配置 {{mqtt.Name}} 失败",ex);
|
||||
NlogHelper.Error($"添加MQTT配置 {{mqtt.Name}} 失败", ex);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
@@ -157,11 +159,11 @@ public class MqttRepository
|
||||
.ExecuteCommandAsync();
|
||||
// DeleteAsync menu entry
|
||||
var menu = await _menuRepository.GetMenuByDataIdAsync(mqtt.Id, MenuType.MqttMenu);
|
||||
if (menu!=null )
|
||||
if (menu != null)
|
||||
{
|
||||
await _menuRepository.DeleteAsync(menu, db);
|
||||
}
|
||||
|
||||
|
||||
await db.CommitTranAsync();
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"删除Mqtt配置ID '{mqtt.Id}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
@@ -170,7 +172,7 @@ public class MqttRepository
|
||||
catch (Exception ex)
|
||||
{
|
||||
await db.RollbackTranAsync();
|
||||
NlogHelper.Error( $"删除MQTT配置 {{mqtt.Name}} 失败",ex);
|
||||
NlogHelper.Error($"删除MQTT配置 {{mqtt.Name}} 失败", ex);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,17 +31,28 @@ public class VarDataRepository
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
using (var _db = DbContext.GetInstance())
|
||||
using (var db = DbContext.GetInstance())
|
||||
{
|
||||
var result = await _db.Queryable<DbVariableData>()
|
||||
.In(id)
|
||||
.SingleAsync();
|
||||
var result = await GetByIdAsync(id, db);
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"根据ID '{id}' 获取VariableData耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据ID获取VariableData
|
||||
/// </summary>
|
||||
/// <param name="id">主键ID</param>
|
||||
/// <param name="db">SqlSugarClient实例</param>
|
||||
/// <returns></returns>
|
||||
public async Task<DbVariableData> GetByIdAsync(int id, SqlSugarClient db)
|
||||
{
|
||||
return await db.Queryable<DbVariableData>()
|
||||
.In(id)
|
||||
.SingleAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有VariableData
|
||||
/// </summary>
|
||||
@@ -50,19 +61,30 @@ public class VarDataRepository
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
using (var _db = DbContext.GetInstance())
|
||||
using (var db = DbContext.GetInstance())
|
||||
{
|
||||
var result = await _db.Queryable<DbVariableData>()
|
||||
.Includes(d => d.VariableTable)
|
||||
.Includes(d => d.VariableTable.Device)
|
||||
.ToListAsync();
|
||||
var result = await GetAllAsync(db);
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"获取所有VariableData耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return result.Select(d => _mapper.Map<VariableData>(d))
|
||||
.ToList();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有VariableData
|
||||
/// </summary>
|
||||
/// <param name="db">SqlSugarClient实例</param>
|
||||
/// <returns></returns>
|
||||
public async Task<List<VariableData>> GetAllAsync(SqlSugarClient db)
|
||||
{
|
||||
var result = await db.Queryable<DbVariableData>()
|
||||
.Includes(d => d.VariableTable)
|
||||
.Includes(d => d.VariableTable.Device)
|
||||
.ToListAsync();
|
||||
return result.Select(d => _mapper.Map<VariableData>(d))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有VariableData
|
||||
/// </summary>
|
||||
@@ -71,17 +93,29 @@ public class VarDataRepository
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
using (var _db = DbContext.GetInstance())
|
||||
using (var db = DbContext.GetInstance())
|
||||
{
|
||||
var result = await _db.Queryable<DbVariableData>()
|
||||
.Where(d => d.VariableTableId == varTableId)
|
||||
.ToListAsync();
|
||||
var result = await GetByVariableTableIdAsync(varTableId, db);
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"获取变量表的所有变量{result.Count()}个耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return result.Select(d=>_mapper.Map<VariableData>(d)).ToList();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有VariableData
|
||||
/// </summary>
|
||||
/// <param name="db">SqlSugarClient实例</param>
|
||||
/// <returns></returns>
|
||||
public async Task<List<VariableData>> GetByVariableTableIdAsync(int varTableId, SqlSugarClient db)
|
||||
{
|
||||
var result = await db.Queryable<DbVariableData>()
|
||||
.Where(d => d.VariableTableId == varTableId)
|
||||
.ToListAsync();
|
||||
return result.Select(d => _mapper.Map<VariableData>(d))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新增VariableData
|
||||
/// </summary>
|
||||
@@ -170,16 +204,28 @@ public class VarDataRepository
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
using (var _db = DbContext.GetInstance())
|
||||
using (var db = DbContext.GetInstance())
|
||||
{
|
||||
var result = await _db.Updateable<DbVariableData>(_mapper.Map<DbVariableData>(variableData))
|
||||
.ExecuteCommandAsync();
|
||||
var result = await UpdateAsync(variableData, db);
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"更新VariableData '{variableData.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新VariableData
|
||||
/// </summary>
|
||||
/// <param name="variableData">VariableData实体</param>
|
||||
/// <param name="db">SqlSugarClient实例</param>
|
||||
/// <returns></returns>
|
||||
public async Task<int> UpdateAsync(VariableData variableData, SqlSugarClient db)
|
||||
{
|
||||
var result = await db.Updateable<DbVariableData>(_mapper.Map<DbVariableData>(variableData))
|
||||
.ExecuteCommandAsync();
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新VariableData
|
||||
/// </summary>
|
||||
@@ -188,7 +234,7 @@ public class VarDataRepository
|
||||
public async Task<int> UpdateAsync(List<VariableData> variableDatas)
|
||||
{
|
||||
using var _db = DbContext.GetInstance();
|
||||
return await UpdateAsync(variableDatas, _db);
|
||||
return await UpdateAsync(variableDatas, _db);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -230,18 +276,13 @@ public class VarDataRepository
|
||||
/// 根据ID删除VariableData
|
||||
/// </summary>
|
||||
/// <param name="id">主键ID</param>
|
||||
/// <param name="db">SqlSugarClient实例</param>
|
||||
/// <returns></returns>
|
||||
public async Task<int> DeleteAsync(VariableData variableData, SqlSugarClient db)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
using var _db = DbContext.GetInstance();
|
||||
|
||||
var result = await _db.Deleteable<DbVariableData>()
|
||||
.Where(d => d.Id == variableData.Id)
|
||||
.ExecuteCommandAsync();
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"删除VariableData: '{variableData.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
var result = await db.Deleteable<DbVariableData>()
|
||||
.Where(d => d.Id == variableData.Id)
|
||||
.ExecuteCommandAsync();
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -267,58 +308,78 @@ public class VarDataRepository
|
||||
/// 删除VariableData
|
||||
/// </summary>
|
||||
/// <param name="id">主键ID</param>
|
||||
/// <param name="db">SqlSugarClient实例</param>
|
||||
/// <returns></returns>
|
||||
public async Task<int> DeleteAsync(IEnumerable<VariableData> variableDatas, SqlSugarClient db)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
using var _db = DbContext.GetInstance();
|
||||
|
||||
var dbList = variableDatas.Select(vd => _mapper.Map<DbVariableData>(vd))
|
||||
.ToList();
|
||||
var result = await _db.Deleteable<DbVariableData>(dbList)
|
||||
.ExecuteCommandAsync();
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"删除VariableData: '{variableDatas.Count()}'个 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
var result = await db.Deleteable<DbVariableData>(dbList)
|
||||
.ExecuteCommandAsync();
|
||||
return result;
|
||||
}
|
||||
|
||||
// public VarDataRepository(IMapper mapper)
|
||||
// {
|
||||
// _mapper = mapper;
|
||||
// }
|
||||
|
||||
/// <summary>
|
||||
/// 为变量添加MQTT服务器关联。
|
||||
/// 为变量添加MQTT服务器关联,并指定别名。
|
||||
/// </summary>
|
||||
/// <param name="variableMqttList"></param>
|
||||
/// <param name="variableDatas">要添加MQTT服务器的变量数据列表。</param>
|
||||
/// <param name="mqtt">要关联的MQTT服务器。</param>
|
||||
/// <returns>成功添加关联的数量。</returns>
|
||||
public async Task<int> AddMqttToVariablesAsync(IEnumerable<VariableData> variableDatas, Mqtt mqtt)
|
||||
/// <returns>成功添加或更新关联的数量。</returns>
|
||||
public async Task<int> AddMqttToVariablesAsync(IEnumerable<VariableMqtt> variableMqttList)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
int addedCount = 0;
|
||||
int affectedCount = 0;
|
||||
using var db = DbContext.GetInstance();
|
||||
//插入列表
|
||||
List<DbVariableMqtt> insertDbVM = new List<DbVariableMqtt>();
|
||||
|
||||
using (var db = DbContext.GetInstance())
|
||||
foreach (var variableMqtt in variableMqttList)
|
||||
{
|
||||
foreach (var variableData in variableDatas)
|
||||
var existingAlias = await db.Queryable<DbVariableMqtt>()
|
||||
.Where(it => it.VariableDataId == variableMqtt.VariableData.Id && it.MqttId == variableMqtt.Mqtt.Id)
|
||||
.FirstAsync();
|
||||
if (existingAlias == null)
|
||||
{
|
||||
// 检查是否已存在该关联
|
||||
var existingAssociation = await db.Queryable<DbVariableDataMqtt>()
|
||||
.Where(x => x.VariableDataId == variableData.Id && x.MqttId == mqtt.Id)
|
||||
.FirstAsync();
|
||||
|
||||
if (existingAssociation == null)
|
||||
{
|
||||
// 如果不存在,则添加新的关联
|
||||
await db.Insertable(new DbVariableDataMqtt
|
||||
{
|
||||
VariableDataId = variableData.Id,
|
||||
MqttId = mqtt.Id
|
||||
}).ExecuteCommandAsync();
|
||||
addedCount++;
|
||||
}
|
||||
// 如果不存在,则添加新的关联
|
||||
insertDbVM.Add(new DbVariableMqtt
|
||||
{
|
||||
VariableDataId = variableMqtt.VariableData.Id,
|
||||
MqttId = variableMqtt.Mqtt.Id,
|
||||
MqttAlias = variableMqtt.MqttAlias,
|
||||
CreateTime = DateTime.Now,
|
||||
UpdateTime = DateTime.Now
|
||||
});
|
||||
|
||||
affectedCount++;
|
||||
}
|
||||
else if (existingAlias.MqttAlias !=variableMqtt.MqttAlias)
|
||||
{
|
||||
// 如果存在但别名不同,则更新别名
|
||||
await db.Updateable<DbVariableMqtt>()
|
||||
.SetColumns(it => it.MqttAlias == variableMqtt.MqttAlias)
|
||||
.Where(it => it.VariableDataId == variableMqtt.VariableData.Id && it.MqttId == variableMqtt.Mqtt.Id)
|
||||
.ExecuteCommandAsync();
|
||||
NlogHelper.Info(
|
||||
$"为 {variableMqtt.VariableData.Name} 变量更新MQTT服务器{variableMqtt.Mqtt.Name}关联.");
|
||||
affectedCount++;
|
||||
}
|
||||
}
|
||||
|
||||
if (insertDbVM.Count > 0)
|
||||
{
|
||||
await db.Insertable<DbVariableMqtt>(insertDbVM).ExecuteCommandAsync();
|
||||
NlogHelper.Info(
|
||||
$"为 {insertDbVM.Count} 个变量添加MQTT服务器 关联,成功影响 {affectedCount} 个,耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
}
|
||||
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"为 {variableDatas.Count()} 个变量添加MQTT服务器 '{mqtt.Name}' 关联,成功添加 {addedCount} 个,耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return addedCount;
|
||||
|
||||
return affectedCount;
|
||||
}
|
||||
}
|
||||
@@ -95,7 +95,7 @@ public partial class Mqtt : ObservableObject
|
||||
/// <summary>
|
||||
/// 关联的变量数据列表。
|
||||
/// </summary>
|
||||
public List<VariableData>? VariableDatas { get; set; }
|
||||
public List<VariableMqtt>? VariableMqtts { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否连接。
|
||||
|
||||
@@ -175,7 +175,7 @@ public partial class VariableData : ObservableObject, IEquatable<VariableData>
|
||||
/// <summary>
|
||||
/// 关联的MQTT配置列表。
|
||||
/// </summary>
|
||||
public List<Mqtt> Mqtts { get; set; }
|
||||
public List<VariableMqtt>? VariableMqtts { get; set; }
|
||||
|
||||
partial void OnPollLevelTypeChanged(PollLevelType value)
|
||||
{
|
||||
|
||||
@@ -25,6 +25,7 @@ public class MappingProfile : Profile
|
||||
CreateMap<DbVariableData, VariableData>().ReverseMap();
|
||||
// --- MQTT 和 变量数据 映射 ---
|
||||
CreateMap<DbMqtt, Mqtt>().ReverseMap();
|
||||
CreateMap<DbVariableMqtt, VariableMqtt>().ReverseMap();
|
||||
|
||||
CreateMap<DbMenu, MenuBean>().ReverseMap();
|
||||
|
||||
|
||||
@@ -237,4 +237,28 @@ public class DialogService :IDialogService
|
||||
var dialog = new ImportResultDialog(vm);
|
||||
await dialog.ShowAsync();
|
||||
}
|
||||
|
||||
public async Task<string?> ShowMqttAliasDialog(string variableName, string mqttServerName)
|
||||
{
|
||||
var vm = new MqttAliasDialogViewModel(variableName, mqttServerName);
|
||||
var dialog = new MqttAliasDialog(vm);
|
||||
var result = await dialog.ShowAsync();
|
||||
if (result == ContentDialogResult.Primary)
|
||||
{
|
||||
return vm.MqttAlias;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<List<VariableMqtt>> ShowMqttAliasBatchEditDialog(List<VariableData> selectedVariables, Mqtt selectedMqtt)
|
||||
{
|
||||
var vm = new MqttAliasBatchEditDialogViewModel(selectedVariables, selectedMqtt);
|
||||
var dialog = new MqttAliasBatchEditDialog(vm);
|
||||
var result = await dialog.ShowAsync();
|
||||
if (result == ContentDialogResult.Primary)
|
||||
{
|
||||
return vm.VariablesToEdit.ToList();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -27,4 +27,6 @@ public interface IDialogService
|
||||
Task<OpcUaUpdateType?> ShowOpcUaUpdateTypeDialog();
|
||||
Task<bool?> ShowIsActiveDialog(bool currentIsActive);
|
||||
Task ShowImportResultDialog(List<string> importedVariables, List<string> existingVariables);
|
||||
Task<string?> ShowMqttAliasDialog(string variableName, string mqttServerName);
|
||||
Task<List<VariableMqtt>> ShowMqttAliasBatchEditDialog(List<VariableData> selectedVariables, Mqtt selectedMqtt);
|
||||
}
|
||||
@@ -9,6 +9,7 @@ using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
using PMSWPF.Data.Repositories;
|
||||
|
||||
namespace PMSWPF.ViewModels
|
||||
{
|
||||
@@ -32,7 +33,9 @@ namespace PMSWPF.ViewModels
|
||||
/// 与当前MQTT服务器关联的变量数据集合。
|
||||
/// </summary>
|
||||
[ObservableProperty]
|
||||
private ObservableCollection<VariableData> _associatedVariables;
|
||||
private ObservableCollection<VariableMqtt> _associatedVariables;
|
||||
|
||||
private readonly VariableMqttAliasRepository _variableMqttAliasRepository;
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数。
|
||||
@@ -41,19 +44,19 @@ namespace PMSWPF.ViewModels
|
||||
/// <param name="dataServices">数据服务。</param>
|
||||
/// <param name="dialogService">对话框服务。</param>
|
||||
public MqttServerDetailViewModel(ILogger<MqttServerDetailViewModel> logger, DataServices dataServices,
|
||||
IDialogService dialogService)
|
||||
IDialogService dialogService, VariableMqttAliasRepository variableMqttAliasRepository)
|
||||
{
|
||||
_logger = logger;
|
||||
_dataServices = dataServices;
|
||||
_dialogService = dialogService;
|
||||
AssociatedVariables = new ObservableCollection<VariableData>();
|
||||
_variableMqttAliasRepository = variableMqttAliasRepository;
|
||||
}
|
||||
|
||||
public override void OnLoaded()
|
||||
{
|
||||
if (CurrentMqtt.VariableDatas != null)
|
||||
if (CurrentMqtt.VariableMqtts != null)
|
||||
{
|
||||
AssociatedVariables = new ObservableCollection<VariableData>(CurrentMqtt.VariableDatas);
|
||||
AssociatedVariables =new ObservableCollection<VariableMqtt>(CurrentMqtt.VariableMqtts) ;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,11 +111,11 @@ namespace PMSWPF.ViewModels
|
||||
foreach (var variable in variablesList) // 使用ToList()避免在迭代时修改集合
|
||||
{
|
||||
// 移除变量与当前MQTT服务器的关联
|
||||
variable.Mqtts?.Remove(CurrentMqtt);
|
||||
// 标记变量为已修改,以便保存时更新数据库
|
||||
variable.IsModified = true;
|
||||
AssociatedVariables.Remove(variable);
|
||||
_logger.LogInformation($"Removed variable {variable.Name} from MQTT server {CurrentMqtt.Name}.");
|
||||
// variable.Mqtts?.Remove(CurrentMqtt);
|
||||
// // 标记变量为已修改,以便保存时更新数据库
|
||||
// variable.IsModified = true;
|
||||
// AssociatedVariables.Remove(variable);
|
||||
// _logger.LogInformation($"Removed variable {variable.Name} from MQTT server {CurrentMqtt.Name}.");
|
||||
}
|
||||
|
||||
// TODO: 这里需要调用DataServices来更新数据库中VariableData的Mqtt关联
|
||||
|
||||
@@ -13,6 +13,7 @@ using PMSWPF.Extensions;
|
||||
using PMSWPF.Helper;
|
||||
using PMSWPF.Models;
|
||||
using PMSWPF.Services;
|
||||
using SqlSugar;
|
||||
|
||||
namespace PMSWPF.ViewModels;
|
||||
|
||||
@@ -520,6 +521,7 @@ partial class VariableTableViewModel : ViewModelBase
|
||||
// 使用 AutoMapper 更新现有对象的属性,保持对象引用不变
|
||||
_mapper.Map(newVariable, currentVariable);
|
||||
}
|
||||
|
||||
// 从字典中移除已处理的项,剩余的将是新增项
|
||||
latestVariablesDict.Remove(currentVariable.Id);
|
||||
}
|
||||
@@ -582,7 +584,8 @@ partial class VariableTableViewModel : ViewModelBase
|
||||
// 根据协议类型检查S7地址或NodeId是否重复
|
||||
if (variableTable.ProtocolType == ProtocolType.S7)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(varData.S7Address) && DataVariables.Any(v => v.S7Address == varData.S7Address))
|
||||
if (!string.IsNullOrEmpty(varData.S7Address) &&
|
||||
DataVariables.Any(v => v.S7Address == varData.S7Address))
|
||||
{
|
||||
isDuplicate = true;
|
||||
duplicateReason = $"S7地址 '{varData.S7Address}' 已存在。";
|
||||
@@ -774,41 +777,69 @@ partial class VariableTableViewModel : ViewModelBase
|
||||
return; // 用户取消选择
|
||||
}
|
||||
|
||||
// 调用新的仓库方法来添加MQTT服务器关联
|
||||
var addedCount = await _varDataRepository.AddMqttToVariablesAsync(validVariables, selectedMqtt);
|
||||
// 显示批量编辑别名对话框
|
||||
var editedVariableMqtts = await _dialogService.ShowMqttAliasBatchEditDialog(validVariables, selectedMqtt);
|
||||
|
||||
if (addedCount > 0)
|
||||
if (editedVariableMqtts == null || !editedVariableMqtts.Any())
|
||||
{
|
||||
// 更新已经加载的变量的Mqtt服务器和Mqtt服务器的变量表
|
||||
foreach (var variable in validVariables)
|
||||
{
|
||||
// 更新变量的 Mqtts 集合
|
||||
if (variable.Mqtts == null)
|
||||
NotificationHelper.ShowInfo("没有变量别名被设置或已取消。");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
int totalAffectedCount = 0;
|
||||
// 调用仓库方法来添加或更新MQTT服务器关联和别名
|
||||
var affectedCount = await _varDataRepository.AddMqttToVariablesAsync(editedVariableMqtts);
|
||||
totalAffectedCount += affectedCount;
|
||||
if (affectedCount == 0)
|
||||
{
|
||||
NotificationHelper.ShowInfo("没有任何要添加或者更新的MQTT服务器.");
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var editedVariableMqtt in editedVariableMqtts)
|
||||
{
|
||||
|
||||
// 更新内存中的 VariableData 对象
|
||||
var originalVariable = editedVariableMqtt.VariableData;
|
||||
if (originalVariable.VariableMqtts == null)
|
||||
{
|
||||
variable.Mqtts = new List<Mqtt>();
|
||||
}
|
||||
if (!variable.Mqtts.Any(m => m.Id == selectedMqtt.Id))
|
||||
{
|
||||
variable.Mqtts.Add(selectedMqtt);
|
||||
originalVariable.VariableMqtts = new List<VariableMqtt>();
|
||||
}
|
||||
|
||||
// 更新 Mqtt 服务器的 VariableDatas 集合
|
||||
if (selectedMqtt.VariableDatas == null)
|
||||
// 检查是否已存在该变量与该MQTT服务器的关联
|
||||
var existingVariableMqtt
|
||||
= originalVariable.VariableMqtts.FirstOrDefault(vm => vm.MqttId ==
|
||||
editedVariableMqtt.Mqtt.Id);
|
||||
|
||||
if (existingVariableMqtt == null)
|
||||
{
|
||||
selectedMqtt.VariableDatas = new List<VariableData>();
|
||||
// 如果不存在,则添加新的关联
|
||||
originalVariable.VariableMqtts.Add(new VariableMqtt
|
||||
{
|
||||
VariableDataId = originalVariable.Id,
|
||||
MqttId = editedVariableMqtt.Mqtt.Id,
|
||||
MqttAlias = editedVariableMqtt.MqttAlias,
|
||||
Mqtt = editedVariableMqtt.Mqtt // 关联Mqtt对象,方便UI显示
|
||||
});
|
||||
}
|
||||
if (!selectedMqtt.VariableDatas.Any(v => v.Id == variable.Id))
|
||||
else
|
||||
{
|
||||
selectedMqtt.VariableDatas.Add(variable);
|
||||
// 如果存在,则更新别名
|
||||
existingVariableMqtt.MqttAlias = editedVariableMqtt.MqttAlias;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (totalAffectedCount > 0)
|
||||
{
|
||||
// 刷新界面以反映更改
|
||||
await RefreshDataView();
|
||||
NotificationHelper.ShowSuccess($"已成功为 {addedCount} 个变量添加MQTT服务器: {selectedMqtt.Name}");
|
||||
NotificationHelper.ShowSuccess($"已成功为 {totalAffectedCount} 个变量添加/更新MQTT服务器: {selectedMqtt.Name} 的别名。");
|
||||
}
|
||||
else
|
||||
{
|
||||
NotificationHelper.ShowInfo($"没有新的变量关联到MQTT服务器: {selectedMqtt.Name},可能已存在。");
|
||||
NotificationHelper.ShowInfo($"没有新的变量关联或别名更新到MQTT服务器: {selectedMqtt.Name}。");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
@@ -90,12 +90,13 @@
|
||||
IsReadOnly="True"
|
||||
SelectionMode="Extended">
|
||||
<DataGrid.Columns>
|
||||
<DataGridTextColumn Header="名称" Binding="{Binding Name}"/>
|
||||
<DataGridTextColumn Header="地址" Binding="{Binding S7Address}"/>
|
||||
<DataGridTextColumn Header="数据类型" Binding="{Binding DataType}"/>
|
||||
<DataGridTextColumn Header="当前值" Binding="{Binding DataValue}"/>
|
||||
<DataGridTextColumn Header="显示值" Binding="{Binding DisplayValue}"/>
|
||||
<DataGridTextColumn Header="更新时间" Binding="{Binding UpdateTime, StringFormat='yyyy-MM-dd HH:mm:ss'}"/>
|
||||
<DataGridTextColumn Header="变量名称" Binding="{Binding VariableData.Name}"/>
|
||||
<DataGridTextColumn Header="MQTT发送名称" Binding="{Binding MqttAlias}"/>
|
||||
<DataGridTextColumn Header="地址" Binding="{Binding VariableData.S7Address}"/>
|
||||
<DataGridTextColumn Header="数据类型" Binding="{Binding VariableData.DataType}"/>
|
||||
<DataGridTextColumn Header="当前值" Binding="{Binding VariableData.DataValue}"/>
|
||||
<DataGridTextColumn Header="显示值" Binding="{Binding VariableData.DisplayValue}"/>
|
||||
<DataGridTextColumn Header="更新时间" Binding="{Binding VariableData.UpdateTime, StringFormat='yyyy-MM-dd HH:mm:ss'}"/>
|
||||
</DataGrid.Columns>
|
||||
</DataGrid>
|
||||
</Grid>
|
||||
|
||||
Reference in New Issue
Block a user