diff --git a/EgwProxy.Shelly/Clients/IShellyPro3Em.cs b/EgwProxy.Shelly/Clients/IShellyPro3Em.cs new file mode 100644 index 0000000..60f272b --- /dev/null +++ b/EgwProxy.Shelly/Clients/IShellyPro3Em.cs @@ -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> GetStatus(CancellationToken cancellationToken, TimeSpan? timeout = null); + } +} diff --git a/EgwProxy.Shelly/Clients/ShellyPro3EmClient.cs b/EgwProxy.Shelly/Clients/ShellyPro3EmClient.cs new file mode 100644 index 0000000..ef53785 --- /dev/null +++ b/EgwProxy.Shelly/Clients/ShellyPro3EmClient.cs @@ -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> GetStatus(CancellationToken cancellationToken, TimeSpan? timeout = null) + { + var endpoint = ServerUri.AppendPathSegment("Shelly.GetStatus"); + var requestMessage = new HttpRequestMessage(HttpMethod.Get, endpoint); + return await ExecuteRequestAsync(requestMessage, cancellationToken, timeout); + } + + public async Task> GetEmStatus(CancellationToken cancellationToken, TimeSpan? timeout = null) + { + var endpoint = ServerUri.AppendPathSegment("EM.GetStatus?id=0"); + var requestMessage = new HttpRequestMessage(HttpMethod.Get, endpoint); + return await ExecuteRequestAsync(requestMessage, cancellationToken, timeout); + } + +#if false + public async Task> 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(requestMessage, cancellationToken, timeout); + } +#endif + + protected override async Task> HandleOkResponse(HttpResponseMessage response) + { + var readAsStringAsync = await response.Content.ReadAsStringAsync(); + var settings = new JsonSerializerSettings + { + Converters = new List { new EnergyDtoConverter() } + }; + var shelly1Status = JsonConvert.DeserializeObject(readAsStringAsync, settings); + return ShellyResult.Success(shelly1Status, readAsStringAsync); + } + } +} diff --git a/EgwProxy.Shelly/Converters/EnergyDtoConverter.cs b/EgwProxy.Shelly/Converters/EnergyDtoConverter.cs new file mode 100644 index 0000000..81de237 --- /dev/null +++ b/EgwProxy.Shelly/Converters/EnergyDtoConverter.cs @@ -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 + { + 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() ?? 0, + TotalCurrent = (double)obj["total_current"], + TotalActivePower = (double)obj["total_act_power"], + TotalApparentPower = (double)obj["total_aprt_power"], + UserCalibratedPhase = obj["user_calibrated_phase"]?.ToObject>() ?? new List() + }; + + 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"); + } + } + +} diff --git a/EgwProxy.Shelly/DTO/BaseServiceDto.cs b/EgwProxy.Shelly/DTO/BaseServiceDto.cs index bb026c7..e708e74 100644 --- a/EgwProxy.Shelly/DTO/BaseServiceDto.cs +++ b/EgwProxy.Shelly/DTO/BaseServiceDto.cs @@ -1,6 +1,6 @@ using Newtonsoft.Json; -namespace EgwProxy.Shelly.DTO.Shelly1PM +namespace EgwProxy.Shelly.DTO { public class BaseServiceDto { diff --git a/EgwProxy.Shelly/DTO/Gen2/EnergyDto.cs b/EgwProxy.Shelly/DTO/Gen2/EnergyDto.cs new file mode 100644 index 0000000..d0348e5 --- /dev/null +++ b/EgwProxy.Shelly/DTO/Gen2/EnergyDto.cs @@ -0,0 +1,57 @@ +using Newtonsoft.Json; +using System.Collections.Generic; + +namespace EgwProxy.Shelly.DTO.Gen2 +{ + /// + /// Energy Info + /// + public class EnergyDto + { + + public int Id { get; set; } + + /// + /// Phase A data + /// + public PhaseDataDto PhaseA { get; set; } = new PhaseDataDto(); + /// + /// Phase B data + /// + public PhaseDataDto PhaseB { get; set; } = new PhaseDataDto(); + /// + /// Phase C data + /// + public PhaseDataDto PhaseC { get; set; } = new PhaseDataDto(); + + /// + /// Corrente Neutro + /// + [JsonProperty("n_current")] + public double NeutralCurrent { get; set; } = 0; + + /// + /// Corrente Totale + /// + [JsonProperty("total_current")] + public double TotalCurrent { get; set; } = 0; + + /// + /// Corrente Totale Attiva + /// + [JsonProperty("total_act_power")] + public double TotalActivePower { get; set; } = 0; + + /// + /// Corrente Totale Apparente + /// + [JsonProperty("total_aprt_power")] + public double TotalApparentPower { get; set; } = 0; + + /// + /// Calibrazione fasi manuale + /// + [JsonProperty("user_calibrated_phase")] + public List UserCalibratedPhase { get; set; } = new List(); + } +} diff --git a/EgwProxy.Shelly/DTO/Gen2/PhaseDataDto.cs b/EgwProxy.Shelly/DTO/Gen2/PhaseDataDto.cs new file mode 100644 index 0000000..8ccdf4c --- /dev/null +++ b/EgwProxy.Shelly/DTO/Gen2/PhaseDataDto.cs @@ -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; + } +} diff --git a/EgwProxy.Shelly/DTO/ShellyGen2StatusDto.cs b/EgwProxy.Shelly/DTO/ShellyGen2StatusDto.cs index 30bf2a1..be02120 100644 --- a/EgwProxy.Shelly/DTO/ShellyGen2StatusDto.cs +++ b/EgwProxy.Shelly/DTO/ShellyGen2StatusDto.cs @@ -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 /// public class ShellyGen2StatusDto { - /// /// WiFi data /// @@ -22,6 +21,13 @@ namespace EgwProxy.Shelly.DTO [JsonProperty("cloud")] public CloudDto ShellyCloud { get; set; } + + /// + /// EnergyMonitor 0 data + /// + [JsonProperty("em:0")] + public EnergyDto EmData { get; set; } + /// /// MQTT queue state /// diff --git a/EgwProxy.Shelly/EgwProxy.Shelly.csproj b/EgwProxy.Shelly/EgwProxy.Shelly.csproj index f4608dd..60fff12 100644 --- a/EgwProxy.Shelly/EgwProxy.Shelly.csproj +++ b/EgwProxy.Shelly/EgwProxy.Shelly.csproj @@ -83,13 +83,16 @@ - + - + + + +