-
Notifications
You must be signed in to change notification settings - Fork 0
/
tibber.cpp
211 lines (160 loc) · 6.52 KB
/
tibber.cpp
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
201
202
203
204
205
206
207
208
209
210
211
/*
Energy Price Calculator
Copyright (C) 2022 Jeremias Bosch
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "tibber.h"
#include "credentials.h"
#include <ArduinoJson.h>
namespace CONSTANTS {
// tibber API
const char* API_SERVER = "api.tibber.com"; // Server URL
const int HTTPS_PORT = 443;
// SHA1 Fingerprint of api.tibber.com certificate
const char SSL_FINGERPRINT[] PROGMEM = "01 E9 FC DD 05 1D 0B C3 21 DB 38 0E 8C C1 10 4E 38 CF 40 DF";
}
Tibber::Tibber() {
}
void Tibber::initialize () {
m_client.setFingerprint(CONSTANTS::SSL_FINGERPRINT);
m_initialized = true;
}
const TibberResponse& Tibber::read() {
m_lastResponse = TibberResponse();
if (connectToWebserver()) {
if (queryAndParsePriceInformation()) {}
}
return m_lastResponse;
}
const TibberResponse& Tibber::lastResponse() const{
return m_lastResponse;
}
bool Tibber::connectToWebserver() {
if (!m_initialized) {
return false;
}
int r = 0; //retry counter
while ((!m_client.connect(CONSTANTS::API_SERVER, CONSTANTS::HTTPS_PORT)) && (r < 30)) {
delay(100);
Serial.print(".");
r++;
}
if (r == 30) {
Serial.println("Connection failed");
// displayError("Server Connection Failed");
delay(1000);
return false;
}
else {
Serial.println("Connected to web");
}
return true;
}
bool Tibber::queryAndParsePriceInformation() {
DynamicJsonDocument doc(6000);
doc["query"] = "{ viewer { homes { currentSubscription { priceInfo { today { total startsAt } tomorrow { total startsAt } current { total startsAt level } } } } } }";
m_client.println("POST /v1-beta/gql HTTP/1.1");
m_client.println("Host: " + String(CONSTANTS::API_SERVER));
m_client.println("Authorization: Bearer " + String(TIBBER_TOKEN));
m_client.println("Content-Type: application/json");
m_client.print("Content-Length: ");
m_client.println(measureJson(doc));
m_client.println();
serializeJson(doc, m_client);
m_client.println();
delay(100);
while (m_client.connected()) {
Serial.println("connected read headers");
String line = m_client.readStringUntil('\n');
if (line == "\r") {
break;
}
}
String response = "";
delay(300);
// if there are incoming bytes available
// from the server, read them and print them:
while (m_client.available()) {
char c = m_client.read();
response = response + c;
}
m_client.stop();
// Deserialize the resonse into the JSON document
// Reuse the existing document
DeserializationError error = deserializeJson(doc, response);
// Test if parsing succeeds.
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return false;
}
// read the current price informations from response json
// let us assume the API is stable and we will know about changes a
// TODO: add some checks
// TODO: make this look nicer
m_lastResponse.current.price = doc["data"]["viewer"]["homes"][0]["currentSubscription"]["priceInfo"]["current"]["total"];
m_lastResponse.current.sinceDate = doc["data"]["viewer"]["homes"][0]["currentSubscription"]["priceInfo"]["current"]["startsAt"].as<String>();
m_lastResponse.current.level = doc["data"]["viewer"]["homes"][0]["currentSubscription"]["priceInfo"]["current"]["level"].as<String>();
// m_lastResponse.current.sinceDate = isoDateTimeStringToClock(m_lastResponse.current.sinceDate);
// those are JsonArrays
// containting the hourly prices
JsonArray today = doc["data"]["viewer"]["homes"][0]["currentSubscription"]["priceInfo"]["today"];
JsonArray tomorrow = doc["data"]["viewer"]["homes"][0]["currentSubscription"]["priceInfo"]["tomorrow"];
// do not consider historical data for today
// there is no timemachine to go back and use a lower price (well, maybe there is, but I'm pretty sure the energy consumption must be paid in the current price range, though...
bool considerTodaysInfo = false;
int currentPriceInTodaysIndex = 0;
// todo use limits
m_lastResponse.todayHighestPrice = -1;
m_lastResponse.todayLowestPrice = 10;
for (int i = 0; i < today.size(); ++i) {
if (String(today[i]["startsAt"]) == m_lastResponse.current.sinceDate) {
considerTodaysInfo = true;
currentPriceInTodaysIndex = i;
}
if (!considerTodaysInfo) {
continue;
}
TibberPriceInfo info;
info.price = today[i]["total"];
info.sinceDate = static_cast<String>(today[i]["startsAt"]);
m_lastResponse.today.push_back(info);
if (today[i]["total"] > m_lastResponse.todayHighestPrice) {
m_lastResponse.todayHighestPrice = today[i]["total"];
m_lastResponse.todayHighestPriceStartsAt = static_cast<String>(today[i]["startsAt"]);
}
if (today[i]["total"] < m_lastResponse.todayLowestPrice) {
m_lastResponse.todayLowestPrice = today[i]["total"];
m_lastResponse.todayLowestPriceStartsAt = static_cast<String>(today[i]["startsAt"]);
}
}
// no data yet for tomorrow
if (tomorrow.size() == 0) {
m_lastResponse.tomorrowHighestPrice = -1;
m_lastResponse.tomorrowLowestPrice = 10;
}
for (int i = 0; i < tomorrow.size(); ++i) {
TibberPriceInfo info;
info.price = tomorrow[i]["total"];
info.sinceDate = static_cast<String>(tomorrow[i]["startsAt"]);
m_lastResponse.tomorrow.push_back(info);
if (tomorrow[i]["total"] > m_lastResponse.tomorrowHighestPrice) {
m_lastResponse.tomorrowHighestPrice = tomorrow[i]["total"];
m_lastResponse.tomorrowHighestPriceStartsAt = static_cast<String>(tomorrow[i]["startsAt"]);
}
if (tomorrow[i]["total"] < m_lastResponse.tomorrowLowestPrice) {
m_lastResponse.tomorrowLowestPrice = tomorrow[i]["total"];
m_lastResponse.tomorrowLowestPriceStartsAt = static_cast<String>(tomorrow[i]["startsAt"]);
}
}
return true;
}