using Serein.Library; using Serein.Library.Api; using Serein.Library.FlowNode; using Serein.Library.Utils; using Serein.NodeFlow.Services; using System.Reflection; namespace Serein.NodeFlow.Env { /* SetExportDirectory(string directory) UseRemoteEdit(string wspath) IFlowEnvironment env = new (); env.LoadProject() List apiInfos = env.GetInterfaceInfo(); List enventInfos = env.GetEventInfo(); flowApiService = env.GetFlowApiService(); flowEventService = env.GetFlowEventService(); object result = flowApiService.Invoke("", params); TResult result = flowApiService.Invoke("", params); object result = await flowApiService.InvokeAsync("", params); TResult result = await flowApiService.InvokeAsync("", params); flowEventService.Monitor("", (e) => { object data = e.EventData; Debug.Writeline(e.EventName); }); */ /// /// 流程运行环境 /// public class FlowEnvironment : IFlowEnvironment { /// /// 流程运行环境构造函数 /// public FlowEnvironment() { ISereinIOC ioc = new SereinIOC(); ioc.Register(()=> ioc) // 注册IOC .Register(() => this) .Register() .Register() .Register() .Register() .Register() .Register() .Register() .Register() .Build(); // 默认使用本地环境 currentFlowEnvironment = ioc.Get(); currentFlowEnvironmentEvent = ioc.Get(); SereinEnv.SetEnv(currentFlowEnvironment); } /* /// /// 本地环境事件 /// private readonly IFlowEnvironmentEvent flowEnvironmentEvent; /// /// 远程环境事件 /// private IFlowEnvironmentEvent remoteFlowEnvironmentEvent; */ /// /// 管理当前环境 /// private IFlowEnvironment currentFlowEnvironment; /// /// 管理当前环境事件 /// private IFlowEnvironmentEvent currentFlowEnvironmentEvent; private int _loadingProjectFlag = 0; // 使用原子自增代替锁 /// /// 传入false时,将停止数据通知。传入true时, /// /// public void SetProjectLoadingFlag(bool value) { Interlocked.Exchange(ref _loadingProjectFlag, value ? 1 : 0); } /// /// 判断是否正在加载项目 /// /// public bool IsLoadingProject() { return Interlocked.CompareExchange(ref _loadingProjectFlag, 1, 1) == 1; } /// public IFlowEnvironment CurrentEnv => currentFlowEnvironment; /// public UIContextOperation UIContextOperation => currentFlowEnvironment.UIContextOperation; /// public IFlowEdit FlowEdit => currentFlowEnvironment.FlowEdit; /// public IFlowControl FlowControl => currentFlowEnvironment.FlowControl; /// public ISereinIOC IOC => currentFlowEnvironment.IOC; /// public IFlowEnvironmentEvent Event => currentFlowEnvironment.Event; /// public string EnvName => currentFlowEnvironment.EnvName; /// public string ProjectFileLocation => currentFlowEnvironment.EnvName; /// public bool IsGlobalInterrupt => currentFlowEnvironment.IsGlobalInterrupt; /// public bool IsControlRemoteEnv => currentFlowEnvironment.IsControlRemoteEnv; /// public InfoClass InfoClass { get => currentFlowEnvironment.InfoClass; set => currentFlowEnvironment.InfoClass = value; } /// public RunState FlowState { get => currentFlowEnvironment.FlowState; set => currentFlowEnvironment.FlowState = value; } /// public void ActivateFlipflopNode(string nodeGuid) { currentFlowEnvironment.FlowControl.ActivateFlipflopNode(nodeGuid); } /// public async Task<(bool, RemoteMsgUtil)> ConnectRemoteEnv(string addres, int port, string token) { // 连接成功,切换远程环境 (var isConnect, var remoteMsgUtil) = await currentFlowEnvironment.ConnectRemoteEnv(addres, port, token); if (isConnect) { /* remoteFlowEnvironment ??= new RemoteFlowEnvironment(remoteMsgUtil, this.Event, this.UIContextOperation); currentFlowEnvironment = remoteFlowEnvironment;*/ } return (isConnect, remoteMsgUtil); } /// public async Task ExitFlowAsync() { return await currentFlowEnvironment.FlowControl.ExitFlowAsync(); } /// public void ExitRemoteEnv() { currentFlowEnvironment.ExitRemoteEnv(); } /// public async Task GetEnvInfoAsync() { return await currentFlowEnvironment.GetEnvInfoAsync(); } /// public async Task GetProjectInfoAsync() { return await currentFlowEnvironment.GetProjectInfoAsync(); } /// public void LoadLibrary(string dllPath) { currentFlowEnvironment.LoadLibrary(dllPath); } /// public void SaveProject() { currentFlowEnvironment.SaveProject(); } /// public void LoadProject(string filePath) { //if (flowEnvInfo is null) return; SetProjectLoadingFlag(false); currentFlowEnvironment.LoadProject(filePath); SetProjectLoadingFlag(true); } /// public async Task LoadProjetAsync(string filePath) { //if (flowEnvInfo is null) return; SetProjectLoadingFlag(false); await currentFlowEnvironment.LoadProjetAsync(filePath); SetProjectLoadingFlag(true); } /// public void MonitorObjectNotification(string nodeGuid, object monitorData, MonitorObjectEventArgs.ObjSourceType sourceType) { currentFlowEnvironment.FlowControl.MonitorObjectNotification(nodeGuid, monitorData, sourceType); } /// public bool TryUnloadLibrary(string assemblyName) { return currentFlowEnvironment.TryUnloadLibrary(assemblyName); } /// /// 输出信息 /// /// 日志内容 /// 日志类别 /// 日志级别 public void WriteLine(InfoType type, string message, InfoClass @class = InfoClass.Trivial) { currentFlowEnvironment.WriteLine(type, message, @class); } #region MyRegion #if false public async Task AddInterruptExpressionAsync(string key, string expression) { return await currentFlowEnvironment.AddInterruptExpressionAsync(key, expression); } public async Task<(bool, string[])> CheckObjMonitorStateAsync(string key) { return await currentFlowEnvironment.CheckObjMonitorStateAsync(key); } public async Task GetOrCreateGlobalInterruptAsync() { return await currentFlowEnvironment.InterruptNode(); } public void SetMonitorObjState(string key, bool isMonitor) { currentFlowEnvironment.SetMonitorObjState(key, isMonitor); } public async Task SetNodeInterruptAsync(string nodeGuid, bool isInterrupt) { return await currentFlowEnvironment.SetNodeInterruptAsync(nodeGuid, isInterrupt); } #endif #endregion /// public async Task StartFlowAsync(string[] canvasGuids) { return await currentFlowEnvironment.FlowControl.StartFlowAsync(canvasGuids); } /// public async Task StartFlowAsync(string startNodeGuid) { return await currentFlowEnvironment.FlowControl.StartFlowAsync(startNodeGuid); } /// public async Task StartRemoteServerAsync(int port = 7525) { await currentFlowEnvironment.StartRemoteServerAsync(port); } /// public void StopRemoteServer() { currentFlowEnvironment.StopRemoteServer(); } /// public void TerminateFlipflopNode(string nodeGuid) { currentFlowEnvironment.FlowControl.TerminateFlipflopNode(nodeGuid); } /// public void TriggerInterrupt(string nodeGuid, string expression, InterruptTriggerEventArgs.InterruptTriggerType type) { currentFlowEnvironment.FlowControl.TriggerInterrupt(nodeGuid, expression, type); } /// public void SetUIContextOperation(UIContextOperation uiContextOperation) { currentFlowEnvironment.SetUIContextOperation(uiContextOperation); } /// public void UseExternalIOC(ISereinIOC ioc) { currentFlowEnvironment.FlowControl.UseExternalIOC(ioc); } /// public bool TryGetNodeModel(string nodeGuid, out IFlowNode nodeModel) { return currentFlowEnvironment.TryGetNodeModel(nodeGuid, out nodeModel); } /// public bool TryGetDelegateDetails(string libraryName, string methodName, out DelegateDetails del) { return currentFlowEnvironment.TryGetDelegateDetails(libraryName, methodName, out del); } /// public bool TryGetMethodDetailsInfo(string libraryName, string methodName, out MethodDetailsInfo mdInfo) { return currentFlowEnvironment.TryGetMethodDetailsInfo(libraryName, methodName, out mdInfo); } /// public async Task NotificationNodeValueChangeAsync(string nodeGuid, string path, object value) { if (!IsLoadingProject()) { return; } if (currentFlowEnvironment.IsControlRemoteEnv) { await currentFlowEnvironment.NotificationNodeValueChangeAsync(nodeGuid, path, value); } } #region 流程依赖类库的接口 /// public bool LoadNativeLibraryOfRuning(string file) { return currentFlowEnvironment.LoadNativeLibraryOfRuning(file); } /// public void LoadAllNativeLibraryOfRuning(string path, bool isRecurrence = true) { currentFlowEnvironment.LoadAllNativeLibraryOfRuning(path,isRecurrence); } #endregion } }