-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
💾 Feat: The first time that dashboard invoke plugin function throw ne…
…twork connection.
- Loading branch information
1 parent
a9134a4
commit d190b00
Showing
3 changed files
with
201 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |