临时提交

This commit is contained in:
2025-08-25 20:16:57 +08:00
parent 645671e4b9
commit 8290c96b1b
20 changed files with 1024 additions and 448 deletions

View File

@@ -273,86 +273,66 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable
/// 此命令通常绑定到UI中的“从OPC UA导入”按钮。
/// </summary>
[RelayCommand]
private async Task ImportFromOpcUaServer()
private async void ImportFromOpcUaServer()
{
// ContentDialog processingDialog = null; // 用于显示处理中的对话框
// try
// {
// // 检查OPC UA Endpoint URL是否已设置
// string opcUaEndpointUrl = VariableTable?.Device?.OpcUaEndpointUrl;
// if (string.IsNullOrEmpty(opcUaEndpointUrl))
// {
// NotificationHelper.ShowError("OPC UA Endpoint URL 未设置。请在设备详情中配置。");
// return;
// }
//
// // 显示OPC UA导入对话框让用户选择要导入的变量
// var importedVariables = await _dialogService.ShowOpcUaImportDialog(opcUaEndpointUrl);
// if (importedVariables == null || !importedVariables.Any())
// {
// return; // 用户取消或没有选择任何变量
// }
//
// // 显示处理中的对话框
// processingDialog = _dialogService.ShowProcessingDialog("正在处理...", "正在导入OPC UA变量,请稍等片刻....");
//
// // 在进行重复检查之前,先刷新 Variables 集合,确保其包含所有最新数据
// await RefreshDataView();
//
// List<Variable> newVariables = new List<Variable>();
// List<string> importedVariableNames = new List<string>();
// List<string> existingVariableNames = new List<string>();
//
// foreach (var variableData in importedVariables)
// {
// // 判断是否存在重复变量,仅在当前 VariableTable 的 Variables 中查找
// bool isDuplicate = Variables.Any(existingVar =>
// (existingVar.Name == variableData.Name) ||
// (!string.IsNullOrEmpty(variableData.NodeId) &&
// existingVar.NodeId == variableData.NodeId) ||
// (!string.IsNullOrEmpty(variableData.OpcUaNodeId) &&
// existingVar.OpcUaNodeId == variableData.OpcUaNodeId)
// );
//
// if (isDuplicate)
// {
// existingVariableNames.Add(variableData.Name);
// }
// else
// {
// variableData.CreateTime = DateTime.Now;
// variableData.VariableTableId = VariableTable.Id;
// variableData.ProtocolType = ProtocolType.OpcUA; // 确保协议类型正确
// variableData.IsModified = false;
// newVariables.Add(variableData);
// importedVariableNames.Add(variableData.Name);
// }
// }
//
// if (newVariables.Any())
// {
// // 批量插入新变量数据到数据库
// var resVarDataCount = await _varDataRepository.AddAsync(newVariables);
// NlogHelper.Info($"成功导入OPC UA变量{resVarDataCount}个。");
// }
//
// // 再次刷新 Variables 集合,以反映新添加的数据
// await RefreshDataView();
//
// processingDialog?.Hide(); // 隐藏处理中的对话框
//
// // 显示导入结果对话框
// await _dialogService.ShowImportResultDialog(importedVariableNames, existingVariableNames);
// }
// catch (Exception e)
// {
// // 捕获并显示错误通知
// NotificationHelper.ShowError($"从OPC UA服务器导入变量的过程中发生了不可预期的错误{e.Message}", e);
// }
// finally
// {
// processingDialog?.Hide(); // 确保在任何情况下都隐藏对话框
// }
try
{
// 检查OPC UA Endpoint URL是否已设置
string opcUaEndpointUrl = CurrentVariableTable.Device.OpcUaServerUrl;
if (string.IsNullOrEmpty(opcUaEndpointUrl))
{
NotificationHelper.ShowError("OPC UA Endpoint URL 未设置。请在设备详情中配置。");
return;
}
// 显示OPC UA导入对话框让用户选择要导入的变量
ImportOpcUaDialogViewModel importOpcUaDialogViewModel = new ImportOpcUaDialogViewModel();
var importedVariables = await _dialogService.ShowDialogAsync(importOpcUaDialogViewModel);
if (importedVariables == null || !importedVariables.Any())
{
return; // 用户取消或没有选择任何变量
}
//var importedVariableDtos = _mapper.Map<List<VariableDto>>(importedVariables);
//foreach (var variableDto in importedVariableDtos)
//{
// variableDto.CreatedAt = DateTime.Now;
// variableDto.UpdatedAt = DateTime.Now;
// variableDto.VariableTableId = CurrentVariableTable.Id;
// variableDto.Protocol = ProtocolType.OpcUa; // 确保协议类型正确
//}
//var existList = await _variableAppService.FindExistingVariablesAsync(importedVariableDtos);
//if (existList.Count > 0)
//{
// // 拼接要删除的变量名称,用于确认提示
// var existNames = string.Join("、", existList.Select(v => v.Name));
// var confrimDialogViewModel
// = new ConfirmDialogViewModel("存在已经添加的变量", $"变量名称:{existNames},已经存在,是否跳过继续添加其他的变量。取消则不添加任何变量", "继续");
// var res = await _dialogService.ShowDialogAsync(confrimDialogViewModel);
// if (!res) return;
// // 从导入列表中删除已经存在的变量
// importedVariableDtos.RemoveAll(variableDto => existList.Contains(variableDto));
//}
//if (importedVariableDtos.Count != 0)
//{
// var isSuccess = await _variableAppService.BatchImportVariablesAsync(importedVariableDtos);
// if (isSuccess)
// {
// _variableItemList.AddRange(_mapper.Map<List<VariableItemViewModel>>(importedVariableDtos));
// NotificationHelper.ShowSuccess($"从OPC UA服务器导入变量成功共导入变量{importedVariableDtos.Count}个");
// }
//}
//else
//{
// NotificationHelper.ShowSuccess($"列表中没有要添加的变量了。");
//}
}
catch (Exception e)
{
NotificationHelper.ShowError($"从OPC UA服务器导入变量的过程中发生了不可预期的错误{e.Message}", e);
}
}