-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnordpool.cpp
83 lines (62 loc) · 2.26 KB
/
nordpool.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
#include "nordpool.h"
#include "app.h"
#include "args.h"
#include "common.h"
#include "json.h"
#include <QDateTime>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QStringLiteral>
#include <QUrl>
#include <fmt/format.h>
namespace El {
// -----------------------------------------------------------------------------
NordPool::NordPool(App const &app, QObject *parent)
: QObject(parent)
, _app(app)
{}
NordPool::~NordPool() = default;
auto NordPool::get_prices(QString const ®ion, int start_h, int end_h) -> PriceBlocks
{
using namespace Qt::Literals::StringLiterals;
constexpr char const *URL = "https://dashboard.elering.ee";
auto const start = to_datetime(start_h);
auto const end = to_datetime(end_h);
fmt::print("Küsin võrgust Nord Pool hindasid perioodile {} ... {}\n", start, end);
// create the network access manager if needed
if (_manager == nullptr) {
_manager = new QNetworkAccessManager{this};
connect(_manager, &QNetworkAccessManager::finished, this, [this](QNetworkReply *) { _done = true; });
}
// prepare the request
auto const query = QStringLiteral(u"%1/api/nps/price?start=%2&end=%3")
.arg(URL,
start.toUTC().toString(u"yyyy-MM-ddThh\'\%3A\'mm\'\%3A\'ss.zzzZ"_s),
end.toUTC().toString(u"yyyy-MM-ddThh\'\%3A\'mm\'\%3A\'ss.zzzZ"_s));
if (_app.args().verbose()) {
fmt::print("GET {}\n", query);
}
QNetworkRequest rqst{};
rqst.setUrl(QUrl{query});
rqst.setRawHeader("accept", "*/*");
_done = false;
auto *reply = _manager->get(rqst);
if (reply == nullptr) {
throw Exception{"võrgupäring ebaõnnestus"};
}
// wait for the results
constexpr int MAX_TIME_MS = 5000;
if (!App::wait_for(_done, MAX_TIME_MS)) {
throw Exception{"võrgupäring aegus"};
}
// check for errors
if (reply->error() != QNetworkReply::NoError) {
throw Exception{fmt::format("võrgupäring ebaõnnestus: {}", reply->errorString())};
}
auto prices = Json::from_json(reply->readAll(), region);
// delete the reply
reply->deleteLater();
return prices.prices();
}
} // namespace El