Files
mapo-iob-libs/EgwProxy.Shelly/Clients/ShellyPro3EmClient.cs
T
2025-08-07 08:35:34 +02:00

66 lines
2.9 KiB
C#

using EgwProxy.Shelly.Converters;
using EgwProxy.Shelly.DTO;
using EgwProxy.Shelly.DTO.Gen2;
using EgwProxy.Shelly.Options;
using Flurl;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace EgwProxy.Shelly.Clients
{
public class ShellyPro3EmClient : ShellyClientBase, IShellyPro3Em
{
public ShellyPro3EmClient(HttpClient httpClient, ShellyOptions shellyOptions) : base(httpClient, shellyOptions)
{
}
public async Task<ShellyResult<ShellyGen2StatusDto>> GetStatus(CancellationToken cancellationToken, TimeSpan? timeout = null)
{
var endpoint = ServerUri.AppendPathSegment("Shelly.GetStatus");
var requestMessage = new HttpRequestMessage(HttpMethod.Get, endpoint);
return await ExecuteRequestAsync<ShellyGen2StatusDto>(requestMessage, cancellationToken, timeout);
}
public async Task<ShellyResult<EMDto>> GetEmStatus(CancellationToken cancellationToken, int id, TimeSpan? timeout = null)
{
var endpoint = ServerUri.AppendPathSegment("EM.GetStatus").AppendQueryParam("id", id);
var requestMessage = new HttpRequestMessage(HttpMethod.Get, endpoint);
return await ExecuteRequestAsync<EMDto>(requestMessage, cancellationToken, timeout);
}
public async Task<ShellyResult<EMDataDto>> GetEmDataStatus(CancellationToken cancellationToken, int id, TimeSpan? timeout = null)
{
var endpoint = ServerUri.AppendPathSegment("EMData.GetStatus").AppendQueryParam("id", id);
var requestMessage = new HttpRequestMessage(HttpMethod.Get, endpoint);
return await ExecuteRequestAsync<EMDataDto>(requestMessage, cancellationToken, timeout);
}
#if false
public async Task<ShellyResult<DTO.Shelly1PM.SwitchDto>> GetSwitchStatus(CancellationToken cancellationToken, int id, TimeSpan? timeout = null)
{
var endpoint = ServerUri.AppendPathSegment("Switch.GetStatus").AppendQueryParam("id", id);
var requestMessage = new HttpRequestMessage(HttpMethod.Get, endpoint);
return await ExecuteRequestAsync<DTO.Shelly1PM.SwitchDto>(requestMessage, cancellationToken, timeout);
}
#endif
protected override async Task<ShellyResult<T>> HandleOkResponse<T>(HttpResponseMessage response)
{
var readAsStringAsync = await response.Content.ReadAsStringAsync();
var settings = new JsonSerializerSettings
{
Converters = new List<JsonConverter> {
new EMDtoConverter(),
new EMDataDtoConverter()
}
};
var shelly1Status = JsonConvert.DeserializeObject<T>(readAsStringAsync, settings);
return ShellyResult<T>.Success(shelly1Status, readAsStringAsync);
}
}
}