-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
248 lines (210 loc) · 7.68 KB
/
main.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
#include <iostream>
#include <locale>
#include <cstdlib>
#include <sstream>
#include <algorithm>
#include <memory>
#include <limits>
#include <SQLiteCpp/SQLiteCpp.h>
#include "httphandler.hpp"
#include "gitversion.hpp"
#if !defined(DB_PATH)
#define DB_PATH "/srv/http/data/vbus.sqlite"
#endif
#define CSV_HEADER "Datum,Ofen,Speicher-unten,Speicher-oben,Heizung,Ventil-Ofen,Ventil-Heizung"
#define SELECT_TIMESPAN "SELECT" \
" datetime(time, 'localtime') as time," \
" temp1, temp2, temp3, temp4, pump1, pump2" \
" FROM data" \
" WHERE time > (SELECT DATETIME('now', ?))" \
" ORDER BY time"
#define SELECT_TIMESPAN_RANGE "SELECT" \
" datetime(time, 'localtime') as time," \
" temp1, temp2, temp3, temp4, pump1, pump2" \
" FROM data" \
" WHERE time > (SELECT DATETIME(?, 'utc')) AND time < (SELECT DATETIME(?, 'utc', ?))" \
" ORDER BY time"
#define SELECT_SINGLE "SELECT" \
" datetime(time, 'localtime') as time," \
" temp1, temp2, temp3, temp4, pump1, pump2" \
" FROM data" \
" ORDER BY id DESC LIMIT 1"
#define SELECT_CURRENT "SELECT" \
" datetime(time, 'localtime') as time," \
" temp1, temp2, temp3, temp4, pump1, pump2" \
" FROM data" \
" WHERE time > (SELECT DATETIME('now', '-5 minutes'))" \
" ORDER BY id DESC LIMIT 1"
void printCsvResult(SQLite::Statement& query, std::ostream& stream, bool isFirstRow, bool clip);
void printJsonResult(SQLite::Statement& query, std::ostream& stream, bool isFirstRow);
// See: http://stackoverflow.com/questions/15220861/how-can-i-set-the-comma-to-be-a-decimal-point
class punct_facet: public std::numpunct<char>
{
protected:
char do_decimal_point() const {
return '.';
}
};
int main(int argc, char const *argv[])
{
for (size_t idx = 1; idx < argc; idx++)
{
auto arg = std::string(argv[idx]);
if (arg == "-v" || arg == "--version")
{
std::cout << "vbus-server " << GitMetadata::Version() << std::endl;
return 0;
}
}
// Set decimal seperator to '.'
std::cout.imbue(std::locale(std::cout.getloc(), new punct_facet()));
double minTemp[4];
double maxTemp[4];
std::fill_n(minTemp, 4, std::numeric_limits<double>::infinity());
std::fill_n(maxTemp, 4, -std::numeric_limits<double>::infinity());
// Get HTTP handler and set default paramter
HttpHandler http(std::getenv("HTTP_ACCEPT_ENCODING"), std::getenv("REQUEST_URI"), std::cout);
http.setDefault("timespan", "-1 hour");
http.setDefault("format", "csv");
http.setDefault("start", "now");
http.setDefault("clip", "0");
// Print HTTP header
if (http.param("format") == "csv")
{
http.setContentType("text/comma-separated-values");
}
else if (http.param("format") == "json")
{
http.setContentType("application/json");
}
http.sendHeader();
auto outputStream = http.getStream();
try
{
SQLite::Database db(DB_PATH);
db.setBusyTimeout(3000);
// Build sqlite query
std::unique_ptr<SQLite::Statement> query;
if (http.param("timespan") == "single")
{
query.reset(new SQLite::Statement(db, SELECT_SINGLE));
}
else if (http.param("timespan") == "current")
{
query.reset(new SQLite::Statement(db, SELECT_CURRENT));
}
else
{
if (http.param("start") != "now")
{
query.reset(new SQLite::Statement(db, SELECT_TIMESPAN_RANGE));
query->bind(1, http.param("start"));
query->bind(2, http.param("start"));
query->bind(3, http.param("timespan"));
}
else
{
query.reset(new SQLite::Statement(db, SELECT_TIMESPAN));
query->bind(1, http.param("timespan"));
}
}
if (http.param("format") == "csv")
{
*outputStream << CSV_HEADER << std::endl;
}
else if (http.param("format") == "json")
{
*outputStream << "{\"data\":[" << std::endl;
}
bool isFirstRow = true;
while (query->executeStep())
{
if (http.param("format") == "csv")
{
printCsvResult(*query, *outputStream, isFirstRow, http.param("clip") == "1");
}
else if (http.param("format") == "json")
{
printJsonResult(*query, *outputStream, isFirstRow);
}
// Update min/max stats
for (int idx=0; idx<4; idx++)
{
minTemp[idx] = std::min(minTemp[idx], query->getColumn(idx+1).getDouble());
maxTemp[idx] = std::max(maxTemp[idx], query->getColumn(idx+1).getDouble());
}
http.process();
isFirstRow = false;
}
// Add min/max stats to json response
if (http.param("format") == "json")
{
if (http.param("timespan") == "single" || http.param("timespan") == "current")
{
*outputStream << std::endl << "]}" << std::endl;
}
else
{
*outputStream << std::endl << "]";
for (int idx=0; idx<4; idx++)
{
*outputStream << ",\"temp" << idx+1 << "\": {\"min\": " << minTemp[idx] << ", \"max\": " << maxTemp[idx] << "}" << std::endl;
}
*outputStream << "}";
}
http.process();
}
http.flush();
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
}
void printCsvResult(SQLite::Statement& query, std::ostream& stream, bool isFirstRow, bool clip)
{
stream << query.getColumn(0).getText() << ","
<< query.getColumn(1).getDouble() << ","
<< query.getColumn(2).getDouble() << ","
<< query.getColumn(3).getDouble() << ","
<< query.getColumn(4).getDouble() << ",";
if (clip && query.getColumn(5).getDouble() > query.getColumn(1).getDouble())
{
stream << query.getColumn(1).getDouble() << ",";
}
else if (clip && query.getColumn(5).getInt() == 0)
{
stream << "NaN,";
}
else
{
stream << query.getColumn(5).getDouble() << ",";
}
if (clip && query.getColumn(6).getDouble() > query.getColumn(3).getDouble())
{
stream << query.getColumn(3).getDouble() << std::endl;
}
else if (clip && query.getColumn(6).getInt() == 0)
{
stream << "NaN" << std::endl;
}
else
{
stream << query.getColumn(6).getDouble() << std::endl;
}
}
void printJsonResult(SQLite::Statement& query, std::ostream& stream, bool isFirstRow)
{
if (!isFirstRow) {
stream << "," << std::endl;
}
stream << "{\"timestamp\":\"" << query.getColumn(0).getText() << "\","
<< "\"temp1\":" << query.getColumn(1).getDouble() << ","
<< "\"temp2\":" << query.getColumn(2).getDouble() << ","
<< "\"temp3\":" << query.getColumn(3).getDouble() << ","
<< "\"temp4\":" << query.getColumn(4).getDouble() << ","
<< "\"valve1\":" << query.getColumn(5).getInt() << ","
<< "\"valve2\":" << query.getColumn(6).getInt() << "}";
}