-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathchart.c
389 lines (294 loc) · 10 KB
/
chart.c
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
* chart.c: P4 Charting Client
*
* See the README file for copyright information and how to reach the author.
*
*/
#include <errno.h>
#include <signal.h>
#include "lib/db.h"
#include "lib/common.h"
//***************************************************************************
// Globals
//***************************************************************************
cDbConnection* connection;
cDbTable* sDb;
cDbTable* sfDb;
const char* dbhost = "localhost";
const char* dbname = "";
const char* dbuser = "";
const char* dbpass = "";
const char* filter = "";
int intervall = na;
int dbport = 3306;
int shutdown;
void downF(int aSignal)
{
shutdown = yes;
}
//***************************************************************************
// init / exit
//***************************************************************************
int initDb()
{
static int initialized = no;
if (!initialized)
{
if (dbDict.in("/etc/p4d.dat") != success)
{
tell(eloAlways, "Fatal: Dictionary not loaded, aborting!");
return 1;
}
cDbConnection::init();
cDbConnection::setEncoding("utf8");
cDbConnection::setHost(dbhost);
cDbConnection::setPort(dbport);
cDbConnection::setName(dbname);
cDbConnection::setUser(dbuser);
cDbConnection::setPass(dbpass);
initialized = yes;
}
connection = new cDbConnection();
sDb = new cDbTable(connection, "samples");
if (sDb->open() != success) return fail;
sfDb = new cDbTable(connection, "valuefacts");
if (sfDb->open() != success) return fail;
tell(eloAlways, "Connection to database established");
return success;
}
int exitDb()
{
sDb->close();
sfDb->close();
delete sDb; sDb = 0;
delete sfDb; sfDb = 0;
delete connection; connection = 0;
return done;
}
//***************************************************************************
// Usage
//***************************************************************************
void showUsage(const char* name)
{
printf("Usage: %s [options] - dump actual data to ascii file\n"
" -f <file> - output file\n"
" -h <host> - database host\n"
" -P <port> - database port\n"
" -d <name> - database name\n"
" -u <user> - database user\n"
" -p <pass> - database password\n"
" -l <logvel> - log level {0-4}\n"
" -i <intervall> - intervall in seconds\n"
" -t - log to console\n"
" -F <filter> - get only parameter which match name eg. 'Time, Kesseltemperatur, Abgastemperatur'\n",
name);
}
//***************************************************************************
// Chart
//***************************************************************************
int printActual(FILE* fp, cDbStatement* s, long lastTime)
{
char line[500];
sprintf(line, "Uhrzeit = %s\n", l2pTime(lastTime, "%H:%M").c_str());
fputs(line, fp);
for (int r = s->find(); r; r = s->fetch())
{
const char* f = 0;
char* token = 0;
int min = 0;
int max = 0;
double value = sDb->getRow()->getValue("VALUE")->getFloatValue();
const char* unit = sfDb->getRow()->getValue("UNIT")->getStrValue();
const char* title = sfDb->getRow()->getValue("TITLE")->getStrValue();
const char* utitle = sfDb->getRow()->getValue("USRTITLE")->getStrValue();
const char* text = sDb->getRow()->getValue("TEXT")->getStrValue();
tell(eloDebug, "check '%s' (%s)", utitle, title);
if (!isEmpty(utitle))
title = utitle;
// check and get the filter token for the curren 'title' from the filter string 'filter'
if (!isEmpty(filter) && !(f = strcasestr(filter, title)))
continue;
if (f)
{
int len = strlen(f);
if (const char* p = strchr(f, ','))
len = p-f;
asprintf(&token, "%.*s", len, f);
}
if (token)
{
char* p = token;
// 'token' like "Puffertemperatur oben:Puffer oben"
// Syntax: "<db title>:<display title>:<min>:<max>"
for (int i = 1; (p = strchr(p, ':')); i++)
{
*p = 0;
p++;
switch (i)
{
case 1: title = p; break;
case 2: min = atoi(p); break;
case 3: max = atoi(p); break;
}
}
}
if (strcmp(unit, "°") == 0)
unit = "°C";
if (!isEmpty(text))
fprintf(fp, "%s = %s", title, text);
else if (strcmp(unit, "txt") == 0)
fprintf(fp, "%s = %s", title, l2pTime(value, "%d. %H:%M").c_str());
else if (value != int(value))
fprintf(fp, "%s = %2.1f%s", title, value, unit);
else
fprintf(fp, "%s = %d%s", title, (int)value, unit);
// ------------------------------------------------
// set the color .... #TODO to be configurable !!
const char* color = 0;
// heading title color
if (sfDb->getRow()->getValue("TITLE")->hasValue("Heizungsstatus"))
{
if (sDb->getRow()->getValue("TEXT")->hasValue("Betriebsbereit"))
color = "#34c634";
else if (sDb->getRow()->getValue("TEXT")->hasValue("Heizen"))
color = "#f00";
else if (sDb->getRow()->getValue("TEXT")->hasValue("Anheizen") ||
sDb->getRow()->getValue("TEXT")->hasValue("Vorwärmen") ||
sDb->getRow()->getValue("TEXT")->hasValue("Zünden") ||
sDb->getRow()->getValue("TEXT")->hasValue("Vorbereitung"))
color = "#ffb725";
else if (sDb->getRow()->getValue("TEXT")->hasValue("STÖRUNG"))
color = "yellow";
else
color = "blue";
if (!isEmpty(color))
fprintf(fp, " color %s", color);
}
// temp value color
if ((min || max) && sfDb->getRow()->getValue("UNIT")->hasValue("°"))
{
if (min && value < min)
color = "blue";
if (max && value > max)
color = "red";
if (!isEmpty(color))
fprintf(fp, " vcolor %s", color);
}
fprintf(fp, "\n");
free(token);
}
return done;
}
//***************************************************************************
// Actual
//***************************************************************************
int actualAscii(const char* file)
{
FILE* fp {};
// select max(time) from samples;
cDbStatement* selMaxTime = new cDbStatement(sDb);
selMaxTime->build("select max(");
selMaxTime->bind("TIME", cDBS::bndOut);
selMaxTime->build(") from %s;", sDb->TableName());
selMaxTime->prepare();
// select s.value, f.name, f.title, f.unit
// from samples s, valuefacts f
// where s.address = f.address and s.type = f.type and s.time = ?;
cDbStatement* s = new cDbStatement(sDb);
s->build("select ");
s->setBindPrefix("s.");
s->bind("VALUE", cDBS::bndOut);
s->bind("TEXT", cDBS::bndOut, ", ");
s->setBindPrefix("f.");
s->bind(sfDb->getValue("NAME"), cDBS::bndOut, ", ");
s->bind(sfDb->getValue("TITLE"), cDBS::bndOut, ", ");
s->bind(sfDb->getValue("USRTITLE"), cDBS::bndOut, ", ");
s->bind(sfDb->getValue("UNIT"), cDBS::bndOut, ", ");
s->build(" from %s s, %s f where ", sDb->TableName(), sfDb->TableName());
s->build("s.address = f.address ");
s->build("and s.type = f.type ");
s->setBindPrefix("s.");
s->bind(sDb->getValue("TIME"), cDBS::bndIn | cDBS::bndSet, "and ");
s->build(";");
s->prepare();
shutdown = no;
while (!shutdown)
{
if (isEmpty(file) || strcmp(file, "-") == 0)
fp = stdout;
else
fp = fopen(file, "w");
if (!fp)
{
tell(eloAlways, "Error: Can't open file '%s' for writing, error was '%s'", file, strerror(errno));
return 0;
}
sDb->clear();
// get last Time (newest data)
if (!selMaxTime->find())
break;
long lastTime = sDb->getRow()->getValue("TIME")->getTimeValue();
selMaxTime->freeResult();
// get values
sDb->clear();
sfDb->clear();
sDb->setValue("TIME", lastTime);
// print the values
printActual(fp, s, lastTime);
tell(eloAlways, "updated values");
s->freeResult();
if (fp && fp != stdout)
fclose(fp);
if (intervall == na)
break;
sleep(intervall);
}
tell(eloAlways, "Shutdown regular");
delete selMaxTime;
delete s;
return 0;
}
//***************************************************************************
// Main
//***************************************************************************
int main(int argc, char** argv)
{
const char* file = 0;
eloquence = eloAlways;
logstdout = no;
if (argc == 1 || (argc > 1 && (argv[1][0] == '?' || (strcmp(argv[1], "-h") == 0) || (strcmp(argv[1], "--help") == 0))))
{
showUsage(argv[0]);
return 0;
}
// Parse command line
for (int i = 0; argv[i]; i++)
{
if (argv[i][0] != '-' || strlen(argv[i]) != 2)
continue;
switch (argv[i][1])
{
case 'l': if (argv[i+1]) eloquence = (Eloquence)atoi(argv[++i]); break;
case 'P': if (argv[i+1]) dbport = atoi(argv[++i]); break;
case 'd': if (argv[i+1]) dbname = argv[++i]; break;
case 'h': if (argv[i+1]) dbhost = argv[++i]; break;
case 'u': if (argv[i+1]) dbuser = argv[++i]; break;
case 'p': if (argv[i+1]) dbpass = argv[++i]; break;
case 'f': if (argv[i+1]) file = argv[++i]; break;
case 'F': if (argv[i+1]) filter = argv[++i]; break;
case 'i': if (argv[i+1]) intervall = atoi(argv[++i]); break;
case 't': logstdout = yes; break;
}
}
// init database connection
if (initDb() != success)
return 1;
// register SIGINT
::signal(SIGINT, downF);
::signal(SIGTERM, downF);
actualAscii(file);
// exit
exitDb();
cDbConnection::exit();
return 0;
}