Шибалкин Александр
5497b89c3b
All checks were successful
Unit tests / test (push) Successful in 1m3s
105 lines
3.3 KiB
C#
105 lines
3.3 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using DD.Persistence.Client.Clients.Base;
|
|
using DD.Persistence.Client.Clients.Interfaces;
|
|
using DD.Persistence.Client.Clients.Interfaces.Refit;
|
|
using DD.Persistence.Models;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using System.Globalization;
|
|
|
|
namespace DD.Persistence.Client.Clients;
|
|
|
|
public class SetpointClient : BaseClient, ISetpointClient
|
|
{
|
|
private readonly IRefitSetpointClient refitSetpointClient;
|
|
|
|
public SetpointClient(IRefitClientFactory<IRefitSetpointClient> refitSetpointClientFactory, ILogger<SetpointClient> logger) : base(logger)
|
|
{
|
|
this.refitSetpointClient = refitSetpointClientFactory.Create();
|
|
}
|
|
|
|
public async Task<IEnumerable<SetpointValueDto>> GetCurrent(IEnumerable<Guid> setpointKeys, CancellationToken token)
|
|
{
|
|
var result = await ExecuteGetResponse(
|
|
async () => await refitSetpointClient.GetCurrent(setpointKeys, token), token);
|
|
|
|
return result!;
|
|
}
|
|
|
|
public async Task<Dictionary<Guid, object?>> GetCurrentDictionary(Dictionary<Guid, Type> setpointConfigs, CancellationToken token)
|
|
{
|
|
var data = await GetCurrent(setpointConfigs.Keys, token);
|
|
var dict = DeserializeResultToDict(setpointConfigs, data);
|
|
|
|
return dict;
|
|
}
|
|
|
|
|
|
private static Dictionary<Guid, object?> DeserializeResultToDict(Dictionary<Guid, Type> setpointConfigs, IEnumerable<SetpointValueDto> data)
|
|
{
|
|
var dict = new Dictionary<Guid, object?>();
|
|
|
|
|
|
foreach (var valueDto in data)
|
|
{
|
|
if (valueDto.Value is not null &&
|
|
valueDto.Value is JsonElement element &&
|
|
setpointConfigs.TryGetValue(valueDto.Key, out var type) &&
|
|
type is not null)
|
|
|
|
dict[valueDto.Key] = Convert.ChangeType(element.GetString(), type, CultureInfo.InvariantCulture) ?? valueDto.Value;
|
|
else
|
|
dict[valueDto.Key] = valueDto.Value;
|
|
}
|
|
|
|
return dict;
|
|
}
|
|
|
|
public async Task<IEnumerable<SetpointValueDto>> GetHistory(IEnumerable<Guid> setpointKeys, DateTimeOffset historyMoment, CancellationToken token)
|
|
{
|
|
var result = await ExecuteGetResponse(
|
|
async () => await refitSetpointClient.GetHistory(setpointKeys, historyMoment, token), token);
|
|
|
|
return result!;
|
|
}
|
|
|
|
public async Task<Dictionary<Guid, IEnumerable<SetpointLogDto>>> GetLog(IEnumerable<Guid> setpointKeys, CancellationToken token)
|
|
{
|
|
var result = await ExecuteGetResponse(
|
|
async () => await refitSetpointClient.GetLog(setpointKeys, token), token);
|
|
|
|
return result!;
|
|
}
|
|
|
|
public async Task<DatesRangeDto> GetDatesRangeAsync(CancellationToken token)
|
|
{
|
|
var result = await ExecuteGetResponse(
|
|
async () => await refitSetpointClient.GetDatesRangeAsync(token), token);
|
|
|
|
return result!;
|
|
}
|
|
|
|
public async Task<IEnumerable<SetpointLogDto>> GetPart(DateTimeOffset dateBegin, int take, CancellationToken token)
|
|
{
|
|
var result = await ExecuteGetResponse(
|
|
async () => await refitSetpointClient.GetPart(dateBegin, take, token), token);
|
|
|
|
return result!;
|
|
}
|
|
|
|
public async Task Add(Guid setpointKey, object newValue, CancellationToken token)
|
|
{
|
|
await ExecutePostResponse(
|
|
async () => await refitSetpointClient.Add(setpointKey, newValue, token), token);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
refitSetpointClient.Dispose();
|
|
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
|
|
|
|
}
|