1 feat: 为MqttServerDetailView添加取消关联功能
2 3 - 在MqttServerDetailView的DataGrid右键菜单中添加"取消关联"选项 4 - 实现UnassignAlias命令,允许用户从MQTT服务器取消关联变量 5 - 使用确认对话框以防止误操作 6 - 集成现有的删除API来实际移除变量与MQTT服务器的关联 7 - 更新UI以提供用户反馈 8 9 此功能允许用户通过右键菜单轻松取消MQTT服务器与变量的关联。
This commit is contained in:
@@ -32,15 +32,17 @@ public class MqttAliasManagementService : IMqttAliasManagementService
|
|||||||
// Add to cache
|
// Add to cache
|
||||||
if (_storageService.MqttServers.TryGetValue(newAlias.MqttServerId, out var server))
|
if (_storageService.MqttServers.TryGetValue(newAlias.MqttServerId, out var server))
|
||||||
{
|
{
|
||||||
|
newAlias.MqttServer = server;
|
||||||
server.VariableAliases.Add(newAlias);
|
server.VariableAliases.Add(newAlias);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add to cache
|
// Add to cache
|
||||||
if (_storageService.Variables.TryGetValue(newAlias.VariableId, out var variable))
|
if (_storageService.Variables.TryGetValue(newAlias.VariableId, out var variable))
|
||||||
{
|
{
|
||||||
|
newAlias.Variable = variable;
|
||||||
variable.MqttAliases.Add(newAlias);
|
variable.MqttAliases.Add(newAlias);
|
||||||
}
|
}
|
||||||
|
_storageService.MqttAliases.TryAdd(newAlias.Id, newAlias);
|
||||||
_eventService.RaiseMqttAliasChanged(this, new MqttAliasChangedEventArgs(ActionChangeType.Added, newAlias));
|
_eventService.RaiseMqttAliasChanged(this, new MqttAliasChangedEventArgs(ActionChangeType.Added, newAlias));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,51 +92,19 @@ public class MqttAliasManagementService : IMqttAliasManagementService
|
|||||||
|
|
||||||
public async Task<bool> DeleteAsync(int id)
|
public async Task<bool> DeleteAsync(int id)
|
||||||
{
|
{
|
||||||
var res = await DeleteBatchAsync(new List<int> { id });
|
var result = await _appService.RemoveAliasAsync(id);
|
||||||
return res > 0;
|
if (result == 0) return false;
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<int> DeleteBatchAsync(List<int> ids)
|
if (_storageService.MqttAliases.TryGetValue(id, out var mqttAlias))
|
||||||
{
|
|
||||||
int counter = 0;
|
|
||||||
foreach (var id in ids)
|
|
||||||
{
|
{
|
||||||
if (!_storageService.MqttAliases.TryGetValue(id, out var mqttAlias))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var variableId = mqttAlias.VariableId;
|
mqttAlias.MqttServer.VariableAliases.Remove(mqttAlias);
|
||||||
var mqttServerId = mqttAlias.MqttServerId;
|
mqttAlias.Variable.MqttAliases.Remove(mqttAlias);
|
||||||
|
_storageService.MqttAliases.TryRemove(mqttAlias.Id,out _);
|
||||||
var result = await _appService.RemoveAliasAsync(id);
|
|
||||||
if (result == 0) continue;
|
|
||||||
|
|
||||||
// Remove from cache
|
|
||||||
if (_storageService.MqttServers.TryGetValue(mqttServerId, out var server))
|
|
||||||
{
|
|
||||||
var aliasToRemove = server.VariableAliases.FirstOrDefault(a => a.Id == id);
|
|
||||||
if (aliasToRemove != null)
|
|
||||||
{
|
|
||||||
server.VariableAliases.Remove(aliasToRemove);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Remove from cache
|
|
||||||
if (_storageService.Variables.TryGetValue(mqttServerId, out var variable))
|
|
||||||
{
|
|
||||||
var aliasToRemove = variable.MqttAliases.FirstOrDefault(a => a.Id == id);
|
|
||||||
if (aliasToRemove != null)
|
|
||||||
{
|
|
||||||
variable.MqttAliases.Remove(aliasToRemove);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_eventService.RaiseMqttAliasChanged(
|
_eventService.RaiseMqttAliasChanged(
|
||||||
this, new MqttAliasChangedEventArgs(ActionChangeType.Deleted, mqttAlias));
|
this, new MqttAliasChangedEventArgs(ActionChangeType.Deleted, mqttAlias));
|
||||||
counter++;
|
|
||||||
}
|
}
|
||||||
return counter;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -133,21 +133,23 @@ public class MqttAliasDataService : IMqttAliasDataService
|
|||||||
public async Task<bool> DeleteMqttAlias(MqttAliasItem mqttAlias)
|
public async Task<bool> DeleteMqttAlias(MqttAliasItem mqttAlias)
|
||||||
{
|
{
|
||||||
// 从数据库和内存中删除MQTT别名
|
// 从数据库和内存中删除MQTT别名
|
||||||
var result = await _mqttAliasManagementService.UpdateAsync(new MqttAlias
|
var result = await _mqttAliasManagementService.DeleteAsync(mqttAlias.Id);
|
||||||
{
|
|
||||||
Id = mqttAlias.Id,
|
|
||||||
VariableId = mqttAlias.VariableId,
|
|
||||||
MqttServerId = mqttAlias.MqttServerId,
|
|
||||||
Alias = null // Setting alias to null effectively removes the alias
|
|
||||||
});
|
|
||||||
|
|
||||||
if (result > 0)
|
if (result )
|
||||||
{
|
{
|
||||||
// 从界面删除MQTT别名
|
|
||||||
_dataStorageService.MqttAliases.Remove(mqttAlias.Id);
|
if (_dataStorageService.MqttServers.TryGetValue(mqttAlias.MqttServerId, out var mqttServerItem))
|
||||||
|
{
|
||||||
|
mqttServerItem.VariableAliases.Remove(mqttAlias);
|
||||||
|
}
|
||||||
|
if (_dataStorageService.Variables.TryGetValue(mqttAlias.VariableId, out var variableItem))
|
||||||
|
{
|
||||||
|
variableItem.MqttAliases.Remove(mqttAlias);
|
||||||
|
}
|
||||||
|
_dataStorageService.MqttAliases.Remove(mqttAlias.Id, out _);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result > 0;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ namespace DMS.WPF.ViewModels
|
|||||||
private readonly INotificationService _notificationService;
|
private readonly INotificationService _notificationService;
|
||||||
private readonly IEventService _eventService;
|
private readonly IEventService _eventService;
|
||||||
private readonly IMqttManagementService _mqttManagementService;
|
private readonly IMqttManagementService _mqttManagementService;
|
||||||
|
private readonly IMqttAliasDataService _mqttAliasDataService;
|
||||||
private readonly IWPFDataService _wpfDataService;
|
private readonly IWPFDataService _wpfDataService;
|
||||||
private readonly IDataStorageService _dataStorageService;
|
private readonly IDataStorageService _dataStorageService;
|
||||||
private readonly INavigationService _navigationService;
|
private readonly INavigationService _navigationService;
|
||||||
@@ -58,6 +59,7 @@ namespace DMS.WPF.ViewModels
|
|||||||
INotificationService notificationService,
|
INotificationService notificationService,
|
||||||
IEventService eventService,
|
IEventService eventService,
|
||||||
IMqttManagementService mqttManagementService,
|
IMqttManagementService mqttManagementService,
|
||||||
|
IMqttAliasDataService mqttAliasDataService,
|
||||||
IWPFDataService wpfDataService,
|
IWPFDataService wpfDataService,
|
||||||
IDataStorageService dataStorageService,
|
IDataStorageService dataStorageService,
|
||||||
INavigationService navigationService)
|
INavigationService navigationService)
|
||||||
@@ -67,6 +69,7 @@ namespace DMS.WPF.ViewModels
|
|||||||
_notificationService = notificationService;
|
_notificationService = notificationService;
|
||||||
this._eventService = eventService;
|
this._eventService = eventService;
|
||||||
_mqttManagementService = mqttManagementService;
|
_mqttManagementService = mqttManagementService;
|
||||||
|
this._mqttAliasDataService = mqttAliasDataService;
|
||||||
this._wpfDataService = wpfDataService;
|
this._wpfDataService = wpfDataService;
|
||||||
this._dataStorageService = dataStorageService;
|
this._dataStorageService = dataStorageService;
|
||||||
_navigationService = navigationService;
|
_navigationService = navigationService;
|
||||||
@@ -259,6 +262,59 @@ namespace DMS.WPF.ViewModels
|
|||||||
_notificationService.ShowError($"修改发送名称时发生错误:{e.Message}");
|
_notificationService.ShowError($"修改发送名称时发生错误:{e.Message}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 取消关联变量
|
||||||
|
/// </summary>
|
||||||
|
[RelayCommand]
|
||||||
|
private async Task UnassignAlias()
|
||||||
|
{
|
||||||
|
if (SelectedMqttAliaes.Count == 0)
|
||||||
|
{
|
||||||
|
_notificationService.ShowError("请选择要取消关联的变量项。");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var mqttAliasItems= SelectedMqttAliaes.Cast<MqttAliasItem>().ToList();
|
||||||
|
// 拼接要删除的变量名称,用于确认提示
|
||||||
|
var names = string.Join("、", mqttAliasItems.Select(v => v.Alias));
|
||||||
|
|
||||||
|
// 显示确认删除对话框
|
||||||
|
ConfirmDialogViewModel confirmDialogViewModel
|
||||||
|
= new ConfirmDialogViewModel("取消关联", $"确认要取消关联变量:{names}与MQTT服务器的关联吗,取消后不可恢复,确认要取消吗?", "取消关联");
|
||||||
|
var isDel = await _dialogService.ShowDialogAsync(confirmDialogViewModel);
|
||||||
|
|
||||||
|
if (!isDel)
|
||||||
|
return; // 如果用户取消删除,则返回
|
||||||
|
|
||||||
|
int successCount = 0;
|
||||||
|
|
||||||
|
foreach (var mqttAliasItem in mqttAliasItems)
|
||||||
|
{
|
||||||
|
var deleteResult = await _mqttAliasDataService.DeleteMqttAlias(mqttAliasItem);
|
||||||
|
if (deleteResult)
|
||||||
|
{
|
||||||
|
successCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (successCount > 0)
|
||||||
|
{
|
||||||
|
_notificationService.ShowSuccess($"成功取消关联:{successCount}个变量");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_notificationService.ShowError("取消关联失败。");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_logger.LogError(e, "取消关联变量时发生错误");
|
||||||
|
_notificationService.ShowError($"取消关联时发生错误:{e.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public override Task OnNavigatedFromAsync(NavigationParameter parameter)
|
public override Task OnNavigatedFromAsync(NavigationParameter parameter)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -439,6 +439,9 @@
|
|||||||
Command="{Binding ModifyAliasCommand}"
|
Command="{Binding ModifyAliasCommand}"
|
||||||
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}, Path=SelectedItems[0]}"
|
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}, Path=SelectedItems[0]}"
|
||||||
Header="修改发送名称" />
|
Header="修改发送名称" />
|
||||||
|
<MenuItem
|
||||||
|
Command="{Binding UnassignAliasCommand}"
|
||||||
|
Header="取消关联" />
|
||||||
</ContextMenu>
|
</ContextMenu>
|
||||||
</DataGrid.ContextMenu>
|
</DataGrid.ContextMenu>
|
||||||
<DataGrid.Columns>
|
<DataGrid.Columns>
|
||||||
|
|||||||
Reference in New Issue
Block a user