-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
TibberService.cs
200 lines (179 loc) · 6.31 KB
/
TibberService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using GraphQL;
using GraphQL.Client.Abstractions;
using GraphQL.Client.Http;
using Microsoft.Extensions.Options;
using System.Net.Http.Headers;
using System.Reflection;
using System.Text;
using System.Text.Json;
using TeslaMateAgile.Data;
using TeslaMateAgile.Data.Options;
using TeslaMateAgile.Services.Interfaces;
namespace TeslaMateAgile.Services;
public class TibberService : IDynamicPriceDataService
{
private readonly HttpClient _client;
private readonly GraphQLHttpClientOptions _graphQLHttpClientOptions;
private readonly TibberOptions _options;
private readonly IGraphQLJsonSerializer _graphQLJsonSerializer;
private readonly static ProductInfoHeaderValue _userAgent = new(nameof(TeslaMateAgile), Assembly.GetExecutingAssembly().GetName().Version.ToString());
public TibberService(
HttpClient client,
IGraphQLJsonSerializer graphQLJsonSerializer,
IOptions<TibberOptions> options
)
{
_client = client;
_options = options.Value;
_graphQLHttpClientOptions = new GraphQLHttpClientOptions { EndPoint = new Uri(_options.BaseUrl), DefaultUserAgentRequestHeader = _userAgent };
_graphQLJsonSerializer = graphQLJsonSerializer;
}
public async Task<IEnumerable<Price>> GetPriceData(DateTimeOffset from, DateTimeOffset to)
{
var fetch = (int)Math.Ceiling((to - from).TotalHours) + 2;
var request = new GraphQLHttpRequest
{
Query = @"
query PriceData($after: String, $first: Int) {
viewer {
homes {
id,
currentSubscription {
priceInfo {
range(resolution: HOURLY, after: $after, first: $first) {
nodes {
total
startsAt
}
}
current {
total
startsAt
level
}
}
}
}
}
}
",
OperationName = "PriceData",
Variables = new
{
after = Convert.ToBase64String(Encoding.UTF8.GetBytes(from.AddHours(-1).ToString("o"))),
first = fetch
}
};
var graphQLHttpResponse = await SendRequest(request);
var homes = graphQLHttpResponse
.Data
.Viewer
.Homes;
Home home;
if (_options.HomeId != Guid.Empty)
{
home = homes.FirstOrDefault(x => x.Id == _options.HomeId);
if (home == null)
{
throw new Exception($"Home with id {_options.HomeId} not found");
}
}
else
{
home = homes.First();
}
var priceInfo = home
.CurrentSubscription
.PriceInfo;
var prices = priceInfo
.Range
.Nodes
.Select(x => new Price
{
ValidFrom = x.StartsAt,
ValidTo = x.StartsAt.AddHours(1),
Value = x.Total
})
.ToList();
var count = prices.Count();
// The Tibber range API only returns historical and does not include the current price
// This will add the current price to the list if it should be in there but isn't
if (count + 1 == fetch
&& priceInfo.Current.StartsAt >= from.AddHours(-1)
&& priceInfo.Current.StartsAt < to
&& !prices.Any(x => x.ValidFrom == priceInfo.Current.StartsAt)
)
{
prices.Add(new Price
{
ValidFrom = priceInfo.Current.StartsAt,
ValidTo = priceInfo.Current.StartsAt.AddHours(1),
Value = priceInfo.Current.Total
});
}
else if (count != fetch)
{
throw new Exception($"Mismatch of requested price info from Tibber API (expected: {fetch}, actual: {count})");
}
return prices;
}
private async Task<GraphQLHttpResponse<ResponseType>> SendRequest(GraphQLHttpRequest request)
{
using var httpRequestMessage = request.ToHttpRequestMessage(_graphQLHttpClientOptions, _graphQLJsonSerializer);
using var httpResponseMessage = await _client.SendAsync(httpRequestMessage);
var contentStream = await httpResponseMessage.Content.ReadAsStreamAsync();
if (httpResponseMessage.IsSuccessStatusCode)
{
var graphQLResponse = await JsonSerializer.DeserializeAsync<GraphQLResponse<ResponseType>>(contentStream, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
if (graphQLResponse == null)
{
throw new Exception($"Deserialization of Tibber API response failed");
}
var graphQLHttpResponse = graphQLResponse.ToGraphQLHttpResponse(httpResponseMessage.Headers, httpResponseMessage.StatusCode);
if (graphQLHttpResponse.Errors?.Any() ?? false)
{
var errorMessages = string.Join(", ", graphQLHttpResponse.Errors.Select(x => x.Message));
throw new HttpRequestException($"Failed to call Tibber API: {errorMessages}");
}
return graphQLHttpResponse;
}
string content = null;
if (contentStream != null)
{
using var sr = new StreamReader(contentStream);
content = await sr.ReadToEndAsync();
}
throw new GraphQLHttpRequestException(httpResponseMessage.StatusCode, httpResponseMessage.Headers, content);
}
private class ResponseType
{
public Viewer Viewer { get; set; }
}
private class Viewer
{
public List<Home> Homes { get; set; }
}
private class Home
{
public Guid Id { get; set; }
public Subscription CurrentSubscription { get; set; }
}
private class Subscription
{
public PriceInfo PriceInfo { get; set; }
}
private class PriceInfo
{
public RangeInfo Range { get; set; }
public Node Current { get; set; }
}
private class RangeInfo
{
public List<Node> Nodes { get; set; }
}
private class Node
{
public decimal Total { get; set; }
public DateTimeOffset StartsAt { get; set; }
}
}