From f3a90df4524df536c2897c9db6190feef7b1b12a Mon Sep 17 00:00:00 2001
From: fengjiayi <12821976+ning_xi@user.noreply.gitee.com>
Date: Fri, 27 Sep 2024 10:30:19 +0800
Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E4=BA=86=E6=96=B0=E7=9A=84?=
=?UTF-8?q?=E7=A4=BA=E4=BE=8B=E5=B7=A5=E7=A8=8B?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.gitignore | 1 -
Library/Entity/ExplicitData.cs | 4 +-
Library/NodeAttribute.cs | 47 ++++---
Library/Utils/EnumHelper.cs | 7 +
Net461DllTest/Data/MyData.cs | 15 ++
Net461DllTest/Device/PlcDevice.cs | 48 +++++++
Net461DllTest/Flow/LogicControl.cs | 131 ++++++++++++++++++
Net461DllTest/Flow/ViewLogicControl.cs | 100 +++++++++++++
Net461DllTest/Net461DllTest.csproj | 103 ++++++++++++++
Net461DllTest/Signal/OrderSignal.cs | 21 +++
Net461DllTest/Signal/Signal.cs | 13 ++
.../View/FromWorkBenchView.Designer.cs | 96 +++++++++++++
Net461DllTest/View/FromWorkBenchView.cs | 51 +++++++
Net461DllTest/View/FromWorkBenchView.resx | 120 ++++++++++++++++
Net461DllTest/View/TeseFormView.Designer.cs | 60 ++++++++
Net461DllTest/View/TeseFormView.cs | 33 +++++
Net461DllTest/View/TeseFormView.resx | 120 ++++++++++++++++
.../ViewModel/FromWorkBenchViewModel.cs | 35 +++++
Net461DllTest/Web/ApiController.cs | 32 +++++
Net461DllTest/packages.config | 4 +
NodeFlow/Base/NodeModelBaseFunc.cs | 117 +++++-----------
NodeFlow/FlowEnvironment.cs | 3 -
NodeFlow/Tool/MethodDetailsHelper.cs | 26 ++--
README.md | 2 +-
24 files changed, 1062 insertions(+), 127 deletions(-)
create mode 100644 Net461DllTest/Data/MyData.cs
create mode 100644 Net461DllTest/Device/PlcDevice.cs
create mode 100644 Net461DllTest/Flow/LogicControl.cs
create mode 100644 Net461DllTest/Flow/ViewLogicControl.cs
create mode 100644 Net461DllTest/Net461DllTest.csproj
create mode 100644 Net461DllTest/Signal/OrderSignal.cs
create mode 100644 Net461DllTest/Signal/Signal.cs
create mode 100644 Net461DllTest/View/FromWorkBenchView.Designer.cs
create mode 100644 Net461DllTest/View/FromWorkBenchView.cs
create mode 100644 Net461DllTest/View/FromWorkBenchView.resx
create mode 100644 Net461DllTest/View/TeseFormView.Designer.cs
create mode 100644 Net461DllTest/View/TeseFormView.cs
create mode 100644 Net461DllTest/View/TeseFormView.resx
create mode 100644 Net461DllTest/ViewModel/FromWorkBenchViewModel.cs
create mode 100644 Net461DllTest/Web/ApiController.cs
create mode 100644 Net461DllTest/packages.config
diff --git a/.gitignore b/.gitignore
index e5c3f88..2e24693 100644
--- a/.gitignore
+++ b/.gitignore
@@ -23,4 +23,3 @@ obj/
# 排除某些项目
-Net461DllTest/
\ No newline at end of file
diff --git a/Library/Entity/ExplicitData.cs b/Library/Entity/ExplicitData.cs
index e2aa190..ff97094 100644
--- a/Library/Entity/ExplicitData.cs
+++ b/Library/Entity/ExplicitData.cs
@@ -26,7 +26,7 @@ namespace Serein.Library.Entity
/////
///// 显式类型
/////
- //public Type ExplicitType { get; set; }
+ public Type ExplicitType { get; set; }
/////
///// 显示类型编号>
@@ -55,7 +55,7 @@ namespace Serein.Library.Entity
{
Index = Index,
IsExplicitData = IsExplicitData,
- // ExplicitType = ExplicitType,
+ ExplicitType = ExplicitType,
ExplicitTypeName = ExplicitTypeName,
DataType = DataType,
ParameterName = ParameterName,
diff --git a/Library/NodeAttribute.cs b/Library/NodeAttribute.cs
index e75c061..7479c56 100644
--- a/Library/NodeAttribute.cs
+++ b/Library/NodeAttribute.cs
@@ -73,6 +73,30 @@ namespace Serein.Library.Attributes
}
}
+ ///
+ /// 枚举值转换器,要求枚举项标记的BindValueAttribute特性,与搭配的参数类型一致,否则参数不会传入
+ ///
+
+ [AttributeUsage(AttributeTargets.Parameter)]
+ public class EnumTypeConvertorAttribute : Attribute
+ {
+ public Type EnumType { get; }
+
+ public EnumTypeConvertorAttribute(Type @enum)
+ {
+ if (@enum.IsEnum)
+ {
+ EnumType = @enum;
+ }
+ else
+ {
+ throw new ArgumentException("需要枚举类型");
+ }
+ }
+ }
+
+
+
[AttributeUsage(AttributeTargets.Field)]
public class PLCValueAttribute : Attribute
{
@@ -121,27 +145,4 @@ namespace Serein.Library.Attributes
}
}
-
- ///
- /// 枚举值转换器
- ///
-
- //[AttributeUsage(AttributeTargets.Parameter)]
- //public class EnumConvertorAttribute : Attribute
- //{
- // public Type Enum { get; }
-
- // public EnumConvertorAttribute(Type @enum)
- // {
- // if (@enum.IsEnum)
- // {
- // Enum = @enum;
- // }
- // else
- // {
- // throw new ArgumentException("需要枚举类型");
- // }
- // }
- //}
-
}
diff --git a/Library/Utils/EnumHelper.cs b/Library/Utils/EnumHelper.cs
index 3743e34..0ef6161 100644
--- a/Library/Utils/EnumHelper.cs
+++ b/Library/Utils/EnumHelper.cs
@@ -16,6 +16,13 @@ namespace Serein.Library.Utils
return attribute != null ? (TResult)valueSelector(attribute) : default;
}
+ public static object GetBoundValue(Type enumType,object enumValue, Func valueSelector)
+ {
+ var fieldInfo = enumType.GetField(enumValue.ToString());
+ var attribute = fieldInfo.GetCustomAttribute();
+
+ return attribute != null ? valueSelector(attribute) : default;
+ }
//public static TResult GetBoundValue(TEnum enumValue,
// Func valueSelector)
diff --git a/Net461DllTest/Data/MyData.cs b/Net461DllTest/Data/MyData.cs
new file mode 100644
index 0000000..5ad9543
--- /dev/null
+++ b/Net461DllTest/Data/MyData.cs
@@ -0,0 +1,15 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Net461DllTest.Data
+{
+ public class MyData
+ {
+ public int[] Values { get; set; } = new int[] { 1, 1, 4, 5, 1, 4, 1, 9, 9, 9 };
+ public int Count { get; set; }
+ public string Tips { get; set; }
+ }
+}
diff --git a/Net461DllTest/Device/PlcDevice.cs b/Net461DllTest/Device/PlcDevice.cs
new file mode 100644
index 0000000..a7eb7ad
--- /dev/null
+++ b/Net461DllTest/Device/PlcDevice.cs
@@ -0,0 +1,48 @@
+using Net461DllTest.Data;
+using Net461DllTest.Signal;
+using Serein.Library.Attributes;
+using Serein.Library.NodeFlow.Tool;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Net461DllTest.Device
+{
+
+
+ public class PlcDevice : ChannelFlowTrigger
+ {
+ [AutoInjection]
+ public MyData MyData { get; set; }
+
+ public PlcDevice()
+ {
+ PlcId = 114514 + 10000000 * new Random().Next(1, 9);
+ }
+ public int PlcId { get; set; }
+
+ public void InitDevice(string ip, int port, string tips)
+ {
+ Write($"模拟设备初始化 :{Environment.NewLine}" +
+ $" ip :{ip}{Environment.NewLine}" +
+ $"port:{port}{Environment.NewLine}" +
+ $"tips:{tips}{Environment.NewLine}");
+ }
+
+ public void Write(T value)
+ {
+ Console.WriteLine($"{value}");
+ }
+ public void Read()
+ {
+ Console.WriteLine($"读取数据:... ");
+ }
+ public void Disconnect()
+ {
+ Console.WriteLine($"断开连接...");
+ }
+ }
+
+}
diff --git a/Net461DllTest/Flow/LogicControl.cs b/Net461DllTest/Flow/LogicControl.cs
new file mode 100644
index 0000000..0562ac4
--- /dev/null
+++ b/Net461DllTest/Flow/LogicControl.cs
@@ -0,0 +1,131 @@
+using Net461DllTest.Data;
+using Net461DllTest.Device;
+using Net461DllTest.Signal;
+using Net461DllTest.Web;
+using Serein.Library.Api;
+using Serein.Library.Attributes;
+using Serein.Library.Enums;
+using Serein.Library.Ex;
+using Serein.Library.Framework.NodeFlow;
+using Serein.Library.NodeFlow.Tool;
+using Serein.Library.Web;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Net461DllTest.Flow
+{
+ [DynamicFlow] // 标记该类存在节点方法
+ public class LogicControl
+ {
+ [AutoInjection] // 标记该属性为依赖项,需要注入
+ public PlcDevice MyPlc { get; set; }
+
+
+ #region 初始化、初始化完成以及退出的事件
+ [NodeAction(NodeType.Init)] // Init : 初始化事件,流程启动时执行
+ public void Init(IDynamicContext context)
+ {
+ context.Env.IOC.Register(); // 注册Plc设备
+ context.Env.IOC.Register(); // 注册数据类
+ context.Env.IOC.Register(); // 注册Web服务
+ // // 注册控制器
+ context.Env.IOC.Run(router => {
+ router.RegisterController(typeof(ApiController));
+ });
+ }
+
+ [NodeAction(NodeType.Loading)] // Loading 初始化完成已注入依赖项,可以开始逻辑上的操作
+ public void Loading(IDynamicContext context)
+ {
+ context.Env.IOC.Run((web) =>
+ {
+ web.Start("http://*:8089/"); // 开启 Web 服务
+ });
+ }
+
+ [NodeAction(NodeType.Exit)] // 流程结束时自动执行
+ public void Exit(IDynamicContext context)
+ {
+ MyPlc.Disconnect();
+ MyPlc.CancelAllTasks();
+ }
+
+ #endregion
+
+ #region 触发器
+
+ [NodeAction(NodeType.Flipflop, "等待信号触发", ReturnType = typeof(int))]
+ public async Task WaitTask(OrderSignal order = OrderSignal.A)
+ {
+ try
+ {
+ TriggerData triggerData = await MyPlc.CreateChannelWithTimeoutAsync(order, TimeSpan.FromMinutes(5), 0);
+ if (triggerData.Type == TriggerType.Overtime)
+ {
+ throw new FlipflopException("超时取消");
+ }
+ //int.TryParse(triggerData.Value.ToString(),out int result);
+ MyPlc.MyData.Count += (int)triggerData.Value;
+ return new FlipflopContext(FlipflopStateType.Succeed, MyPlc.MyData.Count);
+ }
+ catch (FlipflopException)
+ {
+ throw;
+ }
+ catch (Exception)
+ {
+ return new FlipflopContext(FlipflopStateType.Error);
+ }
+
+ }
+
+ #endregion
+
+ #region 动作
+
+ [NodeAction(NodeType.Action, "初始化")]
+ public PlcDevice PlcInit(string ip = "192.168.1.1",
+ int port = 6688,
+ string tips = "测试")
+ {
+ MyPlc.InitDevice(ip, port, tips);
+ return MyPlc;
+ }
+
+
+ [NodeAction(NodeType.Action, "自增")]
+ public PlcDevice 自增(int number = 1)
+ {
+ MyPlc.MyData.Count += number;
+ return MyPlc;
+ }
+
+ [NodeAction(NodeType.Action, "重置计数")]
+ public void 重置计数()
+ {
+ MyPlc.MyData.Count = 0;
+ }
+
+ [NodeAction(NodeType.Action, "触发信号")]
+ public void 光电1信号触发(int data)
+ {
+ MyPlc.Write($"{MyPlc.PlcId.ToString("00000")} - 信号源[光电1] - 模拟写入 : {data}{Environment.NewLine}");
+ }
+
+ //[NodeAction(NodeType.Action, "触发光电2")]
+ //public void 光电2信号触发(int data)
+ //{
+ // MyPlc.Write($"{MyPlc.PlcId.ToString("00000")} - 信号源[光电2] - 模拟写入 : {data}{Environment.NewLine}");
+ //}
+
+ //[NodeAction(NodeType.Action, "触发光电3")]
+ //public void 光电3信号触发(int data)
+ //{
+ // MyPlc.Write($"{MyPlc.PlcId.ToString("00000")} - 信号源[光电3] - 模拟写入 : {data}{Environment.NewLine}");
+ //}
+ #endregion
+ }
+}
diff --git a/Net461DllTest/Flow/ViewLogicControl.cs b/Net461DllTest/Flow/ViewLogicControl.cs
new file mode 100644
index 0000000..ef15caa
--- /dev/null
+++ b/Net461DllTest/Flow/ViewLogicControl.cs
@@ -0,0 +1,100 @@
+using Net461DllTest.Data;
+using Net461DllTest.Device;
+using Net461DllTest.View;
+using Net461DllTest.ViewModel;
+using Serein.Library.Api;
+using Serein.Library.Attributes;
+using Serein.Library.Enums;
+using Serein.Library.Utils;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using System.Windows.Forms;
+
+namespace Net461DllTest.Flow
+{
+
+ public class ViewManagement
+ {
+
+ private List
public bool TryGetMethodDetails(string name, out MethodDetails? md)
{
- var isPass = false;
if (!string.IsNullOrEmpty(name))
{
md = MethodDetailss.FirstOrDefault(it => it.MethodName == name);
@@ -617,8 +616,6 @@ namespace Serein.NodeFlow
md = null;
return false;
}
-
-
}
///
diff --git a/NodeFlow/Tool/MethodDetailsHelper.cs b/NodeFlow/Tool/MethodDetailsHelper.cs
index 0a6afad..f304788 100644
--- a/NodeFlow/Tool/MethodDetailsHelper.cs
+++ b/NodeFlow/Tool/MethodDetailsHelper.cs
@@ -2,6 +2,7 @@
using Serein.Library.Attributes;
using Serein.Library.Core.NodeFlow;
using Serein.Library.Entity;
+using System;
using System.Collections.Concurrent;
using System.Reflection;
@@ -99,26 +100,25 @@ public static class MethodDetailsHelperTmp
return parameters.Select((it, index) =>
{
Type paremType;
- //var attribute = it.ParameterType.GetCustomAttribute();
- //if (attribute is not null && attribute.Enum.IsEnum)
- //{
- // // 存在选择器
- // paremType = attribute.Enum;
- //}
- //else
- //{
- // paremType = it.ParameterType;
- //}
- paremType = it.ParameterType;
+ var attribute = it.GetCustomAttribute();
+ if (attribute is not null)
+ {
+ // 存在选择器
+ paremType = attribute.EnumType;
+ }
+ else
+ {
+ paremType = it.ParameterType;
+ }
string explicitTypeName = GetExplicitTypeName(paremType);
var items = GetExplicitItems(paremType, explicitTypeName);
if ("Bool".Equals(explicitTypeName)) explicitTypeName = "Select"; // 布尔值 转为 可选类型
return new ExplicitData
{
- IsExplicitData = it.HasDefaultValue,
+ IsExplicitData = attribute is null ? it.HasDefaultValue: true,
Index = index,
- // ExplicitType = it.ParameterType,
ExplicitTypeName = explicitTypeName,
+ ExplicitType = paremType,
DataType = it.ParameterType,
ParameterName = it.Name,
DataValue = it.HasDefaultValue ? it.DefaultValue.ToString() : "",
diff --git a/README.md b/README.md
index 4833faf..6015247 100644
--- a/README.md
+++ b/README.md
@@ -12,7 +12,7 @@ https://space.bilibili.com/33526379
# 如何加载我的DLL?
-使用 **DynamicFlow** 特性标记你的类,可以参照 **MyDll** 与 **SereinWAT** 的实现。编译为 Dll文件 后,拖入到软件中即可。
+使用 **DynamicFlow** 特性标记你的类,可以参照 **Net461DllTest** 的实现。编译为 Dll文件 后,拖入到软件中即可。
# 如何让我的方法成为节点?
使用 **NodeAction** 特性标记你的方法。