Skip to content

Commit

Permalink
Merge pull request #947 from pkuehnel/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
pkuehnel authored Nov 17, 2023
2 parents 9d51697 + 067b642 commit d2e10b1
Show file tree
Hide file tree
Showing 54 changed files with 2,293 additions and 72 deletions.
2 changes: 1 addition & 1 deletion Plugins.Modbus/Plugins.Modbus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="7.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="6.5.0" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Plugins.SmaEnergymeter/Plugins.SmaEnergymeter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<PackageReference Include="Quartz" Version="3.7.0" />
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="7.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Plugins.SolarEdge/Plugins.SolarEdge.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="7.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions Plugins.Solax/Plugins.Solax.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.12" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.13" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="7.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

Expand Down
11 changes: 11 additions & 0 deletions TeslaSolarCharger.GridPriceProvider/Data/Options/AwattarOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.ComponentModel.DataAnnotations;

namespace TeslaSolarCharger.GridPriceProvider.Data.Options;

public class AwattarOptions
{
[Required]
public string BaseUrl { get; set; }

public decimal VATMultiplier { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.ComponentModel.DataAnnotations;

namespace TeslaSolarCharger.GridPriceProvider.Data.Options;

public class EnerginetOptions
{
[Required]
public string BaseUrl { get; set; }

[Required]
public EnerginetRegion Region { get; set; }

[Required]
public EnerginetCurrency Currency { get; set; }

public decimal? VAT { get; set; }

public FixedPriceOptions? FixedPrices { get; set; }
}

public enum EnerginetRegion
{
DK1,
DK2,
NO2,
SE3,
SE4
}

public enum EnerginetCurrency
{
DKK,
EUR
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.ComponentModel.DataAnnotations;

namespace TeslaSolarCharger.GridPriceProvider.Data.Options;

public class FixedPriceOptions
{
public List<string> Prices { get; set; } = new();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.ComponentModel.DataAnnotations;

namespace TeslaSolarCharger.GridPriceProvider.Data.Options;

public class HomeAssistantOptions
{
[Required]
public string BaseUrl { get; set; }

[Required]
public string AccessToken { get; set; }

[Required]
public string EntityId { get; set; }
}
18 changes: 18 additions & 0 deletions TeslaSolarCharger.GridPriceProvider/Data/Options/OctopusOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.ComponentModel.DataAnnotations;

namespace TeslaSolarCharger.GridPriceProvider.Data.Options;

public class OctopusOptions
{
[Required]
public string BaseUrl { get; set; }

[Required]
public string ProductCode { get; set; }

[Required]
public string TariffCode { get; set; }

[Required]
public string RegionCode { get; set; }
}
12 changes: 12 additions & 0 deletions TeslaSolarCharger.GridPriceProvider/Data/Options/TibberOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.ComponentModel.DataAnnotations;

namespace TeslaSolarCharger.GridPriceProvider.Data.Options;

public class TibberOptions
{
[Required]
public string BaseUrl { get; set; }

[Required]
public string AccessToken { get; set; }
}
10 changes: 10 additions & 0 deletions TeslaSolarCharger.GridPriceProvider/Data/Price.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace TeslaSolarCharger.GridPriceProvider.Data;

public class Price
{
public decimal Value { get; set; }

public DateTimeOffset ValidFrom { get; set; }

public DateTimeOffset ValidTo { get; set; }
}
19 changes: 19 additions & 0 deletions TeslaSolarCharger.GridPriceProvider/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using GraphQL.Client.Abstractions;
using GraphQL.Client.Serializer.SystemTextJson;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using TeslaSolarCharger.GridPriceProvider.Services;
using TeslaSolarCharger.GridPriceProvider.Services.Interfaces;

namespace TeslaSolarCharger.GridPriceProvider;

public static class ServiceCollectionExtensions
{
public static IServiceCollection AddGridPriceProvider(this IServiceCollection services)
{
services.AddHttpClient();
services.AddTransient<IFixedPriceService, FixedPriceService>();

return services;
}
}
68 changes: 68 additions & 0 deletions TeslaSolarCharger.GridPriceProvider/Services/AwattarService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using Microsoft.Extensions.Options;
using System.Text.Json;
using System.Text.Json.Serialization;
using TeslaSolarCharger.GridPriceProvider.Data;
using TeslaSolarCharger.GridPriceProvider.Data.Options;
using TeslaSolarCharger.GridPriceProvider.Services.Interfaces;

namespace TeslaSolarCharger.GridPriceProvider.Services;

public class AwattarService : IPriceDataService
{
private readonly HttpClient _client;
private readonly AwattarOptions _options;

public AwattarService(HttpClient client, IOptions<AwattarOptions> options)
{
_client = client;
_options = options.Value;
}

public async Task<IEnumerable<Price>> GetPriceData(DateTimeOffset from, DateTimeOffset to)
{
var url = $"marketdata?start={from.UtcDateTime.AddHours(-1):o}&end={to.UtcDateTime.AddHours(1):o}";
var resp = await _client.GetAsync(url);
resp.EnsureSuccessStatusCode();
var agileResponse = await JsonSerializer.DeserializeAsync<AwattarResponse>(await resp.Content.ReadAsStreamAsync());
if (agileResponse == null)
{
throw new Exception($"Deserialization of aWATTar API response failed");
}
if (agileResponse.Results.Any(x => x.Unit != "Eur/MWh"))
{
throw new Exception($"Unknown price unit(s) detected from aWATTar API: {string.Join(", ", agileResponse.Results.Select(x => x.Unit).Distinct())}");
}
return agileResponse.Results.Select(x => new Price
{
Value = (x.MarketPrice / 1000) * _options.VATMultiplier,
ValidFrom = DateTimeOffset.FromUnixTimeSeconds(x.StartTimestamp / 1000),
ValidTo = DateTimeOffset.FromUnixTimeSeconds(x.EndTimestamp / 1000)
});
}

public class AwattarPrice
{
[JsonPropertyName("marketprice")]
public decimal MarketPrice { get; set; }

[JsonPropertyName("unit")]
public string Unit { get; set; }

[JsonPropertyName("start_timestamp")]
public long StartTimestamp { get; set; }

[JsonPropertyName("end_timestamp")]
public long EndTimestamp { get; set; }
}

public class AwattarResponse
{
[JsonPropertyName("data")]
public List<AwattarPrice> Results { get; set; }
}

public Task<IEnumerable<Price>> GetPriceData(DateTimeOffset from, DateTimeOffset to, string? configString)
{
throw new NotImplementedException();
}
}
91 changes: 91 additions & 0 deletions TeslaSolarCharger.GridPriceProvider/Services/EnerginetService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//using Microsoft.Extensions.Options;
//using System.Text.Json;
//using System.Text.Json.Serialization;
//using TeslaSolarCharger.GridPriceProvider.Data;
//using TeslaSolarCharger.GridPriceProvider.Data.Options;
//using TeslaSolarCharger.GridPriceProvider.Services.Interfaces;

//namespace TeslaSolarCharger.GridPriceProvider.Services;

//public class EnerginetService : IPriceDataService
//{
// private readonly HttpClient _client;
// private readonly EnerginetOptions _options;
// private readonly FixedPriceService _fixedPriceService;

// public EnerginetService(HttpClient client, IOptions<EnerginetOptions> options)
// {
// _client = client;
// _options = options.Value;

// if (_options.FixedPrices != null)
// {
// _fixedPriceService = new FixedPriceService(Options.Create(_options.FixedPrices));
// }
// }

// public async Task<IEnumerable<Price>> GetPriceData(DateTimeOffset from, DateTimeOffset to)
// {
// var url = "Elspotprices?offset=0&start=" + from.AddHours(-2).AddMinutes(-1).UtcDateTime.ToString("yyyy-MM-ddTHH:mm") + "&end=" + to.AddHours(2).AddMinutes(1).UtcDateTime.ToString("yyyy-MM-ddTHH:mm") + "&filter={\"PriceArea\":[\"" + _options.Region + "\"]}&sort=HourUTC ASC&timezone=dk".Replace(@"\", string.Empty); ;
// var resp = await _client.GetAsync(url);

// resp.EnsureSuccessStatusCode();

// var prices = new List<Price>();
// var EnerginetResponse = await JsonSerializer.DeserializeAsync<EnerginetResponse>(await resp.Content.ReadAsStreamAsync());

// if (EnerginetResponse.Records.Count > 0)
// {
// foreach (var record in EnerginetResponse.Records)
// {
// decimal fixedPrice = 0;
// if (_fixedPriceService != null)
// {
// var fixedPrices = await _fixedPriceService.GetPriceData(record.HourUTC, record.HourUTC.AddHours(1));
// fixedPrice = fixedPrices.Sum(p => p.Value);
// }

// var spotPrice = _options.Currency switch
// {
// EnerginetCurrency.DKK => record.SpotPriceDKK,
// EnerginetCurrency.EUR => record.SpotPriceEUR,
// _ => throw new ArgumentOutOfRangeException(nameof(_options.Currency)),
// };

// var price = ((spotPrice / 1000) + fixedPrice);
// if (_options.VAT.HasValue)
// {
// price *= _options.VAT.Value;
// }
// prices.Add(new Price
// {
// ValidFrom = record.HourUTC,
// ValidTo = record.HourUTC.AddHours(1),
// Value = price
// });
// }
// }

// return prices;
// }

// private class EnerginetResponse
// {
// [JsonPropertyName("records")]
// public List<EnerginetResponseRow> Records { get; set; }
// }

// private class EnerginetResponseRow
// {
// private DateTime _hourUTC;

// [JsonPropertyName("HourUTC")]
// public DateTime HourUTC { get => _hourUTC; set => _hourUTC = DateTime.SpecifyKind(value, DateTimeKind.Utc); }

// [JsonPropertyName("SpotPriceDKK")]
// public decimal SpotPriceDKK { get; set; }

// [JsonPropertyName("SpotPriceEUR")]
// public decimal SpotPriceEUR { get; set; }
// }
//}
Loading

0 comments on commit d2e10b1

Please sign in to comment.