From 908dc60439cab339aad31a18c0953f9368f767c9 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Fri, 20 Jun 2025 18:53:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E7=9B=B8=E5=85=B3=E4=BA=86=E7=B1=BB=EF=BC=8C=E5=92=8C?= =?UTF-8?q?=E6=9E=9A=E4=B8=BE=E7=B1=BB=E5=9E=8B=E7=9B=B8=E5=85=B3=E7=9A=84?= =?UTF-8?q?=E7=B1=BB=EF=BC=8C=E5=B9=B6=E4=B8=94=E5=B0=86=E6=9E=9A=E4=B8=BE?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E7=BB=91=E5=AE=9A=E5=88=B0=E5=89=8D=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.xaml.cs | 7 +++ Data/Entities/DbDataVariable.cs | 40 ++++++++++++ Data/Entities/DbDevice.cs | 15 ++++- Data/Entities/DbMqtt.cs | 11 ++++ Data/Entities/DbPLC.cs | 64 -------------------- Data/Entities/DbS7DataVariable.cs | 10 +++ Data/Entities/DbUser.cs | 11 ++++ Data/Entities/DbVariableTable.cs | 25 ++++++++ Data/Repositories/BaseRepositories.cs | 19 ++++++ Data/Repositories/DevicesRepositories.cs | 26 +++++--- Data/Repositories/PlcRepositories.cs | 21 ------- Enums/DeviceType.cs | 13 ++++ Enums/ProtocolType.cs | 15 +++++ Enums/SignalType.cs | 28 +++++++++ Extensions/EnumBindingSourceExtension.cs | 50 +++++++++++++++ Extensions/ObjectExtensions.cs | 23 +++++++ Models/DataVariable.cs | 27 +++++++++ Models/Device.cs | 10 ++- Models/Mqtt.cs | 6 ++ Models/User.cs | 6 ++ Models/VariableTable.cs | 14 +++++ Resources/DevicesItemTemplateDictionary.xaml | 2 +- Services/INotificationService.cs | 2 +- ValueConverts/EnumDescriptionConverter.cs | 35 +++++++++++ ViewModels/DevicesViewModel.cs | 47 ++++---------- ViewModels/Dialogs/DeviceDialogViewModel.cs | 2 + Views/DevicesView.xaml | 3 +- Views/Dialogs/DeviceDialog.xaml | 53 ++++++++++++---- Views/MainView.xaml | 3 +- 29 files changed, 437 insertions(+), 151 deletions(-) create mode 100644 Data/Entities/DbDataVariable.cs create mode 100644 Data/Entities/DbMqtt.cs delete mode 100644 Data/Entities/DbPLC.cs create mode 100644 Data/Entities/DbS7DataVariable.cs create mode 100644 Data/Entities/DbUser.cs create mode 100644 Data/Entities/DbVariableTable.cs delete mode 100644 Data/Repositories/PlcRepositories.cs create mode 100644 Enums/DeviceType.cs create mode 100644 Enums/ProtocolType.cs create mode 100644 Enums/SignalType.cs create mode 100644 Extensions/EnumBindingSourceExtension.cs create mode 100644 Models/DataVariable.cs create mode 100644 Models/Mqtt.cs create mode 100644 Models/User.cs create mode 100644 Models/VariableTable.cs create mode 100644 ValueConverts/EnumDescriptionConverter.cs diff --git a/App.xaml.cs b/App.xaml.cs index a77d419..9d47706 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -41,10 +41,17 @@ namespace PMSWPF protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); + CheckDb(); + MainWindow = Services.GetRequiredService(); MainWindow.Show(); } + private void CheckDb() + { + + } + // [STAThread] // static void Main(string[] args) // { diff --git a/Data/Entities/DbDataVariable.cs b/Data/Entities/DbDataVariable.cs new file mode 100644 index 0000000..96feb25 --- /dev/null +++ b/Data/Entities/DbDataVariable.cs @@ -0,0 +1,40 @@ +using PMSWPF.Enums; +using SqlSugar; +using SqlSugar.DbConvert; + +namespace PMSWPF.Data.Entities; +[SugarTable("DataVariable")] +public class DbDataVariable +{ + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]//数据库是自增才配自增 + public int Id { get; set; } + [SugarColumn(IsNullable = true)] + public int ?VariableTableId { get; set; } + [Navigate(NavigateType.ManyToOne, nameof(DbDataVariable.VariableTableId))] + public DbVariableTable? VariableTable { get; set; } + public string Name { get; set; } + [SugarColumn(IsNullable = true)] + public string? Description { get; set; } + public string NodeId { get; set; } + [SugarColumn(ColumnDataType="varchar(20)",SqlParameterDbType=typeof(EnumToStringConvert))] + public ProtocolType ProtocolType { get; set; } + public string DataType { get; set; } + [SugarColumn(IsNullable = true)] + public List? Mqtts { get; set; } + public string DataValue { get; set; } + public string DisplayValue { get; set; } + public DateTime UpdateTime { get; set; } + [SugarColumn(IsNullable = true)] + public DbUser? UpdateUser { get; set; } + public string Converstion { get; set; } + public bool IsDeleted { get; set; } + public bool IsActive { get; set; } + public bool IsSave { get; set; } + public Double SaveRange { get; set; } + public bool IsAlarm { get; set; } + public Double AlarmMin { get; set; } + public Double AlarmMax { get; set; } + [SugarColumn(ColumnDataType="varchar(20)",SqlParameterDbType=typeof(EnumToStringConvert))] + public SignalType SignalType { get; set; } + +} \ No newline at end of file diff --git a/Data/Entities/DbDevice.cs b/Data/Entities/DbDevice.cs index 004fbcd..c65d4f0 100644 --- a/Data/Entities/DbDevice.cs +++ b/Data/Entities/DbDevice.cs @@ -1,4 +1,8 @@ +using System.Net.Sockets; +using PMSWPF.Enums; using SqlSugar; +using SqlSugar.DbConvert; +using ProtocolType = PMSWPF.Enums.ProtocolType; namespace PMSWPF.Data.Entities; @@ -9,9 +13,18 @@ public class DbDevice [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]//数据库是自增才配自增 public int Id { get; set; } public string Name { get; set; } - public string Description { get; set; } + [SugarColumn(IsNullable = true)] + public string? Description { get; set; } public string Ip { get; set; } public bool IsActive { get; set; } public bool IsRuning { get; set; } + [SugarColumn(ColumnDataType="varchar(20)",SqlParameterDbType=typeof(EnumToStringConvert))] + public DeviceType DeviceType { get; set; } + + [Navigate(NavigateType.OneToMany, nameof(DbVariableTable.DeviceId))] + [SugarColumn(IsNullable = true)] + public List? VariableTables { get; set; } + [SugarColumn(ColumnDataType="varchar(20)",SqlParameterDbType=typeof(EnumToStringConvert))] + public ProtocolType ProtocolType { get; set; } } \ No newline at end of file diff --git a/Data/Entities/DbMqtt.cs b/Data/Entities/DbMqtt.cs new file mode 100644 index 0000000..962422c --- /dev/null +++ b/Data/Entities/DbMqtt.cs @@ -0,0 +1,11 @@ +using SqlSugar; + +namespace PMSWPF.Data.Entities; + +[SugarTable("Mqtt")] +public class DbMqtt +{ + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]//数据库是自增才配自增 + public int Id { get; set; } + +} \ No newline at end of file diff --git a/Data/Entities/DbPLC.cs b/Data/Entities/DbPLC.cs deleted file mode 100644 index 5b1cf42..0000000 --- a/Data/Entities/DbPLC.cs +++ /dev/null @@ -1,64 +0,0 @@ -using PMSWPF.Enums; -using SqlSugar; -using SqlSugar.DbConvert; - -namespace PMSWPF.Data.Entities -{ - public class DbPLC - { - [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]//数据库是自增才配自增 - public int id { get; set; } - /// - /// PLC名称 - /// - public string Name { get; set; } - /// - /// PLC品牌 - /// - /// //新版本:存储字符串 SqlSugar 5.1.4.66-preview02 - [SugarColumn(ColumnDataType = "varchar(30)", SqlParameterDbType = typeof(EnumToStringConvert))] - public PlcBrand PlcBrand { get; set; } - /// - /// PLC类型 - /// - public int CpuType { get; set; } - /// - /// PLC节点ID - /// - public string NodeID { get; set; } - /// - /// PLC IP地址 - /// - public string IP { get; set; } - /// - /// PLC状态 - /// - public string Status { get; set; } - /// - /// PLC连接类型 - /// - public string ConnType { get; set; } - /// - /// PLC连接时间 - /// - public DateTime ConnTime { get; set; } - /// - /// 是否启用 - /// - public bool IsEnable { get; set; } - - public DbPLC() - { - - } - public DbPLC(string name = "", string nodeID = "", string ip = "", string status = "", string connType = "") - { - this.Name = name; - this.NodeID = nodeID; - this.IP = ip; - this.Status = status; - this.ConnType = connType; - this.ConnTime = DateTime.Now; - } - } -} diff --git a/Data/Entities/DbS7DataVariable.cs b/Data/Entities/DbS7DataVariable.cs new file mode 100644 index 0000000..2a2ccba --- /dev/null +++ b/Data/Entities/DbS7DataVariable.cs @@ -0,0 +1,10 @@ +using SqlSugar; + +namespace PMSWPF.Data.Entities; + +[SugarTable("S7DataVariable")] +public class DbS7DataVariable:DbDataVariable +{ + + +} \ No newline at end of file diff --git a/Data/Entities/DbUser.cs b/Data/Entities/DbUser.cs new file mode 100644 index 0000000..1de1165 --- /dev/null +++ b/Data/Entities/DbUser.cs @@ -0,0 +1,11 @@ +using SqlSugar; + +namespace PMSWPF.Data.Entities; + +[SugarTable("User")] +public class DbUser +{ + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]//数据库是自增才配自增 + public int Id { get; set; } + +} \ No newline at end of file diff --git a/Data/Entities/DbVariableTable.cs b/Data/Entities/DbVariableTable.cs new file mode 100644 index 0000000..feb4139 --- /dev/null +++ b/Data/Entities/DbVariableTable.cs @@ -0,0 +1,25 @@ +using PMSWPF.Enums; +using PMSWPF.Models; +using SqlSugar; +using SqlSugar.DbConvert; + +namespace PMSWPF.Data.Entities; + +[SugarTable("VariableTable")] +public class DbVariableTable +{ + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]//数据库是自增才配自增 + public int Id { get; set; } + public string Name { get; set; } + [SugarColumn(IsNullable = true)] + public string? Description { get; set; } + [SugarColumn(ColumnDataType="varchar(20)",SqlParameterDbType=typeof(EnumToStringConvert))] + public ProtocolType ProtocolType { get; set; } + + [Navigate(NavigateType.OneToMany, nameof(DbDataVariable.VariableTableId))] + public List? DataVariables { get; set; } + [SugarColumn(IsNullable = true)] + public int? DeviceId { get; set; } + [Navigate(NavigateType.ManyToOne, nameof(DbVariableTable.DeviceId))] + public Device? Device { get; set; } +} \ No newline at end of file diff --git a/Data/Repositories/BaseRepositories.cs b/Data/Repositories/BaseRepositories.cs index ccdc7da..e44ac60 100644 --- a/Data/Repositories/BaseRepositories.cs +++ b/Data/Repositories/BaseRepositories.cs @@ -1,3 +1,4 @@ +using PMSWPF.Data.Entities; using SqlSugar; namespace PMSWPF.Data.Repositories; @@ -9,5 +10,23 @@ public class BaseRepositories public BaseRepositories() { _db = DbContext.GetInstance(); + _db.DbMaintenance.CreateDatabase(); + CheckDbTables(); + } + + private void CheckDbTables() + { + if(!_db.DbMaintenance.IsAnyTable()) + _db.CodeFirst.InitTables(); + if(!_db.DbMaintenance.IsAnyTable()) + _db.CodeFirst.InitTables(); + if(!_db.DbMaintenance.IsAnyTable()) + _db.CodeFirst.InitTables(); + if(!_db.DbMaintenance.IsAnyTable()) + _db.CodeFirst.InitTables(); + if(!_db.DbMaintenance.IsAnyTable()) + _db.CodeFirst.InitTables(); + if(!_db.DbMaintenance.IsAnyTable()) + _db.CodeFirst.InitTables(); } } \ No newline at end of file diff --git a/Data/Repositories/DevicesRepositories.cs b/Data/Repositories/DevicesRepositories.cs index 6685839..aa9352a 100644 --- a/Data/Repositories/DevicesRepositories.cs +++ b/Data/Repositories/DevicesRepositories.cs @@ -1,5 +1,8 @@ using PMSWPF.Data.Entities; +using PMSWPF.Enums; using PMSWPF.Excptions; +using PMSWPF.Extensions; +using PMSWPF.Models; namespace PMSWPF.Data.Repositories; @@ -7,23 +10,26 @@ public class DevicesRepositories:BaseRepositories { public DevicesRepositories():base() { - var tableExist= _db.DbMaintenance.IsAnyTable(); - if (!tableExist) - { - _db.CodeFirst.InitTables(); - } + } - public async Task Add(DbDevice dbDevice) + public async Task Add(Device device) { - var exist=await _db.Queryable().Where(d=>d.Name==dbDevice.Name).FirstAsync(); + var exist=await _db.Queryable().Where(d=>d.Name==device.Name).FirstAsync(); if (exist != null) { throw new DbExistException("设备名称已经存在。"); } - var res= await _db.Insertable(dbDevice).ExecuteCommandAsync(); - - return res; + DbDevice dbDevice=new DbDevice(); + device.CopyTo(dbDevice); + dbDevice.VariableTables=new List(); + DbVariableTable dbVariableTable=new DbVariableTable(); + dbVariableTable.Name = "默认变量表"; + dbVariableTable.Description = "默认变量表"; + dbVariableTable.ProtocolType = ProtocolType.S7; + dbDevice.VariableTables.Add(dbVariableTable); + return await _db.InsertNav(dbDevice).Include(d=>d.VariableTables).ExecuteCommandAsync(); + } public async Task> GetAll() diff --git a/Data/Repositories/PlcRepositories.cs b/Data/Repositories/PlcRepositories.cs deleted file mode 100644 index 29e2375..0000000 --- a/Data/Repositories/PlcRepositories.cs +++ /dev/null @@ -1,21 +0,0 @@ -using PMSWPF.Data.Entities; -using SqlSugar; - -namespace PMSWPF.Data.Repositories -{ - internal class PlcRepositories - { - private SqlSugarClient _db; - - public PlcRepositories() - { - - _db = DbContext.GetInstance(); - var tabExist = _db.DbMaintenance.IsAnyTable(nameof(DbPLC), false); - if (tabExist) - { - _db.CodeFirst.InitTables(); - } - } - } -} diff --git a/Enums/DeviceType.cs b/Enums/DeviceType.cs new file mode 100644 index 0000000..bb5a36f --- /dev/null +++ b/Enums/DeviceType.cs @@ -0,0 +1,13 @@ +using System.ComponentModel; + +namespace PMSWPF.Enums; + +public enum DeviceType +{ + [Description("西门子PLC")] + SiemensPLC, + [Description("三菱PLC")] + MelsecPLC + + +} \ No newline at end of file diff --git a/Enums/ProtocolType.cs b/Enums/ProtocolType.cs new file mode 100644 index 0000000..0f37636 --- /dev/null +++ b/Enums/ProtocolType.cs @@ -0,0 +1,15 @@ +using System.ComponentModel; + +namespace PMSWPF.Enums; + +public enum ProtocolType +{ + [Description("S7协议")] + S7 , + [Description("OpcUA协议")] + OpcUA , + [Description("ModbusRtu协议")] + ModbusRtu , + [Description("ModbusTcp协议")] + ModbusTcp +} \ No newline at end of file diff --git a/Enums/SignalType.cs b/Enums/SignalType.cs new file mode 100644 index 0000000..0714f0c --- /dev/null +++ b/Enums/SignalType.cs @@ -0,0 +1,28 @@ +using System.ComponentModel; + +namespace PMSWPF.Enums; + +public enum SignalType +{ + [Description("启动信号")] + StartSignal, + [Description("停止信号")] + StopSignal, + [Description("报警信号")] + AlarmSignal, + [Description("准备信号")] + ReadySignal, + [Description("复位信号")] + ResetSignal, + [Description("运行信号")] + RunSignal, + [Description("设定频率")] + SetHZSignal, + [Description("当前频率")] + GetHZSignal, + [Description("当前电流")] + CurrentASignal, + [Description("其他信号")] + OtherASignal + +} \ No newline at end of file diff --git a/Extensions/EnumBindingSourceExtension.cs b/Extensions/EnumBindingSourceExtension.cs new file mode 100644 index 0000000..99c2557 --- /dev/null +++ b/Extensions/EnumBindingSourceExtension.cs @@ -0,0 +1,50 @@ +using System.Windows.Markup; + +namespace PMSWPF.Extensions; + +class EnumBindingSourceExtension : MarkupExtension +{ + private Type? _enumType; + + public Type? EnumType + { + get => _enumType; + set + { + if (value != _enumType) + { + if (value != null) + { + Type enumType = Nullable.GetUnderlyingType(value) ?? value; + if (!enumType.IsEnum) + throw new ArgumentException("Type must be for an Enum."); + } + + _enumType = value; + } + } + } + + public EnumBindingSourceExtension() { } + + public EnumBindingSourceExtension(Type enumType) + { + EnumType = enumType; + } + + public override object ProvideValue(IServiceProvider serviceProvider) + { + if (_enumType == null) + throw new InvalidOperationException("The EnumType must be specified."); + + var actualEnumType = Nullable.GetUnderlyingType(_enumType) ?? _enumType; + var enumValues = Enum.GetValues(actualEnumType); + + if (actualEnumType == _enumType) + return enumValues; + + var tempArray = Array.CreateInstance(actualEnumType, enumValues.Length + 1); + enumValues.CopyTo(tempArray, 1); + return tempArray; + } +} \ No newline at end of file diff --git a/Extensions/ObjectExtensions.cs b/Extensions/ObjectExtensions.cs index 365026f..d0f394b 100644 --- a/Extensions/ObjectExtensions.cs +++ b/Extensions/ObjectExtensions.cs @@ -25,4 +25,27 @@ public static class ObjectExtensions } } } + + /// + /// 创建一个泛型对象,将source对象上的所有属性的值,都转换到新创建对象上 + /// + /// + /// + /// + public static void NewTo(this Object source ) where T : new() + { + T target = new T(); + var sourceType = source.GetType(); + var targetType = target.GetType(); + var sourceProperties = sourceType.GetProperties(); + foreach (PropertyInfo sourceProperty in sourceProperties) + { + PropertyInfo targetProperty = targetType.GetProperty(sourceProperty.Name); + if (targetProperty!= null && targetProperty.CanWrite && sourceProperty.CanRead && targetProperty.PropertyType == sourceProperty.PropertyType) + { + object value = sourceProperty.GetValue(source, null); + targetProperty.SetValue(target, value, null); + } + } + } } \ No newline at end of file diff --git a/Models/DataVariable.cs b/Models/DataVariable.cs new file mode 100644 index 0000000..2781095 --- /dev/null +++ b/Models/DataVariable.cs @@ -0,0 +1,27 @@ +using PMSWPF.Enums; + +namespace PMSWPF.Models; + +public class DataVariable +{ + public int Id { get; set; } + public string Name { get; set; } + public string Description { get; set; } + public string NodeId { get; set; } + public ProtocolType ProtocolType { get; set; } + public string DataType { get; set; } + public List Mqtts { get; set; } + public string DataValue { get; set; } + public string DisplayValue { get; set; } + public DateTime UpdateTime { get; set; } + public User UpdateUser { get; set; } + public string Converstion { get; set; } + public bool IsDeleted { get; set; } + public bool IsActive { get; set; } + public bool IsSave { get; set; } + public Double SaveRange { get; set; } + public bool IsAlarm { get; set; } + public Double AlarmMin { get; set; } + public Double AlarmMax { get; set; } + public SignalType SignalType { get; set; } +} \ No newline at end of file diff --git a/Models/Device.cs b/Models/Device.cs index afee96e..68418b5 100644 --- a/Models/Device.cs +++ b/Models/Device.cs @@ -1,4 +1,7 @@ using CommunityToolkit.Mvvm.ComponentModel; +using PMSWPF.Enums; +using SqlSugar; +using SqlSugar.DbConvert; namespace PMSWPF.Models; @@ -13,8 +16,13 @@ public partial class Device:ObservableObject [ObservableProperty] private string ip ; [ObservableProperty] - private bool isActive ; + private bool isActive =true; [ObservableProperty] private bool isRuning ; + [SugarColumn(ColumnDataType="varchar(20)",SqlParameterDbType=typeof(EnumToStringConvert))] + public DeviceType DeviceType { get; set; } + + public List? DataVariables { get; set; } + public ProtocolType ProtocolType { get; set; } } \ No newline at end of file diff --git a/Models/Mqtt.cs b/Models/Mqtt.cs new file mode 100644 index 0000000..ffc2850 --- /dev/null +++ b/Models/Mqtt.cs @@ -0,0 +1,6 @@ +namespace PMSWPF.Models; + +public class Mqtt +{ + +} \ No newline at end of file diff --git a/Models/User.cs b/Models/User.cs new file mode 100644 index 0000000..0854dc8 --- /dev/null +++ b/Models/User.cs @@ -0,0 +1,6 @@ +namespace PMSWPF.Models; + +public class User +{ + +} \ No newline at end of file diff --git a/Models/VariableTable.cs b/Models/VariableTable.cs new file mode 100644 index 0000000..3fd9412 --- /dev/null +++ b/Models/VariableTable.cs @@ -0,0 +1,14 @@ +using PMSWPF.Enums; + +namespace PMSWPF.Models; + +public class VariableTable +{ + + public int Id { get; set; } + public string Name { get; set; } + public string Description { get; set; } + public ProtocolType ProtocolType { get; set; } + public List DataVariables { get; set; } + +} \ No newline at end of file diff --git a/Resources/DevicesItemTemplateDictionary.xaml b/Resources/DevicesItemTemplateDictionary.xaml index 657234f..8a25519 100644 --- a/Resources/DevicesItemTemplateDictionary.xaml +++ b/Resources/DevicesItemTemplateDictionary.xaml @@ -4,7 +4,7 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> - + diff --git a/Services/INotificationService.cs b/Services/INotificationService.cs index d5a5064..a5a2857 100644 --- a/Services/INotificationService.cs +++ b/Services/INotificationService.cs @@ -6,5 +6,5 @@ namespace PMSWPF.Services; public interface INotificationService { void Show(Notification notification); - void Show(string message, NotificationType type, bool IsGlobal); + void Show(string message, NotificationType type = NotificationType.Info, bool IsGlobal = true); } \ No newline at end of file diff --git a/ValueConverts/EnumDescriptionConverter.cs b/ValueConverts/EnumDescriptionConverter.cs new file mode 100644 index 0000000..254fb0f --- /dev/null +++ b/ValueConverts/EnumDescriptionConverter.cs @@ -0,0 +1,35 @@ +using System.ComponentModel; +using System.Globalization; +using System.Windows; +using System.Windows.Data; + +namespace PMSWPF.ValueConverts; + +public class EnumDescriptionConverter : IValueConverter +{ + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + if (value == null) + return DependencyProperty.UnsetValue; + + return GetEnumDescription(value); + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + return string.Empty; + } + + private string GetEnumDescription(object enumObj) + { + var fi = enumObj.GetType().GetField(enumObj.ToString()); + + DescriptionAttribute[] attributes = + (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); + + if (attributes != null && attributes.Length > 0) + return attributes[0].Description; + else + return enumObj.ToString(); + } +} \ No newline at end of file diff --git a/ViewModels/DevicesViewModel.cs b/ViewModels/DevicesViewModel.cs index 7e5eafc..5cdaac4 100644 --- a/ViewModels/DevicesViewModel.cs +++ b/ViewModels/DevicesViewModel.cs @@ -23,9 +23,10 @@ public partial class DevicesViewModel : ViewModelBase private readonly IDeviceDialogService _deviceDialogService; private readonly DevicesRepositories _devicesRepositories; private readonly INotificationService _notificationService; - [ObservableProperty] private ObservableCollection _devices = new (); + [ObservableProperty] private ObservableCollection _devices = new(); - public DevicesViewModel(IDeviceDialogService deviceDialogService, DevicesRepositories devicesRepositories,INotificationService notificationService) + public DevicesViewModel(IDeviceDialogService deviceDialogService, DevicesRepositories devicesRepositories, + INotificationService notificationService) { _deviceDialogService = deviceDialogService; _devicesRepositories = devicesRepositories; @@ -45,56 +46,30 @@ public partial class DevicesViewModel : ViewModelBase } [RelayCommand] - public void Test() - { - // GrowlInfo info = new GrowlInfo(); - // info.Message = "Hello"; - // info.Type = InfoType.Error; - // info.ShowCloseButton = true; - _notificationService.Show( "Hello",NotificationType.Info,true); - // Growl.Error("Hello"); - // Growl.Info("Hello"); - // Growl.Success("Hello"); - // Growl.WarningGlobal("Hello"); - // Growl.SuccessGlobal("Hello"); - // Growl.FatalGlobal("Hello"); - // Growl.Ask("Hello", isConfirmed => - // { - // Growl.Info(isConfirmed.ToString()); - // return true; - // }); - - - } - - [RelayCommand] - public async void AddDevice() + public async void AddDevice() { + Device device = null; try { - Device device = await _deviceDialogService.ShowAddDeviceDialog(); + device= await _deviceDialogService.ShowAddDeviceDialog(); if (device != null) { - DbDevice dbDevice = new DbDevice(); - device.CopyTo(dbDevice); - var rowCount = await _devicesRepositories.Add(dbDevice); - if (rowCount > 0) + var isOk = await _devicesRepositories.Add(device); + if (isOk) { // MessageBox.Show("Device added successfully"); await OnLoadedAsync(); - _notificationService.Show(new Notification(){Message = "Hello World!",Type = NotificationType.Success,IsGlobal = true}); + _notificationService.Show($"设备添加成功:{device.Name}", NotificationType.Success); } } } catch (DbExistException e) { - Console.WriteLine(e); - MessageBox.Show(e.Message); + _notificationService.Show($"设备添加失败:名称为{device?.Name}的设备已经存在。请更换是被名称", NotificationType.Error); } catch (Exception e) { - Console.WriteLine(e); - MessageBox.Show(e.Message); + _notificationService.Show($"添加设备的过程中发生错误:{e.Message}", NotificationType.Error); } } diff --git a/ViewModels/Dialogs/DeviceDialogViewModel.cs b/ViewModels/Dialogs/DeviceDialogViewModel.cs index 4cd6e9d..4d138ac 100644 --- a/ViewModels/Dialogs/DeviceDialogViewModel.cs +++ b/ViewModels/Dialogs/DeviceDialogViewModel.cs @@ -1,5 +1,6 @@ using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; +using PMSWPF.Enums; using PMSWPF.Extensions; using PMSWPF.Models; @@ -13,6 +14,7 @@ public partial class DeviceDialogViewModel:ObservableObject private string title="添加设备"; [ObservableProperty] private Device device; + public DeviceDialogViewModel(Device saveDevice) { _saveDevice = saveDevice; diff --git a/Views/DevicesView.xaml b/Views/DevicesView.xaml index 743574d..b937bb3 100644 --- a/Views/DevicesView.xaml +++ b/Views/DevicesView.xaml @@ -17,12 +17,11 @@