mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 23:16:30 +00:00
b21f8e320f
add tensorrt engine
113 lines
3.6 KiB
C#
113 lines
3.6 KiB
C#
using System.Diagnostics;
|
|
using System.Text;
|
|
using Azaion.CommonSecurity.DTO;
|
|
using Azaion.CommonSecurity.DTO.Commands;
|
|
using MessagePack;
|
|
using Microsoft.Extensions.Options;
|
|
using NetMQ;
|
|
using NetMQ.Sockets;
|
|
|
|
namespace Azaion.CommonSecurity.Services;
|
|
|
|
public interface IExternalClient
|
|
{
|
|
void Stop();
|
|
|
|
void Send(RemoteCommand create);
|
|
T? Get<T>(int retries = 24, int tryTimeoutSeconds = 5, CancellationToken ct = default) where T : class;
|
|
byte[]? GetBytes(int retries = 24, int tryTimeoutSeconds = 5, CancellationToken ct = default);
|
|
}
|
|
|
|
public abstract class BaseZeroMqExternalClient : IExternalClient
|
|
{
|
|
private readonly DealerSocket _dealer = new();
|
|
private readonly Guid _clientId = Guid.NewGuid();
|
|
|
|
private readonly ExternalClientConfig _externalClientConfig;
|
|
|
|
protected abstract string ClientPath { get; }
|
|
|
|
protected BaseZeroMqExternalClient(ExternalClientConfig config)
|
|
{
|
|
_externalClientConfig = config;
|
|
Start();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
try
|
|
{
|
|
using var process = new Process();
|
|
process.StartInfo = new ProcessStartInfo
|
|
{
|
|
FileName = ClientPath,
|
|
//Arguments = $"-e {credentials.Email} -p {credentials.Password} -f {apiConfig.ResourcesFolder}",
|
|
//RedirectStandardOutput = true,
|
|
//RedirectStandardError = true,
|
|
//CreateNoWindow = true
|
|
};
|
|
|
|
process.OutputDataReceived += (_, e) => { if (e.Data != null) Console.WriteLine(e.Data); };
|
|
process.ErrorDataReceived += (_, e) => { if (e.Data != null) Console.WriteLine(e.Data); };
|
|
process.Start();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
//throw;
|
|
}
|
|
|
|
_dealer.Options.Identity = Encoding.UTF8.GetBytes(_clientId.ToString("N"));
|
|
_dealer.Connect($"tcp://{_externalClientConfig.ZeroMqHost}:{_externalClientConfig.ZeroMqPort}");
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
if (!_dealer.IsDisposed)
|
|
{
|
|
_dealer.SendFrame(MessagePackSerializer.Serialize(new RemoteCommand(CommandType.Exit)));
|
|
_dealer.Close();
|
|
}
|
|
}
|
|
|
|
public void Send(RemoteCommand command)
|
|
{
|
|
_dealer.SendFrame(MessagePackSerializer.Serialize(command));
|
|
}
|
|
|
|
public T? Get<T>(int retries = 24, int tryTimeoutSeconds = 5, CancellationToken ct = default) where T : class
|
|
{
|
|
var bytes = GetBytes(retries, tryTimeoutSeconds, ct);
|
|
return bytes != null ? MessagePackSerializer.Deserialize<T>(bytes, cancellationToken: ct) : null;
|
|
}
|
|
|
|
public byte[]? GetBytes(int retries = 24, int tryTimeoutSeconds = 5, CancellationToken ct = default)
|
|
{
|
|
var tryNum = 0;
|
|
while (!ct.IsCancellationRequested && tryNum++ < retries)
|
|
{
|
|
if (!_dealer.TryReceiveFrameBytes(TimeSpan.FromSeconds(tryTimeoutSeconds), out var bytes))
|
|
continue;
|
|
|
|
return bytes;
|
|
}
|
|
|
|
if (!ct.IsCancellationRequested)
|
|
throw new Exception($"Unable to get bytes after {tryNum} retries, {tryTimeoutSeconds} seconds each");
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public class InferenceExternalClient(IOptions<InferenceClientConfig> inferenceClientConfig)
|
|
: BaseZeroMqExternalClient(inferenceClientConfig.Value)
|
|
{
|
|
protected override string ClientPath => SecurityConstants.EXTERNAL_INFERENCE_PATH;
|
|
}
|
|
|
|
public class GpsDeniedExternalClient(IOptions<GpsDeniedClientConfig> gpsDeniedClientConfig)
|
|
: BaseZeroMqExternalClient(gpsDeniedClientConfig.Value)
|
|
{
|
|
protected override string ClientPath => SecurityConstants.EXTERNAL_GPS_DENIED_PATH;
|
|
}
|