修改了无法添加基础节点的bug,增加WebSocket JSON ID字段,远程环境交互使用消息ID作为响应key。

This commit is contained in:
fengjiayi
2024-10-22 00:13:13 +08:00
parent 838158f446
commit 0a7e24d318
48 changed files with 1209 additions and 500 deletions

View File

@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Serein.NodeFlow.Env
namespace Serein.NodeFlow.Env
{
/// <summary>
/// 消息主题

View File

@@ -1,28 +1,15 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Bson;
using Newtonsoft.Json.Linq;
using Serein.Library;
using Serein.Library.Api;
using Serein.Library.Network.WebSocketCommunication;
using Serein.Library.Utils;
using Serein.Library.Utils.SereinExpression;
using Serein.NodeFlow.Model;
using Serein.NodeFlow.Tool;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Sockets;
using System.Numerics;
using System.Reflection;
using System.Security.Cryptography;
using System.Threading;
using System.Xml.Linq;
using static Serein.Library.Utils.ChannelFlowInterrupt;
using static Serein.NodeFlow.FlowStarter;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace Serein.NodeFlow.Env
{
@@ -41,6 +28,7 @@ namespace Serein.NodeFlow.Env
public const string SpaceName = $"{nameof(Serein)}.{nameof(NodeFlow)}.{nameof(Model)}";
public const string ThemeKey = "theme";
public const string DataKey = "data";
public const string MsgIdKey = "msgid";
/// <summary>
/// 流程运行环境
@@ -80,10 +68,6 @@ namespace Serein.NodeFlow.Env
/// <summary>
/// 打开远程管理
/// </summary>
@@ -112,6 +96,7 @@ namespace Serein.NodeFlow.Env
Console.WriteLine("结束远程管理异常:" + ex);
}
}
#endregion
#region
@@ -347,7 +332,6 @@ namespace Serein.NodeFlow.Env
}
/// <summary>
/// 异步运行
/// </summary>
@@ -566,8 +550,17 @@ namespace Serein.NodeFlow.Env
}
else
{
MethodDetails? methodDetails = null;
if (!string.IsNullOrEmpty(nodeInfo.MethodName))
{
MethodDetailss.TryGetValue(nodeInfo.MethodName, out methodDetails);// 加载项目时尝试获取方法信息
}
else
{
MethodDetailss.TryGetValue(nodeInfo.MethodName, out var methodDetails);// 加载项目时尝试获取方法信息
}
var nodeModel = FlowFunc.CreateNode(this, controlType, methodDetails); // 加载项目时创建节点
nodeModel.LoadInfo(nodeInfo); // 创建节点model
if (nodeModel is null)
@@ -575,6 +568,8 @@ namespace Serein.NodeFlow.Env
nodeInfo.Guid = string.Empty;
continue;
}
TryAddNode(nodeModel); // 加载项目时将节点加载到环境中
if (nodeInfo.ChildNodeGuids?.Length > 0)
{
@@ -662,9 +657,6 @@ namespace Serein.NodeFlow.Env
}
/// <summary>
/// 加载远程环境
/// </summary>
@@ -679,7 +671,17 @@ namespace Serein.NodeFlow.Env
return (false, null);
}
// 没有连接远程环境,可以重新连接
var remoteEnvControl = new RemoteEnvControl(addres, port, token);
var controlConfiguration = new RemoteEnvControl.ControlConfiguration
{
Addres = addres,
Port = port,
Token = token,
ThemeJsonKey = FlowEnvironment.ThemeKey,
MsgIdJsonKey = FlowEnvironment.MsgIdKey,
DataJsonKey = FlowEnvironment.DataKey,
};
var remoteEnvControl = new RemoteEnvControl(controlConfiguration);
var result = await remoteEnvControl.ConnectAsync();
if (!result)
{
@@ -805,7 +807,6 @@ namespace Serein.NodeFlow.Env
}
}
TryAddNode(nodeModel);
nodeModel.Position = position;
@@ -1199,30 +1200,11 @@ namespace Serein.NodeFlow.Env
}
NodeValueChangeLogger.Add((nodeGuid, path, value));
var setExp = $"@Set .{path} = {value}"; // 生成 set 表达式
SerinExpressionEvaluator.Evaluate(setExp, nodeModel, out _); // 更改对应的数据
//Console.WriteLine($"本地环境收到数据更改通知:{value}");
//var getExp = $"@Get .{path}";
////Console.WriteLine($"取值表达式:{getExp}");
SerinExpressionEvaluator.Evaluate(setExp, nodeModel, out _); // 更改对应的数据
//var getResult = SerinExpressionEvaluator.Evaluate(getExp, nodeModel, out _);
////Console.WriteLine($"原数据 :{getResult}");
//if (getResult.Equals(value))
//{
// Console.WriteLine("无须修改");
// return;
//}
//NodeValueChangeLogger.Add((nodeGuid, path, value));
//var setExp = $"@Set .{path} = {value}";
////Console.WriteLine($"设值表达式:{setExp}");
//SerinExpressionEvaluator.Evaluate(setExp, nodeModel, out _);
//getResult = SerinExpressionEvaluator.Evaluate(getExp, nodeModel, out _);
//Console.WriteLine($"新数据 :{getResult}");
//Console.WriteLine($"Set表达式{setExp},result : {getResult}");
}

View File

@@ -1,14 +1,6 @@
using Serein.Library;
using Serein.Library.Api;
using Serein.Library.Utils;
using Serein.Library.Web;
using Serein.NodeFlow.Tool;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Serein.NodeFlow.Env
{

View File

@@ -1,12 +1,6 @@
using Serein.Library;
using Serein.Library.Api;
using Serein.NodeFlow.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Serein.NodeFlow.Env
{

View File

@@ -1,12 +1,7 @@
using Newtonsoft.Json;
using Serein.Library;
using Serein.Library;
using Serein.Library.Network.WebSocketCommunication;
using Serein.Library.Network.WebSocketCommunication.Handle;
using Serein.Library.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Serein.NodeFlow.Env
{
@@ -17,33 +12,43 @@ namespace Serein.NodeFlow.Env
/// 客户端的消息管理(用于处理服务端的响应)
/// </summary>
[AutoSocketModule(ThemeKey = FlowEnvironment.ThemeKey, DataKey = FlowEnvironment.DataKey)]
[AutoSocketModule(ThemeKey = FlowEnvironment.ThemeKey,
DataKey = FlowEnvironment.DataKey,
MsgIdKey = FlowEnvironment.MsgIdKey)]
public class MsgControllerOfClient : ISocketHandleModule
{
public Guid HandleGuid => new Guid();
private readonly Func<string, object?, Task> SendCommandAsync;
// 消息主题data - task等待
private readonly Func<string, string, object?, Task> SendCommandFunc;
private readonly RemoteFlowEnvironment remoteFlowEnvironment;
public MsgControllerOfClient(RemoteFlowEnvironment remoteFlowEnvironment, Func<string, object?, Task> func)
public MsgControllerOfClient(RemoteFlowEnvironment remoteFlowEnvironment, Func<string, string, object?, Task> func)
{
this.remoteFlowEnvironment = remoteFlowEnvironment;
SendCommandAsync = func;
SendCommandFunc = func;
}
private async Task SendCommandAsync(string msgId, string theme, object? data)
{
await SendCommandFunc.Invoke(msgId, theme, data);
}
/// <summary>
/// 发送请求并等待远程环境响应
/// 发送请求
/// </summary>
/// <returns></returns>
/// <exception cref="NotImplementedException">超时触发</exception>
public async Task SendAsync(string signal, object? sendData = null, int overtimeInMs = 100)
public async Task SendAsync(string signal, object? data = null, int overtimeInMs = 100)
{
//Console.WriteLine($"指令[{signal}]value{JsonConvert.SerializeObject(sendData)}");
if (!DebounceHelper.CanExecute(signal, overtimeInMs))
{
return;
}
await SendCommandAsync.Invoke(signal, sendData);
var msgId = MsgIdHelper.GenerateId().ToString();
await SendCommandAsync(msgId, signal, data);
}
/// <summary>
@@ -51,11 +56,13 @@ namespace Serein.NodeFlow.Env
/// </summary>
/// <returns></returns>
/// <exception cref="NotImplementedException">超时触发</exception>
public async Task<TResult> SendAndWaitDataAsync<TResult>(string signal, object? sendData = null, int overtimeInMs = 50)
public async Task<TResult> SendAndWaitDataAsync<TResult>(string theme, object? data = null, int overtimeInMs = 50)
{
//Console.WriteLine($"指令[{signal}]value{JsonConvert.SerializeObject(sendData)}");
_ = SendCommandAsync.Invoke(signal, sendData);
return await remoteFlowEnvironment.WaitData<TResult>(signal);
var msgId = MsgIdHelper.GenerateId().ToString();
_ = SendCommandAsync(msgId, theme, data);
return await remoteFlowEnvironment.WaitData<TResult>(msgId);
//if (DebounceHelper.CanExecute(signal, overtimeInMs))
//{
@@ -86,63 +93,63 @@ namespace Serein.NodeFlow.Env
/// <summary>
/// 远程环境发来项目信息
/// </summary>
/// <param name="msgId"></param>
/// <param name="flowEnvInfo"></param>
[AutoSocketHandle(ThemeValue = EnvMsgTheme.GetEnvInfo)]
public void GetEnvInfo([UseMsgData] FlowEnvInfo flowEnvInfo)
public void GetEnvInfo([UseMsgId] string msgId, [UseData] FlowEnvInfo flowEnvInfo)
{
remoteFlowEnvironment.TriggerSignal(EnvMsgTheme.GetEnvInfo, flowEnvInfo);
remoteFlowEnvironment.TriggerSignal(msgId, flowEnvInfo);
}
/// <summary>
/// 远程环境发来项目信息
/// </summary>
/// <param name="msgId"></param>
/// <param name="sereinProjectData"></param>
[AutoSocketHandle(ThemeValue = EnvMsgTheme.GetProjectInfo)]
public void GetProjectInfo([UseMsgData] SereinProjectData sereinProjectData)
public void GetProjectInfo([UseMsgId] string msgId, [UseData] SereinProjectData sereinProjectData)
{
remoteFlowEnvironment.TriggerSignal(EnvMsgTheme.GetProjectInfo, sereinProjectData);
remoteFlowEnvironment.TriggerSignal(msgId, sereinProjectData);
}
[AutoSocketHandle(ThemeValue = EnvMsgTheme.SetNodeInterrupt)]
public void SetNodeInterrupt()
public void SetNodeInterrupt([UseMsgId] string msgId)
{
remoteFlowEnvironment.TriggerSignal(EnvMsgTheme.GetProjectInfo, null);
remoteFlowEnvironment.TriggerSignal(msgId, null);
}
[AutoSocketHandle(ThemeValue = EnvMsgTheme.AddInterruptExpression)]
public void AddInterruptExpression()
public void AddInterruptExpression([UseMsgId] string msgId)
{
remoteFlowEnvironment.TriggerSignal(EnvMsgTheme.AddInterruptExpression, null);
remoteFlowEnvironment.TriggerSignal(msgId, null);
}
[AutoSocketHandle(ThemeValue = EnvMsgTheme.CreateNode)]
public void CreateNode([UseMsgData] NodeInfo nodeInfo)
public void CreateNode([UseMsgId] string msgId, [UseData] NodeInfo nodeInfo)
{
remoteFlowEnvironment.TriggerSignal(EnvMsgTheme.CreateNode, nodeInfo);
remoteFlowEnvironment.TriggerSignal(msgId, nodeInfo);
}
[AutoSocketHandle(ThemeValue = EnvMsgTheme.RemoveNode)]
public void RemoveNode(bool state)
public void RemoveNode([UseMsgId] string msgId, bool state)
{
remoteFlowEnvironment.TriggerSignal(EnvMsgTheme.RemoveNode, state);
remoteFlowEnvironment.TriggerSignal(msgId, state);
}
[AutoSocketHandle(ThemeValue = EnvMsgTheme.ConnectNode)]
public void ConnectNode(bool state)
public void ConnectNode([UseMsgId] string msgId, bool state)
{
remoteFlowEnvironment.TriggerSignal(EnvMsgTheme.ConnectNode, state);
remoteFlowEnvironment.TriggerSignal(msgId, state);
}
[AutoSocketHandle(ThemeValue = EnvMsgTheme.RemoveConnect)]
public void RemoveConnect(bool state)
public void RemoveConnect([UseMsgId] string msgId, bool state)
{
remoteFlowEnvironment.TriggerSignal(EnvMsgTheme.RemoveConnect, state);
remoteFlowEnvironment.TriggerSignal(msgId, state);
}

View File

@@ -1,22 +1,18 @@
using Newtonsoft.Json.Linq;
using Serein.Library.Api;
using Serein.Library;
using Serein.Library.Api;
using Serein.Library.Network.WebSocketCommunication;
using Serein.Library.Network.WebSocketCommunication.Handle;
using Serein.Library.Utils;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Serein.NodeFlow.Env
{
/// <summary>
/// 服务端的消息管理(用于处理客户端的请求)
/// </summary>
[AutoSocketModule(ThemeKey = FlowEnvironment.ThemeKey, DataKey = FlowEnvironment.DataKey)]
[AutoSocketModule(ThemeKey = FlowEnvironment.ThemeKey,
DataKey = FlowEnvironment.DataKey,
MsgIdKey = FlowEnvironment.MsgIdKey)]
public class MsgControllerOfServer : ISocketHandleModule
{
/// <summary>
@@ -225,7 +221,7 @@ namespace Serein.NodeFlow.Env
/// <summary>
/// 获取当前环境信息(远程连接)
/// 获取当前环境信息
/// </summary>
/// <returns></returns>
[AutoSocketHandle(ThemeValue = EnvMsgTheme.GetEnvInfo)]
@@ -316,6 +312,7 @@ namespace Serein.NodeFlow.Env
var nodeInfo = await environment.CreateNodeAsync(nodeControlType, position, mdInfo); // 监听到客户端创建节点的请求
return nodeInfo;
}
/// <summary>
/// 从远程环境移除节点
/// </summary>
@@ -464,10 +461,6 @@ namespace Serein.NodeFlow.Env
}
}

View File

@@ -3,8 +3,6 @@ using Serein.Library.Api;
using Serein.Library.Utils;
using Serein.NodeFlow.Tool;
using System.Collections.Concurrent;
using System.Threading;
using System.Xml.Linq;
namespace Serein.NodeFlow.Env
{
@@ -104,9 +102,8 @@ namespace Serein.NodeFlow.Env
public void LoadProject(FlowEnvInfo flowEnvInfo, string filePath)
{
Console.WriteLine("远程环境尚未实现的接口LoadProject");
//Console.WriteLine("远程环境尚未实现的接口LoadProject");
// dll面板
var libmds = flowEnvInfo.LibraryMds;
foreach (var lib in libmds)
@@ -143,8 +140,11 @@ namespace Serein.NodeFlow.Env
else
{
MethodDetails? methodDetails;
MethodDetailss.TryGetValue(nodeInfo.MethodName, out methodDetails);// 尝试获取方法信息
MethodDetails? methodDetails = null;
if (!string.IsNullOrEmpty(nodeInfo.MethodName))
{
MethodDetailss.TryGetValue(nodeInfo.MethodName, out methodDetails);// 加载远程环境时尝试获取方法信息
}
var nodeModel = FlowFunc.CreateNode(this, controlType, methodDetails); // 加载远程项目时创建节点
nodeModel.LoadInfo(nodeInfo); // 创建节点model
@@ -160,7 +160,6 @@ namespace Serein.NodeFlow.Env
{
regionChildNodes.Add((nodeModel, nodeInfo.ChildNodeGuids));
UIContextOperation?.Invoke(() => OnNodeCreate?.Invoke(new NodeCreateEventArgs(nodeModel, nodeInfo.Position)));
//OnNodeCreate?.Invoke(new NodeCreateEventArgs(nodeModel, nodeInfo.Position));
}
else
{
@@ -181,7 +180,6 @@ namespace Serein.NodeFlow.Env
continue;
}
// 存在节点
//OnNodeCreate?.Invoke(new NodeCreateEventArgs(childNode, true, item.region.Guid));
UIContextOperation?.Invoke(() => OnNodeCreate?.Invoke(new NodeCreateEventArgs(childNode, true, item.region.Guid)));
}
}
@@ -252,7 +250,10 @@ namespace Serein.NodeFlow.Env
});
SetStartNode(projectData.StartNode);
OnProjectLoaded?.Invoke(new ProjectLoadedEventArgs());
UIContextOperation?.Invoke(() =>
{
OnProjectLoaded?.Invoke(new ProjectLoadedEventArgs());
});
}
private bool TryAddNode(NodeModelBase nodeModel)
@@ -394,7 +395,10 @@ namespace Serein.NodeFlow.Env
public void MoveNode(string nodeGuid, double x, double y)
{
OnNodeMoved.Invoke(new NodeMovedEventArgs(nodeGuid, x, y));
UIContextOperation.Invoke(() =>
{
OnNodeMoved.Invoke(new NodeMovedEventArgs(nodeGuid, x, y));
});
_ = msgClient.SendAsync(EnvMsgTheme.MoveNode,
new
{
@@ -404,13 +408,14 @@ namespace Serein.NodeFlow.Env
});
}
public void SetStartNode(string nodeGuid)
{
_ = msgClient.SendAsync(EnvMsgTheme.SetStartNode, new
{
nodeGuid
});
// UIContextOperation?.Invoke(() => OnStartNodeChange?.Invoke(new StartNodeChangeEventArgs(oldNodeGuid, StartNode.Guid)));
//UIContextOperation?.Invoke(() => OnStartNodeChange?.Invoke(new StartNodeChangeEventArgs(nodeGuid,nodeGuid)));
}
public async Task<bool> ConnectNodeAsync(string fromNodeGuid, string toNodeGuid, ConnectionType connectionType)
@@ -440,13 +445,22 @@ namespace Serein.NodeFlow.Env
mdInfo = methodDetailsInfo,
});
MethodDetailss.TryGetValue(methodDetailsInfo.MethodName, out var methodDetails);// 加载项目时尝试获取方法信息
MethodDetails? methodDetails = null;
if (!string.IsNullOrEmpty(nodeInfo.MethodName))
{
MethodDetailss.TryGetValue(nodeInfo.MethodName, out methodDetails);// 加载远程环境时尝试获取方法信息
}
//MethodDetailss.TryGetValue(methodDetailsInfo.MethodName, out var methodDetails);// 加载项目时尝试获取方法信息
var nodeModel = FlowFunc.CreateNode(this, nodeControlType, methodDetails); // 远程环境下加载节点
nodeModel.LoadInfo(nodeInfo);
TryAddNode(nodeModel);
// 通知UI更改
OnNodeCreate?.Invoke(new NodeCreateEventArgs(nodeModel, position));
UIContextOperation.Invoke(() =>
{
OnNodeCreate?.Invoke(new NodeCreateEventArgs(nodeModel, position));
});
return nodeInfo;
}
@@ -460,10 +474,13 @@ namespace Serein.NodeFlow.Env
});
if (result)
{
OnNodeConnectChange?.Invoke(new NodeConnectChangeEventArgs(fromNodeGuid,
UIContextOperation.Invoke(() =>
{
OnNodeConnectChange?.Invoke(new NodeConnectChangeEventArgs(fromNodeGuid,
toNodeGuid,
connectionType,
NodeConnectChangeEventArgs.ConnectChangeType.Remote));
});
}
return result;
}
@@ -476,7 +493,10 @@ namespace Serein.NodeFlow.Env
});
if (result)
{
OnNodeRemove?.Invoke(new NodeRemoveEventArgs(nodeGuid));
UIContextOperation.Invoke(() =>
{
OnNodeRemove?.Invoke(new NodeRemoveEventArgs(nodeGuid));
});
}
else
{