-Update libreria  x Pro3Em
This commit is contained in:
Samuele Locatelli
2025-08-06 18:38:31 +02:00
parent a3d7c936da
commit fa258cf09b
8 changed files with 222 additions and 5 deletions
+12
View File
@@ -0,0 +1,12 @@
using EgwProxy.Shelly.DTO;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace EgwProxy.Shelly.Clients
{
public interface IShellyPro3Em
{
Task<ShellyResult<ShellyGen2StatusDto>> GetStatus(CancellationToken cancellationToken, TimeSpan? timeout = null);
}
}
@@ -0,0 +1,55 @@
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, Shelly1PmOptions 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<EnergyDto>> GetEmStatus(CancellationToken cancellationToken, TimeSpan? timeout = null)
{
var endpoint = ServerUri.AppendPathSegment("EM.GetStatus?id=0");
var requestMessage = new HttpRequestMessage(HttpMethod.Get, endpoint);
return await ExecuteRequestAsync<EnergyDto>(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 EnergyDtoConverter() }
};
var shelly1Status = JsonConvert.DeserializeObject<T>(readAsStringAsync, settings);
return ShellyResult<T>.Success(shelly1Status, readAsStringAsync);
}
}
}
@@ -0,0 +1,66 @@
using EgwProxy.Shelly.DTO.Gen2;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EgwProxy.Shelly.Converters
{
public class EnergyDtoConverter : JsonConverter<EnergyDto>
{
public override EnergyDto ReadJson(JsonReader reader, Type objectType, EnergyDto existingValue, bool hasExistingValue, JsonSerializer serializer)
{
JObject obj = JObject.Load(reader);
var dto = new EnergyDto
{
Id = (int)obj["id"],
NeutralCurrent = obj["n_current"]?.ToObject<double?>() ?? 0,
TotalCurrent = (double)obj["total_current"],
TotalActivePower = (double)obj["total_act_power"],
TotalApparentPower = (double)obj["total_aprt_power"],
UserCalibratedPhase = obj["user_calibrated_phase"]?.ToObject<List<double>>() ?? new List<double>()
};
dto.PhaseA = new PhaseDataDto
{
Current = (double)obj["a_current"],
Voltage = (double)obj["a_voltage"],
ActivePower = (double)obj["a_act_power"],
ApparentPower = (double)obj["a_aprt_power"],
PowerFactor = (double)obj["a_pf"],
Frequency = (double)obj["a_freq"]
};
dto.PhaseB = new PhaseDataDto
{
Current = (double)obj["b_current"],
Voltage = (double)obj["b_voltage"],
ActivePower = (double)obj["b_act_power"],
ApparentPower = (double)obj["b_aprt_power"],
PowerFactor = (double)obj["b_pf"],
Frequency = (double)obj["b_freq"]
};
dto.PhaseC = new PhaseDataDto
{
Current = (double)obj["c_current"],
Voltage = (double)obj["c_voltage"],
ActivePower = (double)obj["c_act_power"],
ApparentPower = (double)obj["c_aprt_power"],
PowerFactor = (double)obj["c_pf"],
Frequency = (double)obj["c_freq"]
};
return dto;
}
public override void WriteJson(JsonWriter writer, EnergyDto value, JsonSerializer serializer)
{
throw new NotImplementedException("Serialization not implemented");
}
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
using Newtonsoft.Json;
namespace EgwProxy.Shelly.DTO.Shelly1PM
namespace EgwProxy.Shelly.DTO
{
public class BaseServiceDto
{
+57
View File
@@ -0,0 +1,57 @@
using Newtonsoft.Json;
using System.Collections.Generic;
namespace EgwProxy.Shelly.DTO.Gen2
{
/// <summary>
/// Energy Info
/// </summary>
public class EnergyDto
{
public int Id { get; set; }
/// <summary>
/// Phase A data
/// </summary>
public PhaseDataDto PhaseA { get; set; } = new PhaseDataDto();
/// <summary>
/// Phase B data
/// </summary>
public PhaseDataDto PhaseB { get; set; } = new PhaseDataDto();
/// <summary>
/// Phase C data
/// </summary>
public PhaseDataDto PhaseC { get; set; } = new PhaseDataDto();
/// <summary>
/// Corrente Neutro
/// </summary>
[JsonProperty("n_current")]
public double NeutralCurrent { get; set; } = 0;
/// <summary>
/// Corrente Totale
/// </summary>
[JsonProperty("total_current")]
public double TotalCurrent { get; set; } = 0;
/// <summary>
/// Corrente Totale Attiva
/// </summary>
[JsonProperty("total_act_power")]
public double TotalActivePower { get; set; } = 0;
/// <summary>
/// Corrente Totale Apparente
/// </summary>
[JsonProperty("total_aprt_power")]
public double TotalApparentPower { get; set; } = 0;
/// <summary>
/// Calibrazione fasi manuale
/// </summary>
[JsonProperty("user_calibrated_phase")]
public List<double> UserCalibratedPhase { get; set; } = new List<double>();
}
}
+18
View File
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EgwProxy.Shelly.DTO.Gen2
{
public class PhaseDataDto
{
public double ActivePower { get; set; } = 0;
public double ApparentPower { get; set; } = 0;
public double Current { get; set; } = 0;
public double Frequency { get; set; } = 0;
public double PowerFactor { get; set; } = 0;
public double Voltage { get; set; } = 0;
}
}
+8 -2
View File
@@ -1,4 +1,4 @@
using EgwProxy.Shelly.DTO.Shelly1PM;
using EgwProxy.Shelly.DTO.Gen2;
using Newtonsoft.Json;
namespace EgwProxy.Shelly.DTO
@@ -9,7 +9,6 @@ namespace EgwProxy.Shelly.DTO
/// </summary>
public class ShellyGen2StatusDto
{
/// <summary>
/// WiFi data
/// </summary>
@@ -22,6 +21,13 @@ namespace EgwProxy.Shelly.DTO
[JsonProperty("cloud")]
public CloudDto ShellyCloud { get; set; }
/// <summary>
/// EnergyMonitor 0 data
/// </summary>
[JsonProperty("em:0")]
public EnergyDto EmData { get; set; }
/// <summary>
/// MQTT queue state
/// </summary>
+5 -2
View File
@@ -83,13 +83,16 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Clients\IShelly1.cs" />
<Compile Include="Clients\IShellyPro3Em.cs" />
<Compile Include="Clients\IShelly1Pm.cs" />
<Compile Include="Clients\Shelly1Client.cs" />
<Compile Include="Clients\ShellyPro3EmClient.cs" />
<Compile Include="Clients\Shelly1PmClient.cs" />
<Compile Include="Clients\ShellyClientBase.cs" />
<Compile Include="Converters\EnergyDtoConverter.cs" />
<Compile Include="DTO\BaseServiceDto.cs" />
<Compile Include="DTO\CloudDto.cs" />
<Compile Include="DTO\Gen2\EnergyDto.cs" />
<Compile Include="DTO\Gen2\PhaseDataDto.cs" />
<Compile Include="DTO\Shelly1PmStatusDto.cs" />
<Compile Include="DTO\Shelly1PM\EnergyDto.cs" />
<Compile Include="DTO\Shelly1PM\InputDto.cs" />