52 lines
1.8 KiB
C#
52 lines
1.8 KiB
C#
using enums;
|
|
using interfaces;
|
|
using Microsoft.Extensions.Options;
|
|
using options;
|
|
|
|
namespace services;
|
|
|
|
internal class SmappeeChargerService : IChargerService
|
|
{
|
|
private readonly ChargerOptions options;
|
|
private readonly IBearerTokenService tokenService;
|
|
private readonly IHttpClientFactory httpClientFactory;
|
|
|
|
public SmappeeChargerService(
|
|
IOptions<ChargerOptions> options,
|
|
IBearerTokenService tokenService,
|
|
IHttpClientFactory httpClientFactory
|
|
)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(tokenService);
|
|
this.options = options?.Value ?? throw new ArgumentNullException(nameof(options));
|
|
this.tokenService = tokenService;
|
|
this.httpClientFactory =
|
|
httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory));
|
|
}
|
|
|
|
public async Task ExecuteRequest(ChargerRequests request)
|
|
{
|
|
var token = await tokenService.GetToken(false);
|
|
var httpClient = httpClientFactory.CreateClient("smappee");
|
|
httpClient.BaseAddress = options.Url;
|
|
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}");
|
|
var response = request switch
|
|
{
|
|
ChargerRequests.Enable => await httpClient.PutAsJsonAsync<SmappeeRequestBody>(
|
|
$"/dev/v3/chargingstations/${options.ChargingStation}/connectors/{options.Connector}/mode",
|
|
new SmappeeRequestBody("NORMAL", new Limit("AMPERE", 6))
|
|
),
|
|
ChargerRequests.Disable => await httpClient.PutAsJsonAsync(
|
|
"/dev/v3/chargingstations",
|
|
new SmappeeRequestBody("PAUSED", null)
|
|
),
|
|
_ => throw new NotImplementedException(),
|
|
};
|
|
response.EnsureSuccessStatusCode();
|
|
}
|
|
|
|
record SmappeeRequestBody(string mode, Limit? limit);
|
|
|
|
record Limit(string unit, int value);
|
|
}
|