-
Notifications
You must be signed in to change notification settings - Fork 1
/
class.Database.php
executable file
·627 lines (533 loc) · 23 KB
/
class.Database.php
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
<?PHP
////////////////////////////////////////////////////
//
// WeatherOffice
//
// https://github.com/BmdOnline/WeatherOffice
//
// Copyright (C) 09/2018
// BmdOnline,
//
// See COPYING for license info
//
////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Class Database handles data queries
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
CREATE TABLE `ws2300_weather` (
`datetime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`temp_in` decimal(4,1) NOT NULL DEFAULT '0.0',
`temp_out` decimal(4,1) NOT NULL DEFAULT '0.0',
`dewpoint` decimal(4,1) NOT NULL DEFAULT '0.0',
`rel_hum_in` tinyint(3) NOT NULL DEFAULT '0',
`rel_hum_out` tinyint(3) NOT NULL DEFAULT '0',
`wind_speed` decimal(3,1) NOT NULL DEFAULT '0.0',
`wind_angle` decimal(4,1) NOT NULL DEFAULT '0.0',
`wind_direction` char(3) NOT NULL DEFAULT '',
`wind_chill` decimal(4,1) NOT NULL DEFAULT '0.0',
`rain_1h` decimal(3,1) NOT NULL DEFAULT '0.0',
`rain_24h` decimal(3,1) NOT NULL DEFAULT '0.0',
`rain_total` decimal(5,1) NOT NULL DEFAULT '0.0',
`rel_pressure` decimal(5,1) NOT NULL DEFAULT '0.0',
`tendency` varchar(7) NOT NULL DEFAULT '',
`forecast` varchar(6) NOT NULL DEFAULT '',
PRIMARY KEY (`datetime`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
*/
require_once 'class.dbMySQL.php';
class DATABASE
{
private $tableWeather = "ws2300_weather";
private $tableCache = "ws2300_cache";
private $tableMinMaxAvg = "ws2300_MinMaxAvg";
private $tableAdditionalSensors = "ws2300_additionalsensors";
private $tableAdditionalValues = "ws2300_additionalvalues";
private $currentDB;
function __construct(string $host, string $username, string $passwd, string $dbname) {
//
// connect to the database
//
$this->currentDB = new dbMySQL($host, $username, $passwd, $dbname);
}
function __destruct() {
$this->close();
}
function connected() {
return $this->currentDB->connected();
}
function close() {
$this->currentDB->close();
}
function free() {
$this->currentDB->free();
}
function freeall() {
$this->currentDB->freeall();
}
function store($name) {
$this->currentDB->store($name);
}
function restore($name) {
$this->currentDB->restore($name);
}
function getNextRow() {
return $this->currentDB->getNextRow();
}
function seekRow($position) {
$this->currentDB->seekRow($position);
}
function getRowsCount() {
return $this->currentDB->getRowsCount();
}
private function _parseField($field, $table, $family=null) {
$lstTables = array($this->tableWeather, $this->tableAdditionalValues);
$field = str_replace("`", "", $field);
$field = trim($field);
if ($family == null) {
$family = $field;
}
$family = explode(".", strtolower($family));
if (in_array($table, $lstTables)) {
switch($family[0]) {
case 'timestamp';
if (!isset($family[1])) {
if ($field==null) {
// field=null, replace with original field name
$newField = "datetime";
} elseif ($field==$family[0]) {
// replace datetime with conversion to timestamp
$newField = "date_format(datetime, \"%Y%m%d%H%i%s\")";
} else {
// replace datetime with conversion to timestamp, but using $field instead of datetime
$newField = "date_format($field, \"%Y%m%d%H%i%s\")";
}
} elseif ($family[1]=="rev") {
// replace datetime with conversion to timestamp, but using $field instead of datetime
$newField = "str_to_date($field,\"%Y%m%d%H%i%s\")";
}
break;
case 'rec_date';
if ($field==null) {
// field=null, replace with original field name
$newField = "datetime";
} else {
$newField = "date(datetime)";
}
break;
case 'rec_time';
if ($field==null) {
// field=null, replace with original field name
$newField = "datetime";
} else {
$newField = "time(datetime)";
}
break;
case 'windspeed':
$newField = "wind_speed";
break;
default:
$newField = $field;
break;
}
} else {
$newField = $field;
}
return $newField;
}
private function _parseFields($fields, $table) {
foreach(explode(",", $fields) as $field) {
$newField = $this->_parseField($field, $table);
if ($newField != "*") {
$newField = $newField . " as " . $field;
}
$newFields[] = $newField;
}
return implode(", ", $newFields);
}
function _getFieldsFromPeriod($fields, $table, $starttime, $stoptime, $strict=true, $condition=null, $count=-1) {
$fields = $this->_parseFields($fields, $table);
$tsField = $this->_parseField(null, $table, "timestamp");
$query = "select " . $fields;
$query .= " from `$table`";
$startVal = $this->_parseField($starttime, $table, "timestamp.rev");
$stopVal = $this->_parseField($stoptime, $table, "timestamp.rev");
if ($strict) {
$query .= " where $tsField>$startVal";
$query .= " and $tsField<$stopVal";
} else {
$query .= " where $tsField>=$startVal";
$query .= " and $tsField<=$stopVal";
}
if ($condition) {
$query .= "and " . $condition;
}
$query .= " order by " . $tsField;
if ($count==1) {
return $this->currentDB->getRow($query);
} else {
return $this->currentDB->getRows($query);
}
}
/**
* All queries related to weather table
*
*/
function getWeatherFirstDate() {
$tsField = $this->_parseField(null, $this->tableWeather, "timestamp");
$field = "min($tsField)";
$field = $this->_parseField($field, $this->tableWeather, "timestamp");
$query = "select " . $field;
$query .=" from `$this->tableWeather`";
return $this->currentDB->getValue($query);
}
function getWeatherLastDate() {
$tsField = $this->_parseField(null, $this->tableWeather, "timestamp");
$field = "max($tsField)";
$field = $this->_parseField($field, $this->tableWeather, "timestamp");
$query = "select " . $field;
$query .=" from `$this->tableWeather`";
return $this->currentDB->getValue($query);
}
function getWeatherFieldsFromDate($fields, $timestamp) {
$fields = $this->_parseFields($fields, $this->tableWeather);
$tsField = $this->_parseField(null, $this->tableWeather, "timestamp");
$tsVal = $this->_parseField($timestamp, $this->tableWeather, "timestamp.rev");
$query = "select " . $fields;
$query .= " from `$this->tableWeather`";
$query .= " where $tsField=$tsVal";
return $this->currentDB->getRow($query);
}
function getWeatherFieldsFromPeriod($fields, $starttime, $stoptime, $strict=true, $count=-1) {
$condition = null;
return $this->_getFieldsFromPeriod($fields, $this->tableWeather, $starttime, $stoptime, $strict, $condition, $count);
}
function getWeatherFromDate($timestamp) {
$fields = "timestamp, rec_date, rec_time,";
$fields .= " `temp_in`, `temp_out`, `dewpoint`, `rel_hum_in`, `rel_hum_out`,";
$fields .= " windspeed, `wind_angle`, `wind_direction`, `wind_chill`,";
$fields .= " `rain_1h`, `rain_24h`, `rain_total`, `rel_pressure`, `tendency`, `forecast`";
return $this->getWeatherFieldsFromDate($fields, $timestamp);
}
function getWeatherFromPeriod($starttime, $stoptime, $strict=true, $count=-1) {
$fields = "timestamp, rec_date, rec_time,";
$fields .= " `temp_in`, `temp_out`, `dewpoint`, `rel_hum_in`, `rel_hum_out`,";
$fields .= " windspeed, `wind_angle`, `wind_direction`, `wind_chill`,";
$fields .= " `rain_1h`, `rain_24h`, `rain_total`, `rel_pressure`, `tendency`, `forecast`";
return $this->getWeatherFieldsFromPeriod($fields, $starttime, $stoptime, $strict, $count);
}
/**
* All queries related to cache
*
*/
function haveCache() {
return ($this->currentDB->tableExists($this->tableCache));
}
function createTableCache() {
$query = "CREATE TABLE IF NOT EXISTS `$this->tableCache` (";
$query .= " `startTime` bigint(14) unsigned NOT NULL default '0',";
$query .= " `stopTime` bigint(14) unsigned NOT NULL default '0',";
$query .= " `day` int(10) unsigned NOT NULL default '0',";
$query .= " `rows` int(10) unsigned NOT NULL default '0',";
$query .= " `accessTime` int(10) unsigned NOT NULL default '0',";
$query .= " `value` text default NULL,";
$query .= " PRIMARY KEY (`startTime`,`stopTime`, `day`)) CHARSET=utf8;";
if ($this->currentDB->query($query, true)) {
return true;
} else {
return false;
}
}
function storeCacheValues($startTime, $stopTime, $rows, $day, $stat) {
$dataString = $this->currentDB->escapeString(serialize($stat));
$now = time();
$query = "REPLACE INTO `$this->tableCache`";
$query .= " SET startTime=$startTime, stopTime=$stopTime,";
$query .= " rows=$rows, day=$day, accessTime=$now, value=\"$dataString\"";
if ($this->currentDB->query($query, true)) {
return true;
} else {
return false;
}
}
function getCacheValues($startTime, $stopTime, $rows, $day) {
$query = "SELECT value FROM `$this->tableCache`";
$query .= " WHERE startTime=$startTime AND stopTime=$stopTime";
$query .= " AND day=$day AND rows=$rows";
return $this->currentDB->getRows($query);
}
function updateCacheTime($startTime, $stopTime, $rows, $day) {
$now = time();
$query = "UPDATE `$this->tableCache`";
$query .= " SET accessTime=$now";
$query .= " WHERE startTime=$startTime AND stopTime=$stopTime";
$query .= " AND day=$day AND rows=$rows";
if ($this->currentDB->query($query, true)) {
return true;
} else {
return false;
}
}
/**
* All queries related to additional MinMaxAvg
*
*/
function haveMinMaxAvg() {
return ($this->currentDB->tableExists($this->tableMinMaxAvg));
}
function createTableMinMaxAvg($fields, $ops) {
$columns[] = "`timestamp` bigint(14) DEFAULT '0'";
$columns[] = "`type` char(10) NOT NULL DEFAULT '0'";
foreach ($fields as $field) {
$field = strtolower($field);
foreach ($ops as $op) {
$op = strtolower($op);
switch($field) {
case 'rel_pressure';
$precision = "4,0";
break;
case 'wind_angle';
$precision = "4,1";
break;
case 'rain_total';
$precision = "5,1";
break;
default:
$precision="3,1";
break;
}
$columns[] = "${field}_$op decimal($precision) DEFAULT NULL";
}
}
$columns[] = "PRIMARY KEY (`timestamp`)";
$columnCreate = implode(", ", $columns);
$query = "CREATE TABLE IF NOT EXISTS `$this->tableMinMaxAvg` (";
$query .= $columnCreate;
$query .= ") CHARSET=utf8;";
//$query .= ") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
if ($this->currentDB->query($query, true)) {
return true;
} else {
return false;
}
}
function calculateMinMaxAvg($fields, $Type, $tStampStart, $tStampLenght, $startTS) {
foreach ($fields as $field)
{
if($field != "rain_total")
{
$srcField = $this->_parseField($field, $this->tableWeather);
$arrFields[] = "ROUND(AVG($srcField),1) AS ${field}_avg";
$arrFields[] = "ROUND(MIN($srcField),1) AS ${field}_min";
$arrFields[] = "ROUND(MAX($srcField),1) AS ${field}_max ";
$arrTarget[] = "${field}_avg";
$arrTarget[] = "${field}_min";
$arrTarget[] = "${field}_max";
}
}
$queryFields = implode(", ", $arrFields);
$targetCols = implode(", ", $arrTarget);
$field = "timestamp";
$timestamp = $this->_parseField($field, $this->tableWeather);
$query = "REPLACE INTO `$this->tableMinMaxAvg`(timestamp, type, $targetCols)";
$query .= " SELECT substr($timestamp, $tStampStart, $tStampLenght) as timestamp,";
$query .= " \"${Type}\" as Type, $queryFields";
$query .= " FROM `$this->tableWeather`";
$query .= " WHERE $timestamp > $startTS";
$query .= " GROUP BY substr($timestamp, $tStampStart, $tStampLenght)";
if ($this->currentDB->query($query)) {
return true;
} else {
return false;
}
}
function aggregateMinMaxAvg($fields, $Type, $tStampStart, $tStampLenght, $dataType) {
foreach ($fields as $field)
{
$srcField = $this->_parseField($field, $this->tableMinMaxAvg);
if($field == "rain_total" && $Type != 'MONTH')
{
$arrFields[] = "ROUND(SUM(${srcField}_avg),1) AS ${field}_avg";
$arrFields[] = "ROUND(SUM(${srcField}_min),1) AS ${field}_min";
$arrFields[] = "ROUND(SUM(${srcField}_max),1) AS ${field}_max ";
}
else
{
$arrFields[] = "ROUND(AVG(${srcField}_avg),1) AS ${field}_avg";
$arrFields[] = "ROUND(MIN(${srcField}_min),1) AS ${field}_min";
$arrFields[] = "ROUND(MAX(${srcField}_max),1) AS ${field}_max ";
}
$arrTarget[] = "${field}_avg";
$arrTarget[] = "${field}_min";
$arrTarget[] = "${field}_max";
}
$queryFields = implode(", ", $arrFields);
$targetCols = implode(", ", $arrTarget);
$field = "timestamp";
$timestamp = $this->_parseField($field, $this->tableMinMaxAvg);
$query = "REPLACE INTO `$this->tableMinMaxAvg`(timestamp, type, $targetCols)";
$query .= " SELECT substr($timestamp, $tStampStart, $tStampLenght) as timestamp,";
$query .= " \"${Type}\" as Type, $queryFields";
$query .= " FROM `$this->tableMinMaxAvg`";
$query .= " WHERE type='$dataType'";
$query .= " GROUP BY substr($timestamp, $tStampStart, $tStampLenght)";
if ($this->currentDB->query($query)) {
return true;
} else {
return false;
}
}
function updateMinMaxAvg($tStampStart, $tStampLenght, $tRainStart, $tRainLenght, $startTS) {
$field = "timestamp";
$timestamp = $this->_parseField($field, $this->tableWeather);
$query = "update `$this->tableMinMaxAvg` AS tar";
$query .= " inner join";
$query .= " (SELECT SUBSTR(YYYYDD,$tStampStart, $tStampLenght) AS timestamp,";
$query .= " ROUND(AVG(rainfall),1) AS rain_total_avg,";
$query .= " ROUND(MIN(rainfall),1) AS rain_total_min,";
$query .= " ROUND(MAX(rainfall),1) AS rain_total_max";
$query .= " FROM (select max(rain_total)-min(rain_total) AS rainfall,";
$query .= " substr($timestamp ,$tRainStart, $tRainLenght) AS YYYYDD";
$query .= " from `$this->tableWeather`";
$query .= " where $timestamp > $startTS";
$query .= " group by substr($timestamp ,$tRainStart, $tRainLenght)) AS T1";
$query .= " GROUP BY SUBSTR(YYYYDD,$tStampStart, $tStampLenght))";
$query .= " as sor on tar.timestamp = sor.timestamp";
$query .= " set";
$query .= " tar.rain_total_avg=sor.rain_total_avg,";
$query .= " tar.rain_total_min=sor.rain_total_min,";
$query .= " tar.rain_total_max=sor.rain_total_max";
if ($this->currentDB->query($query)) {
return true;
} else {
return false;
}
}
function getMinMaxAvgFirstDate($type) {
$query = "select min(timestamp)";
$query .=" from `$this->tableMinMaxAvg`";
$query .= " where type='$type'";
return $this->currentDB->getValue($query);
}
function getMinMaxAvgLastDate($type) {
$query = "select max(timestamp)";
$query .= " from `$this->tableMinMaxAvg`";
$query .= " where type='$type'";
return $this->currentDB->getValue($query);
}
function getMinMaxAvgExtremeValue($field, $op, $type) {
$query = "select timestamp, $field";
$query .= " from `$this->tableMinMaxAvg`";
$query .=" where type='$type'";
$query .= " and $field=(SELECT $op($field) FROM `$this->tableMinMaxAvg` WHERE type='$type')";
return $this->currentDB->getRow($query);
}
function getMinMaxAvgFieldsFromType($fields, $type) {
$fields = $this->_parseFields($fields, $this->tableMinMaxAvg);
$query = "select " . $fields;
$query .= " from `$this->tableMinMaxAvg`";
$query .= " where type='$type'";
return $this->currentDB->getRows($query);
}
function getMinMaxAvgFieldsFromDate($fields, $type, $timestamp) {
$fields = $this->_parseFields($fields, $this->tableMinMaxAvg);
$query = "select " . $fields;
$query .= " from `$this->tableMinMaxAvg`";
$query .= " where type='$type'";
$query .= " and timestamp=$timestamp";
return $this->currentDB->getRow($query);
}
function getMinMaxAvgFromDate($type, $timestamp) {
$fields = "*";
return $this->getMinMaxAvgFieldsFromDate($fields, $type, $timestamp);
}
function getMinMaxAvgMeanValues() {
$query = "select substr(DAY,5,2) as MONTH,";
$query .= " round(avg(DAY_TEMP_MAX),1) as MEAN_TEMP_MAX,";
$query .= " round(avg(DAY_TEMP_MIN),1) as MEAN_TEMP_MIN";
$query .= " FROM (SELECT SUBSTR(timestamp, 1,8) AS DAY,";
$query .= " MAX(temp_out_max) AS DAY_TEMP_MAX,";
$query .= " MIN(temp_out_min) AS DAY_TEMP_MIN";
$query .= " FROM `$this->tableMinMaxAvg`";
$query .= " WHERE Type='DAY'";
$query .= " GROUP BY substr(timestamp, 1,8)) AS T1";
$query .= " group by substr(DAY,5,2)";
return $this->currentDB->getRows($query);
}
function getMinMaxAvgRainDaysValues() {
$query = "select substr(YYYYDD,5,2) as MONTH,";
$query .= " round(avg(RAINDAYS)) as RAINDAYS_AVG";
$query .= " from (SELECT COUNT(DAY) AS RAINDAYS, SUBSTR(DAY,1,6) AS YYYYDD";
$query .= " FROM (select DAY, RAINFALL";
$query .= " from (SELECT SUBSTR(timestamp,1,8) as DAY, type as Type, rain_total_max as rainfall";
$query .= " FROM `$this->tableMinMaxAvg`";
$query .= " GROUP BY substr(timestamp,1,8)) AS T1";
$query .= " where Type='DAY' and rainfall > 0) as T2";
$query .= " GROUP BY SUBSTR(DAY,1,6)) AS T3";
$query .= " group by substr(YYYYDD,5,2)";
$query .= " order by substr(YYYYDD,5,2);";
return $this->currentDB->getRows($query);
}
/**
* All queries related to additional sensors
*
*/
function haveSensors($active=null) {
if ($this->currentDB->tableExists($this->tableAdditionalSensors)) {
if ($active) {
$this->listSensors($active);
return $this->currentDB->getRowsCount();
} else {
return true;
}
} else {
return false;
}
}
function listSensors($active=null) {
$fields = "id, name, filename, linenumber, unit, Active";
$fields = $this->_parseFields($fields, $this->tableAdditionalSensors);
$query = "select " . $fields;
$query .= " from `$this->tableAdditionalSensors`";
if ($active) {
$query .= " where active=$active";
}
$query .= " order by id";
return $this->currentDB->query($query);
}
function addSensors($sensorId, $name, $filename, $linenumber, $unit) {
$query = "INSERT INTO $this->tableAdditionalSensors";
$query .= " Values($sensorId,";
$query .= " \"$name\",";
$query .= " \"$filename\",";
$query .= " \"$linenumber\",";
$query .= " \"$unit\", 1)";
return $this->currentDB->query($query);
}
function getSensorLastId() {
$query = "select max(id)";
$query .=" from `$this->tableAdditionalSensors`";
return $this->_getValue($query);
}
function getSensorFromId($sensorId) {
$query = "select *";
$query .= " from `$this->tableAdditionalSensors`";
$query .= " where id=\"$sensorId\"";
return $this->currentDB->getRows($query);
}
function addSensorsValue($sensorId, $timestamp, $value) {
$query = "INSERT INTO $this->tableAdditionalValues";
$query .= " Values($sensorId,";
$query .= " \"$timestamp\",";
$query .= " \"$value\")";
return $this->currentDB->query($query);
}
function getSensorValuesFromPeriod($sensorId, $starttime, $stoptime, $strict=true, $count=-1) {
$fields = "value, timestamp";
$condition = " id=\"$sensorId\"";
return $this->_getFieldsFromPeriod($fields, $this->tableAdditionalValues, $starttime, $stoptime, $strict, $condition, $count);
}
}
?>