示例工程的修改,以依赖注入的方式

This commit is contained in:
fengjiayi
2024-09-30 10:02:06 +08:00
parent c69fa0ccae
commit f900cac4ea
3 changed files with 49 additions and 25 deletions

View File

@@ -13,6 +13,7 @@ using Serein.Library.NodeFlow.Tool;
using Serein.Library.Utils; using Serein.Library.Utils;
using Serein.Library.Web; using Serein.Library.Web;
using System; using System;
using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Reflection; using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -131,19 +132,20 @@ namespace Net461DllTest.LogicControl
} }
// [BindConvertor(typeof(PlcVarEnum), typeof(PlcVarConvertor))]
[NodeAction(NodeType.Action, "PLC获取变量")] [NodeAction(NodeType.Action, "PLC获取变量")]
public object ReadVar([BindConvertor(typeof(PlcVarEnum), typeof(PlcVarConvertor))] PlcVarInfo varInfo) public object ReadVar(PlcVarEnum plcVarEnum)
{ {
var varInfo = Convertor(plcVarEnum);
var result = MyPlc.Read(varInfo); var result = MyPlc.Read(varInfo);
Console.WriteLine($"获取变量成功:({varInfo})\t result = {result}"); Console.WriteLine($"获取变量成功:({varInfo})\t result = {result}");
return result; return result;
} }
[NodeAction(NodeType.Action, "PLC写入变量")] [NodeAction(NodeType.Action, "PLC写入变量")]
public SiemensPlcDevice WriteVar2(object value, [BindConvertor(typeof(PlcVarEnum), typeof(PlcVarConvertor))] PlcVarInfo varInfo) public SiemensPlcDevice WriteVar2(object value, PlcVarEnum plcVarEnum)
{ {
var varInfo = Convertor(plcVarEnum);
if (MyPlc.State == PlcState.Runing) if (MyPlc.State == PlcState.Runing)
{ {
if (varInfo.IsProtected) if (varInfo.IsProtected)
@@ -164,27 +166,48 @@ namespace Net461DllTest.LogicControl
} }
/// <summary> private readonly Dictionary<PlcVarEnum, PlcVarInfo> VarInfoDict;
/// 转换器,用于将枚举转为自定义特性中的数据 public PlcVarInfo Convertor(PlcVarEnum plcVarEnum)
/// </summary>
public class PlcVarConvertor: IEnumConvertor<PlcVarEnum, PlcVarInfo>
{ {
public PlcVarInfo Convertor(PlcVarEnum plcVarValue) if (VarInfoDict.ContainsKey(plcVarEnum))
{ {
if (plcVarValue == PlcVarEnum.None) return VarInfoDict[plcVarEnum];
}
if (plcVarEnum == PlcVarEnum.None)
{ {
throw new Exception("非预期枚举值"); throw new Exception("非预期枚举值");
} }
var plcValue = EnumHelper.GetBoundValue<PlcVarEnum, PlcValueAttribute, PlcVarInfo>(plcVarValue, attr => attr.PlcInfo) var plcValue = EnumHelper.GetBoundValue<PlcVarEnum, PlcValueAttribute, PlcVarInfo>(plcVarEnum, attr => attr.PlcInfo)
?? throw new Exception($"获取变量异常:{plcVarValue}没有标记PlcValueAttribute"); ?? throw new Exception($"获取变量异常:{plcVarEnum}没有标记PlcValueAttribute");
if (string.IsNullOrEmpty(plcValue.VarAddress)) if (string.IsNullOrEmpty(plcValue.VarAddress))
{ {
throw new Exception($"获取变量异常:{plcVarValue},变量地址为空"); throw new Exception($"获取变量异常:{plcVarEnum},变量地址为空");
} }
VarInfoDict.Add(plcVarEnum, plcValue);
return plcValue; return plcValue;
} }
} /// <summary>
/// 转换器,用于将枚举转为自定义特性中的数据
/// </summary>
//public class PlcVarConvertor: IEnumConvertor<PlcVarEnum, PlcVarInfo>
//{
// public PlcVarInfo Convertor(PlcVarEnum plcVarValue)
// {
// if (plcVarValue == PlcVarEnum.None)
// {
// throw new Exception("非预期枚举值");
// }
// var plcValue = EnumHelper.GetBoundValue<PlcVarEnum, PlcValueAttribute, PlcVarInfo>(plcVarValue, attr => attr.PlcInfo)
// ?? throw new Exception($"获取变量异常:{plcVarValue}没有标记PlcValueAttribute");
// if (string.IsNullOrEmpty(plcValue.VarAddress))
// {
// throw new Exception($"获取变量异常:{plcVarValue},变量地址为空");
// }
// return plcValue;
// }
//}
#endregion #endregion
} }

View File

@@ -755,6 +755,7 @@ namespace Serein.NodeFlow
/// <returns></returns> /// <returns></returns>
public bool AddInterruptExpression(string key, string expression) public bool AddInterruptExpression(string key, string expression)
{ {
if(string.IsNullOrEmpty(expression)) return false;
if (dictMonitorObjExpInterrupt.TryGetValue(key, out var condition)) if (dictMonitorObjExpInterrupt.TryGetValue(key, out var condition))
{ {
condition.Clear(); // 暂时 condition.Clear(); // 暂时

View File

@@ -314,8 +314,8 @@ namespace Serein.WorkBench.Themes
#region #region
if (member is PropertyInfo property) if (member is PropertyInfo property)
{ {
#region #region (
if (typeof(IEnumerable).IsAssignableFrom(property.PropertyType) && property.GetValue(obj) is IEnumerable collection && collection is not null) if (property.PropertyType != typeof(string) && typeof(IEnumerable).IsAssignableFrom(property.PropertyType) && property.GetValue(obj) is IEnumerable collection && collection is not null)
{ {
TreeViewItem memberNode = new TreeViewItem { Header = member.Name }; TreeViewItem memberNode = new TreeViewItem { Header = member.Name };
// 处理集合类型的属性 // 处理集合类型的属性
@@ -377,8 +377,8 @@ namespace Serein.WorkBench.Themes
#region #region
else if (member is FieldInfo field) else if (member is FieldInfo field)
{ {
#region #region (
if (typeof(IEnumerable).IsAssignableFrom(field.FieldType) && field.GetValue(obj) is IEnumerable collection && collection is not null) if (field.FieldType != typeof(string) && typeof(IEnumerable).IsAssignableFrom(field.FieldType) && field.GetValue(obj) is IEnumerable collection && collection is not null)
{ {
TreeViewItem memberNode = new TreeViewItem { Header = member.Name }; TreeViewItem memberNode = new TreeViewItem { Header = member.Name };
// 处理集合类型的字段 // 处理集合类型的字段