2025-07-01 11:11:02 +08:00
|
|
|
|
using System.Collections.ObjectModel;
|
2025-07-07 21:41:46 +08:00
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
|
using System.Windows.Data;
|
|
|
|
|
|
using System.Windows.Input;
|
2025-06-30 14:19:22 +08:00
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
2025-07-03 12:55:00 +08:00
|
|
|
|
using CommunityToolkit.Mvvm.Input;
|
2025-07-04 14:15:44 +08:00
|
|
|
|
using iNKORE.UI.WPF.Modern.Controls;
|
2025-07-03 20:12:07 +08:00
|
|
|
|
using Newtonsoft.Json;
|
2025-07-02 18:33:08 +08:00
|
|
|
|
using PMSWPF.Data.Repositories;
|
|
|
|
|
|
using PMSWPF.Enums;
|
2025-07-03 20:12:07 +08:00
|
|
|
|
using PMSWPF.Extensions;
|
2025-07-02 18:33:08 +08:00
|
|
|
|
using PMSWPF.Helper;
|
2025-06-30 14:19:22 +08:00
|
|
|
|
using PMSWPF.Models;
|
2025-07-03 12:55:00 +08:00
|
|
|
|
using PMSWPF.Services;
|
2025-06-30 14:19:22 +08:00
|
|
|
|
|
2025-06-23 17:01:06 +08:00
|
|
|
|
namespace PMSWPF.ViewModels;
|
|
|
|
|
|
|
2025-06-30 14:19:22 +08:00
|
|
|
|
partial class VariableTableViewModel : ViewModelBase
|
2025-06-23 17:01:06 +08:00
|
|
|
|
{
|
2025-07-03 12:55:00 +08:00
|
|
|
|
private readonly IDialogService _dialogService;
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
private VariableTable variableTable;
|
2025-07-07 21:41:46 +08:00
|
|
|
|
private ObservableCollection<VariableData> _dataVariables = new ObservableCollection<VariableData>();
|
2025-07-03 12:55:00 +08:00
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
2025-07-07 21:41:46 +08:00
|
|
|
|
private VariableData _selectedVariableData;
|
2025-07-03 20:12:07 +08:00
|
|
|
|
|
2025-07-03 15:29:07 +08:00
|
|
|
|
[ObservableProperty]
|
2025-07-07 21:41:46 +08:00
|
|
|
|
private string _searchText;
|
|
|
|
|
|
|
|
|
|
|
|
public ICollectionView VariableDataView { get; private set; }
|
2025-07-03 15:29:07 +08:00
|
|
|
|
|
2025-07-02 18:33:08 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 是否是第一次加载,防止ToggleSwitch第一次加载触发改变事件
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public bool IsLoadCompletion { get; set; } = false;
|
|
|
|
|
|
|
|
|
|
|
|
private readonly VarTableRepository _varTableRepository;
|
2025-07-03 12:55:00 +08:00
|
|
|
|
private readonly VarDataRepository _varDataRepository;
|
2025-07-07 21:41:46 +08:00
|
|
|
|
private ObservableCollection<VariableData>? _originalDataVariables;
|
2025-07-02 18:33:08 +08:00
|
|
|
|
|
2025-07-03 12:55:00 +08:00
|
|
|
|
|
2025-07-08 20:33:32 +08:00
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
private bool _isS7ProtocolSelected;
|
|
|
|
|
|
|
2025-07-09 13:06:21 +08:00
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
private bool _isOpcUaProtocolSelected;
|
|
|
|
|
|
|
2025-07-03 12:55:00 +08:00
|
|
|
|
public VariableTableViewModel(IDialogService dialogService)
|
2025-07-02 18:33:08 +08:00
|
|
|
|
{
|
2025-07-03 12:55:00 +08:00
|
|
|
|
_dialogService = dialogService;
|
2025-07-02 18:33:08 +08:00
|
|
|
|
IsLoadCompletion = false;
|
|
|
|
|
|
_varTableRepository = new VarTableRepository();
|
2025-07-03 12:55:00 +08:00
|
|
|
|
_varDataRepository = new VarDataRepository();
|
2025-07-07 21:41:46 +08:00
|
|
|
|
_dataVariables = new ObservableCollection<VariableData>(); // Initialize here
|
|
|
|
|
|
VariableDataView = CollectionViewSource.GetDefaultView(_dataVariables);
|
|
|
|
|
|
VariableDataView.Filter = FilterVariables;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private bool FilterVariables(object item)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(SearchText))
|
|
|
|
|
|
{
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (item is VariableData variable)
|
|
|
|
|
|
{
|
|
|
|
|
|
var searchTextLower = SearchText.ToLower();
|
|
|
|
|
|
return variable.Name?.ToLower().Contains(searchTextLower) == true ||
|
|
|
|
|
|
variable.Description?.ToLower().Contains(searchTextLower) == true ||
|
|
|
|
|
|
variable.NodeId?.ToLower().Contains(searchTextLower) == true ||
|
|
|
|
|
|
variable.S7Address?.ToLower().Contains(searchTextLower) == true ||
|
|
|
|
|
|
variable.DataValue?.ToLower().Contains(searchTextLower) == true ||
|
|
|
|
|
|
variable.DisplayValue?.ToLower().Contains(searchTextLower) == true;
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
partial void OnSearchTextChanged(string value)
|
|
|
|
|
|
{
|
|
|
|
|
|
VariableDataView?.Refresh();
|
2025-07-02 18:33:08 +08:00
|
|
|
|
}
|
2025-07-03 12:55:00 +08:00
|
|
|
|
|
2025-07-02 18:33:08 +08:00
|
|
|
|
|
2025-07-07 21:41:46 +08:00
|
|
|
|
|
2025-06-23 17:01:06 +08:00
|
|
|
|
public override void OnLoaded()
|
|
|
|
|
|
{
|
2025-07-08 20:33:32 +08:00
|
|
|
|
IsS7ProtocolSelected = VariableTable.ProtocolType == ProtocolType.S7;
|
2025-07-09 13:06:21 +08:00
|
|
|
|
IsOpcUaProtocolSelected = VariableTable.ProtocolType == ProtocolType.OpcUA;
|
2025-07-08 20:33:32 +08:00
|
|
|
|
|
2025-07-02 18:33:08 +08:00
|
|
|
|
if (VariableTable.DataVariables != null)
|
2025-07-01 11:11:02 +08:00
|
|
|
|
{
|
2025-07-07 21:41:46 +08:00
|
|
|
|
_dataVariables = new ObservableCollection<VariableData>(VariableTable.DataVariables);
|
|
|
|
|
|
VariableDataView = CollectionViewSource.GetDefaultView(_dataVariables);
|
|
|
|
|
|
VariableDataView.Filter = FilterVariables;
|
|
|
|
|
|
|
2025-07-03 20:12:07 +08:00
|
|
|
|
// 3. 创建原始数据的深拷贝备份
|
|
|
|
|
|
// 推荐使用 JSON 序列化/反序列化进行深度拷贝
|
2025-07-07 21:41:46 +08:00
|
|
|
|
var serialized = JsonConvert.SerializeObject(_dataVariables);
|
2025-07-03 20:12:07 +08:00
|
|
|
|
_originalDataVariables = JsonConvert.DeserializeObject<ObservableCollection<VariableData>>(serialized);
|
2025-07-05 17:15:03 +08:00
|
|
|
|
|
|
|
|
|
|
// 在数据加载完成后,将所有变量的 IsModified 状态重置为 false
|
2025-07-07 21:41:46 +08:00
|
|
|
|
foreach (var variableData in _dataVariables)
|
2025-07-05 17:15:03 +08:00
|
|
|
|
{
|
|
|
|
|
|
variableData.IsModified = false;
|
|
|
|
|
|
}
|
2025-07-01 11:11:02 +08:00
|
|
|
|
}
|
2025-07-03 12:55:00 +08:00
|
|
|
|
|
2025-07-03 15:03:36 +08:00
|
|
|
|
|
2025-07-02 18:33:08 +08:00
|
|
|
|
IsLoadCompletion = true;
|
2025-06-23 17:01:06 +08:00
|
|
|
|
}
|
2025-07-04 13:40:14 +08:00
|
|
|
|
|
2025-07-03 22:16:47 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 退出当前实体时调用
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public override async Task<bool> OnExitAsync()
|
|
|
|
|
|
{
|
2025-07-07 21:41:46 +08:00
|
|
|
|
var modifiedDatas = _dataVariables.Where(d => d.IsModified == true)
|
2025-07-03 22:16:47 +08:00
|
|
|
|
.ToList();
|
|
|
|
|
|
if (modifiedDatas.Count == 0)
|
|
|
|
|
|
return true;
|
|
|
|
|
|
var isExit = await _dialogService.ShowConfrimeDialog(
|
|
|
|
|
|
"数据未保存", $"你有{modifiedDatas.Count}个修改的变量没有保存,离开后这些数据就可能丢失了确认要离开吗?", "离开");
|
|
|
|
|
|
if (!isExit)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 不保存数据,还原原来的数据
|
|
|
|
|
|
foreach (var modifiedData in modifiedDatas)
|
|
|
|
|
|
{
|
2025-07-04 13:40:14 +08:00
|
|
|
|
var oldData = _originalDataVariables.First(od => od.Id == modifiedData.Id);
|
|
|
|
|
|
oldData.CopyTo(modifiedData);
|
2025-07-03 22:16:47 +08:00
|
|
|
|
modifiedData.IsModified = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 保存修改过的变量数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
private async void SaveModifiedVarData()
|
|
|
|
|
|
{
|
2025-07-07 21:41:46 +08:00
|
|
|
|
var modifiedDatas = _dataVariables.Where(d => d.IsModified == true)
|
2025-07-03 22:16:47 +08:00
|
|
|
|
.ToList();
|
|
|
|
|
|
///更新数据库
|
|
|
|
|
|
await _varDataRepository.UpdateAsync(modifiedDatas);
|
|
|
|
|
|
// 还原修改状态
|
|
|
|
|
|
foreach (var modifiedData in modifiedDatas)
|
|
|
|
|
|
{
|
|
|
|
|
|
modifiedData.IsModified = false;
|
|
|
|
|
|
}
|
2025-07-04 13:40:14 +08:00
|
|
|
|
|
2025-07-06 15:36:53 +08:00
|
|
|
|
NotificationHelper.ShowSuccess($"修改的{modifiedDatas.Count}变量保存成功.");
|
2025-07-03 22:16:47 +08:00
|
|
|
|
}
|
2025-07-02 18:33:08 +08:00
|
|
|
|
|
2025-07-03 15:29:07 +08:00
|
|
|
|
[RelayCommand]
|
2025-07-04 13:40:14 +08:00
|
|
|
|
private async void EditVarData(VariableTable variableTable)
|
2025-07-03 15:29:07 +08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
// // 1. 显示添加设备对话框
|
|
|
|
|
|
var varData = await _dialogService.ShowEditVarDataDialog(SelectedVariableData);
|
|
|
|
|
|
// // 如果用户取消或对话框未返回设备,则直接返回
|
|
|
|
|
|
if (varData == null)
|
|
|
|
|
|
return;
|
|
|
|
|
|
varData.VariableTableId = variableTable.Id;
|
|
|
|
|
|
// 更新数据库
|
|
|
|
|
|
await _varDataRepository.UpdateAsync(varData);
|
|
|
|
|
|
// 更新当前页面的
|
|
|
|
|
|
var index = variableTable.DataVariables.IndexOf(SelectedVariableData);
|
|
|
|
|
|
// 更新变量表中的
|
|
|
|
|
|
if (index >= 0 && index < variableTable.DataVariables.Count)
|
|
|
|
|
|
variableTable.DataVariables[index] = varData;
|
2025-07-06 15:36:53 +08:00
|
|
|
|
NotificationHelper.ShowSuccess($"编辑变量成功:{varData?.Name}");
|
2025-07-03 15:29:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
2025-07-06 15:15:38 +08:00
|
|
|
|
NotificationHelper.ShowError($"编辑变量的过程中发生了不可预期的错误:{e.Message}", e);
|
2025-07-03 15:29:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-04 13:40:14 +08:00
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
private async void ImprotFromTiaVarTable()
|
|
|
|
|
|
{
|
2025-07-04 14:15:44 +08:00
|
|
|
|
ContentDialog processingDialog = null;
|
2025-07-04 13:40:14 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
// 让用户选择导入的Excel文件
|
|
|
|
|
|
var filePath = await _dialogService.ShowImportExcelDialog();
|
|
|
|
|
|
if (string.IsNullOrEmpty(filePath))
|
|
|
|
|
|
return;
|
|
|
|
|
|
// 读取Excel转换成VariableData列表
|
|
|
|
|
|
var importVarDataList = ExcelHelper.ImprotFromTiaVariableTable(filePath);
|
|
|
|
|
|
if (importVarDataList.Count == 0)
|
|
|
|
|
|
return;
|
2025-07-04 14:15:44 +08:00
|
|
|
|
processingDialog= _dialogService.ShowProcessingDialog("正在处理...", "正在导入变量,请稍等片刻....");
|
|
|
|
|
|
|
2025-07-04 13:40:14 +08:00
|
|
|
|
foreach (var variableData in importVarDataList)
|
|
|
|
|
|
{
|
|
|
|
|
|
variableData.CreateTime=DateTime.Now;
|
|
|
|
|
|
variableData.VariableTableId = VariableTable.Id;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 插入数据库
|
2025-07-04 18:33:48 +08:00
|
|
|
|
var resVarDataCount= await _varDataRepository.AddAsync(importVarDataList);
|
2025-07-04 13:40:14 +08:00
|
|
|
|
//更新界面
|
|
|
|
|
|
// variableTable.DataVariables.AddRange(resVarDataList);
|
2025-07-04 18:33:48 +08:00
|
|
|
|
variableTable.DataVariables= await _varDataRepository.GetAllAsync();
|
2025-07-07 21:41:46 +08:00
|
|
|
|
_dataVariables=new ObservableCollection<VariableData>(variableTable.DataVariables);
|
2025-07-04 14:15:44 +08:00
|
|
|
|
processingDialog?.Hide();
|
2025-07-04 18:33:48 +08:00
|
|
|
|
|
|
|
|
|
|
string msgSuccess = $"成功导入变量:{resVarDataCount}个。";
|
2025-07-06 19:51:53 +08:00
|
|
|
|
NlogHelper.Info(msgSuccess);
|
2025-07-06 15:36:53 +08:00
|
|
|
|
NotificationHelper.ShowSuccess(msgSuccess);
|
2025-07-04 13:40:14 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
2025-07-06 15:15:38 +08:00
|
|
|
|
NotificationHelper.ShowError($"从TIA导入变量的过程中发生了不可预期的错误:{e.Message}", e);
|
2025-07-04 13:40:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-09 13:06:21 +08:00
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
private async Task ImportFromOpcUaServer()
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var importedVariables = await _dialogService.ShowOpcUaImportDialog();
|
|
|
|
|
|
if (importedVariables == null || !importedVariables.Any())
|
|
|
|
|
|
{
|
|
|
|
|
|
return; // 用户取消或没有选择任何变量
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ContentDialog processingDialog = _dialogService.ShowProcessingDialog("正在处理...", "正在导入OPC UA变量,请稍等片刻....");
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var variableData in importedVariables)
|
|
|
|
|
|
{
|
|
|
|
|
|
variableData.CreateTime = DateTime.Now;
|
|
|
|
|
|
variableData.VariableTableId = VariableTable.Id;
|
|
|
|
|
|
variableData.ProtocolType = ProtocolType.OpcUA; // 确保协议类型正确
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 插入数据库
|
|
|
|
|
|
var resVarDataCount = await _varDataRepository.AddAsync(importedVariables);
|
|
|
|
|
|
|
|
|
|
|
|
// 更新界面
|
|
|
|
|
|
variableTable.DataVariables = await _varDataRepository.GetAllAsync();
|
|
|
|
|
|
_dataVariables = new ObservableCollection<VariableData>(variableTable.DataVariables);
|
|
|
|
|
|
processingDialog?.Hide();
|
|
|
|
|
|
|
|
|
|
|
|
string msgSuccess = $"成功导入OPC UA变量:{resVarDataCount}个。";
|
|
|
|
|
|
NlogHelper.Info(msgSuccess);
|
|
|
|
|
|
NotificationHelper.ShowSuccess(msgSuccess);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
NotificationHelper.ShowError($"从OPC UA服务器导入变量的过程中发生了不可预期的错误:{e.Message}", e);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-03 15:29:07 +08:00
|
|
|
|
|
2025-07-03 12:55:00 +08:00
|
|
|
|
[RelayCommand]
|
2025-07-04 13:40:14 +08:00
|
|
|
|
private async void AddVarData(VariableTable variableTable)
|
2025-07-03 12:55:00 +08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
// // 1. 显示添加设备对话框
|
|
|
|
|
|
var varData = await _dialogService.ShowAddVarDataDialog();
|
|
|
|
|
|
// // 如果用户取消或对话框未返回设备,则直接返回
|
|
|
|
|
|
if (varData == null)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
varData.VariableTableId = variableTable.Id;
|
2025-07-03 22:16:47 +08:00
|
|
|
|
// 更新数据库
|
2025-07-05 00:04:00 +08:00
|
|
|
|
await _varDataRepository.AddAsync(varData);
|
2025-07-03 22:16:47 +08:00
|
|
|
|
// 更新当前页面的
|
2025-07-07 21:41:46 +08:00
|
|
|
|
_dataVariables.Add(varData);
|
2025-07-06 15:36:53 +08:00
|
|
|
|
NotificationHelper.ShowSuccess($"添加变量成功:{varData?.Name}");
|
2025-07-03 12:55:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
2025-07-06 15:15:38 +08:00
|
|
|
|
NotificationHelper.ShowError($"添加变量的过程中发生了不可预期的错误:{e.Message}", e);
|
2025-07-03 12:55:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-05 00:04:00 +08:00
|
|
|
|
[RelayCommand]
|
2025-07-05 00:18:59 +08:00
|
|
|
|
public async Task DeleteVarData(List<VariableData> variablesToDelete)
|
2025-07-05 00:04:00 +08:00
|
|
|
|
{
|
2025-07-05 00:18:59 +08:00
|
|
|
|
if (variablesToDelete == null || !variablesToDelete.Any())
|
2025-07-05 00:04:00 +08:00
|
|
|
|
{
|
2025-07-06 15:36:53 +08:00
|
|
|
|
NotificationHelper.ShowInfo("请选择要删除的变量");
|
2025-07-05 00:04:00 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-05 00:18:59 +08:00
|
|
|
|
var names = string.Join("、", variablesToDelete.Select(v => v.Name));
|
2025-07-05 00:04:00 +08:00
|
|
|
|
var confirm = await _dialogService.ShowConfrimeDialog(
|
|
|
|
|
|
"删除确认",
|
2025-07-05 00:18:59 +08:00
|
|
|
|
$"确定要删除选中的 {variablesToDelete.Count} 个变量吗?\n\n{names}",
|
2025-07-05 00:04:00 +08:00
|
|
|
|
"删除");
|
|
|
|
|
|
|
|
|
|
|
|
if (!confirm)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-07-05 00:18:59 +08:00
|
|
|
|
var result = await _varDataRepository.DeleteAsync(variablesToDelete);
|
2025-07-05 00:04:00 +08:00
|
|
|
|
if (result > 0)
|
|
|
|
|
|
{
|
2025-07-05 00:18:59 +08:00
|
|
|
|
foreach (var variable in variablesToDelete)
|
|
|
|
|
|
{
|
2025-07-07 21:41:46 +08:00
|
|
|
|
_dataVariables.Remove(variable);
|
2025-07-05 00:18:59 +08:00
|
|
|
|
}
|
2025-07-06 15:36:53 +08:00
|
|
|
|
NotificationHelper.ShowSuccess($"成功删除 {result} 个变量");
|
2025-07-05 00:04:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-07-06 15:36:53 +08:00
|
|
|
|
NotificationHelper.ShowError("删除变量失败");
|
2025-07-05 00:04:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
2025-07-06 15:15:38 +08:00
|
|
|
|
NotificationHelper.ShowError($"删除变量的过程中发生了不可预期的错误:{e.Message}", e);
|
2025-07-05 00:04:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-05 16:13:46 +08:00
|
|
|
|
[RelayCommand]
|
2025-07-05 16:27:21 +08:00
|
|
|
|
public async Task ChangePollLevel(List<VariableData> variablesToChange)
|
2025-07-05 16:13:46 +08:00
|
|
|
|
{
|
2025-07-05 16:27:21 +08:00
|
|
|
|
if (variablesToChange == null || !variablesToChange.Any())
|
2025-07-05 16:13:46 +08:00
|
|
|
|
{
|
2025-07-06 15:36:53 +08:00
|
|
|
|
NotificationHelper.ShowInfo("请选择要修改轮询频率的变量");
|
2025-07-05 16:13:46 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-05 16:27:21 +08:00
|
|
|
|
var newPollLevelType = await _dialogService.ShowPollLevelDialog(variablesToChange.First().PollLevelType);
|
2025-07-05 16:13:46 +08:00
|
|
|
|
if (newPollLevelType.HasValue)
|
|
|
|
|
|
{
|
2025-07-05 16:27:21 +08:00
|
|
|
|
foreach (var variable in variablesToChange)
|
|
|
|
|
|
{
|
|
|
|
|
|
variable.PollLevelType = newPollLevelType.Value;
|
|
|
|
|
|
variable.IsModified=false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await _varDataRepository.UpdateAsync(variablesToChange);
|
2025-07-06 15:36:53 +08:00
|
|
|
|
NotificationHelper.ShowSuccess($"已成功更新 {variablesToChange.Count} 个变量的轮询频率");
|
2025-07-05 16:13:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-05 18:15:21 +08:00
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
public async Task AddMqttServerToVariables(IList<VariableData> variablesToAddMqtt)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (variablesToAddMqtt == null || !variablesToAddMqtt.Any())
|
|
|
|
|
|
{
|
2025-07-06 15:36:53 +08:00
|
|
|
|
NotificationHelper.ShowInfo("请选择要添加MQTT服务器的变量");
|
2025-07-05 18:15:21 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var selectedMqtt = await _dialogService.ShowMqttSelectionDialog();
|
|
|
|
|
|
if (selectedMqtt == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return; // 用户取消选择
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (VariableData variable in variablesToAddMqtt)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (variable.Mqtts == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
variable.Mqtts = new List<Mqtt>();
|
|
|
|
|
|
}
|
|
|
|
|
|
// 避免重复添加
|
|
|
|
|
|
if (!variable.Mqtts.Any(m => m.Id == selectedMqtt.Id))
|
|
|
|
|
|
{
|
|
|
|
|
|
variable.Mqtts.Add(selectedMqtt);
|
2025-07-05 22:57:54 +08:00
|
|
|
|
// variable.IsModified = true; // 标记为已修改
|
2025-07-05 18:15:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 批量更新数据库
|
|
|
|
|
|
await _varDataRepository.UpdateAsync(variablesToAddMqtt.ToList());
|
2025-07-06 15:36:53 +08:00
|
|
|
|
NotificationHelper.ShowSuccess($"已成功为 {variablesToAddMqtt.Count} 个变量添加MQTT服务器: {selectedMqtt.Name}");
|
2025-07-05 18:15:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2025-07-06 15:15:38 +08:00
|
|
|
|
NotificationHelper.ShowError($"添加MQTT服务器失败: {ex.Message}", ex);
|
2025-07-05 18:15:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-04 13:40:14 +08:00
|
|
|
|
// [RelayCommand]
|
|
|
|
|
|
// private async void ImportFromExcel()
|
|
|
|
|
|
// {
|
|
|
|
|
|
// var filePath = await _dialogService.ShowImportExcelDialog();
|
|
|
|
|
|
// if (!string.IsNullOrEmpty(filePath))
|
|
|
|
|
|
// {
|
|
|
|
|
|
// // TODO: Implement Excel import logic using the filePath
|
|
|
|
|
|
// NotificationHelper.ShowMessage($"Successfully imported from {filePath}", NotificationType.Success);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
2025-07-03 20:12:07 +08:00
|
|
|
|
|
2025-07-03 12:55:00 +08:00
|
|
|
|
|
2025-07-02 18:33:08 +08:00
|
|
|
|
public async Task OnIsActiveChanged(bool active)
|
|
|
|
|
|
{
|
|
|
|
|
|
var res = await _varTableRepository.Edit(VariableTable);
|
|
|
|
|
|
if (res > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
var statusMessage = active ? "已启用" : "已停用";
|
2025-07-06 15:36:53 +08:00
|
|
|
|
NotificationHelper.ShowSuccess($"变量表:{VariableTable.Name},{statusMessage}");
|
2025-07-02 18:33:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-07-06 15:36:53 +08:00
|
|
|
|
NotificationHelper.ShowError($"变量表:{VariableTable.Name},状态修改失败,状态:{active}");
|
2025-07-02 18:33:08 +08:00
|
|
|
|
// _logger.LogInformation($"变量表:{VariableTable.Name},状态修改失败,状态:{active}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-06-23 17:01:06 +08:00
|
|
|
|
}
|