mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 12:46:30 +00:00
better view for class distribution
This commit is contained in:
@@ -1,18 +1,16 @@
|
||||
using System.Diagnostics;
|
||||
using Azaion.Common.DTO;
|
||||
using Azaion.CommonSecurity;
|
||||
using Azaion.CommonSecurity.DTO;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Options;
|
||||
using NetMQ;
|
||||
using NetMQ.Sockets;
|
||||
|
||||
namespace Azaion.Common.Services;
|
||||
|
||||
public interface IGpsMatcherClient
|
||||
public interface IGpsMatcherClient : IDisposable
|
||||
{
|
||||
|
||||
void StartMatching(StartMatchingEvent startEvent);
|
||||
GpsMatchResult? GetResult(int retries = 2, int tryTimeoutSeconds = 5, CancellationToken ct = default);
|
||||
void Stop();
|
||||
}
|
||||
|
||||
@@ -33,17 +31,22 @@ public class StartMatchingEvent
|
||||
|
||||
public class GpsMatcherClient : IGpsMatcherClient
|
||||
{
|
||||
private readonly IMediator _mediator;
|
||||
private readonly GpsDeniedClientConfig _gpsDeniedClientConfig;
|
||||
private string _requestAddress;
|
||||
private readonly RequestSocket _requestSocket = new();
|
||||
private string _subscriberAddress;
|
||||
private readonly SubscriberSocket _subscriberSocket = new();
|
||||
|
||||
public GpsMatcherClient(IOptions<GpsDeniedClientConfig> gpsDeniedClientConfig)
|
||||
|
||||
public GpsMatcherClient(IMediator mediator, IOptions<GpsDeniedClientConfig> gpsDeniedClientConfig)
|
||||
{
|
||||
_mediator = mediator;
|
||||
_gpsDeniedClientConfig = gpsDeniedClientConfig.Value;
|
||||
Start();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
private void Start(CancellationToken ct = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -60,58 +63,70 @@ public class GpsMatcherClient : IGpsMatcherClient
|
||||
|
||||
process.OutputDataReceived += (_, e) => { if (e.Data != null) Console.WriteLine(e.Data); };
|
||||
process.ErrorDataReceived += (_, e) => { if (e.Data != null) Console.WriteLine(e.Data); };
|
||||
//process.Start();
|
||||
process.Start();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
//throw;
|
||||
}
|
||||
_requestSocket.Connect($"tcp://{_gpsDeniedClientConfig.ZeroMqHost}:{_gpsDeniedClientConfig.ZeroMqPort}");
|
||||
_subscriberSocket.Connect($"tcp://{_gpsDeniedClientConfig.ZeroMqHost}:{_gpsDeniedClientConfig.ZeroMqSubscriberPort}");
|
||||
|
||||
_requestAddress = $"tcp://{_gpsDeniedClientConfig.ZeroMqHost}:{_gpsDeniedClientConfig.ZeroMqPort}";
|
||||
_requestSocket.Connect(_requestAddress);
|
||||
|
||||
_subscriberAddress = $"tcp://{_gpsDeniedClientConfig.ZeroMqHost}:{_gpsDeniedClientConfig.ZeroMqSubscriberPort}";
|
||||
_subscriberSocket.Connect(_subscriberAddress);
|
||||
_subscriberSocket.Subscribe("");
|
||||
_subscriberSocket.ReceiveReady += async (_, e) => await ProcessClientCommand(e.Socket, ct);
|
||||
}
|
||||
|
||||
private async Task ProcessClientCommand(NetMQSocket socket, CancellationToken ct)
|
||||
{
|
||||
while (socket.TryReceiveFrameString(TimeSpan.Zero, out var str))
|
||||
{
|
||||
if (string.IsNullOrEmpty(str))
|
||||
continue;
|
||||
|
||||
switch (str)
|
||||
{
|
||||
case "FINISHED":
|
||||
await _mediator.Publish(new GPSMatcherFinishedEvent(), ct);
|
||||
break;
|
||||
case "OK":
|
||||
await _mediator.Publish(new GPSMatcherJobAcceptedEvent(), ct);
|
||||
break;
|
||||
default:
|
||||
var parts = str.Split(',');
|
||||
if (parts.Length != 5)
|
||||
throw new Exception("Matching Result Failed");
|
||||
|
||||
await _mediator.Publish(new GPSMatcherResultEvent
|
||||
{
|
||||
Index = int.Parse(parts[0]),
|
||||
Image = parts[1],
|
||||
Latitude = double.Parse(parts[2]),
|
||||
Longitude = double.Parse(parts[3]),
|
||||
MatchType = parts[4]
|
||||
}, ct);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void StartMatching(StartMatchingEvent e)
|
||||
{
|
||||
_requestSocket.SendFrame(e.ToString());
|
||||
var response = _requestSocket.ReceiveFrameString();
|
||||
if (response != "OK")
|
||||
throw new Exception("Start Matching Failed");
|
||||
}
|
||||
|
||||
public GpsMatchResult? GetResult(int retries = 15, int tryTimeoutSeconds = 5, CancellationToken ct = default)
|
||||
public void Stop() => _requestSocket.SendFrame("STOP");
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
var tryNum = 0;
|
||||
while (!ct.IsCancellationRequested && tryNum++ < retries)
|
||||
{
|
||||
if (!_subscriberSocket.TryReceiveFrameString(TimeSpan.FromSeconds(tryTimeoutSeconds), out var update))
|
||||
continue;
|
||||
if (update == "FINISHED")
|
||||
return null;
|
||||
_requestSocket.SendFrame("EXIT");
|
||||
_requestSocket.Disconnect(_requestAddress);
|
||||
_requestSocket.Dispose();
|
||||
|
||||
var parts = update.Split(',');
|
||||
if (parts.Length != 5)
|
||||
throw new Exception("Matching Result Failed");
|
||||
|
||||
return new GpsMatchResult
|
||||
{
|
||||
Index = int.Parse(parts[0]),
|
||||
Image = parts[1],
|
||||
Latitude = double.Parse(parts[2]),
|
||||
Longitude = double.Parse(parts[3]),
|
||||
MatchType = parts[4]
|
||||
};
|
||||
}
|
||||
|
||||
if (!ct.IsCancellationRequested)
|
||||
throw new Exception($"Unable to get bytes after {tryNum} retries, {tryTimeoutSeconds} seconds each");
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
_requestSocket.SendFrame("STOP");
|
||||
_subscriberSocket.Disconnect(_subscriberAddress);
|
||||
_subscriberSocket.Dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user