Compare commits

...

3 Commits

Author SHA1 Message Date
agp8x 3286e8b2fb improved highcharts test 2015-05-07 21:03:37 +02:00
agp8x db8125640b added testcase for highcharts 2015-05-07 20:22:05 +02:00
agp8x c20a864700 moved creation of marker file 2015-05-07 19:45:15 +02:00
3 changed files with 170 additions and 2 deletions

162
highcharts.php Normal file
View File

@ -0,0 +1,162 @@
<?php
/**
* Created by PhpStorm.
* User: agp8x
* Date: 07.05.15
* Time: 18:36
*/
$start = microtime(true);
include "config.php";
include "lib/DBLib.php";
function average($list)
{
if (!$list) {
return null;
}
$i = 0;
$sum = 0;
foreach ($list as $value) {
$i++;
$sum += $value['value'] / 100;
}
if ($i > 0) {
return round($sum / $i, 1);
} else {
return null;
}
}
$db = new DBLib($database['host'], $database['user'], $database['password'], $database['database']);
$sensors = ["temp1" => "Innen", "temp2" => "Aussen"];
$show_days = true;
$series = array();
$days = [];
foreach ($sensors as $sensor => $name) {
$date = new DateTime("2015-04-01T00:00:00+02:00", new DateTimeZone("Europe/Berlin"));
$date2 = clone $date;
$day_summaries = array();
for ($i = 0; $i < $date->format("t"); $i++) {
$start = $date->format("U");
$date->add(new DateInterval("P1D"));
$end = $date->format("U");
$entries = $db->selectRange($sensor, "*", array("time", $start, $end));
$day_summaries[] = average($entries);
if ($show_days) {
$hours = [];
for ($j = 0; $j < 24; $j++) {
$start = $date2->format("U");
$date2->add(new DateInterval("PT1H"));
$end = $date2->format("U");
$entries = $db->selectRange($sensor, "*", array("time", $start, $end));
$hours[] = average($entries);
}
$days[] = ["name" => $date->format("d.m.Y") . " (" . $name . ")", "data" => $hours];
}
}
$series[] = array("name" => $name, "data" => $day_summaries);
}
$json = json_encode($series, JSON_NUMERIC_CHECK /*| JSON_PRETTY_PRINT*/);
$json2 = json_encode($days, JSON_NUMERIC_CHECK /*| JSON_PRETTY_PRINT*/);
$runtime = "Runtime: " . ((microtime(true) - $start) / 1000 / 1000);
$runtime .= " s";
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Highcharts Example</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<style type="text/css">
${demo.css}
</style>
<script type="text/javascript">
$(function () {
$('#container').highcharts({
title: {
text: 'Monthly Average Temperature',
x: -20 //center
},
subtitle: {
text: 'April 2014\<br\>Source: agp8x.org',
x: -20
},
xAxis: {
categories: []
},
yAxis: {
title: {
text: 'Temperature (°C)'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
valueSuffix: '°C'
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
borderWidth: 0
},
series: <?= $json ?>
});
});
$(function () {
$('#container2').highcharts({
title: {
text: 'Daily Average Temperature',
x: -20 //center
},
subtitle: {
text: 'April 2014\<br\>Source: agp8x.org',
x: -20
},
xAxis: {
categories: []
},
yAxis: {
title: {
text: 'Temperature (°C)'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
valueSuffix: '°C'
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
borderWidth: 0
},
series: <?= $json2 ?>
});
});
</script>
</head>
<body>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
<div id="container2" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
<div style='position:fixed;bottom:20px;right:50px;'><?= ($runtime) ?></div>
</body>
</html>

2
recent
View File

@ -1 +1 @@
Temperatur: 13.43 C<br>Luftfeuchtigkeit: 89.5 %<br>Helligkeit: 0.01 Lux<br>Luftdruck: 982.473mbar
Temperatur: 10.37 C<br>Luftfeuchtigkeit: 70.2 %<br>Helligkeit: 0.01 Lux<br>Luftdruck: 981.66mbar

View File

@ -7,11 +7,17 @@ $db=new DBLib();
$db=new DBLib($database['host'],$database['user'],$database['password'],$database['database']);
function markerFile($file="newData"){
if (! file_exists($file)){
file_put_contents($file,"");
}
}
$dir=scandir('data');
$temp=$humi=$ambi=$baro=0;
foreach($dir as $file){
if(validFile($file)){
file_put_contents('newData',"");
markerFile();
echo "parsing file: ".$file."\n";
importLogfiletoDatabase('data/'.$file);
//recent records on frontpage: