mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 10:36:30 +00:00
d842466594
put cryptography lib to fixed version fix race condition bug in queue handler add lock to db writing and backup to file db on each write
102 lines
3.6 KiB
C#
102 lines
3.6 KiB
C#
using System.IO;
|
|
using Azaion.CommonSecurity;
|
|
using Azaion.CommonSecurity.DTO;
|
|
using MediatR;
|
|
using Microsoft.Extensions.Options;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
namespace Azaion.Common.Services;
|
|
|
|
public interface IGpsMatcherService
|
|
{
|
|
Task RunGpsMatching(string userRouteDir, double initialLatitude, double initialLongitude, CancellationToken detectToken = default);
|
|
void StopGpsMatching();
|
|
}
|
|
|
|
public class GpsMatcherService(IGpsMatcherClient gpsMatcherClient, ISatelliteDownloader satelliteTileDownloader, IOptions<DirectoriesConfig> dirConfig)
|
|
: IGpsMatcherService,
|
|
INotificationHandler<GPSMatcherResultEvent>,
|
|
INotificationHandler<GPSMatcherFinishedEvent>
|
|
{
|
|
private readonly DirectoriesConfig _dirConfig = dirConfig.Value;
|
|
private const int ZOOM_LEVEL = 18;
|
|
private const int POINTS_COUNT = 10;
|
|
private const int DISTANCE_BETWEEN_POINTS_M = 100;
|
|
private const double SATELLITE_RADIUS_M = DISTANCE_BETWEEN_POINTS_M * (POINTS_COUNT + 1);
|
|
|
|
private string _routeDir = "";
|
|
private string _userRouteDir = "";
|
|
private List<string> _allRouteImages = new();
|
|
private Dictionary<string, int> _currentRouteImages = new();
|
|
private double _currentLat;
|
|
private double _currentLon;
|
|
private CancellationToken _detectToken;
|
|
private int _currentIndex;
|
|
|
|
|
|
public async Task RunGpsMatching(string userRouteDir, double initialLatitude, double initialLongitude, CancellationToken detectToken = default)
|
|
{
|
|
_routeDir = Path.Combine(SecurityConstants.EXTERNAL_GPS_DENIED_FOLDER, _dirConfig.GpsRouteDirectory);
|
|
_userRouteDir = userRouteDir;
|
|
|
|
_allRouteImages = Directory.GetFiles(userRouteDir)
|
|
.OrderBy(x => x).ToList();
|
|
|
|
_currentLat = initialLatitude;
|
|
_currentLon = initialLongitude;
|
|
|
|
_detectToken = detectToken;
|
|
await StartMatchingRound(0);
|
|
}
|
|
|
|
private async Task StartMatchingRound(int startIndex)
|
|
{
|
|
//empty route dir
|
|
if (Directory.Exists(_routeDir))
|
|
Directory.Delete(_routeDir, true);
|
|
Directory.CreateDirectory(_routeDir);
|
|
|
|
_currentRouteImages = _allRouteImages
|
|
.Skip(startIndex)
|
|
.Take(POINTS_COUNT)
|
|
.Select((fullName, index) =>
|
|
{
|
|
var filename = Path.GetFileName(fullName);
|
|
File.Copy(Path.Combine(_userRouteDir, filename), Path.Combine(_routeDir, filename));
|
|
return new { Filename = filename, Index = startIndex + index };
|
|
})
|
|
.ToDictionary(x => x.Filename, x => x.Index);
|
|
|
|
await satelliteTileDownloader.GetTiles(_currentLat, _currentLon, SATELLITE_RADIUS_M, ZOOM_LEVEL, _detectToken);
|
|
gpsMatcherClient.StartMatching(new StartMatchingEvent
|
|
{
|
|
ImagesCount = POINTS_COUNT,
|
|
Latitude = _currentLat,
|
|
Longitude = _currentLon,
|
|
SatelliteImagesDir = _dirConfig.GpsSatDirectory,
|
|
RouteDir = _dirConfig.GpsRouteDirectory
|
|
});
|
|
}
|
|
|
|
public void StopGpsMatching()
|
|
{
|
|
gpsMatcherClient.Stop();
|
|
}
|
|
|
|
public Task Handle(GPSMatcherResultEvent result, CancellationToken cancellationToken)
|
|
{
|
|
_currentRouteImages.Remove(result.Image);
|
|
_currentLat = result.Latitude;
|
|
_currentLon = result.Longitude;
|
|
_currentIndex = _currentRouteImages[result.Image];
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public async Task Handle(GPSMatcherFinishedEvent notification, CancellationToken cancellationToken)
|
|
{
|
|
if (_currentRouteImages.Count == 0 && _currentIndex < _allRouteImages.Count)
|
|
await StartMatchingRound(_currentIndex);
|
|
}
|
|
}
|
|
|