using Newtonsoft.Json;
using Serein.Library;
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 MsgControllerOfClient : ISocketHandleModule
{
public Guid HandleGuid => new Guid();
// 消息主题,data - task等待
private readonly Func SendCommandFunc;
private readonly RemoteFlowEnvironment remoteFlowEnvironment;
public MsgControllerOfClient(RemoteFlowEnvironment remoteFlowEnvironment, Func func)
{
this.remoteFlowEnvironment = remoteFlowEnvironment;
SendCommandFunc = func;
}
///
/// 处理需要返回的消息
///
///
///
///
///
private async Task SendCommandAsync(string msgId, string theme, object? data)
{
await SendCommandFunc.Invoke(msgId, theme, data);
}
///
/// 发送请求
///
///
/// 超时触发
public async Task SendAsync(string theme, object? data = null, int overtimeInMs = 100)
{
var msgId = MsgIdHelper.GenerateId().ToString();
SereinEnv.WriteLine(InfoType.INFO, $"[{msgId}] => {theme}");
await SendCommandAsync(msgId, theme, data); // 客户端发送消息
}
///
/// 发送请求并等待远程环境响应
///
///
/// 超时触发
public async Task SendAndWaitDataAsync(string theme, object? data = null, int overtimeInMs = 50)
{
var msgId = MsgIdHelper.GenerateId().ToString();
_ = SendCommandAsync(msgId, theme, data); // 客户端发送消息
var result = await remoteFlowEnvironment.WaitTriggerAsync(msgId);
if (result.Type == TriggerDescription.Overtime)
{
throw new Exception($"主题【{theme}】异常,服务端未响应");
}
else if (result.Type == TriggerDescription.TypeInconsistency)
{
throw new Exception($"主题【{theme}】异常,服务端返回数据类型与预期不一致{result.Value?.GetType()}");
}
return result.Value;
}
#region 消息接收
///
/// 远程环境发来项目信息
///
///
///
[AutoSocketHandle(ThemeValue = EnvMsgTheme.GetEnvInfo, IsReturnValue = false)]
public void GetEnvInfo([UseMsgId] string msgId, [UseData] FlowEnvInfo flowEnvInfo)
{
_ = remoteFlowEnvironment.InvokeTriggerAsync(msgId, flowEnvInfo);
}
///
/// 远程环境发来项目信息
///
///
///
[AutoSocketHandle(ThemeValue = EnvMsgTheme.GetProjectInfo, IsReturnValue = false)]
public void GetProjectInfo([UseMsgId] string msgId, [UseData] SereinProjectData sereinProjectData)
{
_ = remoteFlowEnvironment.InvokeTriggerAsync(msgId, sereinProjectData);
}
///
/// 开始流程
///
///
///
[AutoSocketHandle(ThemeValue = EnvMsgTheme.StartFlow, IsReturnValue = false)]
public void StartFlow([UseMsgId] string msgId, bool state)
{
_ = remoteFlowEnvironment.InvokeTriggerAsync(msgId, state);
}
///
/// 结束流程
///
///
///
[AutoSocketHandle(ThemeValue = EnvMsgTheme.ExitFlow, IsReturnValue = false)]
public void ExitFlow([UseMsgId] string msgId, bool state)
{
_ = remoteFlowEnvironment.InvokeTriggerAsync(msgId, state);
}
///
/// 设置了某个节点为起始节点
///
///
/// 节点Guid
[AutoSocketHandle(ThemeValue = EnvMsgTheme.SetStartNode, IsReturnValue = false)]
public void SetStartNode([UseMsgId] string msgId, string nodeGuid)
{
_ = remoteFlowEnvironment.InvokeTriggerAsync(msgId, nodeGuid);
}
///
/// 从某个节点开始运行
///
///
///
[AutoSocketHandle(ThemeValue = EnvMsgTheme.StartFlowInSelectNode, IsReturnValue = false)]
public void StartFlowInSelectNode([UseMsgId] string msgId, bool state)
{
_ = remoteFlowEnvironment.InvokeTriggerAsync(msgId, state);
}
///
/// 设置节点的中断
///
///
[AutoSocketHandle(ThemeValue = EnvMsgTheme.SetNodeInterrupt, IsReturnValue = false)]
public void SetNodeInterrupt([UseMsgId] string msgId)
{
_ = remoteFlowEnvironment.InvokeTriggerAsync