-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPvOutput.cpp
257 lines (196 loc) · 6.53 KB
/
PvOutput.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include "PvOutput.h"
#include "EnergyMonitorConfig.h"
#include <sstream>
#include <iomanip>
#include <cstdlib>
#include <curl/curl.h>
using namespace std;
namespace
{
const string cAddOutputUrlConfigName = "PvOutputAddOutputUrl";
const string cStatisticUrlConfigName = "PvOutputStatisticUrl";
const string cApiKeyConfigName = "PvOutputApiKey";
const string cSystemIdConfigName = "PvOutputSystemId";
const string cApiKeyHeaderName = "X-Pvoutput-Apikey";
const string cSystemIdHeaderName = "X-Pvoutput-SystemId";
}
PvOutput::PvOutput(const EnergyMonitorConfig& config) :
myCachedDate(0),
myYesterdayGenerated(.0),
myYesterdayConsumed(.0),
myMonthGenerated(.0),
myMonthConsumed(.0),
myYearGenerated(.0),
myYearConsumed(.0)
{
myAddOutputUrl = config.getValue(cAddOutputUrlConfigName);
myStatisticUrl = config.getValue(cStatisticUrlConfigName);
myApiKey = config.getValue(cApiKeyConfigName);
mySystemId = config.getValue(cSystemIdConfigName);
}
void PvOutput::outputEnergy(time_t startTime, int dayExport, int dayImportPeak, int dayImportOffPeak)
{
stringstream outputUrl;
outputUrl << myAddOutputUrl
<< "?d=" << pvOutputDate(startTime)
<< "&e=" << dayExport
<< "&ip=" << dayImportPeak
<< "&io=" << dayImportOffPeak;
CURL* curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, outputUrl.str().c_str());
curl_slist* list = NULL;
list = curl_slist_append(list, (cApiKeyHeaderName + ": " + myApiKey).c_str());
list = curl_slist_append(list, (cSystemIdHeaderName + ": " + mySystemId).c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
curl_easy_perform(curl);
curl_slist_free_all(list);
curl_easy_cleanup(curl);
}
}
double PvOutput::getGeneratedYesterday()
{
updateStatisticData();
return myYesterdayGenerated;
}
double PvOutput::getConsumedYesterday()
{
updateStatisticData();
return myYesterdayConsumed;
}
double PvOutput::getGeneratedMonth()
{
updateStatisticData();
return myMonthGenerated;
}
double PvOutput::getConsumedMonth()
{
updateStatisticData();
return myMonthConsumed;
}
double PvOutput::getGeneratedYear()
{
updateStatisticData();
return myYearGenerated;
}
double PvOutput::getConsumedYear()
{
updateStatisticData();
return myYearConsumed;
}
void PvOutput::updateStatisticData()
{
time_t now = time(NULL);
tm* timeinfo = localtime(&now);
timeinfo->tm_hour = 12;
timeinfo->tm_min = 0;
timeinfo->tm_sec = 0;
time_t base = mktime(timeinfo);
if (base != myCachedDate)
{
int currentMonth = timeinfo->tm_mon;
// Calculate yesterday
--timeinfo->tm_mday;
time_t yesterday = mktime(timeinfo);
// Calculate one month ago
timeinfo = localtime(&now);
--timeinfo->tm_mon;
++timeinfo->tm_mday;
time_t monthStart = mktime(timeinfo);
if (timeinfo->tm_mon == currentMonth)
{
// Avoid invalid corrections for different month lengths
timeinfo->tm_mday = 1;
monthStart = mktime(timeinfo);
}
// Calculate one year ago
timeinfo = localtime(&now);
--timeinfo->tm_year;
++timeinfo->tm_mday;
time_t yearStart = mktime(timeinfo);
if (getStatisticData(yesterday, yesterday, myYesterdayGenerated, myYesterdayConsumed) &&
getStatisticData(monthStart, yesterday, myMonthGenerated, myMonthConsumed) &&
getStatisticData(yearStart, yesterday, myYearGenerated, myYearConsumed))
{
myCachedDate = base;
}
}
}
bool PvOutput::getStatisticData(time_t startDate, time_t endDate, double& generated, double& consumed)
{
bool result = false;
stringstream statisticUrl;
statisticUrl << myStatisticUrl
<< "?df=" << pvOutputDate(startDate)
<< "&dt=" << pvOutputDate(endDate)
<< "&c=1";
CURL* curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, statisticUrl.str().c_str());
curl_slist* list = NULL;
list = curl_slist_append(list, (cApiKeyHeaderName + ": " + myApiKey).c_str());
list = curl_slist_append(list, (cSystemIdHeaderName + ": " + mySystemId).c_str());
stringstream receivedData;
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &storeCurlData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &receivedData);
CURLcode res = curl_easy_perform(curl);
if (res == CURLE_OK)
{
vector<string> results = splitData(receivedData.str());
if (results.size() > 13)
{
int resGenerated = atoi(results[0].c_str());
int resExported = atoi(results[1].c_str());
int resImported = atoi(results[12].c_str()) + atoi(results[13].c_str());
generated = resGenerated / 1000.;
consumed = (resImported + resGenerated - resExported) / 1000.;
result = true;
}
}
curl_slist_free_all(list);
curl_easy_cleanup(curl);
}
return result;
}
string PvOutput::pvOutputDate(time_t timestamp)
{
stringstream result;
tm* timeinfo = localtime(×tamp);
result << setfill('0');
result << setw(4) << timeinfo->tm_year + 1900;
result << setw(2) << timeinfo->tm_mon + 1;
result << setw(2) << timeinfo->tm_mday;
return result.str();
}
size_t PvOutput::storeCurlData(char* ptr, size_t size, size_t nmemb, void* userdata)
{
stringstream* storage = static_cast<stringstream*>(userdata);
size_t totalSize = size * nmemb;
for (size_t i = 0; i < totalSize; ++i)
{
*storage << static_cast<char>(ptr[i]);
}
return totalSize;
}
vector<string> PvOutput::splitData(const string& data)
{
size_t nrResults = 1;
for (string::const_iterator iter = data.begin(); iter != data.end(); ++iter)
{
++nrResults;
}
vector<string> result(nrResults);
size_t currentElement = 0;
size_t currentPos = 0;
size_t nextSplit;
while ((nextSplit = data.find_first_of(',', currentPos)) != string::npos)
{
result[currentElement++] = data.substr(currentPos, nextSplit - currentPos);
currentPos = nextSplit + 1;
}
result[currentElement] = data.substr(currentPos);
return result;
}