重构项目,将项目拆分(临时提交)

This commit is contained in:
2025-07-18 22:21:16 +08:00
parent 2cde703f1a
commit 7ca6e4e127
189 changed files with 1090 additions and 1667 deletions

View File

@@ -0,0 +1,11 @@
using CommunityToolkit.Mvvm.ComponentModel;
namespace DMS.ViewModels.Dialogs;
public partial class ConfrimDialogViewModel : ObservableObject
{
[ObservableProperty] private string _title;
[ObservableProperty] private string primaryButtonText;
[ObservableProperty] private string message;
}

View File

@@ -0,0 +1,37 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DMS.Models;
using S7.Net; // AddAsync this using directive
namespace DMS.ViewModels.Dialogs;
public partial class DeviceDialogViewModel : ObservableObject
{
[ObservableProperty]
private Device _device;
partial void OnDeviceChanged(Device value)
{
if (value != null)
{
System.Diagnostics.Debug.WriteLine($"Device ProtocolType changed to: {value.ProtocolType}");
}
}
[ObservableProperty] private string title ;
[ObservableProperty] private string primaryButContent ;
public DeviceDialogViewModel(Device device)
{
_device = device;
}
// AddAsync a property to expose CpuType enum values for ComboBox
public Array CpuTypes => Enum.GetValues(typeof(CpuType));
[RelayCommand]
public void AddDevice()
{
}
}

View File

@@ -0,0 +1,33 @@
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using DMS.Helper;
using DMS.Models;
namespace DMS.ViewModels.Dialogs;
public partial class ImportExcelDialogViewModel : ObservableObject
{
[ObservableProperty]
private string? _filePath;
[ObservableProperty]
private ObservableCollection<Variable> _variables = new();
partial void OnFilePathChanged(string? value)
{
if (string.IsNullOrEmpty(value))
{
return;
}
try
{
var data = ExcelHelper.ImprotFromTiaVariableTable(value);
Variables = new ObservableCollection<Variable>(data);
}
catch (System.Exception ex)
{
// Handle exception
}
}
}

View File

@@ -0,0 +1,44 @@
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DMS.Models;
namespace DMS.ViewModels.Dialogs;
/// <summary>
/// ImportResultDialogViewModel 是用于显示变量导入结果的视图模型。
/// 它包含成功导入和已存在变量的列表。
/// </summary>
public partial class ImportResultDialogViewModel : ObservableObject
{
/// <summary>
/// 成功导入的变量名称列表。
/// </summary>
public ObservableCollection<string> ImportedVariables { get; }
/// <summary>
/// 已存在的变量名称列表。
/// </summary>
public ObservableCollection<string> ExistingVariables { get; }
/// <summary>
/// 构造函数,初始化导入结果列表。
/// </summary>
/// <param name="importedVariables">成功导入的变量名称列表。</param>
/// <param name="existingVariables">已存在的变量名称列表。</param>
public ImportResultDialogViewModel(List<string> importedVariables, List<string> existingVariables)
{
ImportedVariables = new ObservableCollection<string>(importedVariables);
ExistingVariables = new ObservableCollection<string>(existingVariables);
}
/// <summary>
/// 关闭对话框的命令。
/// </summary>
[RelayCommand]
private void Close()
{
// 在实际应用中这里可能需要通过IDialogService或其他机制来关闭对话框
// 对于ContentDialog通常不需要显式关闭命令因为对话框本身有关闭按钮或通过Result返回
}
}

View File

@@ -0,0 +1,31 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DMS.Core.Enums;
namespace DMS.ViewModels.Dialogs;
public partial class IsActiveDialogViewModel : ObservableObject
{
[ObservableProperty]
private bool? _selectedIsActive;
public IsActiveDialogViewModel(bool? currentIsActive)
{
_selectedIsActive = currentIsActive;
}
[RelayCommand]
private void SelectIsActive(string isActiveString)
{
if (bool.TryParse(isActiveString, out bool isActive))
{
SelectedIsActive = isActive;
}
}
[RelayCommand]
private void Cancel()
{
SelectedIsActive = null;
}
}

View File

@@ -0,0 +1,34 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using DMS.Models;
namespace DMS.ViewModels.Dialogs;
public partial class MqttAliasBatchEditDialogViewModel : ObservableObject
{
[ObservableProperty]
private string _title = "批量设置MQTT别名";
[ObservableProperty]
private ObservableCollection<VariableMqtt> _variablesToEdit;
public Mqtt SelectedMqtt { get; private set; }
public MqttAliasBatchEditDialogViewModel(List<Variable> selectedVariables, Mqtt selectedMqtt)
{
SelectedMqtt = selectedMqtt;
Title=$"设置:{SelectedMqtt.Name}-MQTT服务器关联变量的别名";
VariablesToEdit = new ObservableCollection<VariableMqtt>(
selectedVariables.Select(v => new VariableMqtt(v, selectedMqtt))
);
}
public MqttAliasBatchEditDialogViewModel()
{
// For design time
VariablesToEdit = new ObservableCollection<VariableMqtt>();
}
}

View File

@@ -0,0 +1,34 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
namespace DMS.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()
{
}
}

View File

@@ -0,0 +1,26 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DMS.Models;
namespace DMS.ViewModels.Dialogs;
public partial class MqttDialogViewModel : ObservableObject
{
[ObservableProperty]
private Mqtt _mqtt;
[ObservableProperty] private string title ;
[ObservableProperty] private string primaryButContent ;
public MqttDialogViewModel(Mqtt mqtt)
{
_mqtt = mqtt;
}
[RelayCommand]
public void AddMqtt()
{
}
}

View File

@@ -0,0 +1,25 @@
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using DMS.Data.Repositories;
using System.Threading.Tasks;
using DMS.Models;
using DMS.Services;
namespace DMS.ViewModels.Dialogs;
public partial class MqttSelectionDialogViewModel : ObservableObject
{
[ObservableProperty]
private ObservableCollection<Mqtt> mqtts;
[ObservableProperty]
private Mqtt? selectedMqtt;
public MqttSelectionDialogViewModel(DataServices dataServices)
{
Mqtts = new ObservableCollection<Mqtt>(dataServices.Mqtts);
}
}

View File

@@ -0,0 +1,261 @@
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DMS.Core.Enums;
using DMS.Helper;
using DMS.Models;
using Opc.Ua;
using Opc.Ua.Client;
using Opc.Ua.Configuration;
namespace DMS.ViewModels.Dialogs;
public partial class OpcUaImportDialogViewModel : ObservableObject
{
[ObservableProperty]
private string _endpointUrl = "opc.tcp://127.0.0.1:4855"; // 默认值
[ObservableProperty]
private ObservableCollection<OpcUaNode> _opcUaNodes;
[ObservableProperty]
private ObservableCollection<Variable> _selectedNodeVariables;
public List<Variable> SelectedVariables { get; set; }=new List<Variable>();
[ObservableProperty]
private bool _selectAllVariables;
[ObservableProperty]
private bool _isConnected;
private Session _session;
public OpcUaImportDialogViewModel()
{
OpcUaNodes = new ObservableCollection<OpcUaNode>();
SelectedNodeVariables = new ObservableCollection<Variable>();
// Automatically connect when the ViewModel is created
ConnectCommand.Execute(null);
}
[RelayCommand]
private async Task Connect()
{
try
{
// 断开现有连接
if (_session != null && _session.Connected)
{
await _session.CloseAsync();
_session.Dispose();
_session = null;
}
IsConnected = false;
OpcUaNodes.Clear();
SelectedNodeVariables.Clear();
_session = await ServiceHelper.CreateOpcUaSessionAsync(EndpointUrl);
NotificationHelper.ShowSuccess($"已连接到 OPC UA 服务器: {EndpointUrl}");
IsConnected = true;
// 浏览根节点
await BrowseNodes(OpcUaNodes, ObjectIds.ObjectsFolder);
}
catch (Exception ex)
{
IsConnected = false;
NotificationHelper.ShowError($"连接 OPC UA 服务器失败: {EndpointUrl} - {ex.Message}", ex);
}
}
/// <summary>
/// 处理来自服务器的数据变化通知
/// </summary>
private static void OnNotification(MonitoredItem item, MonitoredItemNotificationEventArgs e)
{
foreach (var value in item.DequeueValues())
{
Console.WriteLine(
$"[通知] {item.DisplayName}: {value.Value} | 时间戳: {value.SourceTimestamp.ToLocalTime()} | 状态: {value.StatusCode}");
}
}
private async Task BrowseNodes(ObservableCollection<OpcUaNode> nodes, NodeId parentNodeId)
{
try
{
Opc.Ua.ReferenceDescriptionCollection references;
byte[] continuationPoint = null;
_session.Browse(
null, // RequestHeader
new ViewDescription(),
parentNodeId,
0u,
BrowseDirection.Forward,
Opc.Ua.ReferenceTypeIds.HierarchicalReferences,
true,
(uint)Opc.Ua.NodeClass.Object | (uint)Opc.Ua.NodeClass.Variable,
out continuationPoint,
out references
);
foreach (var rd in references)
{
NodeType nodeType = NodeType.Folder; // 默认是文件夹
if ((rd.NodeClass & NodeClass.Variable) != 0)
{
nodeType = NodeType.Variable;
}
else if ((rd.NodeClass & NodeClass.Object) != 0)
{
nodeType = NodeType.Object;
}
var opcUaNode = new OpcUaNode(rd.DisplayName.Text, (NodeId)rd.NodeId, nodeType);
nodes.Add(opcUaNode);
// 如果是文件夹或对象,添加一个虚拟子节点,用于懒加载
if (nodeType == NodeType.Folder || nodeType == NodeType.Object)
{
opcUaNode.Children.Add(new OpcUaNode("Loading...", NodeId.Null, NodeType.Folder)); // 虚拟节点
}
}
}
catch (Exception ex)
{
NlogHelper.Error($"浏览 OPC UA 节点失败: {parentNodeId} - {ex.Message}", ex);
NotificationHelper.ShowError($"浏览 OPC UA 节点失败: {parentNodeId} - {ex.Message}", ex);
}
}
public async Task LoadNodeVariables(OpcUaNode node)
{
if (node.NodeType == NodeType.Variable)
{
// 如果是变量节点,直接显示它
SelectedNodeVariables.Clear();
SelectedNodeVariables.Add(new Variable
{
Name = node.DisplayName,
NodeId = node.NodeId.ToString(),
OpcUaNodeId = node.NodeId.ToString(),
ProtocolType = ProtocolType.OpcUA,
IsActive = true // 默认选中
});
return;
}
if (node.IsLoaded || node.IsLoading)
{
return; // 已经加载或正在加载
}
node.IsLoading = true;
node.Children.Clear(); // 清除虚拟节点
try
{
Opc.Ua.ReferenceDescriptionCollection references;
byte[] continuationPoint = null;
_session.Browse(
null, // RequestHeader
new ViewDescription(),
node.NodeId,
0u,
BrowseDirection.Forward,
Opc.Ua.ReferenceTypeIds.HierarchicalReferences,
true,
(uint)Opc.Ua.NodeClass.Object | (uint)Opc.Ua.NodeClass.Variable,
out continuationPoint,
out references
);
foreach (var rd in references)
{
NodeType nodeType = NodeType.Folder;
if ((rd.NodeClass & NodeClass.Variable) != 0)
{
nodeType = NodeType.Variable;
}
else if ((rd.NodeClass & NodeClass.Object) != 0)
{
nodeType = NodeType.Object;
}
var opcUaNode = new OpcUaNode(rd.DisplayName.Text, (NodeId)rd.NodeId, nodeType);
node.Children.Add(opcUaNode);
if (nodeType == NodeType.Folder || nodeType == NodeType.Object)
{
opcUaNode.Children.Add(new OpcUaNode("Loading...", NodeId.Null, NodeType.Folder)); // 虚拟节点
}
// 如果是变量,添加到右侧列表
if (nodeType == NodeType.Variable)
{
// Read the DataType attribute
ReadValueId readValueId = new ReadValueId
{
NodeId = opcUaNode.NodeId,
AttributeId = Attributes.DataType,
// You might need to specify IndexRange and DataEncoding if dealing with arrays or specific encodings
};
DataValueCollection results;
DiagnosticInfoCollection diagnosticInfos;
_session.Read(
null, // RequestHeader
0, // MaxAge
TimestampsToReturn.Source,
new ReadValueIdCollection { readValueId },
out results,
out diagnosticInfos
);
string dataType = string.Empty;
if (results != null && results.Count > 0 && results[0].Value != null)
{
// Convert the NodeId of the DataType to a readable string
NodeId dataTypeNodeId = (NodeId)results[0].Value;
dataType = _session.NodeCache.GetDisplayText(dataTypeNodeId);
}
SelectedNodeVariables.Add(new Variable
{
Name = opcUaNode.DisplayName,
OpcUaNodeId = opcUaNode.NodeId.ToString(),
ProtocolType = ProtocolType.OpcUA,
IsActive = true, // Default selected
DataType = dataType // Assign the read DataType
});
}
}
node.IsLoaded = true;
}
catch (Exception ex)
{
NlogHelper.Error($"加载 OPC UA 节点变量失败: {node.NodeId} - {ex.Message}", ex);
NotificationHelper.ShowError($"加载 OPC UA 节点变量失败: {node.NodeId} - {ex.Message}", ex);
}
finally
{
node.IsLoading = false;
}
}
public ObservableCollection<Variable> GetSelectedVariables()
{
return new ObservableCollection<Variable>(SelectedVariables);
}
}

View File

@@ -0,0 +1,18 @@
using CommunityToolkit.Mvvm.ComponentModel;
using DMS.Core.Enums;
namespace DMS.ViewModels.Dialogs
{
public partial class OpcUaUpdateTypeDialogViewModel : ObservableObject
{
[ObservableProperty]
private OpcUaUpdateType _selectedUpdateType;
public OpcUaUpdateTypeDialogViewModel()
{
// 默认选中第一个
SelectedUpdateType = OpcUaUpdateType.OpcUaPoll;
}
}
}

View File

@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using CommunityToolkit.Mvvm.ComponentModel;
using DMS.Core.Enums;
namespace DMS.ViewModels.Dialogs
{
public partial class PollLevelDialogViewModel : ObservableObject
{
[ObservableProperty]
private PollLevelType _selectedPollLevelType;
public List<PollLevelType> PollLevelTypes { get; }
public PollLevelDialogViewModel(PollLevelType currentPollLevelType)
{
PollLevelTypes = Enum.GetValues(typeof(PollLevelType)).Cast<PollLevelType>().ToList();
SelectedPollLevelType = currentPollLevelType;
}
}
}

View File

@@ -0,0 +1,9 @@
using CommunityToolkit.Mvvm.ComponentModel;
namespace DMS.ViewModels.Dialogs;
public partial class ProcessingDialogViewModel : ObservableObject
{
[ObservableProperty] private string _title;
[ObservableProperty] private string _message;
}

View File

@@ -0,0 +1,14 @@
using CommunityToolkit.Mvvm.ComponentModel;
using DMS.Models;
namespace DMS.ViewModels.Dialogs;
public partial class VarDataDialogViewModel : ObservableObject
{
[ObservableProperty]
private Variable _variable;
[ObservableProperty]
private string title;
[ObservableProperty]
private string primaryButtonText;
}

View File

@@ -0,0 +1,14 @@
using CommunityToolkit.Mvvm.ComponentModel;
using DMS.Models;
namespace DMS.ViewModels.Dialogs;
public partial class VarTableDialogViewModel:ObservableObject
{
[ObservableProperty]
private VariableTable variableTable;
[ObservableProperty]
private string title;
[ObservableProperty]
private string primaryButtonText;
}