-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #947 from pkuehnel/develop
Develop
- Loading branch information
Showing
54 changed files
with
2,293 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
TeslaSolarCharger.GridPriceProvider/Data/Options/AwattarOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
34 changes: 34 additions & 0 deletions
34
TeslaSolarCharger.GridPriceProvider/Data/Options/EnerginetOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
8 changes: 8 additions & 0 deletions
8
TeslaSolarCharger.GridPriceProvider/Data/Options/FixedPriceOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
15 changes: 15 additions & 0 deletions
15
TeslaSolarCharger.GridPriceProvider/Data/Options/HomeAssistantOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
TeslaSolarCharger.GridPriceProvider/Data/Options/OctopusOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
TeslaSolarCharger.GridPriceProvider/Data/Options/TibberOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
TeslaSolarCharger.GridPriceProvider/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
68
TeslaSolarCharger.GridPriceProvider/Services/AwattarService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
91
TeslaSolarCharger.GridPriceProvider/Services/EnerginetService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
// } | ||
//} |
Oops, something went wrong.