mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 10:46:30 +00:00
big refactoring. get rid of static properties and coupled architecture. prepare system for integration tests
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
using System.Text;
|
||||
using Azaion.Common.DTO.Config;
|
||||
using Azaion.Common.Extensions;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Azaion.Common.Services;
|
||||
|
||||
public class FileConfigurationStore : IConfigurationStore
|
||||
{
|
||||
private readonly string _configPath;
|
||||
private readonly IFileSystem _fileSystem;
|
||||
private static readonly Guid SaveConfigTaskId = Guid.NewGuid();
|
||||
|
||||
public FileConfigurationStore(string configPath, IFileSystem fileSystem)
|
||||
{
|
||||
_configPath = configPath;
|
||||
_fileSystem = fileSystem;
|
||||
}
|
||||
|
||||
public async Task<AppConfig> LoadAsync(CancellationToken ct = default)
|
||||
{
|
||||
if (!_fileSystem.FileExists(_configPath))
|
||||
return new AppConfig();
|
||||
|
||||
var json = Encoding.UTF8.GetString(await _fileSystem.ReadAllBytesAsync(_configPath, ct));
|
||||
return JsonConvert.DeserializeObject<AppConfig>(json) ?? new AppConfig();
|
||||
}
|
||||
|
||||
public Task SaveAsync(AppConfig config, CancellationToken ct = default)
|
||||
{
|
||||
ThrottleExt.Throttle(async () =>
|
||||
{
|
||||
var publicConfig = new
|
||||
{
|
||||
config.LoaderClientConfig,
|
||||
config.InferenceClientConfig,
|
||||
config.GpsDeniedClientConfig,
|
||||
config.DirectoriesConfig,
|
||||
config.UIConfig,
|
||||
config.CameraConfig
|
||||
};
|
||||
var json = JsonConvert.SerializeObject(publicConfig, Formatting.Indented);
|
||||
await _fileSystem.WriteAllBytesAsync(_configPath, Encoding.UTF8.GetBytes(json), ct);
|
||||
}, SaveConfigTaskId, TimeSpan.FromSeconds(5));
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user