-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.cpp
142 lines (115 loc) · 4.43 KB
/
calculator.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
/*
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 "calculator.h"
#include "consumptionProfiles.h"
std::vector<CalculationResponse>
Calculator::allPricesForProfile(const TibberResponse& response,
const std::vector<double>& profile) {
std::vector<CalculationResponse> calculationResponse;
time_t now;
struct tm * timeinfo;
time(&now);
timeinfo = localtime(&now);
int currentHour = timeinfo->tm_hour;
int currentMinute = timeinfo->tm_min;
for (int i = 0; i < response.today.size(); ++i) {
double cost = calculateCost( response.today,
response.tomorrow,
currentHour,
currentMinute,
i,
profile);
if (cost > 0) {
calculationResponse.push_back( {
calculationResponse.size(),
isoDateTimeStringToClock(response.today.at(i).sinceDate),
cost
});
}
currentHour += 1;
// dont adjust the minute - timers in many devices only allow to adjust the hour!
}
currentHour = 0;
for (int i = 0; i < response.tomorrow.size(); ++i) {
double cost = calculateCost( {}, // do not consider today
response.tomorrow,
currentHour,
currentMinute,
i,
profile);
if (cost > 0) {
calculationResponse.push_back( {
calculationResponse.size(),
isoDateTimeStringToClock(response.tomorrow.at(i).sinceDate),
cost
});
}
currentHour += 1;
if (currentHour > 23) {
currentHour = 0;
}
}
return calculationResponse;
}
double Calculator::calculateCost(const std::vector<TibberPriceInfo>& today,
const std::vector<TibberPriceInfo>& tomorrow,
int currentHour,
int currentMinute,
int startingIndex,
const std::vector<double>& profile)
{
double totalCostOfAction = 0;
int currentPriceIndex = startingIndex;
auto *priceSource = today.size() > 0 ? &today : &tomorrow;
for (int i = 0; i < profile.size(); ++i) {
if (priceSource->size() <= currentPriceIndex) {
return -1;
}
totalCostOfAction += profile.at(i) / 1000.0 * priceSource->at(currentPriceIndex).price;
// each profile entry is for 10 minutes
currentMinute += ConsumptionProfile::timePerEntry;
// if we are over 60 we add one hour
if (currentMinute > 60) {
// Serial.println("Next price to consider");
currentHour += 1;
currentMinute -= 60;
// there is a now price for each hour, so add one to the the index
currentPriceIndex += 1;
// if we move over the day, lets change the data source
// and reset index and hour
if (currentHour >23) {
if (priceSource != &tomorrow) {
// Serial.println("Next day to consider");
currentHour = 0;
priceSource = &tomorrow;
currentPriceIndex = 0;
} else {
return -1;
}
}
}
}
Serial.println("totalCostOfAction" + String(totalCostOfAction));
return totalCostOfAction;
}
String Calculator::isoDateTimeStringToClock ( String isoDatetime )
{
// read iso time from startsAt and only read the time from it
struct tm tm = {0};
char buf[100];
strptime(isoDatetime.c_str(), "%Y-%m-%dT%H:%M:%S.%fZT", &tm);
strftime(buf, sizeof(buf), "%H:%M", &tm);
return String(buf);
}