mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 11:26:31 +00:00
179 lines
6.6 KiB
C#
179 lines
6.6 KiB
C#
using System.IO;
|
|
using System.Net.Http;
|
|
using System.Reflection;
|
|
using System.Windows;
|
|
using System.Windows.Input;
|
|
using System.Windows.Threading;
|
|
using Azaion.Annotator;
|
|
using Azaion.Annotator.DTO;
|
|
using Azaion.Annotator.Extensions;
|
|
using Azaion.Common;
|
|
using Azaion.Common.DTO;
|
|
using Azaion.Common.DTO.Config;
|
|
using Azaion.Common.Extensions;
|
|
using Azaion.Common.Services;
|
|
using Azaion.Dataset;
|
|
using Azaion.Suite.Services.DTO;
|
|
using CommandLine;
|
|
using LibVLCSharp.Shared;
|
|
using MediatR;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using Newtonsoft.Json;
|
|
using Serilog;
|
|
|
|
namespace Azaion.Suite;
|
|
|
|
public partial class App
|
|
{
|
|
private readonly IHost _host;
|
|
private readonly ILogger<App> _logger;
|
|
private readonly IMediator _mediator;
|
|
|
|
private static readonly List<string> EncryptedResources =
|
|
[
|
|
"Azaion.Annotator.dll",
|
|
"Azaion.Dataset.dll"
|
|
];
|
|
|
|
private static readonly IResourceLoader? ResourceLoader = new ResourceLoader("", "", null!, null!);
|
|
|
|
static App()
|
|
{
|
|
var result = Parser.Default.ParseArguments<SuiteCommandLineOptions>(Environment.GetCommandLineArgs());
|
|
if (result.Errors.Any())
|
|
return;
|
|
ApiConfig apiConfig;
|
|
try
|
|
{
|
|
if (File.Exists(Constants.CONFIG_PATH))
|
|
throw new FileNotFoundException(Constants.CONFIG_PATH);
|
|
var configStr = File.ReadAllText(Constants.CONFIG_PATH);
|
|
apiConfig = JsonConvert.DeserializeObject<AppConfig>(configStr)!.ApiConfig;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
apiConfig = new ApiConfig
|
|
{
|
|
Url = "https://api.azaion.com",
|
|
RetryCount = 3,
|
|
TimeoutSeconds = 40
|
|
};
|
|
}
|
|
var api = new AzaionApiClient(new HttpClient
|
|
{
|
|
BaseAddress = new Uri(apiConfig.Url),
|
|
Timeout = TimeSpan.FromSeconds(apiConfig.TimeoutSeconds)
|
|
});
|
|
var email = result.Value.Email;
|
|
var password = result.Value.Password;
|
|
|
|
api.Login(email, password);
|
|
|
|
ResourceLoader = new ResourceLoader(email, password, api, new HardwareService());
|
|
foreach (var resource in EncryptedResources)
|
|
{
|
|
var stream = ResourceLoader.Load(resource).GetAwaiter().GetResult();
|
|
Assembly.Load(stream.ToArray());
|
|
}
|
|
|
|
new ConfigUpdater().CheckConfig();
|
|
}
|
|
|
|
public App()
|
|
{
|
|
Log.Logger = new LoggerConfiguration()
|
|
.Enrich.FromLogContext()
|
|
.MinimumLevel.Information()
|
|
.WriteTo.Console()
|
|
.WriteTo.File(
|
|
path: "Logs/log.txt",
|
|
rollingInterval: RollingInterval.Day)
|
|
.CreateLogger();
|
|
|
|
_host = Host.CreateDefaultBuilder()
|
|
.ConfigureAppConfiguration((context, config) => config
|
|
.AddCommandLine(Environment.GetCommandLineArgs())
|
|
.AddJsonFile(Constants.CONFIG_PATH, optional: true, reloadOnChange: true))
|
|
.ConfigureServices((context, services) =>
|
|
{
|
|
services.AddSingleton<MainSuite>();
|
|
services.AddSingleton<IHardwareService, HardwareService>();
|
|
services.AddSingleton<IResourceLoader>(ResourceLoader!);
|
|
|
|
services.Configure<AppConfig>(context.Configuration);
|
|
services.ConfigureSection<ApiConfig>(context.Configuration);
|
|
services.ConfigureSection<DirectoriesConfig>(context.Configuration);
|
|
services.ConfigureSection<AnnotationConfig>(context.Configuration);
|
|
services.ConfigureSection<WindowConfig>(context.Configuration);
|
|
services.ConfigureSection<AIRecognitionConfig>(context.Configuration);
|
|
services.ConfigureSection<ThumbnailConfig>(context.Configuration);
|
|
|
|
services.AddSingleton<IConfigUpdater, ConfigUpdater>();
|
|
|
|
services.AddSingleton<Annotator.Annotator>();
|
|
services.AddSingleton<DatasetExplorer>();
|
|
services.AddSingleton<HelpWindow>();
|
|
services.AddSingleton<IAIDetector, YOLODetector>();
|
|
services.AddMediatR(c => c.RegisterServicesFromAssemblies(
|
|
typeof(Annotator.Annotator).Assembly,
|
|
typeof(DatasetExplorer).Assembly));
|
|
services.AddSingleton<LibVLC>(_ => new LibVLC());
|
|
services.AddSingleton<FormState>();
|
|
services.AddSingleton<MediaPlayer>(sp =>
|
|
{
|
|
var libVLC = sp.GetRequiredService<LibVLC>();
|
|
return new MediaPlayer(libVLC);
|
|
});
|
|
services.AddSingleton<AnnotatorEventHandler>();
|
|
services.AddSingleton<VLCFrameExtractor>();
|
|
|
|
services.AddHttpClient<AzaionApiClient>((sp, client) =>
|
|
{
|
|
var apiConfig = sp.GetRequiredService<IOptions<ApiConfig>>().Value;
|
|
client.BaseAddress = new Uri(apiConfig.Url);
|
|
client.Timeout = TimeSpan.FromSeconds(apiConfig.TimeoutSeconds);
|
|
});
|
|
|
|
services.AddSingleton<DatasetExplorer>();
|
|
services.AddSingleton<IGalleryManager, GalleryManager>();
|
|
})
|
|
.Build();
|
|
_mediator = _host.Services.GetRequiredService<IMediator>();
|
|
_logger = _host.Services.GetRequiredService<ILogger<App>>();
|
|
DispatcherUnhandledException += OnDispatcherUnhandledException;
|
|
}
|
|
|
|
private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
|
|
{
|
|
_logger.LogError(e.Exception, e.Exception.Message);
|
|
e.Handled = true;
|
|
}
|
|
|
|
protected override async void OnStartup(StartupEventArgs e)
|
|
{
|
|
EventManager.RegisterClassHandler(typeof(UIElement), UIElement.KeyDownEvent, new RoutedEventHandler(GlobalClick));
|
|
await _host.StartAsync();
|
|
_host.Services.GetRequiredService<MainSuite>().Show();
|
|
|
|
base.OnStartup(e);
|
|
}
|
|
|
|
private void GlobalClick(object sender, RoutedEventArgs e)
|
|
{
|
|
var args = (KeyEventArgs)e;
|
|
var keyEvent = new KeyEvent(sender, args, (sender as FrameworkElement).GetParentWindow());
|
|
_ = ThrottleExt.Throttle(() => _mediator.Publish(keyEvent), TimeSpan.FromMilliseconds(50));
|
|
}
|
|
|
|
protected override async void OnExit(ExitEventArgs e)
|
|
{
|
|
await _host.StopAsync();
|
|
_host.Dispose();
|
|
base.OnExit(e);
|
|
}
|
|
} |