-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.cpp
151 lines (124 loc) · 3.72 KB
/
app.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
#include "app.h"
#include "args.h"
#include "common.h"
#include "consumption.h"
#include "prices.h"
#include <QDateTime>
#include <QTimer>
#include <fmt/format.h>
#include <stdio.h>
namespace El {
// -----------------------------------------------------------------------------
App::App(Args const &args, int &argc, char **argv)
: QCoreApplication(argc, argv)
, _args(args)
, _consumption(new Consumption{*this, this})
{
QTimer::singleShot(0, this, &App::process);
}
App::~App() = default;
bool App::wait_for(bool const &flag, int ms)
{
auto const start = QTime::currentTime().msecsSinceStartOfDay();
// process events until `flag` becomes true or timeout
while (!flag && ((QTime::currentTime().msecsSinceStartOfDay() - start) < ms)) {
processEvents(QEventLoop::AllEvents | QEventLoop::WaitForMoreEvents, 100);
}
return flag;
}
void App::process()
{
// Load the CSV file
if (!_consumption->load(_args.fileName())) {
exit(1);
return;
}
// Load or request Nord Pool prices
if (_args.prices()) {
_prices = new Prices{*this, this};
if (!_prices->load(_args.region(), _consumption->first_record_time(), _consumption->last_record_time())) {
exit(1);
return;
}
}
// calculate and show results
if (!calc()) {
exit(1);
}
quit();
}
bool App::calc()
{
// Calculate and show totals
if (!calc_summary()) {
return false;
}
if (!show_summary()) {
return false;
}
return true;
}
bool App::calc_summary()
{
// VAT multiplier
auto const vat = 1.0 + _args.km();
for (auto const &rec : _consumption->records()) {
// Sum kWh
if (rec.isNight()) {
_night_kwh += rec.kWh();
}
else {
_day_kwh += rec.kWh();
}
// Sum cost
if (_prices) {
auto const price = _prices->get_price(rec.startTime());
if (!price) {
fmt::print("WARNING: puudub hinnainfo ajale {}\n", rec.startTime());
continue;
}
auto const cost = price.value() * rec.kWh();
auto const margin = (_args.margin() * rec.kWh()) / vat;
if (_args.verbose()) {
fmt::print("\t{}\t{:.3f} kWh\t{:.3f} EUR\t@{:.4f} EUR\n",
rec.startTime(),
rec.kWh(),
(cost + margin) * vat,
price.value() * vat);
}
if (rec.isNight()) {
_night_eur += (cost + margin);
}
else {
_day_eur += (cost + margin);
}
}
}
return true;
}
bool App::show_summary()
{
// VAT multipler
auto const vat = 1.0 + _args.km();
if (_args.startDay() && _args.startNight()) {
fmt::print("arvesti näit\n\töö: {:10.3f}\tpäev: {:10.3f}\n",
_args.startNight().value() + _night_kwh,
_args.startDay().value() + _day_kwh);
}
fmt::print("kulu kWh\n\töö: {:10.3f} kWh\tpäev: {:10.3f} kWh\tkokku: {:10.3f} kWh\n",
_night_kwh,
_day_kwh,
_night_kwh + _day_kwh);
if (_prices) {
fmt::print("kulu EUR\n\töö: {:10.2f} EUR\tpäev: {:10.2f} EUR\tkokku: {:10.2f} EUR\n",
_night_eur * vat,
_day_eur * vat,
(_night_eur + _day_eur) * vat);
fmt::print("hind EUR/kWh\n\töö: {:6.4f} EUR/kWh\tpäev: {:6.4f} EUR/kWh\tkeskmine: {:6.4f} EUR/kWh\n",
(_night_eur / _night_kwh) * vat,
(_day_eur / _day_kwh) * vat,
((_night_eur + _day_eur) / (_night_kwh + _day_kwh)) * vat);
}
return true;
}
} // namespace El