2024-10-22 00:13:13 +08:00
|
|
|
|
using Serein.Library;
|
2024-10-20 12:10:57 +08:00
|
|
|
|
using Serein.Library.Network.WebSocketCommunication;
|
2024-10-22 00:13:13 +08:00
|
|
|
|
using Serein.Library.Network.WebSocketCommunication.Handle;
|
2024-10-20 12:10:57 +08:00
|
|
|
|
using Serein.Library.Utils;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Serein.NodeFlow.Env
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 客户端的消息管理(用于处理服务端的响应)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
2024-10-22 00:13:13 +08:00
|
|
|
|
[AutoSocketModule(ThemeKey = FlowEnvironment.ThemeKey,
|
|
|
|
|
|
DataKey = FlowEnvironment.DataKey,
|
|
|
|
|
|
MsgIdKey = FlowEnvironment.MsgIdKey)]
|
2024-10-20 12:10:57 +08:00
|
|
|
|
public class MsgControllerOfClient : ISocketHandleModule
|
|
|
|
|
|
{
|
|
|
|
|
|
public Guid HandleGuid => new Guid();
|
2024-10-22 00:13:13 +08:00
|
|
|
|
|
|
|
|
|
|
// 消息主题,data - task等待
|
|
|
|
|
|
private readonly Func<string, string, object?, Task> SendCommandFunc;
|
2024-10-20 12:10:57 +08:00
|
|
|
|
private readonly RemoteFlowEnvironment remoteFlowEnvironment;
|
|
|
|
|
|
|
2024-10-22 00:13:13 +08:00
|
|
|
|
public MsgControllerOfClient(RemoteFlowEnvironment remoteFlowEnvironment, Func<string, string, object?, Task> func)
|
2024-10-20 12:10:57 +08:00
|
|
|
|
{
|
|
|
|
|
|
this.remoteFlowEnvironment = remoteFlowEnvironment;
|
2024-10-22 00:13:13 +08:00
|
|
|
|
SendCommandFunc = func;
|
|
|
|
|
|
}
|
|
|
|
|
|
private async Task SendCommandAsync(string msgId, string theme, object? data)
|
|
|
|
|
|
{
|
|
|
|
|
|
await SendCommandFunc.Invoke(msgId, theme, data);
|
2024-10-20 12:10:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-10-22 00:13:13 +08:00
|
|
|
|
|
2024-10-20 12:10:57 +08:00
|
|
|
|
/// <summary>
|
2024-10-22 00:13:13 +08:00
|
|
|
|
/// 发送请求
|
2024-10-20 12:10:57 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
/// <exception cref="NotImplementedException">超时触发</exception>
|
2024-10-22 00:13:13 +08:00
|
|
|
|
public async Task SendAsync(string signal, object? data = null, int overtimeInMs = 100)
|
2024-10-20 12:10:57 +08:00
|
|
|
|
{
|
2024-10-20 21:59:42 +08:00
|
|
|
|
//Console.WriteLine($"指令[{signal}],value:{JsonConvert.SerializeObject(sendData)}");
|
|
|
|
|
|
if (!DebounceHelper.CanExecute(signal, overtimeInMs))
|
2024-10-20 12:10:57 +08:00
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2024-10-22 00:13:13 +08:00
|
|
|
|
var msgId = MsgIdHelper.GenerateId().ToString();
|
|
|
|
|
|
await SendCommandAsync(msgId, signal, data);
|
2024-10-20 12:10:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 发送请求并等待远程环境响应
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
/// <exception cref="NotImplementedException">超时触发</exception>
|
2024-10-22 00:13:13 +08:00
|
|
|
|
public async Task<TResult> SendAndWaitDataAsync<TResult>(string theme, object? data = null, int overtimeInMs = 50)
|
2024-10-20 12:10:57 +08:00
|
|
|
|
{
|
2024-10-20 21:59:42 +08:00
|
|
|
|
//Console.WriteLine($"指令[{signal}],value:{JsonConvert.SerializeObject(sendData)}");
|
2024-10-22 00:13:13 +08:00
|
|
|
|
|
|
|
|
|
|
var msgId = MsgIdHelper.GenerateId().ToString();
|
|
|
|
|
|
_ = SendCommandAsync(msgId, theme, data);
|
|
|
|
|
|
return await remoteFlowEnvironment.WaitData<TResult>(msgId);
|
2024-10-20 12:10:57 +08:00
|
|
|
|
|
2024-10-20 21:59:42 +08:00
|
|
|
|
//if (DebounceHelper.CanExecute(signal, overtimeInMs))
|
|
|
|
|
|
//{
|
|
|
|
|
|
// _ = SendCommandAsync.Invoke(signal, sendData);
|
|
|
|
|
|
// return await remoteFlowEnvironment.WaitData<TResult>(signal);
|
|
|
|
|
|
|
|
|
|
|
|
// //(var type, var result) = await remoteFlowEnvironment.WaitDataWithTimeoutAsync<TResult>(signal, TimeSpan.FromSeconds(150));
|
|
|
|
|
|
// //if (type == TriggerType.Overtime)
|
|
|
|
|
|
// //{
|
|
|
|
|
|
// // throw new NotImplementedException("超时触发");
|
|
|
|
|
|
// //}
|
|
|
|
|
|
// //else
|
|
|
|
|
|
// //{
|
|
|
|
|
|
// // return result;
|
|
|
|
|
|
// //}
|
|
|
|
|
|
//}
|
|
|
|
|
|
//else
|
|
|
|
|
|
//{
|
|
|
|
|
|
// return default;
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
2024-10-20 12:10:57 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region 消息接收
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 远程环境发来项目信息
|
|
|
|
|
|
/// </summary>
|
2024-10-22 00:13:13 +08:00
|
|
|
|
/// <param name="msgId"></param>
|
2024-10-20 12:10:57 +08:00
|
|
|
|
/// <param name="flowEnvInfo"></param>
|
|
|
|
|
|
[AutoSocketHandle(ThemeValue = EnvMsgTheme.GetEnvInfo)]
|
2024-10-22 00:13:13 +08:00
|
|
|
|
public void GetEnvInfo([UseMsgId] string msgId, [UseData] FlowEnvInfo flowEnvInfo)
|
2024-10-20 12:10:57 +08:00
|
|
|
|
{
|
2024-10-22 00:13:13 +08:00
|
|
|
|
remoteFlowEnvironment.TriggerSignal(msgId, flowEnvInfo);
|
2024-10-20 12:10:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 远程环境发来项目信息
|
|
|
|
|
|
/// </summary>
|
2024-10-22 00:13:13 +08:00
|
|
|
|
/// <param name="msgId"></param>
|
2024-10-20 12:10:57 +08:00
|
|
|
|
/// <param name="sereinProjectData"></param>
|
|
|
|
|
|
[AutoSocketHandle(ThemeValue = EnvMsgTheme.GetProjectInfo)]
|
2024-10-22 00:13:13 +08:00
|
|
|
|
public void GetProjectInfo([UseMsgId] string msgId, [UseData] SereinProjectData sereinProjectData)
|
2024-10-20 12:10:57 +08:00
|
|
|
|
{
|
2024-10-22 00:13:13 +08:00
|
|
|
|
remoteFlowEnvironment.TriggerSignal(msgId, sereinProjectData);
|
2024-10-20 12:10:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[AutoSocketHandle(ThemeValue = EnvMsgTheme.SetNodeInterrupt)]
|
2024-10-22 00:13:13 +08:00
|
|
|
|
public void SetNodeInterrupt([UseMsgId] string msgId)
|
2024-10-20 12:10:57 +08:00
|
|
|
|
{
|
2024-10-22 00:13:13 +08:00
|
|
|
|
remoteFlowEnvironment.TriggerSignal(msgId, null);
|
2024-10-20 12:10:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[AutoSocketHandle(ThemeValue = EnvMsgTheme.AddInterruptExpression)]
|
2024-10-22 00:13:13 +08:00
|
|
|
|
public void AddInterruptExpression([UseMsgId] string msgId)
|
2024-10-20 12:10:57 +08:00
|
|
|
|
{
|
2024-10-22 00:13:13 +08:00
|
|
|
|
remoteFlowEnvironment.TriggerSignal(msgId, null);
|
2024-10-20 12:10:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-10-20 21:59:42 +08:00
|
|
|
|
|
|
|
|
|
|
[AutoSocketHandle(ThemeValue = EnvMsgTheme.CreateNode)]
|
2024-10-22 00:13:13 +08:00
|
|
|
|
public void CreateNode([UseMsgId] string msgId, [UseData] NodeInfo nodeInfo)
|
2024-10-20 21:59:42 +08:00
|
|
|
|
{
|
2024-10-22 00:13:13 +08:00
|
|
|
|
remoteFlowEnvironment.TriggerSignal(msgId, nodeInfo);
|
2024-10-20 21:59:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[AutoSocketHandle(ThemeValue = EnvMsgTheme.RemoveNode)]
|
2024-10-22 00:13:13 +08:00
|
|
|
|
public void RemoveNode([UseMsgId] string msgId, bool state)
|
2024-10-20 21:59:42 +08:00
|
|
|
|
{
|
2024-10-22 00:13:13 +08:00
|
|
|
|
remoteFlowEnvironment.TriggerSignal(msgId, state);
|
2024-10-20 21:59:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[AutoSocketHandle(ThemeValue = EnvMsgTheme.ConnectNode)]
|
2024-10-22 00:13:13 +08:00
|
|
|
|
public void ConnectNode([UseMsgId] string msgId, bool state)
|
2024-10-20 21:59:42 +08:00
|
|
|
|
{
|
2024-10-22 00:13:13 +08:00
|
|
|
|
remoteFlowEnvironment.TriggerSignal(msgId, state);
|
2024-10-20 21:59:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[AutoSocketHandle(ThemeValue = EnvMsgTheme.RemoveConnect)]
|
2024-10-22 00:13:13 +08:00
|
|
|
|
public void RemoveConnect([UseMsgId] string msgId, bool state)
|
2024-10-20 21:59:42 +08:00
|
|
|
|
{
|
2024-10-22 00:13:13 +08:00
|
|
|
|
remoteFlowEnvironment.TriggerSignal(msgId, state);
|
2024-10-20 21:59:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-10-20 12:10:57 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|