Skip to content

Commit

Permalink
💾 Feat: The first time that dashboard invoke plugin function throw ne…
Browse files Browse the repository at this point in the history
…twork connection.
  • Loading branch information
Dynesshely committed Feb 25, 2024
1 parent a9134a4 commit d190b00
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 4 deletions.
8 changes: 4 additions & 4 deletions KitX.Shared/WebCommand/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ namespace KitX.Shared.WebCommand;

public struct Command
{
public CommandType Type { get; set; }

public DateTime SendTime { get; set; }

public string Request { get; set; }

public List<Parameter> RequestArgs { get; set; }

public byte[] Body { get; set; }

public int BodyLength { get; set; }

public string FunctionName { get; set; }

public List<Parameter> FunctionArgs { get; set; }

public Dictionary<string, string> Tags { get; set; }
}
39 changes: 39 additions & 0 deletions KitX.Shared/WebCommand/Connector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;

namespace KitX.Shared.WebCommand;

public class Connector
{
private static Connector? _instance;

public static Connector Instance => _instance ??= new Connector();

public Func<Command, string>? Serializer { get; set; }

public Action<Request>? Sender { get; set; }

public Connector SetSerializer(Func<Command, string>? func)
{
Serializer = func;

return this;
}

public Connector SetSender(Action<Request>? sender)
{
Sender = sender;

return this;
}

public RequestBuilder Request(string? commandRequest = null)
{
var builder = new RequestBuilder()
.SetSerializer(Serializer)
.SetSender(Sender)
.SetCommandRequest(commandRequest)
;

return builder;
}
}
158 changes: 158 additions & 0 deletions KitX.Shared/WebCommand/RequestBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
using KitX.Shared.WebCommand.Infos;
using System;

namespace KitX.Shared.WebCommand;

public class RequestBuilder
{
private Request _request = new();

private Command _command = new();

private Func<Command, string>? _serializer;

public Func<Command, string>? Serializer { set => _serializer = value; }

public Action<Request>? Sender { get; set; }

public RequestBuilder()
{

}

public RequestBuilder SetSerializer(Func<Command, string>? func)
{
Serializer = func;

return this;
}

public RequestBuilder SetSender(Action<Request>? sender)
{
Sender = sender;

return this;
}

public RequestBuilder SetCommandRequest(string? command)
{
_command.Request = command ?? string.Empty;

return this;
}

public RequestBuilder UpdateRequest(Func<Request, Request> updater)
{
_request = updater.Invoke(_request);

return this;
}

public RequestBuilder UpdateCommand(Func<Command, Command> updater)
{
_command = updater.Invoke(_command);

return this;
}

private Request Build()
{
_request.Content = _serializer?.Invoke(_command) ?? throw new ArgumentNullException(nameof(Serializer));

return _request;
}

public RequestBuilder Send()
{
if (Sender is null) throw new ArgumentNullException(nameof(Sender));

_command.SendTime = DateTime.UtcNow;

Sender?.Invoke(Build());

return this;
}
}

public static class RequestBuilderExtensions
{
public static RequestBuilder RegisterPlugin(this RequestBuilder builder, byte[] body, int length)
{
builder = builder.UpdateCommand(cmd =>
{
cmd.Request = CommandRequestInfo.RegisterPlugin;
cmd.Body = body;
cmd.BodyLength = length;
return cmd;
});

return builder;
}

public static RequestBuilder RequestWorkingDetail(this RequestBuilder builder)
{
builder = builder.UpdateCommand(cmd =>
{
cmd.Request = CommandRequestInfo.RequestWorkingDetail;
return cmd;
});

return builder;
}

public static RequestBuilder ReceiveWorkingDetail(this RequestBuilder builder, byte[] body, int length)
{
builder = builder.UpdateCommand(cmd =>
{
cmd.Request = CommandRequestInfo.ReceiveWorkingDetail;
cmd.Body = body;
cmd.BodyLength = length;
return cmd;
});

return builder;
}

public static RequestBuilder ReportStatus(this RequestBuilder builder)
{
builder = builder.UpdateCommand(cmd =>
{
cmd.Request = CommandRequestInfo.ReportStatus;
return cmd;
});

return builder;
}

public static RequestBuilder RequestCommand(this RequestBuilder builder)
{
builder = builder.UpdateCommand(cmd =>
{
cmd.Request = CommandRequestInfo.RequestCommand;
return cmd;
});

return builder;
}

public static RequestBuilder ReceiveCommand(this RequestBuilder builder)
{
builder = builder.UpdateCommand(cmd =>
{
cmd.Request = CommandRequestInfo.ReceiveCommand;
return cmd;
});

return builder;
}
}

0 comments on commit d190b00

Please sign in to comment.