添加了数据库相关了类,和枚举类型相关的类,并且将枚举类型绑定到前段
This commit is contained in:
@@ -41,10 +41,17 @@ namespace PMSWPF
|
||||
protected override void OnStartup(StartupEventArgs e)
|
||||
{
|
||||
base.OnStartup(e);
|
||||
CheckDb();
|
||||
|
||||
MainWindow = Services.GetRequiredService<MainView>();
|
||||
MainWindow.Show();
|
||||
}
|
||||
|
||||
private void CheckDb()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// [STAThread]
|
||||
// static void Main(string[] args)
|
||||
// {
|
||||
|
||||
40
Data/Entities/DbDataVariable.cs
Normal file
40
Data/Entities/DbDataVariable.cs
Normal file
@@ -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<DbMqtt>? 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; }
|
||||
|
||||
}
|
||||
@@ -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<DbVariableTable>? VariableTables { get; set; }
|
||||
[SugarColumn(ColumnDataType="varchar(20)",SqlParameterDbType=typeof(EnumToStringConvert))]
|
||||
public ProtocolType ProtocolType { get; set; }
|
||||
|
||||
}
|
||||
11
Data/Entities/DbMqtt.cs
Normal file
11
Data/Entities/DbMqtt.cs
Normal file
@@ -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; }
|
||||
|
||||
}
|
||||
@@ -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; }
|
||||
/// <summary>
|
||||
/// PLC名称
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
/// <summary>
|
||||
/// PLC品牌
|
||||
/// </summary>
|
||||
/// //新版本:存储字符串 SqlSugar 5.1.4.66-preview02
|
||||
[SugarColumn(ColumnDataType = "varchar(30)", SqlParameterDbType = typeof(EnumToStringConvert))]
|
||||
public PlcBrand PlcBrand { get; set; }
|
||||
/// <summary>
|
||||
/// PLC类型
|
||||
/// </summary>
|
||||
public int CpuType { get; set; }
|
||||
/// <summary>
|
||||
/// PLC节点ID
|
||||
/// </summary>
|
||||
public string NodeID { get; set; }
|
||||
/// <summary>
|
||||
/// PLC IP地址
|
||||
/// </summary>
|
||||
public string IP { get; set; }
|
||||
/// <summary>
|
||||
/// PLC状态
|
||||
/// </summary>
|
||||
public string Status { get; set; }
|
||||
/// <summary>
|
||||
/// PLC连接类型
|
||||
/// </summary>
|
||||
public string ConnType { get; set; }
|
||||
/// <summary>
|
||||
/// PLC连接时间
|
||||
/// </summary>
|
||||
public DateTime ConnTime { get; set; }
|
||||
/// <summary>
|
||||
/// 是否启用
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
10
Data/Entities/DbS7DataVariable.cs
Normal file
10
Data/Entities/DbS7DataVariable.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using SqlSugar;
|
||||
|
||||
namespace PMSWPF.Data.Entities;
|
||||
|
||||
[SugarTable("S7DataVariable")]
|
||||
public class DbS7DataVariable:DbDataVariable
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
11
Data/Entities/DbUser.cs
Normal file
11
Data/Entities/DbUser.cs
Normal file
@@ -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; }
|
||||
|
||||
}
|
||||
25
Data/Entities/DbVariableTable.cs
Normal file
25
Data/Entities/DbVariableTable.cs
Normal file
@@ -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<DbDataVariable>? DataVariables { get; set; }
|
||||
[SugarColumn(IsNullable = true)]
|
||||
public int? DeviceId { get; set; }
|
||||
[Navigate(NavigateType.ManyToOne, nameof(DbVariableTable.DeviceId))]
|
||||
public Device? Device { get; set; }
|
||||
}
|
||||
@@ -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<DbDevice>())
|
||||
_db.CodeFirst.InitTables<DbDevice>();
|
||||
if(!_db.DbMaintenance.IsAnyTable<DbVariableTable>())
|
||||
_db.CodeFirst.InitTables<DbVariableTable>();
|
||||
if(!_db.DbMaintenance.IsAnyTable<DbDataVariable>())
|
||||
_db.CodeFirst.InitTables<DbDataVariable>();
|
||||
if(!_db.DbMaintenance.IsAnyTable<DbS7DataVariable>())
|
||||
_db.CodeFirst.InitTables<DbS7DataVariable>();
|
||||
if(!_db.DbMaintenance.IsAnyTable<DbUser>())
|
||||
_db.CodeFirst.InitTables<DbUser>();
|
||||
if(!_db.DbMaintenance.IsAnyTable<DbMqtt>())
|
||||
_db.CodeFirst.InitTables<DbMqtt>();
|
||||
}
|
||||
}
|
||||
@@ -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<DbDevice>();
|
||||
if (!tableExist)
|
||||
{
|
||||
_db.CodeFirst.InitTables<DbDevice>();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public async Task<int> Add(DbDevice dbDevice)
|
||||
public async Task<bool> Add(Device device)
|
||||
{
|
||||
var exist=await _db.Queryable<DbDevice>().Where(d=>d.Name==dbDevice.Name).FirstAsync();
|
||||
var exist=await _db.Queryable<DbDevice>().Where(d=>d.Name==device.Name).FirstAsync();
|
||||
if (exist != null)
|
||||
{
|
||||
throw new DbExistException("设备名称已经存在。");
|
||||
}
|
||||
var res= await _db.Insertable<DbDevice>(dbDevice).ExecuteCommandAsync();
|
||||
|
||||
return res;
|
||||
DbDevice dbDevice=new DbDevice();
|
||||
device.CopyTo<DbDevice>(dbDevice);
|
||||
dbDevice.VariableTables=new List<DbVariableTable>();
|
||||
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<List<DbDevice>> GetAll()
|
||||
|
||||
@@ -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<DbPLC>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
13
Enums/DeviceType.cs
Normal file
13
Enums/DeviceType.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace PMSWPF.Enums;
|
||||
|
||||
public enum DeviceType
|
||||
{
|
||||
[Description("西门子PLC")]
|
||||
SiemensPLC,
|
||||
[Description("三菱PLC")]
|
||||
MelsecPLC
|
||||
|
||||
|
||||
}
|
||||
15
Enums/ProtocolType.cs
Normal file
15
Enums/ProtocolType.cs
Normal file
@@ -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
|
||||
}
|
||||
28
Enums/SignalType.cs
Normal file
28
Enums/SignalType.cs
Normal file
@@ -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
|
||||
|
||||
}
|
||||
50
Extensions/EnumBindingSourceExtension.cs
Normal file
50
Extensions/EnumBindingSourceExtension.cs
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -25,4 +25,27 @@ public static class ObjectExtensions
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建一个泛型对象,将source对象上的所有属性的值,都转换到新创建对象上
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="target"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public static void NewTo<T>(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
27
Models/DataVariable.cs
Normal file
27
Models/DataVariable.cs
Normal file
@@ -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<Mqtt> 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; }
|
||||
}
|
||||
@@ -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<DataVariable>? DataVariables { get; set; }
|
||||
public ProtocolType ProtocolType { get; set; }
|
||||
|
||||
}
|
||||
6
Models/Mqtt.cs
Normal file
6
Models/Mqtt.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace PMSWPF.Models;
|
||||
|
||||
public class Mqtt
|
||||
{
|
||||
|
||||
}
|
||||
6
Models/User.cs
Normal file
6
Models/User.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace PMSWPF.Models;
|
||||
|
||||
public class User
|
||||
{
|
||||
|
||||
}
|
||||
14
Models/VariableTable.cs
Normal file
14
Models/VariableTable.cs
Normal file
@@ -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<DataVariable> DataVariables { get; set; }
|
||||
|
||||
}
|
||||
@@ -4,7 +4,7 @@
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
||||
|
||||
<DataTemplate x:Key="DeviceItemTemplate">
|
||||
<Border Background="#eee" CornerRadius="10" Padding="10" >
|
||||
<Border Background="#eee" CornerRadius="10" Margin="3" Padding="10" >
|
||||
<ikw:SimpleStackPanel Spacing="10" Width="300" Height="200">
|
||||
<TextBlock FontSize="26" FontWeight="Bold" Text="{Binding Name }" />
|
||||
<TextBlock Text="{Binding Description }" />
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
35
ValueConverts/EnumDescriptionConverter.cs
Normal file
35
ValueConverts/EnumDescriptionConverter.cs
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -23,9 +23,10 @@ public partial class DevicesViewModel : ViewModelBase
|
||||
private readonly IDeviceDialogService _deviceDialogService;
|
||||
private readonly DevicesRepositories _devicesRepositories;
|
||||
private readonly INotificationService _notificationService;
|
||||
[ObservableProperty] private ObservableCollection<Device> _devices = new ();
|
||||
[ObservableProperty] private ObservableCollection<Device> _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>(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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -17,12 +17,11 @@
|
||||
<StackPanel ZIndex="1" >
|
||||
<StackPanel Margin="10 5" Orientation="Horizontal">
|
||||
<Button Margin="5" Command="{Binding AddDeviceCommand}" Content="添加"/>
|
||||
<Button Margin="5" Command="{Binding TestCommand}" Content="测试通知"/>
|
||||
</StackPanel>
|
||||
|
||||
|
||||
<ui:GridView x:Name="BasicGridView"
|
||||
Margin="10"
|
||||
Margin="20"
|
||||
IsItemClickEnabled="True"
|
||||
ItemClick="BasicGridView_ItemClick"
|
||||
ItemsSource="{Binding Devices}"
|
||||
|
||||
@@ -7,30 +7,57 @@
|
||||
xmlns:ikw="http://schemas.inkore.net/lib/ui/wpf"
|
||||
xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
|
||||
xmlns:vmd="clr-namespace:PMSWPF.ViewModels.Dialogs"
|
||||
x:Name="dialog"
|
||||
xmlns:vc="clr-namespace:PMSWPF.ValueConverts"
|
||||
xmlns:ex="clr-namespace:PMSWPF.Extensions"
|
||||
xmlns:en="clr-namespace:PMSWPF.Enums"
|
||||
Title="{Binding Title}"
|
||||
d:DesignHeight="756"
|
||||
d:DesignWidth="700"
|
||||
CloseButtonText="取消"
|
||||
Closed="OnClosed"
|
||||
DefaultButton="Primary"
|
||||
PrimaryButtonCommand="{Binding AddDeviceCommand}"
|
||||
PrimaryButtonText="添加"
|
||||
FullSizeDesired="False"
|
||||
IsShadowEnabled="True"
|
||||
|
||||
d:DataContext="{d:DesignInstance vmd:DeviceDialogViewModel}"
|
||||
mc:Ignorable="d">
|
||||
<ikw:SimpleStackPanel Spacing="12">
|
||||
<TextBox ui:ControlHelper.Header="设备名称"
|
||||
Text="{Binding Device.Name, UpdateSourceTrigger=PropertyChanged}" />
|
||||
<TextBox ui:ControlHelper.Header="设备描述"
|
||||
Text="{Binding Device.Description, UpdateSourceTrigger=PropertyChanged}" />
|
||||
|
||||
<ui:ContentDialog.Resources>
|
||||
<ex:EnumBindingSource x:Key="deviceType" EnumType="{x:Type en:DeviceType}"/>
|
||||
<ex:EnumBindingSource x:Key="protocolType" EnumType="{x:Type en:ProtocolType}"/>
|
||||
<vc:EnumDescriptionConverter x:Key="EnumDescriptionConverter"/>
|
||||
</ui:ContentDialog.Resources>
|
||||
<ikw:SimpleStackPanel Width="260" Spacing="12">
|
||||
<!-- 设备名称 -->
|
||||
<TextBlock Text="设备名称" HorizontalAlignment="Left" Style="{StaticResource TextBlockSubTitle}"></TextBlock>
|
||||
<TextBox
|
||||
Text="{Binding Device.Name, UpdateSourceTrigger=PropertyChanged}" />
|
||||
<!-- 设备描述 -->
|
||||
<TextBlock Text="设备描述" HorizontalAlignment="Left" Style="{StaticResource TextBlockSubTitle}"></TextBlock>
|
||||
<TextBox
|
||||
Text="{Binding Device.Description, UpdateSourceTrigger=PropertyChanged}" />
|
||||
<!-- 设备IP地址 -->
|
||||
<TextBlock Text="设备IP地址" HorizontalAlignment="Left" Style="{StaticResource TextBlockSubTitle}"></TextBlock>
|
||||
<TextBox
|
||||
ui:ControlHelper.Header="设备IP地址:"
|
||||
AcceptsReturn="True"
|
||||
Text="{Binding Device.Ip, UpdateSourceTrigger=PropertyChanged}" />
|
||||
|
||||
<!-- 设备类型 -->
|
||||
<TextBlock Text="设备类型" HorizontalAlignment="Left" Style="{StaticResource TextBlockSubTitle}"></TextBlock>
|
||||
<ComboBox SelectedItem="{Binding Device.DeviceType}" ItemsSource="{Binding Source={StaticResource deviceType} }" >
|
||||
<ComboBox.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<TextBlock Text="{Binding Converter={StaticResource EnumDescriptionConverter}}"></TextBlock>
|
||||
</DataTemplate>
|
||||
</ComboBox.ItemTemplate>
|
||||
</ComboBox>
|
||||
|
||||
<!-- 通讯协议-->
|
||||
<TextBlock Text="设备通信协议" HorizontalAlignment="Left" Style="{StaticResource TextBlockSubTitle}"></TextBlock>
|
||||
<ComboBox SelectedItem="{Binding Device.ProtocolType}" ItemsSource="{Binding Source={StaticResource protocolType} }" >
|
||||
<ComboBox.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<TextBlock Text="{Binding Converter={StaticResource EnumDescriptionConverter}}"></TextBlock>
|
||||
</DataTemplate>
|
||||
</ComboBox.ItemTemplate>
|
||||
</ComboBox>
|
||||
<!-- 通讯协议-->
|
||||
<CheckBox Content="是否启用" IsChecked="{Binding Device.IsActive}" />
|
||||
<!-- <TextBox ui:ControlHelper.Header="PrimaryButtonText" -->
|
||||
<!-- Text="{Binding ElementName=dialog, Path=PrimaryButtonText, UpdateSourceTrigger=PropertyChanged}" /> -->
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:vm="clr-namespace:PMSWPF.ViewModels"
|
||||
xmlns:hc="https://handyorg.github.io/handycontrol"
|
||||
Title="MainView"
|
||||
Title="设备管理系统"
|
||||
Width="800"
|
||||
Height="600"
|
||||
ui:WindowHelper.UseModernWindowStyle="True"
|
||||
@@ -19,6 +19,7 @@
|
||||
mc:Ignorable="d">
|
||||
<Grid>
|
||||
<ui:NavigationView
|
||||
|
||||
ExpandedModeThresholdWidth="500"
|
||||
IsTabStop="False"
|
||||
PaneDisplayMode="Left"
|
||||
|
||||
Reference in New Issue
Block a user