mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 10:46:30 +00:00
separate load functionality from inference client to loader client. Call loader client from inference to get the model.
remove dummy dlls, remove resource loader from c#. TODO: Load dlls separately by Loader UI and loader client WIP
This commit is contained in:
@@ -8,6 +8,8 @@ namespace Azaion.Common.DTO.Config;
|
||||
|
||||
public class AppConfig
|
||||
{
|
||||
public LoaderClientConfig LoaderClientConfig { get; set; } = null!;
|
||||
|
||||
public InferenceClientConfig InferenceClientConfig { get; set; } = null!;
|
||||
|
||||
public GpsDeniedClientConfig GpsDeniedClientConfig { get; set; } = null!;
|
||||
@@ -76,6 +78,7 @@ public class ConfigUpdater : IConfigUpdater
|
||||
//Save only user's config
|
||||
var publicConfig = new
|
||||
{
|
||||
config.LoaderClientConfig,
|
||||
config.InferenceClientConfig,
|
||||
config.GpsDeniedClientConfig,
|
||||
config.DirectoriesConfig,
|
||||
|
||||
@@ -17,17 +17,14 @@ namespace Azaion.Common.Services;
|
||||
|
||||
public interface IInferenceClient : IDisposable
|
||||
{
|
||||
event EventHandler<RemoteCommand> BytesReceived;
|
||||
event EventHandler<RemoteCommand>? InferenceDataReceived;
|
||||
event EventHandler<RemoteCommand>? AIAvailabilityReceived;
|
||||
void Send(RemoteCommand create);
|
||||
void Stop();
|
||||
}
|
||||
|
||||
public class InferenceClient : IInferenceClient, IResourceLoader
|
||||
public class InferenceClient(IOptions<InferenceClientConfig> inferenceConfig, IOptions<LoaderClientConfig> loaderConfig) : IInferenceClient
|
||||
{
|
||||
private CancellationTokenSource _waitFileCancelSource = new();
|
||||
|
||||
public event EventHandler<RemoteCommand>? BytesReceived;
|
||||
public event EventHandler<RemoteCommand>? InferenceDataReceived;
|
||||
public event EventHandler<RemoteCommand>? AIAvailabilityReceived;
|
||||
@@ -35,15 +32,10 @@ public class InferenceClient : IInferenceClient, IResourceLoader
|
||||
private readonly DealerSocket _dealer = new();
|
||||
private readonly NetMQPoller _poller = new();
|
||||
private readonly Guid _clientId = Guid.NewGuid();
|
||||
private readonly InferenceClientConfig _inferenceClientConfig;
|
||||
private readonly InferenceClientConfig _inferenceClientConfig = inferenceConfig.Value;
|
||||
private readonly LoaderClientConfig _loaderClientConfig = loaderConfig.Value;
|
||||
|
||||
public InferenceClient(IOptions<InferenceClientConfig> config, CancellationToken ct)
|
||||
{
|
||||
_inferenceClientConfig = config.Value;
|
||||
Start(ct);
|
||||
}
|
||||
|
||||
private void Start(CancellationToken ct = default)
|
||||
private void Start()
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -51,7 +43,7 @@ public class InferenceClient : IInferenceClient, IResourceLoader
|
||||
process.StartInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = SecurityConstants.EXTERNAL_INFERENCE_PATH,
|
||||
Arguments = $"--port {_inferenceClientConfig.ZeroMqPort} --api {_inferenceClientConfig.ApiUrl}",
|
||||
Arguments = $"--port {_inferenceClientConfig.ZeroMqPort} --loader-port {_loaderClientConfig.ZeroMqPort} --api {_inferenceClientConfig.ApiUrl}",
|
||||
//RedirectStandardOutput = true,
|
||||
//RedirectStandardError = true,
|
||||
//CreateNoWindow = true
|
||||
@@ -70,9 +62,9 @@ public class InferenceClient : IInferenceClient, IResourceLoader
|
||||
_dealer.Options.Identity = Encoding.UTF8.GetBytes(_clientId.ToString("N"));
|
||||
_dealer.Connect($"tcp://{_inferenceClientConfig.ZeroMqHost}:{_inferenceClientConfig.ZeroMqPort}");
|
||||
|
||||
_dealer.ReceiveReady += (_, e) => ProcessClientCommand(e.Socket, ct);
|
||||
_dealer.ReceiveReady += (_, e) => ProcessClientCommand(e.Socket);
|
||||
_poller.Add(_dealer);
|
||||
_ = Task.Run(() => _poller.RunAsync(), ct);
|
||||
_ = Task.Run(() => _poller.RunAsync());
|
||||
}
|
||||
|
||||
private void ProcessClientCommand(NetMQSocket socket, CancellationToken ct = default)
|
||||
@@ -99,46 +91,14 @@ public class InferenceClient : IInferenceClient, IResourceLoader
|
||||
}
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
public void Stop() =>
|
||||
Send(RemoteCommand.Create(CommandType.StopInference));
|
||||
|
||||
}
|
||||
|
||||
public void Send(RemoteCommand command)
|
||||
{
|
||||
public void Send(RemoteCommand command) =>
|
||||
_dealer.SendFrame(MessagePackSerializer.Serialize(command));
|
||||
}
|
||||
|
||||
public MemoryStream LoadFile(string fileName, string? folder = null, TimeSpan? timeout = null)
|
||||
{
|
||||
//TODO: Bad solution, look for better implementation
|
||||
byte[] bytes = [];
|
||||
Exception? exception = null;
|
||||
_waitFileCancelSource = new CancellationTokenSource();
|
||||
Send(RemoteCommand.Create(CommandType.Load, new LoadFileData(fileName, folder)));
|
||||
BytesReceived += OnBytesReceived;
|
||||
|
||||
void OnBytesReceived(object? sender, RemoteCommand command)
|
||||
{
|
||||
if (command.Data is null)
|
||||
{
|
||||
exception = new BusinessException(command.Message ?? "File is empty");
|
||||
_waitFileCancelSource.Cancel();
|
||||
}
|
||||
|
||||
bytes = command.Data!;
|
||||
_waitFileCancelSource.Cancel();
|
||||
}
|
||||
_waitFileCancelSource.Token.WaitForCancel(timeout ?? TimeSpan.FromSeconds(15));
|
||||
BytesReceived -= OnBytesReceived;
|
||||
if (exception != null)
|
||||
throw exception;
|
||||
return new MemoryStream(bytes);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_waitFileCancelSource.Dispose();
|
||||
_poller.Stop();
|
||||
_poller.Dispose();
|
||||
|
||||
|
||||
@@ -79,8 +79,5 @@ public class InferenceService : IInferenceService
|
||||
await combinedTokenSource.Token.AsTask();
|
||||
}
|
||||
|
||||
public void StopInference()
|
||||
{
|
||||
_client.Send(RemoteCommand.Create(CommandType.StopInference));
|
||||
}
|
||||
public void StopInference() => _client.Stop();
|
||||
}
|
||||
Reference in New Issue
Block a user