using Serein.Library.FlowNode;
using Serein.Library.Utils;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using static Serein.Library.Utils.ChannelFlowInterrupt;
namespace Serein.Library.Api
{
#region 环境委托
///
/// 流程运行完成
///
///
public delegate void FlowRunCompleteHandler(FlowEventArgs eventArgs);
///
/// 项目加载完成
///
public delegate void ProjectLoadedHandler(ProjectLoadedEventArgs eventArgs);
///
/// 项目准备保存
///
///
public delegate void ProjectSavingHandler(ProjectSavingEventArgs eventArgs);
///
/// 加载项目文件时成功加载了DLL文件
///
public delegate void LoadDllHandler(LoadDllEventArgs eventArgs);
///
/// 移除了加载的dll
///
///
public delegate void RemoteDllHandler(RemoteDllEventArgs eventArgs);
///
/// 运行环境节点连接发生了改变
///
///
public delegate void NodeConnectChangeHandler(NodeConnectChangeEventArgs eventArgs);
///
/// 环境中加载了一个节点
///
///
public delegate void NodeCreateHandler(NodeCreateEventArgs eventArgs);
///
/// 环境中流程起始节点发生了改变
///
///
public delegate void StartNodeChangeHandler(StartNodeChangeEventArgs eventArgs);
///
/// 被监视的对象改变事件
///
///
public delegate void MonitorObjectChangeHandler(MonitorObjectEventArgs eventArgs);
///
/// 节点中断状态改变事件(开启了中断/取消了中断)
///
///
public delegate void NodeInterruptStateChangeHandler(NodeInterruptStateChangeEventArgs eventArgs);
///
/// 节点触发中断事件
///
///
public delegate void ExpInterruptTriggerHandler(InterruptTriggerEventArgs eventArgs);
///
/// IOC容器发生变化
///
public delegate void IOCMembersChangedHandler(IOCMembersChangedEventArgs eventArgs);
///
/// 节点需要定位
///
///
public delegate void NodeLocatedHandler(NodeLocatedEventArgs eventArgs);
///
/// 节点移动了(远程插件)
///
///
public delegate void NodeMovedHandler(NodeMovedEventArgs eventArgs);
///
/// 远程环境内容输出
///
/// 输出的日志类别
/// 输出的文本信息
public delegate void EnvOutHandler(InfoType type, string value);
#endregion
#region 环境事件签名
///
/// 流程事件签名基类
///
public class FlowEventArgs : EventArgs
{
///
/// 是否完成
///
public bool IsSucceed { get; protected set; } = true;
///
/// 错误提示
///
public string ErrorTips { get; protected set; } = string.Empty;
}
//public class LoadNodeEventArgs : FlowEventArgs
//{
// public LoadNodeEventArgs(NodeInfo NodeInfo, MethodDetails MethodDetailss)
// {
// this.NodeInfo = NodeInfo;
// this.MethodDetailss = MethodDetailss;
// }
// ///
// /// 项目文件节点信息参数
// ///
// public NodeInfo NodeInfo { get; protected set; }
// ///
// /// 已加载在环境中的方法描述
// ///
// public MethodDetails MethodDetailss { get; protected set; }
//}
public class ProjectLoadedEventArgs : FlowEventArgs
{
public ProjectLoadedEventArgs()
{
}
}
public class ProjectSavingEventArgs : FlowEventArgs
{
public ProjectSavingEventArgs()
{
}
}
public class LoadDllEventArgs : FlowEventArgs
{
public LoadDllEventArgs(NodeLibraryInfo nodeLibraryInfo, List MethodDetailss)
{
this.NodeLibraryInfo = nodeLibraryInfo;
this.MethodDetailss = MethodDetailss;
}
///
/// 已加载了的程序集
///
public NodeLibraryInfo NodeLibraryInfo { get; protected set; }
///
/// dll文件中有效的流程方法描述
///
public List MethodDetailss { get; protected set; }
}
public class RemoteDllEventArgs : FlowEventArgs
{
public RemoteDllEventArgs()
{
}
}
public class NodeConnectChangeEventArgs : FlowEventArgs
{
///
/// 连接关系改变类型
///
public enum ConnectChangeType
{
///
/// 创建
///
Create,
///
/// 移除
///
Remote,
}
///
/// 更改方法调用关系
///
///
///
///
///
///
public NodeConnectChangeEventArgs(string fromNodeGuid,
string toNodeGuid,
JunctionOfConnectionType junctionOfConnectionType, // 指示需要创建什么类型的连接线
ConnectionInvokeType connectionInvokeType, // 节点调用的方法类型(true/false/error/cancel )
ConnectChangeType changeType) // 需要创建连接线还是删除连接线
{
this.FromNodeGuid = fromNodeGuid;
this.ToNodeGuid = toNodeGuid;
this.ConnectionInvokeType = connectionInvokeType;
this.ChangeType = changeType;
this.JunctionOfConnectionType = junctionOfConnectionType;
}
///
/// 更改参数传递关系
///
///
///
///
///
///
///
public NodeConnectChangeEventArgs(string fromNodeGuid,
string toNodeGuid,
JunctionOfConnectionType junctionOfConnectionType, // 指示需要创建什么类型的连接线
int argIndex,
ConnectionArgSourceType connectionArgSourceType, // 节点对应的方法入参所需参数来源
ConnectChangeType changeType) // 需要创建连接线还是删除连接线
{
this.FromNodeGuid = fromNodeGuid;
this.ToNodeGuid = toNodeGuid;
this.ChangeType = changeType;
this.ArgIndex = argIndex;
this.ConnectionArgSourceType = connectionArgSourceType;
this.JunctionOfConnectionType = junctionOfConnectionType;
}
///
/// 连接关系中始节点的Guid
///
public string FromNodeGuid { get; protected set; }
///
/// 连接关系中目标节点的Guid
///
public string ToNodeGuid { get; protected set; }
///
/// 连接类型
///
public ConnectionInvokeType ConnectionInvokeType { get; protected set; }
///
/// 表示此次需要在两个节点之间创建连接关系,或是移除连接关系
///
public ConnectChangeType ChangeType { get; protected set; }
///
/// 指示需要创建什么类型的连接线
///
public JunctionOfConnectionType JunctionOfConnectionType { get; protected set; }
///
/// 节点对应的方法入参所需参数来源
///
public ConnectionArgSourceType ConnectionArgSourceType { get; protected set; }
///
/// 第几个参数
///
public int ArgIndex { get; protected set; }
}
public class NodeCreateEventArgs : FlowEventArgs
{
///
/// 节点添加事件参数
///
/// 节点对象
/// 位置
public NodeCreateEventArgs(object nodeModel, PositionOfUI position)
{
this.NodeModel = nodeModel;
this.Position = position;
}
///
/// 区域子项节点添加事件参数
///
/// 节点对象
/// 是否添加在区域中
/// 区域Guid
public NodeCreateEventArgs(object nodeModel, bool isAddInRegion, string regeionGuid)
{
this.NodeModel = nodeModel;
this.RegeionGuid = regeionGuid;
this.IsAddInRegion = isAddInRegion;
}
///
/// 节点Model对象,目前需要手动转换对应的类型
///
public object NodeModel { get; private set; }
public PositionOfUI Position { get; private set; }
public bool IsAddInRegion { get; private set; }
public string RegeionGuid { get; private set; }
}
///
/// 环境中移除了一个节点
///
///
public delegate void NodeRemoveHandler(NodeRemoveEventArgs eventArgs);
public class NodeRemoveEventArgs : FlowEventArgs
{
public NodeRemoveEventArgs(string nodeGuid)
{
this.NodeGuid = nodeGuid;
}
///
/// 被移除节点的Guid
///
public string NodeGuid { get; private set; }
}
public class StartNodeChangeEventArgs : FlowEventArgs
{
public StartNodeChangeEventArgs(string oldNodeGuid, string newNodeGuid)
{
this.OldNodeGuid = oldNodeGuid;
this.NewNodeGuid = newNodeGuid; ;
}
///
/// 原来的起始节点Guid
///
public string OldNodeGuid { get; private set; }
///
/// 新的起始节点Guid
///
public string NewNodeGuid { get; private set; }
}
///
/// 监视的节点数据发生变化
///
public class MonitorObjectEventArgs : FlowEventArgs
{
///
/// 变化的数据类别
///
public enum ObjSourceType
{
///
/// 流程节点的数据
///
NodeFlowData,
///
/// IOC容器对象
///
IOCObj,
}
///
/// 在某个节点运行时,监听的数据发生了改变
///
///
///
///
public MonitorObjectEventArgs(string nodeGuid, object monitorData, ObjSourceType objSourceType)
{
NodeGuid = nodeGuid;
NewData = monitorData;
ObjSource = objSourceType;
}
///
/// 中断的节点Guid
///
public string NodeGuid { get; protected set; }
///
/// 监听对象类别
///
public ObjSourceType ObjSource { get; protected set; }
///
/// 新的数据
///
public object NewData { get; protected set; }
}
///
/// 节点中断状态改变事件参数
///
public class NodeInterruptStateChangeEventArgs : FlowEventArgs
{
public NodeInterruptStateChangeEventArgs(string nodeGuid,bool isInterrupt)
{
NodeGuid = nodeGuid;
// Class = @class;
IsInterrupt = isInterrupt;
}
///
/// 中断的节点Guid
///
public string NodeGuid { get; protected set; }
public bool IsInterrupt { get; protected set; }
// public InterruptClass Class { get; protected set; }
}
///
/// 节点触发了中断事件参数
///
public class InterruptTriggerEventArgs : FlowEventArgs
{
public enum InterruptTriggerType
{
///
/// 主动监视中断
///
Monitor,
///
/// 表达式中断
///
Exp,
///
/// 对象监视中断
///
Obj,
}
public InterruptTriggerEventArgs(string nodeGuid, string expression, InterruptTriggerType type)
{
this.NodeGuid = nodeGuid;
this.Expression = expression;
this.Type = type;
}
///
/// 中断的节点Guid
///
public string NodeGuid { get; protected set; }
public string Expression { get; protected set; }
public InterruptTriggerType Type { get; protected set; }
}
///
/// 流程事件签名基类
///
public class IOCMembersChangedEventArgs : FlowEventArgs
{
public enum EventType
{
///
/// 登记了类型
///
Registered,
///
/// 构建了类型
///
Completeuild,
}
public IOCMembersChangedEventArgs(string key, object instance)
{
this.Key = key;
this.Instance = instance;
}
public string Key { get; private set; }
public object Instance { get; private set; }
}
///
/// 节点需要定位
///
public class NodeLocatedEventArgs : FlowEventArgs
{
public NodeLocatedEventArgs(string nodeGuid)
{
NodeGuid = nodeGuid;
}
public string NodeGuid { get; private set; }
}
///
/// 节点移动了
///
public class NodeMovedEventArgs : FlowEventArgs
{
public NodeMovedEventArgs(string nodeGuid, double x, double y)
{
this.NodeGuid = nodeGuid;
this.X = x;
this.Y = y;
}
///
/// 节点唯一标识
///
public string NodeGuid { get; private set; }
///
/// 画布上的x坐标
///
public double X { get; private set; }
///
/// 画布上的y坐标
///
public double Y { get; private set; }
}
#endregion
///
/// 运行环境
///
public interface IFlowEnvironment
{
#region 属性
///
/// 单例模式IOC容器,内部维护了一个实例字典,默认使用类型的FullName作为Key,如果以“接口-实现类”的方式注册,那么将使用接口类型的FullName作为Key。
/// 当某个类型注册绑定成功后,将不会因为其它地方尝试注册相同类型的行为导致类型被重新创建。
///
ISereinIOC IOC { get; }
///
/// 环境名称
///
string EnvName { get; }
///
/// 是否全局中断
///
bool IsGlobalInterrupt { get; }
///
/// 表示是否正在控制远程
/// Local control remote env
///
bool IsControlRemoteEnv { get; }
///
/// 信息输出等级
///
InfoClass InfoClass { get; set; }
///
/// 流程运行状态
///
RunState FlowState { get; set; }
///
/// 全局触发器运行状态
///
RunState FlipFlopState { get; set; }
///
/// 表示当前环境
///
IFlowEnvironment CurrentEnv { get; }
///
/// 由运行环境提供的UI线程上下文操作,用于类库中需要在UI线程中操作视觉元素的场景
///
UIContextOperation UIContextOperation { get; }
#endregion
#region 事件
///
/// 加载Dll
///
event LoadDllHandler OnDllLoad;
///
/// 项目加载完成
///
event ProjectLoadedHandler OnProjectLoaded;
///
/// 项目准备保存
///
event ProjectSavingHandler OnProjectSaving;
///
/// 节点连接属性改变事件
///
event NodeConnectChangeHandler OnNodeConnectChange;
///
/// 节点创建事件
///
event NodeCreateHandler OnNodeCreate;
///
/// 移除节点事件
///
event NodeRemoveHandler OnNodeRemove;
///
/// 起始节点变化事件
///
event StartNodeChangeHandler OnStartNodeChange;
///
/// 流程运行完成事件
///
event FlowRunCompleteHandler OnFlowRunComplete;
///
/// 被监视的对象改变事件
///
event MonitorObjectChangeHandler OnMonitorObjectChange;
///
/// 节点中断状态变化事件
///
event NodeInterruptStateChangeHandler OnNodeInterruptStateChange;
///
/// 触发中断
///
event ExpInterruptTriggerHandler OnInterruptTrigger;
///
/// IOC容器发生改变
///
event IOCMembersChangedHandler OnIOCMembersChanged;
///
/// 节点需要定位
///
event NodeLocatedHandler OnNodeLocated;
///
/// 节点移动了(远程插件)
///
event NodeMovedHandler OnNodeMoved;
///
/// 运行环境输出
///
event EnvOutHandler OnEnvOut;
#endregion
#region 流程接口
///
/// 设置输出
///
//
//
///void SetConsoleOut(); // Action output, Action clearMsg
///
/// 输出信息
///
///
///
void WriteLine(InfoType type, string message, InfoClass @class = InfoClass.Trivial);
/////
///// 使用JSON处理库输出对象信息
/////
/////
//void WriteLineObjToJson(object obj);
///
/// 启动远程服务
///
Task StartRemoteServerAsync(int port = 7525);
///
/// 停止远程服务
///
void StopRemoteServer();
///
/// 加载项目文件
///
/// 包含项目信息的远程环境
///
void LoadProject(FlowEnvInfo flowEnvInfo, string filePath);
///
/// 保存项目
///
void SaveProject();
///
/// 获取当前项目信息
///
///
Task GetProjectInfoAsync();
///
/// 加载远程环境
///
/// 远程环境地址
/// 远程环境端口
/// 密码
Task<(bool, RemoteMsgUtil)> ConnectRemoteEnv(string addres,int port, string token);
///
/// 退出远程环境
///
void ExitRemoteEnv();
///
/// 从文件中加载Dll
///
///
void LoadLibrary(string dllPath);
///
/// 移除DLL
///
/// 程序集的名称
bool UnloadLibrary(string assemblyFullName);
///
/// 清理加载的DLL(待更改)
///
void ClearAll();
///
/// 开始运行
///
Task StartAsync();
///
/// 从选定的节点开始运行
///
///
///
Task StartAsyncInSelectNode(string startNodeGuid);
///
/// 立刻调用某个节点,并获取其返回值
///
/// 调用时的上下文
/// 节点Guid
///
Task