Files
serein-flow/Extend.FlowRemoteManagement/SereinFlowRemoteControl.cs

168 lines
5.1 KiB
C#
Raw Normal View History


using Serein.Library;
using Serein.Library.Api;
using Serein.Library.Network.WebSocketCommunication;
using System.Security.Cryptography.X509Certificates;
2024-10-11 23:08:56 +08:00
using Serein.NodeFlow;
using Serein.Library.Core.NodeFlow;
using Serein.Library.Utils;
using Serein.FlowRemoteManagement.Model;
2024-10-13 19:36:45 +08:00
using System.Reflection;
using Serein.Library.FlowNode;
namespace SereinFlowRemoteManagement
{
2024-10-11 23:08:56 +08:00
/// <summary>
/// SereinFlow 远程控制模块
2024-10-11 23:08:56 +08:00
/// </summary>
[DynamicFlow]
[AutoRegister]
2024-10-11 23:08:56 +08:00
[AutoSocketModule(ThemeKey ="theme",DataKey ="data")]
public class SereinFlowRemoteControl : ISocketHandleModule
{
public int ServerPort { get; set; } = 7525;
#region
2024-10-11 23:08:56 +08:00
public Guid HandleGuid { get; } = new Guid();
private readonly IFlowEnvironment environment;
public SereinFlowRemoteControl(IFlowEnvironment environment)
{
this.environment = environment;
}
[NodeAction(NodeType.Init)]
public void Init(IDynamicContext context)
{
environment.IOC.Register<WebSocketServer>();
}
2024-10-10 20:52:19 +08:00
[NodeAction(NodeType.Loading)]
public async Task Loading(IDynamicContext context)
{
environment.IOC.Run<WebSocketServer>(async (socketServer) =>
{
2024-10-11 23:08:56 +08:00
socketServer.MsgHandleHelper.AddModule(this,
(ex, send) =>
{
send(new
{
code = 400,
ex = ex.Message
});
});
2024-10-13 19:36:45 +08:00
await Console.Out.WriteLineAsync("启动远程管理模块");
await socketServer.StartAsync($"http://*:{ServerPort}/");
});
SereinProjectData projectData = await environment.GetProjectInfoAsync();
}
2024-10-11 23:08:56 +08:00
#endregion
#region
/// <summary>
/// 连接到运行环境,获取当前的节点信息
/// </summary>
/// <param name="Send"></param>
/// <returns></returns>
[AutoSocketHandle]
public async Task<object?> ConnectWorkBench(Func<string, Task> Send)
{
await Send("尝试获取");
try
{
var envInfo = this.environment.GetEnvInfoAsync();
return envInfo;
}
catch (Exception ex)
{
await Send(ex.Message);
return null;
}
}
public void AddNode(string nodeType,string methodName,int x, int y)
{
if(x <= 0 || y <= 0)
{
throw new InvalidOperationException("坐标错误");
}
if (!EnumHelper.TryConvertEnum<NodeControlType>(nodeType, out var connectionType))
{
throw new InvalidOperationException("类型错误");
}
if (this.environment.TryGetMethodDetailsInfo(methodName,out var mdInfo))
{
this.environment.CreateNode(connectionType, new PositionOfUI(x, y), mdInfo); //
}
}
2024-10-11 23:08:56 +08:00
/// <summary>
2024-10-13 19:36:45 +08:00
/// 远程更改两个节点的连接关系
2024-10-11 23:08:56 +08:00
/// </summary>
/// <param name="nodeInfo"></param>
/// <param name="Send"></param>
/// <exception cref="InvalidOperationException"></exception>
[AutoSocketHandle(ThemeValue = "ConnectionChange")]
public void ChangeNodeConnection(ConnectionInfoData nodeInfo, Func<object, Task> Send)
{
if (string.IsNullOrEmpty(nodeInfo.FromNodeGuid) || string.IsNullOrEmpty(nodeInfo.ToNodeGuid))
{
throw new InvalidOperationException("Guid错误");
}
if (!EnumHelper.TryConvertEnum<ConnectionType>(nodeInfo.Type, out var connectionType))
{
throw new InvalidOperationException("类型错误");
}
if (nodeInfo.Op)
{
environment.ConnectNodeAsync(nodeInfo.FromNodeGuid, nodeInfo.ToNodeGuid, connectionType);
2024-10-11 23:08:56 +08:00
}
else
{
environment.RemoveConnect(nodeInfo.FromNodeGuid, nodeInfo.ToNodeGuid, connectionType);
2024-10-11 23:08:56 +08:00
}
}
2024-10-11 23:08:56 +08:00
/// <summary>
/// 远程调用某个节点
/// </summary>
[AutoSocketHandle(ThemeValue = "InvokeNode")]
2024-10-13 19:36:45 +08:00
public async Task InvokeNode(string nodeGuid, Func<object, Task> Send)
2024-10-11 23:08:56 +08:00
{
if (string.IsNullOrEmpty(nodeGuid))
{
throw new InvalidOperationException("Guid错误");
}
2024-10-13 19:36:45 +08:00
await environment.StartAsyncInSelectNode(nodeGuid);
2024-10-13 19:36:45 +08:00
await Send(new
2024-10-11 23:08:56 +08:00
{
2024-10-13 19:36:45 +08:00
state = 200,
tips = "执行完成",
});
2024-10-11 23:08:56 +08:00
}
/// <summary>
/// 获取项目配置文件信息
/// </summary>
[AutoSocketHandle(ThemeValue = "GetProjectInfo")]
public async Task<SereinProjectData> GetProjectInfo()
{
await Task.Delay(0);
return await environment.GetProjectInfoAsync();
2024-10-11 23:08:56 +08:00
}
2024-10-11 23:08:56 +08:00
#endregion
}
}