mirror of
https://gitee.com/langsisi_admin/serein-flow
synced 2026-04-04 15:26:34 +08:00
重写了触发器底层逻辑
This commit is contained in:
@@ -3,9 +3,24 @@ using Serein.Library.NodeFlow.Tool;
|
||||
|
||||
namespace Serein.Library.Api
|
||||
{
|
||||
public interface IFlipflopContext
|
||||
/// <summary>
|
||||
/// 触发器必须使用该接口作为返回值,同时必须用Task泛型表示,否则将不会进行等待触发。
|
||||
/// </summary>
|
||||
public interface IFlipflopContext<out TResult>
|
||||
{
|
||||
/// <summary>
|
||||
/// 触发器完成的状态(根据业务场景手动设置)
|
||||
/// </summary>
|
||||
FlipflopStateType State { get; set; }
|
||||
TriggerData TriggerData { get; set; }
|
||||
/// <summary>
|
||||
/// 触发传递的数据
|
||||
/// </summary>
|
||||
//TriggerData TriggerData { get; set; }
|
||||
|
||||
TriggerType Type { get; set; }
|
||||
/// <summary>
|
||||
/// 触发传递的数据
|
||||
/// </summary>
|
||||
TResult Value { get; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ namespace Serein.Library.Enums
|
||||
Exit,
|
||||
|
||||
/// <summary>
|
||||
/// 触发器(建议手动设置返回类型,方便视图直观)
|
||||
/// 触发器(返回值必须为Task<IFlipflopContext<TResult>>)
|
||||
/// </summary>
|
||||
Flipflop,
|
||||
/// <summary>
|
||||
|
||||
@@ -34,7 +34,7 @@ namespace Serein.Library.Web
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 自动注册控制器
|
||||
/// 标记该类为 Web Api 处理类
|
||||
/// </summary>
|
||||
public class AutoHostingAttribute : Attribute
|
||||
{
|
||||
|
||||
@@ -13,14 +13,14 @@ namespace Serein.Library.Network.WebSocketCommunication
|
||||
/// <para>使用方式:</para>
|
||||
/// <para>[AutoSocketModule(ThemeKey = "theme", DataKey = "data")]</para>
|
||||
/// <para>public class PlcSocketService : ISocketHandleModule</para>
|
||||
/// <para>类中方法示例:void AddUser(string name,age 35)</para>
|
||||
/// <para>类中方法示例:void AddUser(string name,int age)</para>
|
||||
/// <para>Json示例:{ "theme":"AddUser", //【ThemeKey】 </para>
|
||||
/// <para> "data": { // 【DataKey】 </para>
|
||||
/// <para> "name":"张三", </para>
|
||||
/// <para> "age":35, } } </para>
|
||||
/// <para>WebSocket中收到以上该Json时,通过ThemeKey获取到"AddUser",然后找到AddUser()方法,并将"data"作为数据对象,取出相应数据作为入参(args:"张三",35)进行调用(如果有)</para>
|
||||
/// <para>WebSocket中收到以上该Json时,通过ThemeKey获取到"AddUser",然后找到AddUser()方法</para>
|
||||
/// <para>然后根据方法入参名称,从data对应的json数据中取出"name""age"对应的数据作为入参进行调用。AddUser("张三",35)</para>
|
||||
/// <para></para>
|
||||
///
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public sealed class AutoSocketModuleAttribute : Attribute
|
||||
|
||||
@@ -24,7 +24,16 @@ namespace Serein.Library.Network.WebSocketCommunication
|
||||
{
|
||||
listener = new HttpListener();
|
||||
listener.Prefixes.Add(url);
|
||||
listener.Start();
|
||||
try
|
||||
{
|
||||
listener.Start();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await Console.Out.WriteLineAsync(ex.Message);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
while (true)
|
||||
{
|
||||
|
||||
@@ -104,10 +104,6 @@ namespace Serein.Library.Attributes
|
||||
/// </summary>
|
||||
public NodeType MethodDynamicType;
|
||||
/// <summary>
|
||||
/// 显示标注方法返回类型,不影响运行逻辑(用于表示触发器触发后返回的数据)
|
||||
/// </summary>
|
||||
public Type ReturnType;
|
||||
/// <summary>
|
||||
/// 暂无意义
|
||||
/// </summary>
|
||||
public string LockName;
|
||||
|
||||
209
Library/Utils/ConvertHelper.cs
Normal file
209
Library/Utils/ConvertHelper.cs
Normal file
@@ -0,0 +1,209 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Library.Utils
|
||||
{
|
||||
public static class ConvertHelper
|
||||
{
|
||||
public static TResult ToConvert<TResult>(this object data)
|
||||
{
|
||||
var type = typeof(TResult);
|
||||
if (data is null && type.IsValueType)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
return (TResult)data.ToConvert(type);
|
||||
|
||||
}
|
||||
public static object ToConvert(this object data,Type type)
|
||||
{
|
||||
if (type.IsValueType)
|
||||
{
|
||||
if (data == null)
|
||||
{
|
||||
return Activator.CreateInstance(type);
|
||||
}
|
||||
else
|
||||
{
|
||||
return ConvertHelper.ValueParse(type, data);
|
||||
}
|
||||
}
|
||||
return data;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static T ValueParse<T>(object value) where T : struct, IComparable<T>
|
||||
{
|
||||
string valueStr = value.ToString();
|
||||
return valueStr.ToValueData<T>() ;
|
||||
}
|
||||
|
||||
public static object ValueParse(Type type, object value)
|
||||
{
|
||||
string valueStr = value.ToString();
|
||||
return valueStr.ToValueData(type);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static T ToValueData<T>(this string valueStr) where T : struct, IComparable<T>
|
||||
{
|
||||
if (string.IsNullOrEmpty(valueStr))
|
||||
{
|
||||
return default(T);
|
||||
}
|
||||
var type = typeof(T);
|
||||
object result;
|
||||
if (type.IsEnum)
|
||||
{
|
||||
result = Enum.Parse(type, valueStr);
|
||||
}
|
||||
else if (type == typeof(bool))
|
||||
{
|
||||
result = bool.Parse(valueStr);
|
||||
}
|
||||
else if (type == typeof(float))
|
||||
{
|
||||
result = float.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(decimal))
|
||||
{
|
||||
result = decimal.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(double))
|
||||
{
|
||||
result = double.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(sbyte))
|
||||
{
|
||||
result = sbyte.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(byte))
|
||||
{
|
||||
result = byte.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(short))
|
||||
{
|
||||
result = short.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(ushort))
|
||||
{
|
||||
result = ushort.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(int))
|
||||
{
|
||||
result = int.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(uint))
|
||||
{
|
||||
result = uint.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(long))
|
||||
{
|
||||
result = long.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(ulong))
|
||||
{
|
||||
result = ulong.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
#if NET6_0 || NET7_0 || NET8_0
|
||||
else if (type == typeof(nint))
|
||||
{
|
||||
result = nint.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(nuint))
|
||||
{
|
||||
result = nuint.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
#endif
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("非预期值类型");
|
||||
}
|
||||
|
||||
return (T)result;
|
||||
}
|
||||
public static object ToValueData(this string valueStr, Type type)
|
||||
{
|
||||
if (string.IsNullOrEmpty(valueStr))
|
||||
{
|
||||
return Activator.CreateInstance(type);
|
||||
}
|
||||
object result;
|
||||
if (type.IsEnum)
|
||||
{
|
||||
result = Enum.Parse(type, valueStr);
|
||||
}
|
||||
else if (type == typeof(bool))
|
||||
{
|
||||
result = bool.Parse(valueStr);
|
||||
}
|
||||
else if (type == typeof(float))
|
||||
{
|
||||
result = float.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(decimal))
|
||||
{
|
||||
result = decimal.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(double))
|
||||
{
|
||||
result = double.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(sbyte))
|
||||
{
|
||||
result = sbyte.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(byte))
|
||||
{
|
||||
result = byte.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(short))
|
||||
{
|
||||
result = short.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(ushort))
|
||||
{
|
||||
result = ushort.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(int))
|
||||
{
|
||||
result = int.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(uint))
|
||||
{
|
||||
result = uint.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(long))
|
||||
{
|
||||
result = long.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(ulong))
|
||||
{
|
||||
result = ulong.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
#if NET6_0 || NET7_0 || NET8_0
|
||||
else if (type == typeof(nint))
|
||||
{
|
||||
result = nint.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(nuint))
|
||||
{
|
||||
result = nuint.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
#endif
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("非预期值类型");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -358,8 +358,8 @@ namespace Serein.Library.Utils
|
||||
);
|
||||
|
||||
// 创建 lambda 表达式
|
||||
var lambda = Expression.Lambda<Func<object, object[], Task<IFlipflopContext>>>(
|
||||
Expression.Convert(methodCall, typeof(Task<IFlipflopContext>)),
|
||||
var lambda = Expression.Lambda<Func<object, object[], Task<IFlipflopContext<object>>>>(
|
||||
Expression.Convert(methodCall, typeof(Task<IFlipflopContext<object>>)),
|
||||
instanceParam,
|
||||
argsParam
|
||||
);
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using Serein.Library.Utils;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Reactive.Linq;
|
||||
using System.Reactive.Subjects;
|
||||
using System.Threading;
|
||||
using System.Threading.Channels;
|
||||
@@ -22,28 +24,29 @@ namespace Serein.Library.NodeFlow.Tool
|
||||
Overtime
|
||||
}
|
||||
|
||||
public class TriggerData
|
||||
public class TriggerResult<T>
|
||||
{
|
||||
public TriggerType Type { get; set; }
|
||||
public object Value { get; set; }
|
||||
public T Value { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 信号触发器类,带有消息广播功能
|
||||
/// 信号触发器类,带有消息广播功能。
|
||||
/// 使用枚举作为标记,创建
|
||||
/// </summary>
|
||||
public class ChannelFlowTrigger<TSignal> where TSignal : struct, Enum
|
||||
public class FlowTrigger<TSignal> where TSignal : struct, Enum
|
||||
{
|
||||
// 使用并发字典管理每个信号对应的广播列表
|
||||
private readonly ConcurrentDictionary<TSignal, Subject<TriggerData>> _subscribers = new ConcurrentDictionary<TSignal, Subject<TriggerData>>();
|
||||
private readonly ConcurrentDictionary<TSignal, Subject<(TriggerType,object)>> _subscribers = new ConcurrentDictionary<TSignal, Subject<(TriggerType, object)>>();
|
||||
|
||||
/// <summary>
|
||||
/// 获取或创建指定信号的 Subject(消息广播者)
|
||||
/// </summary>
|
||||
/// <param name="signal">枚举信号标识符</param>
|
||||
/// <returns>对应的 Subject</returns>
|
||||
private Subject<TriggerData> GetOrCreateSubject(TSignal signal)
|
||||
private Subject<(TriggerType, object)> GetOrCreateSubject(TSignal signal)
|
||||
{
|
||||
return _subscribers.GetOrAdd(signal, _ => new Subject<TriggerData>());
|
||||
return _subscribers.GetOrAdd(signal, _ => new Subject<(TriggerType, object)>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -52,9 +55,10 @@ namespace Serein.Library.NodeFlow.Tool
|
||||
/// <param name="signal">枚举信号标识符</param>
|
||||
/// <param name="observer">订阅者</param>
|
||||
/// <returns>取消订阅的句柄</returns>
|
||||
public IDisposable Subscribe(TSignal signal, IObserver<TriggerData> observer)
|
||||
public IDisposable Subscribe(TSignal signal, IObserver<(TriggerType, object)> observer)
|
||||
{
|
||||
var subject = GetOrCreateSubject(signal);
|
||||
// (IObserver<(TriggerType, object)>)
|
||||
return subject.Subscribe(observer); // 返回取消订阅的句柄
|
||||
}
|
||||
|
||||
@@ -63,8 +67,8 @@ namespace Serein.Library.NodeFlow.Tool
|
||||
/// </summary>
|
||||
/// <param name="signal">枚举信号标识符</param>
|
||||
/// <param name="outTime">超时时间</param>
|
||||
/// <returns>等待任务</returns>
|
||||
public async Task<TriggerData> CreateChannelWithTimeoutAsync<TResult>(TSignal signal, TimeSpan outTime, TResult outValue)
|
||||
/// <returns>等待任务,返回值为:状态(超时触发,手动触发),数据(超时触发时会使用设置好的数据)</returns>
|
||||
public async Task<(TriggerType, TResult)> CreateTaskWithTimeoutAsync<TResult>(TSignal signal, TimeSpan outTime, TResult outValue)
|
||||
{
|
||||
var subject = GetOrCreateSubject(signal);
|
||||
var cts = new CancellationTokenSource();
|
||||
@@ -77,12 +81,7 @@ namespace Serein.Library.NodeFlow.Tool
|
||||
await Task.Delay(outTime, cts.Token);
|
||||
if (!cts.IsCancellationRequested) // 如果还没有被取消
|
||||
{
|
||||
TriggerData triggerData = new TriggerData()
|
||||
{
|
||||
Value = outValue,
|
||||
Type = TriggerType.Overtime,
|
||||
};
|
||||
subject.OnNext(triggerData); // 广播给所有订阅者
|
||||
subject.OnNext((TriggerType.Overtime, outValue)); // 广播给所有订阅者
|
||||
subject.OnCompleted(); // 通知订阅结束
|
||||
}
|
||||
}
|
||||
@@ -96,39 +95,54 @@ namespace Serein.Library.NodeFlow.Tool
|
||||
}
|
||||
}, cts.Token);
|
||||
|
||||
// 返回一个等待的任务
|
||||
return await WaitForSignalAsync(signal);
|
||||
var result = await WaitSignalAsync<TResult>(signal);// 返回一个可以超时触发的等待任务
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 创建等待任务,触发时通知所有订阅者
|
||||
/// </summary>
|
||||
/// <param name="signal">枚举信号标识符</param>
|
||||
/// <param name="outTime">超时时间</param>
|
||||
/// <returns>等待任务</returns>
|
||||
public async Task<TResult> CreateTaskAsync<TResult>(TSignal signal)
|
||||
{
|
||||
var subject = GetOrCreateSubject(signal);
|
||||
(_,var result) = await WaitSignalAsync<TResult>(signal);
|
||||
|
||||
return result;// 返回一个等待的任务
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 等待指定信号的触发
|
||||
/// </summary>
|
||||
/// <param name="signal">枚举信号标识符</param>
|
||||
/// <returns>等待任务</returns>
|
||||
public async Task<TriggerData> WaitForSignalAsync(TSignal signal)
|
||||
public async Task<(TriggerType, TResult)> WaitSignalAsync<TResult>(TSignal signal)
|
||||
{
|
||||
var taskCompletionSource = new TaskCompletionSource<TriggerData>();
|
||||
var subscription = Subscribe(signal, new Observer<TriggerData>(taskCompletionSource.SetResult));
|
||||
var result = await taskCompletionSource.Task;
|
||||
var taskCompletionSource = new TaskCompletionSource<(TriggerType, object)>();
|
||||
var subscription = Subscribe(signal, new Observer<(TriggerType, object)>(taskCompletionSource.SetResult));
|
||||
(var type,var result) = await taskCompletionSource.Task;
|
||||
subscription.Dispose(); // 取消订阅
|
||||
return result;
|
||||
|
||||
return (type, result.ToConvert<TResult>());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 手动触发信号,并广播给所有订阅者
|
||||
/// </summary>
|
||||
/// <param name="signal">枚举信号标识符</param>
|
||||
/// <returns>是否成功触发</returns>
|
||||
public bool TriggerSignal<TResult>(TSignal signal, TResult value)
|
||||
public bool Trigger<TResult>(TSignal signal, TResult value)
|
||||
{
|
||||
if (_subscribers.TryGetValue(signal, out var subject))
|
||||
{
|
||||
TriggerData triggerData = new TriggerData()
|
||||
{
|
||||
Value = value,
|
||||
Type = TriggerType.External,
|
||||
};
|
||||
subject.OnNext(triggerData); // 广播给所有订阅者
|
||||
subject.OnNext((TriggerType.External, value)); // 广播给所有订阅者
|
||||
//subject.OnCompleted(); // 通知订阅结束
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user