using Newtonsoft.Json.Linq;
using Serein.Library;
using Serein.Library.Api;
using Serein.Library.Network.WebSocketCommunication;
using Serein.Library.Network.WebSocketCommunication.Handle;
using Serein.Library.Utils;
namespace Serein.NodeFlow.Env
{
///
/// 服务端的消息管理(用于处理客户端的请求)
///
[AutoSocketModule(ThemeKey = FlowEnvironment.ThemeKey,
DataKey = FlowEnvironment.DataKey,
MsgIdKey = FlowEnvironment.MsgIdKey)]
public class MsgControllerOfServer : ISocketHandleModule
{
///
/// 受控环境
///
public IFlowEnvironment environment;
///
/// WebSocket处理
///
public Guid HandleGuid { get; } = new Guid();
///
/// 表示是否正在控制远程
/// Local control remote env
///
public bool IsLcR { get; set; }
///
/// 表示是否受到远程控制
/// Remote control local env
///
public bool IsRcL { get; set; }
///
/// 流程环境远程管理服务
///
private WebSocketServer FlowEnvRemoteWebSocket;
///
/// 启动不带Token验证的远程服务
///
public MsgControllerOfServer(IFlowEnvironment environment)
{
this.environment = environment;
FlowEnvRemoteWebSocket ??= new WebSocketServer();
}
///
/// 启动带token验证的远程服务
///
///
///
public MsgControllerOfServer(IFlowEnvironment environment, string token)
{
if (string.IsNullOrEmpty(token))
{
Console.WriteLine("当前没有设置token,但使用了token验证的服务端");
}
this.environment = environment;
FlowEnvRemoteWebSocket ??= new WebSocketServer(token, OnInspectionAuthorized);
}
#region 基本方法
///
/// 启动远程
///
///
///
public async Task StartRemoteServerAsync(int port = 7525)
{
FlowEnvRemoteWebSocket.MsgHandleHelper.AddModule(this,
(ex, send) =>
{
send(new
{
code = 400,
ex = ex.Message
});
});
var url = $"http://*:{port}/";
try
{
await FlowEnvRemoteWebSocket.StartAsync(url);
}
catch (Exception ex)
{
FlowEnvRemoteWebSocket.MsgHandleHelper.RemoveModule(this);
Console.WriteLine("打开远程管理异常:" + ex);
}
}
///
/// 结束远程管理
///
[AutoSocketHandle]
public void StopRemoteServer()
{
try
{
FlowEnvRemoteWebSocket.Stop();
}
catch (Exception ex)
{
Console.WriteLine("结束远程管理异常:" + ex);
}
}
///
/// 验证远程token
///
///
///
private async Task OnInspectionAuthorized(dynamic token)
{
if (IsLcR)
{
return false; // 正在远程控制远程环境时,禁止其它客户端远程控制
}
if (IsRcL)
{
return false; // 正在受到远程控制时,禁止其它客户端远程控制
}
await Task.Delay(0);
var tokenValue = token.ToString();
if ("123456".Equals(tokenValue))
{
// 同时切换远程环境
return true;
}
else
{
return false;
}
}
///
/// 获取发送消息的委托
///
///
private void OnResultSendMsgFunc(Func SendAsync)
{
// 从受控环境向主控环境发送消息。
Func func = async (theme, data) =>
{
JObject sendJson = new JObject
{
[FlowEnvironment.ThemeKey] = theme,
[FlowEnvironment.DataKey] = JObject.FromObject(data),
};
var msg = sendJson.ToString();
await SendAsync(msg);
};
// var remoteEnv = new RemoteFlowEnvironment(func); // 创建一个远程环境
// OnSwitchedEnvironment.Invoke(remoteEnv); // 通知前台切换到了远程环境
}
#endregion
///
/// 异步运行
///
///
[AutoSocketHandle(ThemeValue = EnvMsgTheme.StartFlow)]
private async Task StartAsync()
{
var uiContextOperation = environment.IOC.Get();
await environment.StartAsync();
}
///
/// 从远程环境运行选定的节点
///
///
///
[AutoSocketHandle(ThemeValue = EnvMsgTheme.StartFlowInSelectNode)]
private async Task StartAsyncInSelectNode(string nodeGuid)
{
await environment.StartAsyncInSelectNode(nodeGuid);
}
///
/// 结束流程
///
[AutoSocketHandle(ThemeValue = EnvMsgTheme.ExitFlow)]
private void ExitFlow()
{
environment.ExitFlow();
}
///
/// 激活全局触发器
///
///
[AutoSocketHandle(ThemeValue = EnvMsgTheme.ActivateFlipflopNode)]
private void ActivateFlipflopNode(string nodeGuid)
{
environment.ActivateFlipflopNode(nodeGuid);
}
///
/// 关闭全局触发器
///
///
[AutoSocketHandle(ThemeValue = EnvMsgTheme.TerminateFlipflopNode)]
private void TerminateFlipflopNode(string nodeGuid)
{
environment.TerminateFlipflopNode(nodeGuid);
}
///
/// 获取当前环境信息
///
///
[AutoSocketHandle(ThemeValue = EnvMsgTheme.GetEnvInfo)]
private async Task GetEnvInfoAsync()
{
var envInfo = await environment.GetEnvInfoAsync();
return envInfo;
}
///
/// 加载项目文件
///
/// 环境信息
// [AutoSocketHandle(ThemeValue = EnvMsgTheme.GetProjectInfo)]
private void LoadProject(FlowEnvInfo flowEnvInfo)
{
environment.LoadProject(flowEnvInfo, "");
}
///
/// 连接远程环境
///
/// 远程环境地址
/// 远程环境端口
/// 密码
// [AutoSocketHandle]
public async Task<(bool, RemoteMsgUtil)> ConnectRemoteEnv(string addres, int port, string token)
{
return await environment.ConnectRemoteEnv(addres, port, token);
}
///
/// 退出远程环境
///
// [AutoSocketHandle]
public void ExitRemoteEnv()
{
Console.WriteLine("暂未实现远程退出远程环境");
IsLcR = false;
}
///
/// 序列化当前项目的依赖信息、节点信息
///
///
[AutoSocketHandle(ThemeValue = EnvMsgTheme.GetProjectInfo)]
public async Task GetProjectInfoAsync()
{
return await environment.GetProjectInfoAsync();
}
///
/// 从文件路径中加载DLL
///
///
///
// [AutoSocketHandle(ThemeValue = EnvMsgTheme)]
public void LoadDll(string dllPath)
{
}
///
/// 移除DLL
///
///
///
// [AutoSocketHandle(ThemeValue = EnvMsgTheme)]
public bool RemoteDll(string assemblyFullName)
{
return false;
}
///
/// 从远程环境创建节点
///
///
///
/// 如果是表达式节点条件节点,该项为null
[AutoSocketHandle(ThemeValue = EnvMsgTheme.CreateNode,ArgNotNull = false)]
public async Task CreateNode([Needful] string nodeType, [Needful] PositionOfUI position, MethodDetailsInfo? mdInfo = null)
{
if (!EnumHelper.TryConvertEnum(nodeType, out var nodeControlType))
{
return null;
}
var nodeInfo = await environment.CreateNodeAsync(nodeControlType, position, mdInfo); // 监听到客户端创建节点的请求
return nodeInfo;
}
///
/// 远程从远程环境移除节点
///
///
///
[AutoSocketHandle(ThemeValue = EnvMsgTheme.RemoveNode)]
public async Task