diff --git a/Library.Core/FlipflopContext.cs b/Library.Core/FlipflopContext.cs index a9e6862..cabf969 100644 --- a/Library.Core/FlipflopContext.cs +++ b/Library.Core/FlipflopContext.cs @@ -71,7 +71,7 @@ namespace Serein.Library.Core { public FlipflopStateType State { get; set; } - public TriggerType Type { get; set; } + public TriggerDescription Type { get; set; } public TResult Value { get; set; } public FlipflopContext(FlipflopStateType ffState) diff --git a/Library.Framework/FlipflopContext.cs b/Library.Framework/FlipflopContext.cs index b1be1a5..c3fa51a 100644 --- a/Library.Framework/FlipflopContext.cs +++ b/Library.Framework/FlipflopContext.cs @@ -63,7 +63,7 @@ namespace Serein.Library.Framework.NodeFlow { public FlipflopStateType State { get; set; } - public TriggerType Type { get; set; } + public TriggerDescription Type { get; set; } public TResult Value { get; set; } public FlipflopContext(FlipflopStateType ffState) diff --git a/Library/Api/IFlipflopContext.cs b/Library/Api/IFlipflopContext.cs index 288d2f5..10be21e 100644 --- a/Library/Api/IFlipflopContext.cs +++ b/Library/Api/IFlipflopContext.cs @@ -17,7 +17,7 @@ namespace Serein.Library.Api /// /// 触发类型 /// - TriggerType Type { get; set; } + TriggerDescription Type { get; set; } /// /// 触发时传递的数据 /// diff --git a/Library/Api/IFlowTrigger.cs b/Library/Api/IFlowTrigger.cs new file mode 100644 index 0000000..33b07bb --- /dev/null +++ b/Library/Api/IFlowTrigger.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Serein.Library.Api +{ + /// + /// 触发器接口 + /// + /// + public interface IFlowTrigger + { + /// + /// 等待信号触发并指定超时时间 + /// + /// + /// + /// + /// + Task> WaitTriggerWithTimeoutAsync(TSignal signal, TimeSpan outTime); + /// + /// 等待信号触发 + /// + /// 预期的返回值类型 + /// + /// + Task> WaitTriggerAsync(TSignal signal); + /// + /// 调用触发器 + /// + /// 预期的返回值类型 + /// 信号 + /// 返回值 + /// + Task InvokeTriggerAsync(TSignal signal, TResult value); + /// + /// 取消所有触发器 + /// + void CancelAllTrigger(); + } + +} diff --git a/Library/Enums/NodeType.cs b/Library/Enums/NodeType.cs index 44b472b..90f211e 100644 --- a/Library/Enums/NodeType.cs +++ b/Library/Enums/NodeType.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -72,6 +73,9 @@ namespace Serein.Library /// public enum NodeControlType { + /// + /// 预料之外的情况 + /// None, /// /// 动作节点 @@ -81,25 +85,31 @@ namespace Serein.Library /// 触发器节点 /// Flipflop, + /// /// 表达式操作节点 /// + [Description("base")] ExpOp, /// /// 表达式操作节点 /// + [Description("base")] ExpCondition, /// /// 条件节点区域 /// + [Description("base")] ConditionRegion, /// /// 全局数据 /// + [Description("base")] GlobalData, /// /// 脚本节点 /// + [Description("base")] Script, } diff --git a/Library/FlowNode/NodeModelBaseFunc.cs b/Library/FlowNode/NodeModelBaseFunc.cs index ee5a643..ef01b3a 100644 --- a/Library/FlowNode/NodeModelBaseFunc.cs +++ b/Library/FlowNode/NodeModelBaseFunc.cs @@ -38,7 +38,6 @@ namespace Serein.Library } - /// /// 保存自定义信息 /// @@ -155,7 +154,7 @@ namespace Serein.Library AssemblyName = MethodDetails.AssemblyName, MethodName = MethodDetails?.MethodName, Label = MethodDetails?.MethodAnotherName, - Type = this.GetType().ToString(), + Type = ControlType.ToString() , //this.GetType().ToString(), TrueNodes = trueNodes.ToArray(), FalseNodes = falseNodes.ToArray(), UpstreamNodes = upstreamNodes.ToArray(), diff --git a/Library/Utils/ChannelFlowTrigger.cs b/Library/Utils/ChannelFlowTrigger.cs index 34a5d5e..d65e1d6 100644 --- a/Library/Utils/ChannelFlowTrigger.cs +++ b/Library/Utils/ChannelFlowTrigger.cs @@ -1,5 +1,6 @@  +using Serein.Library.Api; using System; using System.Collections.Concurrent; using System.Threading; @@ -12,18 +13,22 @@ namespace Serein.Library.Utils - public class ChannelFlowTrigger + public class ChannelFlowTrigger : IFlowTrigger { // 使用并发字典管理每个枚举信号对应的 Channel - private readonly ConcurrentDictionary> _channels = new ConcurrentDictionary>(); + private readonly ConcurrentDictionary>> _channels = new ConcurrentDictionary>>(); /// - /// 创建信号并指定超时时间,到期后自动触发(异步方法) + /// 获取或创建指定信号的 Channel /// /// 枚举信号标识符 - /// 超时时间 - /// 等待任务 - public async Task<(TriggerType, TResult)> WaitDataWithTimeoutAsync(TSignal signal, TimeSpan outTime) + /// 对应的 Channel + private Channel> GetOrCreateChannel(TSignal signal) + { + return _channels.GetOrAdd(signal, _ => Channel.CreateUnbounded>()); + } + + public async Task> WaitTriggerWithTimeoutAsync(TSignal signal, TimeSpan outTime) { var channel = GetOrCreateChannel(signal); var cts = new CancellationTokenSource(); @@ -34,7 +39,11 @@ namespace Serein.Library.Utils try { await Task.Delay(outTime, cts.Token); - await channel.Writer.WriteAsync((TriggerType.Overtime, null)); + var outResult = new TriggerResult() + { + Type = TriggerDescription.Overtime + }; + await channel.Writer.WriteAsync(outResult); } catch (OperationCanceledException) { @@ -43,45 +52,51 @@ namespace Serein.Library.Utils }, cts.Token); // 等待信号传入(超时或手动触发) - (var type, var result) = await channel.Reader.ReadAsync(); + var result = await WaitTriggerAsync(signal); // 返回一个可以超时触发的等待任务 + return result; + - return (type, result.ToConvert()); } - /// - /// 创建信号,直到触发 - /// - /// 枚举信号标识符 - /// 等待任务 - public async Task WaitData(TSignal signal) + public async Task> WaitTriggerAsync(TSignal signal) { var channel = GetOrCreateChannel(signal); // 等待信号传入(超时或手动触发) - (var type, var result) = await channel.Reader.ReadAsync(); - return result.ToConvert(); + var result = await channel.Reader.ReadAsync(); + if (result.Value is TResult data) + { + return new TriggerResult() + { + Value = data, + Type = TriggerDescription.External, + }; + } + else + { + return new TriggerResult() + { + Type = TriggerDescription.TypeInconsistency, + }; + } } - - /// - /// 触发信号 - /// - /// 枚举信号标识符 - /// 是否成功触发 - public bool TriggerSignal(TSignal signal, object value) + public async Task InvokeTriggerAsync(TSignal signal, TResult value) { if (_channels.TryGetValue(signal, out var channel)) { // 手动触发信号 - channel.Writer.TryWrite((TriggerType.External,value)); + var result = new TriggerResult() + { + Type = TriggerDescription.External, + Value = value + }; + await channel.Writer.WriteAsync(result); return true; } return false; } - /// - /// 取消所有任务 - /// - public void CancelAllTasks() + public void CancelAllTrigger() { foreach (var channel in _channels.Values) { @@ -89,16 +104,6 @@ namespace Serein.Library.Utils } _channels.Clear(); } - - /// - /// 获取或创建指定信号的 Channel - /// - /// 枚举信号标识符 - /// 对应的 Channel - private Channel<(TriggerType, object)> GetOrCreateChannel(TSignal signal) - { - return _channels.GetOrAdd(signal, _ => Channel.CreateUnbounded<(TriggerType, object)>()); - } } diff --git a/Library/Utils/EnumHelper.cs b/Library/Utils/EnumHelper.cs index b2a06cb..470706a 100644 --- a/Library/Utils/EnumHelper.cs +++ b/Library/Utils/EnumHelper.cs @@ -77,7 +77,7 @@ namespace Serein.Library.Utils /// 枚举值 /// 特性成员选择 /// - public static TResult GetBoundValue(TEnum enumValue, + public static TResult GetAttributeValue(TEnum enumValue, Func valueSelector) where TEnum : Enum where TAttribute : Attribute @@ -88,6 +88,22 @@ namespace Serein.Library.Utils return attribute != null ? valueSelector(attribute) : default; } + /// + /// 从枚举值从获取自定义特性的成员,并自动转换类型 + /// + /// 枚举类型 + /// 自定义特性类型 + /// 枚举值 + /// + public static TAttribute GetAttribute(TEnum enumValue) + where TEnum : Enum + where TAttribute : Attribute + { + var fieldInfo = typeof(TEnum).GetField(enumValue.ToString()); + var attribute = fieldInfo.GetCustomAttribute(); + + return attribute; + } } diff --git a/Library/Utils/SingleSyncFlowTrigger.cs b/Library/Utils/SingleSyncFlowTrigger.cs new file mode 100644 index 0000000..c0093ec --- /dev/null +++ b/Library/Utils/SingleSyncFlowTrigger.cs @@ -0,0 +1,136 @@ +using Newtonsoft.Json.Linq; +using Serein.Library.Api; +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Reactive.Subjects; +using System.Text; +using System.Threading; +using System.Threading.Channels; +using System.Threading.Tasks; + +namespace Serein.Library.Utils +{ + /// + /// 同步的单体消息触发器 + /// + /// + public class SingleSyncFlowTrigger : IFlowTrigger + { + private readonly ConcurrentDictionary>>> _syncChannel + = new ConcurrentDictionary>>>(); + + public void CancelAllTrigger() + { + foreach (var triggers in _syncChannel.Values) + { + foreach (var trigger in triggers) + { + trigger.SetCanceled(); + } + } + + } + + public Task InvokeTriggerAsync(TSingle signal, TResult value) + { + if(_syncChannel.TryGetValue(signal, out var tcss)) + { + var tcs = tcss.Dequeue(); + var result = new TriggerResult + { + Type = TriggerDescription.External, + Value = value, + }; + tcs.SetResult(result); + return Task.FromResult(true); + } + return Task.FromResult(false); + } + + public async Task> WaitTriggerAsync(TSingle signal) + { + if (!_syncChannel.TryGetValue(signal,out var tcss)) + { + tcss = new Queue>>(); + _syncChannel.TryAdd(signal, tcss); + } + var taskCompletionSource = new TaskCompletionSource>(); + tcss.Enqueue(taskCompletionSource); + var result = await taskCompletionSource.Task; + if (result.Value is TResult result2) + { + return new TriggerResult + { + Type = TriggerDescription.External, + Value = result2, + }; + } + else + { + return new TriggerResult + { + Type = TriggerDescription.TypeInconsistency, + }; + } + } + + public async Task> WaitTriggerWithTimeoutAsync(TSingle signal, TimeSpan outTime) + { + if (!_syncChannel.TryGetValue(signal, out var tcss)) + { + tcss = new Queue>>(); + _syncChannel.TryAdd(signal, tcss); + } + + + var taskCompletionSource = new TaskCompletionSource>(); + tcss.Enqueue(taskCompletionSource); + + var cts = new CancellationTokenSource(); + + // 异步任务:超时后自动触发信号 + _ = Task.Run(async () => + { + try + { + await Task.Delay(outTime, cts.Token); + if (!cts.IsCancellationRequested) // 如果还没有被取消 + { + var outResult = new TriggerResult() + { + Type = TriggerDescription.Overtime + }; + taskCompletionSource.SetResult(outResult); // 超时触发 + } + } + catch (OperationCanceledException) + { + // 超时任务被取消 + } + finally + { + cts?.Dispose(); // 确保 cts 被释放 + } + }, cts.Token); + var result = await taskCompletionSource.Task; + cts?.Cancel(); + if (result.Value is TResult result2) + { + return new TriggerResult + { + Type = result.Type, + Value = result2, + }; + } + else + { + return new TriggerResult + { + Type = result.Type, + }; + } + } + } +} diff --git a/Library/Utils/FlowTrigger.cs b/Library/Utils/TaskFlowTrigger.cs similarity index 54% rename from Library/Utils/FlowTrigger.cs rename to Library/Utils/TaskFlowTrigger.cs index 3ae6f54..e6621ab 100644 --- a/Library/Utils/FlowTrigger.cs +++ b/Library/Utils/TaskFlowTrigger.cs @@ -1,4 +1,6 @@ -using Serein.Library.Utils; +using Newtonsoft.Json.Linq; +using Serein.Library.Api; +using Serein.Library.Utils; using System; using System.Collections.Concurrent; using System.Reactive.Linq; @@ -6,13 +8,16 @@ using System.Reactive.Subjects; using System.Threading; using System.Threading.Channels; using System.Threading.Tasks; +using System.Transactions; namespace Serein.Library { + + /// /// 触发类型 /// - public enum TriggerType + public enum TriggerDescription { /// /// 外部触发 @@ -21,32 +26,37 @@ namespace Serein.Library /// /// 超时触发 /// - Overtime + Overtime, + /// + /// 触发了,但类型不一致 + /// + TypeInconsistency } - public class TriggerResult + + public class TriggerResult { - public TriggerType Type { get; set; } - public T Value { get; set; } + public TriggerDescription Type { get; set; } + public TResult Value { get; set; } } /// /// 信号触发器类,带有消息广播功能。 /// 使用枚举作为标记,创建 /// - public class FlowTrigger where TSignal : struct, Enum + public class TaskFlowTrigger : IFlowTrigger where TSignal : struct, Enum { // 使用并发字典管理每个信号对应的广播列表 - private readonly ConcurrentDictionary> _subscribers = new ConcurrentDictionary>(); + private readonly ConcurrentDictionary>> _subscribers = new ConcurrentDictionary>>(); /// /// 获取或创建指定信号的 Subject(消息广播者) /// /// 枚举信号标识符 /// 对应的 Subject - private Subject<(TriggerType, object)> GetOrCreateSubject(TSignal signal) + private Subject> GetOrCreateSubject(TSignal signal) { - return _subscribers.GetOrAdd(signal, _ => new Subject<(TriggerType, object)>()); + return _subscribers.GetOrAdd(signal, _ => new Subject>()); } /// @@ -55,20 +65,23 @@ namespace Serein.Library /// 枚举信号标识符 /// 订阅者 /// 取消订阅的句柄 - public IDisposable Subscribe(TSignal signal, IObserver<(TriggerType, object)> observer) + private IDisposable Subscribe(TSignal signal, Action> action) { + IObserver> observer = new Observer>(action); var subject = GetOrCreateSubject(signal); - // (IObserver<(TriggerType, object)>) return subject.Subscribe(observer); // 返回取消订阅的句柄 } + + /// - /// 创建信号并指定超时时间,触发时通知所有订阅者 + /// 等待触发器并指定超时的时间 /// - /// 枚举信号标识符 + /// 返回值类型 + /// 等待信号 /// 超时时间 - /// 等待任务,返回值为:状态(超时触发,手动触发),数据(超时触发时会使用设置好的数据) - public async Task<(TriggerType, TResult)> CreateTaskWithTimeoutAsync(TSignal signal, TimeSpan outTime, TResult outValue) + /// + public async Task> WaitTriggerWithTimeoutAsync(TSignal signal, TimeSpan outTime) { var subject = GetOrCreateSubject(signal); var cts = new CancellationTokenSource(); @@ -81,7 +94,11 @@ namespace Serein.Library await Task.Delay(outTime, cts.Token); if (!cts.IsCancellationRequested) // 如果还没有被取消 { - subject.OnNext((TriggerType.Overtime, outValue)); // 广播给所有订阅者 + var outResult = new TriggerResult() + { + Type = TriggerDescription.Overtime + }; + subject.OnNext(outResult); // 广播给所有订阅者 subject.OnCompleted(); // 通知订阅结束 } } @@ -94,65 +111,67 @@ namespace Serein.Library cts?.Dispose(); // 确保 cts 被释放 } }, cts.Token); - - var result = await WaitSignalAsync(signal);// 返回一个可以超时触发的等待任务 + var result = await WaitTriggerAsync(signal); // 返回一个可以超时触发的等待任务 return result; } - - /// - /// 创建等待任务,触发时通知所有订阅者 + /// 等待触发 /// - /// 枚举信号标识符 - /// 超时时间 - /// 等待任务 - public async Task CreateTaskAsync(TSignal signal) + /// + /// + /// + public async Task> WaitTriggerAsync(TSignal signal) { - var subject = GetOrCreateSubject(signal); - (_,var result) = await WaitSignalAsync(signal); - - return result;// 返回一个等待的任务 - } - - - /// - /// 等待指定信号的触发 - /// - /// 枚举信号标识符 - /// 等待任务 - public async Task<(TriggerType, TResult)> WaitSignalAsync(TSignal signal) - { - var taskCompletionSource = new TaskCompletionSource<(TriggerType, object)>(); - var subscription = Subscribe(signal, new Observer<(TriggerType, object)>(taskCompletionSource.SetResult)); - (var type,var result) = await taskCompletionSource.Task; + var taskCompletionSource = new TaskCompletionSource>(); + var subscription = Subscribe(signal, taskCompletionSource.SetResult); + var result = await taskCompletionSource.Task; subscription.Dispose(); // 取消订阅 - - return (type, result.ToConvert()); + if(result.Value is TResult data) + { + return new TriggerResult() + { + Value = data, + Type = TriggerDescription.External, + }; + } + else + { + return new TriggerResult() + { + Type = TriggerDescription.TypeInconsistency, + }; + } } - /// /// 手动触发信号,并广播给所有订阅者 /// + /// 触发类型 /// 枚举信号标识符 + /// 传递的数据 /// 是否成功触发 - public bool Trigger(TSignal signal, TResult value) + public Task InvokeTriggerAsync(TSignal signal, TResult value) { if (_subscribers.TryGetValue(signal, out var subject)) { - subject.OnNext((TriggerType.External, value)); // 广播给所有订阅者 - //subject.OnCompleted(); // 通知订阅结束 - return true; + var result = new TriggerResult() + { + Type = TriggerDescription.External, + Value = value + }; + subject.OnNext(result); // 广播给所有订阅者 + subject.OnCompleted(); // 通知订阅结束 + return Task.FromResult(true); } - return false; + return Task.FromResult(false); } - /// /// 取消所有任务 /// - public void CancelAllTasks() + + public void CancelAllTrigger() { foreach (var subject in _subscribers.Values) { @@ -160,9 +179,11 @@ namespace Serein.Library } _subscribers.Clear(); } - } + + + /// /// 观察者类,用于包装 Action /// diff --git a/Net462DllTest/LogicControl/ParkingLogicControl.cs b/Net462DllTest/LogicControl/ParkingLogicControl.cs index 457f7b2..578e95e 100644 --- a/Net462DllTest/LogicControl/ParkingLogicControl.cs +++ b/Net462DllTest/LogicControl/ParkingLogicControl.cs @@ -28,16 +28,16 @@ namespace Net462DllTest.LogicControl [NodeAction(NodeType.Flipflop, "等待车位调取命令")] public async Task> GetPparkingSpace(ParkingCommand parkingCommand = ParkingCommand.GetPparkingSpace) { - var spaceNum = await PrakingDevice.CreateTaskAsync(parkingCommand); - await Console.Out.WriteLineAsync("收到命令:调取车位,车位号" + spaceNum); - return new FlipflopContext(FlipflopStateType.Succeed, spaceNum); + var result = await PrakingDevice.WaitTriggerAsync(parkingCommand); + await Console.Out.WriteLineAsync("收到命令:调取车位,车位号" + result.Value); + return new FlipflopContext(FlipflopStateType.Succeed, result.Value); } [NodeAction(NodeType.Action, "调取指定车位")] - public void Storage(string spaceNum = "101") + public async Task Storage(string spaceNum = "101") { - if (PrakingDevice.Trigger(ParkingCommand.GetPparkingSpace, spaceNum)) + if (await PrakingDevice.InvokeTriggerAsync(ParkingCommand.GetPparkingSpace, spaceNum)) { Console.WriteLine("发送命令成功:调取车位" + spaceNum); diff --git a/Net462DllTest/LogicControl/PlcLogicControl.cs b/Net462DllTest/LogicControl/PlcLogicControl.cs index 976c1f1..6573bd9 100644 --- a/Net462DllTest/LogicControl/PlcLogicControl.cs +++ b/Net462DllTest/LogicControl/PlcLogicControl.cs @@ -38,7 +38,7 @@ namespace Net462DllTest.LogicControl public void Exit(IDynamicContext context) { MyPlc.Close(); - MyPlc.CancelAllTasks(); + MyPlc.CancelAllTrigger(); } #endregion @@ -50,7 +50,7 @@ namespace Net462DllTest.LogicControl { try { - var triggerData = await MyPlc.CreateTaskAsync(varName); + var triggerData = await MyPlc.WaitTriggerAsync(varName); await Console.Out.WriteLineAsync($"PLC变量触发器[{varName}]传递数据:{triggerData}"); return new FlipflopContext(FlipflopStateType.Succeed, triggerData); } diff --git a/Net462DllTest/LogicControl/ViewLogicControl.cs b/Net462DllTest/LogicControl/ViewLogicControl.cs index 51a7a68..8819845 100644 --- a/Net462DllTest/LogicControl/ViewLogicControl.cs +++ b/Net462DllTest/LogicControl/ViewLogicControl.cs @@ -31,15 +31,14 @@ namespace Net462DllTest.LogicControl [NodeAction(NodeType.Flipflop, "等待视图命令")] public async Task> WaitTask(CommandSignal command) { - (var type, var result) = await ViewManagement.CreateTaskWithTimeoutAsync(command, TimeSpan.FromHours(10), 0); - if (type == TriggerType.Overtime) + var result = await ViewManagement.WaitTriggerWithTimeoutAsync(command, TimeSpan.FromHours(10)); + if (result.Type == TriggerDescription.Overtime) { - return new FlipflopContext(FlipflopStateType.Cancel, result); + return new FlipflopContext(FlipflopStateType.Cancel, result.Value); } else { - - return new FlipflopContext(FlipflopStateType.Succeed, result); + return new FlipflopContext(FlipflopStateType.Succeed, result.Value); } } diff --git a/Net462DllTest/Trigger/PrakingDevice.cs b/Net462DllTest/Trigger/PrakingDevice.cs index 99c31f7..c4ab80c 100644 --- a/Net462DllTest/Trigger/PrakingDevice.cs +++ b/Net462DllTest/Trigger/PrakingDevice.cs @@ -4,7 +4,7 @@ using Serein.Library; namespace Net462DllTest.Trigger { [AutoRegister] - public class PrakingDevice : FlowTrigger + public class PrakingDevice : TaskFlowTrigger { } diff --git a/Net462DllTest/Trigger/SiemensPlcDevice.cs b/Net462DllTest/Trigger/SiemensPlcDevice.cs index d34ca5a..8d510e6 100644 --- a/Net462DllTest/Trigger/SiemensPlcDevice.cs +++ b/Net462DllTest/Trigger/SiemensPlcDevice.cs @@ -19,7 +19,7 @@ namespace Net462DllTest.Trigger [AutoRegister] - public class SiemensPlcDevice : FlowTrigger + public class SiemensPlcDevice : TaskFlowTrigger { public SiemensClient Client { get; set; } public SiemensVersion Version { get; set; } @@ -197,7 +197,7 @@ namespace Net462DllTest.Trigger if (isNotification) { Console.WriteLine($"VarName: {signal}\t\tOld Data: {oldData}\tNew Data: {newData}"); - Trigger(signal, newData); + await InvokeTriggerAsync(signal, newData); } @@ -238,7 +238,7 @@ namespace Net462DllTest.Trigger { return VarInfoDict[plcVarEnum]; } - var plcValue = EnumHelper.GetBoundValue(plcVarEnum, attr => attr.Info) + var plcValue = EnumHelper.GetAttributeValue(plcVarEnum, attr => attr.Info) ?? throw new Exception($"获取变量异常:{plcVarEnum},没有标记PlcValueAttribute"); if (string.IsNullOrEmpty(plcValue.Address)) { diff --git a/Net462DllTest/Trigger/ViewManagement.cs b/Net462DllTest/Trigger/ViewManagement.cs index cbd6b79..5238d7d 100644 --- a/Net462DllTest/Trigger/ViewManagement.cs +++ b/Net462DllTest/Trigger/ViewManagement.cs @@ -16,7 +16,7 @@ namespace Net462DllTest.Trigger /// 视图管理 /// [AutoRegister] - public class ViewManagement : FlowTrigger + public class ViewManagement : TaskFlowTrigger { private readonly UIContextOperation uiContextOperation; public ViewManagement(UIContextOperation uiContextOperation) diff --git a/Net462DllTest/ViewModel/FromWorkBenchViewModel.cs b/Net462DllTest/ViewModel/FromWorkBenchViewModel.cs index 2c080ea..5c86dd5 100644 --- a/Net462DllTest/ViewModel/FromWorkBenchViewModel.cs +++ b/Net462DllTest/ViewModel/FromWorkBenchViewModel.cs @@ -119,7 +119,7 @@ namespace Net462DllTest.ViewModel }); CommandGetParkingSpace = new RelayCommand((p) => { - viewManagement.Trigger(SelectedSignal, SpcaeNumber); + _ = viewManagement.InvokeTriggerAsync(SelectedSignal, SpcaeNumber); }); CommandCloseForm = new RelayCommand((p) => { diff --git a/Net462DllTest/Web/FlowController.cs b/Net462DllTest/Web/FlowController.cs index 51d0e65..1d79bdc 100644 --- a/Net462DllTest/Web/FlowController.cs +++ b/Net462DllTest/Web/FlowController.cs @@ -38,7 +38,7 @@ namespace Net462DllTest.Web if (EnumHelper.TryConvertEnum(var, out var signal)) { SereinEnv.WriteLine(InfoType.INFO, $"外部触发 {signal} 信号,信号内容 : {value} "); - plcDevice.Trigger(signal, value);// 通过 Web Api 模拟外部输入信号 + _ = plcDevice.InvokeTriggerAsync(signal, value);// 通过 Web Api 模拟外部输入信号 return new { state = "succeed" }; } else @@ -63,7 +63,7 @@ namespace Net462DllTest.Web if (EnumHelper.TryConvertEnum(command, out var signal)) { SereinEnv.WriteLine(InfoType.INFO, $"外部触发 {signal} 信号,信号内容 : {value} "); - viewManagement.Trigger(signal, value);// 通过 Web Api 模拟外部输入信号 + _ = viewManagement.InvokeTriggerAsync(signal, value);// 通过 Web Api 模拟外部输入信号 return new { state = "succeed" }; } else diff --git a/Net462DllTest/Web/PlcSocketService.cs b/Net462DllTest/Web/PlcSocketService.cs index d0ef32b..bf00bc9 100644 --- a/Net462DllTest/Web/PlcSocketService.cs +++ b/Net462DllTest/Web/PlcSocketService.cs @@ -80,7 +80,7 @@ namespace Net462DllTest.Web socketServer?.Stop(); // 关闭 Web 服务 }); MyPlc.Close(); - MyPlc.CancelAllTasks(); + MyPlc.CancelAllTrigger(); } #endregion diff --git a/NodeFlow/Env/MsgControllerOfClient.cs b/NodeFlow/Env/MsgControllerOfClient.cs index 4da21e0..1831e77 100644 --- a/NodeFlow/Env/MsgControllerOfClient.cs +++ b/NodeFlow/Env/MsgControllerOfClient.cs @@ -67,7 +67,7 @@ namespace Serein.NodeFlow.Env // await Task.Delay(500); //}); await SendCommandAsync(msgId, theme, data); // 客户端发送消息 - return await remoteFlowEnvironment.WaitData(msgId); + return (await remoteFlowEnvironment.WaitTriggerAsync(msgId)).Value; } @@ -81,7 +81,7 @@ namespace Serein.NodeFlow.Env [AutoSocketHandle(ThemeValue = EnvMsgTheme.GetEnvInfo, IsReturnValue = false)] public void GetEnvInfo([UseMsgId] string msgId, [UseData] FlowEnvInfo flowEnvInfo) { - remoteFlowEnvironment.TriggerSignal(msgId, flowEnvInfo); + _ = remoteFlowEnvironment.InvokeTriggerAsync(msgId, flowEnvInfo); } @@ -93,19 +93,19 @@ namespace Serein.NodeFlow.Env [AutoSocketHandle(ThemeValue = EnvMsgTheme.GetProjectInfo, IsReturnValue = false)] public void GetProjectInfo([UseMsgId] string msgId, [UseData] SereinProjectData sereinProjectData) { - remoteFlowEnvironment.TriggerSignal(msgId, sereinProjectData); + _ = remoteFlowEnvironment.InvokeTriggerAsync(msgId, sereinProjectData); } [AutoSocketHandle(ThemeValue = EnvMsgTheme.SetNodeInterrupt, IsReturnValue = false)] public void SetNodeInterrupt([UseMsgId] string msgId) { - remoteFlowEnvironment.TriggerSignal(msgId, null); + _ = remoteFlowEnvironment.InvokeTriggerAsync(msgId, null); } [AutoSocketHandle(ThemeValue = EnvMsgTheme.AddInterruptExpression, IsReturnValue = false)] public void AddInterruptExpression([UseMsgId] string msgId) { - remoteFlowEnvironment.TriggerSignal(msgId, null); + _ = remoteFlowEnvironment.InvokeTriggerAsync(msgId, null); } @@ -113,37 +113,37 @@ namespace Serein.NodeFlow.Env [AutoSocketHandle(ThemeValue = EnvMsgTheme.CreateNode, IsReturnValue = false)] public void CreateNode([UseMsgId] string msgId, [UseData] NodeInfo nodeInfo) { - remoteFlowEnvironment.TriggerSignal(msgId, nodeInfo); + _ = remoteFlowEnvironment.InvokeTriggerAsync(msgId, nodeInfo); } [AutoSocketHandle(ThemeValue = EnvMsgTheme.RemoveNode, IsReturnValue = false)] public void RemoveNode([UseMsgId] string msgId, bool state) { - remoteFlowEnvironment.TriggerSignal(msgId, state); + _ = remoteFlowEnvironment.InvokeTriggerAsync(msgId, state); } [AutoSocketHandle(ThemeValue = EnvMsgTheme.ConnectInvokeNode, IsReturnValue = false)] public void ConnectInvokeNode([UseMsgId] string msgId, bool state) { - remoteFlowEnvironment.TriggerSignal(msgId, state); + _ = remoteFlowEnvironment.InvokeTriggerAsync(msgId, state); } [AutoSocketHandle(ThemeValue = EnvMsgTheme.RemoveInvokeConnect, IsReturnValue = false)] public void RemoveInvokeConnect([UseMsgId] string msgId, bool state) { - remoteFlowEnvironment.TriggerSignal(msgId, state); + _ = remoteFlowEnvironment.InvokeTriggerAsync(msgId, state); } [AutoSocketHandle(ThemeValue = EnvMsgTheme.ConnectArgSourceNode, IsReturnValue = false)] public void ConnectArgSourceNode([UseMsgId] string msgId, bool state) { - remoteFlowEnvironment.TriggerSignal(msgId, state); + _ = remoteFlowEnvironment.InvokeTriggerAsync(msgId, state); } [AutoSocketHandle(ThemeValue = EnvMsgTheme.RemoveArgSourceConnect, IsReturnValue = false)] public void RemoveArgSourceConnect([UseMsgId] string msgId, bool state) { - remoteFlowEnvironment.TriggerSignal(msgId, state); + _ = remoteFlowEnvironment.InvokeTriggerAsync(msgId, state); } diff --git a/NodeFlow/Env/RemoteFlowEnvironment.cs b/NodeFlow/Env/RemoteFlowEnvironment.cs index 95c3bc2..e30c7e3 100644 --- a/NodeFlow/Env/RemoteFlowEnvironment.cs +++ b/NodeFlow/Env/RemoteFlowEnvironment.cs @@ -14,7 +14,7 @@ namespace Serein.NodeFlow.Env /// /// 远程流程环境 /// - public class RemoteFlowEnvironment : ChannelFlowTrigger, IFlowEnvironment + public class RemoteFlowEnvironment : ChannelFlowTrigger, IFlowEnvironment { /// /// 连接到远程环境后切换到的环境接口实现 diff --git a/NodeFlow/Env/FlowFunc.cs b/NodeFlow/FlowFunc.cs similarity index 79% rename from NodeFlow/Env/FlowFunc.cs rename to NodeFlow/FlowFunc.cs index 9c61896..a17be7c 100644 --- a/NodeFlow/Env/FlowFunc.cs +++ b/NodeFlow/FlowFunc.cs @@ -3,8 +3,10 @@ using Serein.Library.Api; using Serein.Library.Utils; using Serein.NodeFlow.Model; using System.Collections.Concurrent; +using System.ComponentModel; +using System.Reflection; -namespace Serein.NodeFlow.Env +namespace Serein.NodeFlow { /// @@ -12,18 +14,14 @@ namespace Serein.NodeFlow.Env /// public static class FlowFunc { - - /// /// 判断是否为基础节点 /// /// public static bool IsBaseNode(this NodeControlType nodeControlType) { - if(nodeControlType == NodeControlType.ExpCondition - || nodeControlType == NodeControlType.ExpOp - || nodeControlType == NodeControlType.GlobalData - || nodeControlType == NodeControlType.Script) + var nodeDesc = EnumHelper.GetAttribute(nodeControlType); + if("base".Equals(nodeDesc?.Description, StringComparison.OrdinalIgnoreCase)) { return true; } @@ -81,23 +79,27 @@ namespace Serein.NodeFlow.Env /// public static NodeControlType GetNodeControlType(NodeInfo nodeInfo) { - // 创建控件实例 - NodeControlType controlType = nodeInfo.Type switch + if(!EnumHelper.TryConvertEnum(nodeInfo.Type, out var controlType)) { - $"{NodeStaticConfig.NodeSpaceName}.{nameof(SingleActionNode)}" => NodeControlType.Action,// 动作节点控件 - $"{NodeStaticConfig.NodeSpaceName}.{nameof(SingleFlipflopNode)}" => NodeControlType.Flipflop, // 触发器节点控件 - - $"{NodeStaticConfig.NodeSpaceName}.{nameof(SingleConditionNode)}" => NodeControlType.ExpCondition,// 条件表达式控件 - $"{NodeStaticConfig.NodeSpaceName}.{nameof(SingleExpOpNode)}" => NodeControlType.ExpOp, // 操作表达式控件 - - $"{NodeStaticConfig.NodeSpaceName}.{nameof(CompositeConditionNode)}" => NodeControlType.ConditionRegion, // 条件区域控件 - - $"{NodeStaticConfig.NodeSpaceName}.{nameof(SingleGlobalDataNode)}" => NodeControlType.GlobalData, // 数据节点 - $"{NodeStaticConfig.NodeSpaceName}.{nameof(SingleScriptNode)}" => NodeControlType.Script, // 数据节点 - _ => NodeControlType.None, - }; - + return NodeControlType.None; + } return controlType; + // 创建控件实例 + //NodeControlType controlType = nodeInfo.Type switch + //{ + // $"{NodeStaticConfig.NodeSpaceName}.{nameof(SingleActionNode)}" => NodeControlType.Action,// 动作节点控件 + // $"{NodeStaticConfig.NodeSpaceName}.{nameof(SingleFlipflopNode)}" => NodeControlType.Flipflop, // 触发器节点控件 + + // $"{NodeStaticConfig.NodeSpaceName}.{nameof(SingleConditionNode)}" => NodeControlType.ExpCondition,// 条件表达式控件 + // $"{NodeStaticConfig.NodeSpaceName}.{nameof(SingleExpOpNode)}" => NodeControlType.ExpOp, // 操作表达式控件 + + // $"{NodeStaticConfig.NodeSpaceName}.{nameof(CompositeConditionNode)}" => NodeControlType.ConditionRegion, // 条件区域控件 + + // $"{NodeStaticConfig.NodeSpaceName}.{nameof(SingleGlobalDataNode)}" => NodeControlType.GlobalData, // 数据节点 + // $"{NodeStaticConfig.NodeSpaceName}.{nameof(SingleScriptNode)}" => NodeControlType.Script, // 数据节点 + // _ => NodeControlType.None, + //}; + //return controlType; } /// @@ -105,7 +107,7 @@ namespace Serein.NodeFlow.Env /// /// /// - public static NodeLibraryInfo ToLibrary(this Library.NodeLibraryInfo libraryInfo) + public static NodeLibraryInfo ToLibrary(this NodeLibraryInfo libraryInfo) { return new NodeLibraryInfo { diff --git a/NodeFlow/FlowStarter.cs b/NodeFlow/FlowStarter.cs index 8d101a9..6bcdc4c 100644 --- a/NodeFlow/FlowStarter.cs +++ b/NodeFlow/FlowStarter.cs @@ -4,7 +4,6 @@ using Serein.Library.Core; using Serein.Library.Network.WebSocketCommunication; using Serein.Library.Utils; using Serein.Library.Web; -using Serein.NodeFlow.Env; using Serein.NodeFlow.Model; using Serein.NodeFlow.Tool; using System.Collections.Concurrent; @@ -16,6 +15,7 @@ namespace Serein.NodeFlow /// public class FlowStarter { + /// /// 控制全局触发器的结束 /// diff --git a/NodeFlow/Model/SingleActionNode.cs b/NodeFlow/Model/SingleActionNode.cs index 22bdd1c..edb2031 100644 --- a/NodeFlow/Model/SingleActionNode.cs +++ b/NodeFlow/Model/SingleActionNode.cs @@ -14,7 +14,14 @@ namespace Serein.NodeFlow.Model } + /// + /// 执行方法 + /// + /// + /// + public override Task ExecutingAsync(IDynamicContext context) + { + return base.ExecutingAsync(context); + } } - - } diff --git a/NodeFlow/Model/SingleFlipflopNode.cs b/NodeFlow/Model/SingleFlipflopNode.cs index f16b6a1..76cc72d 100644 --- a/NodeFlow/Model/SingleFlipflopNode.cs +++ b/NodeFlow/Model/SingleFlipflopNode.cs @@ -1,7 +1,6 @@ using Serein.Library.Api; using Serein.Library; using Serein.Library.Utils; -using Serein.NodeFlow.Env; using static Serein.Library.Utils.ChannelFlowInterrupt; namespace Serein.NodeFlow.Model @@ -48,7 +47,9 @@ namespace Serein.NodeFlow.Model dynamic dynamicFlipflopContext = await dd.InvokeAsync(md.ActingInstance, args); FlipflopStateType flipflopStateType = dynamicFlipflopContext.State; context.NextOrientation = flipflopStateType.ToContentType(); - if (dynamicFlipflopContext.Type == TriggerType.Overtime) + + + if (dynamicFlipflopContext.Type == TriggerDescription.Overtime) { throw new FlipflopException(base.MethodDetails.MethodName + "触发器超时触发。Guid" + base.Guid); } diff --git a/NodeFlow/Model/SingleScriptNode.cs b/NodeFlow/Model/SingleScriptNode.cs index 1b06921..1da8d5c 100644 --- a/NodeFlow/Model/SingleScriptNode.cs +++ b/NodeFlow/Model/SingleScriptNode.cs @@ -15,6 +15,7 @@ using System.Xml.Linq; namespace Serein.NodeFlow.Model { + [NodeProperty(ValuePath = NodeValuePath.Node)] public partial class SingleScriptNode : NodeModelBase { @@ -58,7 +59,6 @@ namespace Serein.NodeFlow.Model } } - public override void OnCreating() { MethodInfo? method = this.GetType().GetMethod(nameof(GetFlowApi)); diff --git a/WorkBench/App.xaml.cs b/WorkBench/App.xaml.cs index 8528204..7505e2f 100644 --- a/WorkBench/App.xaml.cs +++ b/WorkBench/App.xaml.cs @@ -37,6 +37,7 @@ namespace Serein.Workbench string filePath; filePath = @"C:\Users\Az\source\repos\CLBanyunqiState\CLBanyunqiState\bin\Release\net8.0\PLCproject.dnf"; filePath = @"C:\Users\Az\source\repos\CLBanyunqiState\CLBanyunqiState\bin\Release\banyunqi\project.dnf"; + filePath = @"C:\Users\Az\source\repos\CLBanyunqiState\CLBanyunqiState\bin\debug\net8.0\project.dnf"; string content = System.IO.File.ReadAllText(filePath); // 读取整个文件内容 App.FlowProjectData = JsonConvert.DeserializeObject(content); App.FileDataPath = System.IO.Path.GetDirectoryName(filePath)!; // filePath;// diff --git a/WorkBench/MainWindow.xaml.cs b/WorkBench/MainWindow.xaml.cs index f97fbe2..85e0433 100644 --- a/WorkBench/MainWindow.xaml.cs +++ b/WorkBench/MainWindow.xaml.cs @@ -2917,8 +2917,6 @@ namespace Serein.Workbench //SereinEnv.WriteLine(InfoType.ERROR, $"粘贴节点时发生异常:{ex}"); } - - // SereinEnv.WriteLine(InfoType.INFO, $"剪贴板文本内容: {clipboardText}"); } else if (Clipboard.ContainsImage()) diff --git a/Workbench/Node/ViewModel/GlobalDataNodeControlViewModel.cs b/Workbench/Node/ViewModel/GlobalDataNodeControlViewModel.cs index 8e43a86..300fbbf 100644 --- a/Workbench/Node/ViewModel/GlobalDataNodeControlViewModel.cs +++ b/Workbench/Node/ViewModel/GlobalDataNodeControlViewModel.cs @@ -30,7 +30,7 @@ namespace Serein.Workbench.Node.ViewModel CommandCopyDataExp = new RelayCommand( o => { string exp = NodeModel.KeyName; - string copyValue = "@Data " + exp; + string copyValue = $"@Get #{exp}#"; Clipboard.SetDataObject(copyValue); }); }