frontend: caching added
git-svn-id: file:///home/jan/tmp/wetterstation/trunk@247 dd492736-c11a-0410-ad51-8c26713eaf7f
This commit is contained in:
parent
3534adf03a
commit
25960c71aa
|
|
@ -9,6 +9,13 @@
|
||||||
$pg_user = "weatherstation";
|
$pg_user = "weatherstation";
|
||||||
$pg_pass = "";
|
$pg_pass = "";
|
||||||
|
|
||||||
|
/* Caching */
|
||||||
|
$performCaching = true;
|
||||||
|
$cachePath = "/tmp/weatherstation-frontend/cache/";
|
||||||
|
|
||||||
|
/* Debug */
|
||||||
|
$isDebug = false;
|
||||||
|
|
||||||
/* Default-Werte */
|
/* Default-Werte */
|
||||||
$default_set = "small"; // Default Set welches angezeigt wird
|
$default_set = "small"; // Default Set welches angezeigt wird
|
||||||
$default_chart_dir = "images/chart"; // Verzeichnis in dem die Bilder liegen
|
$default_chart_dir = "images/chart"; // Verzeichnis in dem die Bilder liegen
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,109 @@
|
||||||
|
<?
|
||||||
|
|
||||||
|
/* copyright: Jan Losinski, 2006
|
||||||
|
|
||||||
|
at the Moment this is no free software, look at the
|
||||||
|
COPYING-File in the Main-Directory for License-Terms
|
||||||
|
*/
|
||||||
|
|
||||||
|
include_once($path."php_inc/config.inc.php");
|
||||||
|
|
||||||
|
/* Connection-Klasse..ist fuer die Datenbankverbindung zustaendig */
|
||||||
|
class Cacher{
|
||||||
|
|
||||||
|
function _cacheFileName($identifier){
|
||||||
|
$tmp_dir = Config::getCachePath();
|
||||||
|
if(!file_exists($tmp_dir)){
|
||||||
|
mkdir(dirname($tmp_dir), 0700);
|
||||||
|
mkdir($tmp_dir, 0700);
|
||||||
|
}
|
||||||
|
return $tmp_dir.md5($identifier);
|
||||||
|
}
|
||||||
|
function _lockFileName($identifier){
|
||||||
|
return Cacher::_cacheFileName($identifier).".lock";
|
||||||
|
}
|
||||||
|
|
||||||
|
function _checkCache($identifier, $expire){
|
||||||
|
$file = Cacher::_cacheFileName($identifier);
|
||||||
|
if (file_exists($file)){
|
||||||
|
if (filemtime($file) > (time() - ($expire *60))){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function _lockRead($identifier){
|
||||||
|
$handle = fopen(Cacher::_lockFileName($identifier), "w");
|
||||||
|
$i = 0;
|
||||||
|
while (flock($handle, LOCK_SH) != true){
|
||||||
|
usleep(10);
|
||||||
|
if ($i > 10){
|
||||||
|
fclose($handle);
|
||||||
|
echo "bla1";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$i ++;
|
||||||
|
}
|
||||||
|
return $handle;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function _unlockRead($handle){
|
||||||
|
flock($handle, LOCK_UN);
|
||||||
|
fclose($handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function _lockWrite($identifier){
|
||||||
|
$handle = fopen(Cacher::_lockFileName($identifier), "a+");
|
||||||
|
$i = 0;
|
||||||
|
while (flock($handle, LOCK_EX) != true){
|
||||||
|
usleep(10);
|
||||||
|
if ($i > 10){
|
||||||
|
fclose($handle);
|
||||||
|
echo "bla";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$i++;
|
||||||
|
}
|
||||||
|
return $handle;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function _unlockWrite($handle){
|
||||||
|
flock($handle, LOCK_UN);
|
||||||
|
fclose($handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCache($identifier, $expire){
|
||||||
|
if(!Config::performCaching()){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$lock = null;
|
||||||
|
if (Cacher::_checkCache($identifier, $expire) && (($lock = Cacher::_lockRead($identifier)) != false)){
|
||||||
|
$file = Cacher::_cacheFileName($identifier);
|
||||||
|
$handle = fopen($file, "r");
|
||||||
|
$serialized = fread ($handle, filesize ($file));
|
||||||
|
Cacher::_unlockRead($lock);
|
||||||
|
return unserialize($serialized);
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function setCache($identifier, $var){
|
||||||
|
if(!Config::performCaching()){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$lock = null;
|
||||||
|
if (($lock = Cacher::_lockWrite($identifier)) != false){
|
||||||
|
$file = Cacher::_cacheFileName($identifier);
|
||||||
|
$handle = fopen($file, "w");
|
||||||
|
fwrite($handle, serialize($var));
|
||||||
|
fclose($handle);
|
||||||
|
Cacher::_unlockWrite($lock);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -58,5 +58,20 @@ class Config{
|
||||||
global $notStandard;
|
global $notStandard;
|
||||||
return $notStandard;
|
return $notStandard;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getCachePath(){
|
||||||
|
global $cachePath;
|
||||||
|
return $cachePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
function performCaching(){
|
||||||
|
global $performCaching;
|
||||||
|
return $performCaching;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isDebug(){
|
||||||
|
global $isDebug;
|
||||||
|
return $isDebug;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,7 @@ class Connection{
|
||||||
|
|
||||||
/* Eine Zeile holen */
|
/* Eine Zeile holen */
|
||||||
function fetchQueryResultLine($query){
|
function fetchQueryResultLine($query){
|
||||||
|
if (Config::isDebug()) echo $query."\n";
|
||||||
$this->_createConn();
|
$this->_createConn();
|
||||||
$result = pg_query($this->conn, $query)
|
$result = pg_query($this->conn, $query)
|
||||||
or die('Abfrage fehlgeschlagen: ' . pg_last_error(). "\n<br>\nquery: '".$query."'");
|
or die('Abfrage fehlgeschlagen: ' . pg_last_error(). "\n<br>\nquery: '".$query."'");
|
||||||
|
|
@ -47,6 +48,7 @@ class Connection{
|
||||||
|
|
||||||
/* mehrere Zeilen holen */
|
/* mehrere Zeilen holen */
|
||||||
function fetchQueryResultSet($query){
|
function fetchQueryResultSet($query){
|
||||||
|
if (Config::isDebug()) echo $query."\n";
|
||||||
$returnArray = array();
|
$returnArray = array();
|
||||||
$this->_createConn();
|
$this->_createConn();
|
||||||
$result = pg_query($this->conn, $query)
|
$result = pg_query($this->conn, $query)
|
||||||
|
|
@ -58,6 +60,7 @@ class Connection{
|
||||||
|
|
||||||
/* Result roh zurueckgeben */
|
/* Result roh zurueckgeben */
|
||||||
function &getRawResult($query){
|
function &getRawResult($query){
|
||||||
|
if (Config::isDebug()) echo $query."\n";
|
||||||
$this->_createConn();
|
$this->_createConn();
|
||||||
$result = pg_query($this->conn, $query)
|
$result = pg_query($this->conn, $query)
|
||||||
or die('Abfrage fehlgeschlagen: ' . pg_last_error(). "\n<br>\nquery: '".$query."'");
|
or die('Abfrage fehlgeschlagen: ' . pg_last_error(). "\n<br>\nquery: '".$query."'");
|
||||||
|
|
|
||||||
|
|
@ -33,9 +33,13 @@ class Hum{
|
||||||
/* Funktion, die die Klasse mit den Weten initialisiert */
|
/* Funktion, die die Klasse mit den Weten initialisiert */
|
||||||
function _fetchHumData(){
|
function _fetchHumData(){
|
||||||
|
|
||||||
/* Aktuelle Luftfeuchtigkeit bestimmen */
|
$nowData = null;
|
||||||
$nowQuery = "SELECT hum, to_char(timestamp, 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." ORDER BY timestamp DESC LIMIT 1";
|
if (($nowData = Cacher::getCache("HumNow_ID_".$this->sensId, 3)) == false){
|
||||||
$nowData = $this->connection->fetchQueryResultLine($nowQuery);
|
/* Aktuelle Luftfeuchtigkeit bestimmen */
|
||||||
|
$nowQuery = "SELECT hum, to_char(timestamp, 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." ORDER BY timestamp DESC LIMIT 1";
|
||||||
|
$nowData = $this->connection->fetchQueryResultLine($nowQuery);
|
||||||
|
Cacher::setCache("HumNow_ID_".$this->sensId, $nowData);
|
||||||
|
}
|
||||||
|
|
||||||
/* Bestimmte Werte den Klassenvariablen zuordnen */
|
/* Bestimmte Werte den Klassenvariablen zuordnen */
|
||||||
$this->nowHum = $nowData['hum'];
|
$this->nowHum = $nowData['hum'];
|
||||||
|
|
@ -52,8 +56,12 @@ class Hum{
|
||||||
if($this->maxHum == "nc" || $this->minHum == "nc"){
|
if($this->maxHum == "nc" || $this->minHum == "nc"){
|
||||||
$this->_fetchMinMax();
|
$this->_fetchMinMax();
|
||||||
}
|
}
|
||||||
$Query = "SELECT to_char(max(timestamp), 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." AND hum=".$this->maxHum." OR hum=".$this->minHum." GROUP BY hum ORDER BY hum ASC LIMIT 2";
|
$Data = null;
|
||||||
$Data = $this->connection->fetchQueryResultSet($Query);
|
if (($Data = Cacher::getCache("HumExtremDate_ID_".$this->sensId."_MINMAX_".$this->maxTemp."_".$this->minTemp, 30)) == false){
|
||||||
|
$Query = "SELECT to_char(max(timestamp), 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." AND hum=".$this->maxHum." OR hum=".$this->minHum." GROUP BY hum ORDER BY hum ASC LIMIT 2";
|
||||||
|
$Data = $this->connection->fetchQueryResultSet($Query);
|
||||||
|
Cacher::setCache("HumExtremDate_ID_".$this->sensId."_MINMAX_".$this->maxTemp."_".$this->minTemp, $Data);
|
||||||
|
}
|
||||||
$this->minDate = $Data[0]['text_timestamp'];
|
$this->minDate = $Data[0]['text_timestamp'];
|
||||||
$this->maxDate = $Data[1]['text_timestamp'];
|
$this->maxDate = $Data[1]['text_timestamp'];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,9 +32,13 @@ class Press{
|
||||||
/* Funktion, die die Klasse mit den Weten initialisiert */
|
/* Funktion, die die Klasse mit den Weten initialisiert */
|
||||||
function _fetchPressData(){
|
function _fetchPressData(){
|
||||||
|
|
||||||
/* Aktuelle Luftdruck bestimmen */
|
$nowData = null;
|
||||||
$nowQuery = "SELECT press, to_char(timestamp, 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." ORDER BY timestamp DESC LIMIT 1";
|
if (($nowData = Cacher::getCache("PressNow_ID_".$this->sensId, 3)) == false){
|
||||||
$nowData = $this->connection->fetchQueryResultLine($nowQuery);
|
/* Aktuelle Luftdruck bestimmen */
|
||||||
|
$nowQuery = "SELECT press, to_char(timestamp, 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." ORDER BY timestamp DESC LIMIT 1";
|
||||||
|
$nowData = $this->connection->fetchQueryResultLine($nowQuery);
|
||||||
|
Cacher::setCache("PressNow_ID_".$this->sensId, $nowData);
|
||||||
|
}
|
||||||
|
|
||||||
/* Bestimmte Werte den Klassenvariablen zuordnen */
|
/* Bestimmte Werte den Klassenvariablen zuordnen */
|
||||||
$this->nowPress = $nowData['press'];
|
$this->nowPress = $nowData['press'];
|
||||||
|
|
@ -48,11 +52,15 @@ class Press{
|
||||||
}
|
}
|
||||||
|
|
||||||
function _fetchMinMaxDate(){
|
function _fetchMinMaxDate(){
|
||||||
if($this->maxHum == "nc" || $this->minHum == "nc"){
|
if($this->maxPress == "nc" || $this->minPress == "nc"){
|
||||||
$this->_fetchMinMax();
|
$this->_fetchMinMax();
|
||||||
}
|
}
|
||||||
$Query = "SELECT to_char(max(timestamp), 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." AND press=".$this->maxPress." OR press=".$this->minPress." GROUP BY press ORDER BY press ASC LIMIT 2";
|
$Data = null;
|
||||||
$Data = $this->connection->fetchQueryResultSet($Query);
|
if (($Data = Cacher::getCache("PressExtremDate_ID_".$this->sensId."_MINMAX_".$this->maxTemp."_".$this->minTemp, 130)) == false){
|
||||||
|
$Query = "SELECT to_char(max(timestamp), 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." AND press=".$this->maxPress." OR press=".$this->minPress." GROUP BY press ORDER BY press ASC LIMIT 2";
|
||||||
|
$Data = $this->connection->fetchQueryResultSet($Query);
|
||||||
|
Cacher::setCache("PressExtremDate_ID_".$this->sensId."_MINMAX_".$this->maxTemp."_".$this->minTemp, $Data);
|
||||||
|
}
|
||||||
$this->minDate = $Data[0]['text_timestamp'];
|
$this->minDate = $Data[0]['text_timestamp'];
|
||||||
$this->maxDate = $Data[1]['text_timestamp'];
|
$this->maxDate = $Data[1]['text_timestamp'];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -34,13 +34,21 @@ class Rain{
|
||||||
|
|
||||||
/* Momentane Werte aus der Datenbank holen */
|
/* Momentane Werte aus der Datenbank holen */
|
||||||
function _getNowValues($interval){
|
function _getNowValues($interval){
|
||||||
$result = $this->connection->fetchQueryResultLine("SELECT sum(count) as rain FROM ".$this->table." WHERE sens_id=".$this->sensId." AND timestamp>(select (current_timestamp - INTERVAL '".$interval."'))");
|
if (($result = Cacher::getCache("NowRain_ID_".$this->sensId."_Interval_".$interval, 7)) == false){
|
||||||
|
$result = $this->connection->fetchQueryResultLine("SELECT sum(count) as rain FROM ".$this->table." WHERE sens_id=".$this->sensId." AND timestamp>(select (current_timestamp - INTERVAL '".$interval."'))");
|
||||||
|
Cacher::setCache("NowRain_ID_".$this->sensId."_Interval_".$interval, $result);
|
||||||
|
}
|
||||||
return $result['rain'];
|
return $result['rain'];
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Maximal gemessene Werte aus der Datenbank holen */
|
/* Maximal gemessene Werte aus der Datenbank holen */
|
||||||
function _getMaxValues($unit, $dateFormat){ // unit = hour, minute, ...
|
function _getMaxValues($unit, $dateFormat){ // unit = hour, minute, ...
|
||||||
return $this->connection->fetchQueryResultLine("SELECT to_char(ts, '".$dateFormat."') as date, val FROM ".$this->table."_".$unit." WHERE sens_id=".$this->sensId." ORDER BY val DESC, ts DESC LIMIT 1");
|
$res = null;
|
||||||
|
if (($res = Cacher::getCache("MaxRain_ID_".$this->sensId."_Unit_".$unit."_Format_".$dateFormat, 30)) == false ){
|
||||||
|
$res = $this->connection->fetchQueryResultLine("SELECT to_char(ts, '".$dateFormat."') as date, val FROM ".$this->table."_".$unit." WHERE sens_id=".$this->sensId." ORDER BY val DESC, ts DESC LIMIT 1");
|
||||||
|
Cacher::setCache("MaxRain_ID_".$this->sensId."_Unit_".$unit."_Format_".$dateFormat, $res);
|
||||||
|
}
|
||||||
|
return $res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -71,7 +79,10 @@ class Rain{
|
||||||
|
|
||||||
function get_now_all(){
|
function get_now_all(){
|
||||||
if($this->nowAll == "nc");
|
if($this->nowAll == "nc");
|
||||||
$this->nowAll = $this->connection->fetchQueryResultLine("SELECT sum(count) as rain FROM ".$this->table." WHERE sens_id=".$this->sensId);
|
if (( $this->nowAll = Cacher::getCache("AllRainNow_ID_".$this->sensId, 20)) == false){
|
||||||
|
$this->nowAll = $this->connection->fetchQueryResultLine("SELECT sum(count) as rain FROM ".$this->table." WHERE sens_id=".$this->sensId);
|
||||||
|
Cacher::setCache("AllRainNow_ID_".$this->sensId, $this->nowAll);
|
||||||
|
}
|
||||||
return round($this->nowAll['rain'] * 0.001,3);
|
return round($this->nowAll['rain'] * 0.001,3);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
|
|
||||||
include_once($path."php_inc/connection.inc.php");
|
include_once($path."php_inc/connection.inc.php");
|
||||||
|
include_once($path."php_inc/cacher.inc.php");
|
||||||
|
|
||||||
$sensor_data = NULL;
|
$sensor_data = NULL;
|
||||||
$extrema_fields = array(
|
$extrema_fields = array(
|
||||||
|
|
@ -47,10 +48,13 @@ class Sensor{
|
||||||
function _fetchSensorData($sensId, &$connection){
|
function _fetchSensorData($sensId, &$connection){
|
||||||
global $sensor_data;
|
global $sensor_data;
|
||||||
if ($sensor_data == NULL){
|
if ($sensor_data == NULL){
|
||||||
$query = "SELECT sensoren.*, typen.tabelle FROM sensoren, typen where typen.typ = sensoren.typ";
|
if (($sensor_data = Cacher::getCache("SensorData", 20)) == false){
|
||||||
$result = $connection->getRawResult($query);
|
$query = "SELECT sensoren.*, typen.tabelle FROM sensoren, typen where typen.typ = sensoren.typ";
|
||||||
while ($array = pg_fetch_assoc($result)){
|
$result = $connection->getRawResult($query);
|
||||||
$sensor_data[$array['id']] = $array;
|
while ($array = pg_fetch_assoc($result)){
|
||||||
|
$sensor_data[$array['id']] = $array;
|
||||||
|
}
|
||||||
|
Cacher::setCache("SensorData", $sensor_data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $sensor_data[$sensId];
|
return $sensor_data[$sensId];
|
||||||
|
|
@ -87,17 +91,20 @@ class Sensor{
|
||||||
function get_sensor_extrema($sensId, &$connection, $field){
|
function get_sensor_extrema($sensId, &$connection, $field){
|
||||||
global $extrema_fields, $sensor_extrema;
|
global $extrema_fields, $sensor_extrema;
|
||||||
if (! array_key_exists($sensId, $sensor_extrema)){
|
if (! array_key_exists($sensId, $sensor_extrema)){
|
||||||
$data = Sensor::_fetchSensorData($sensId, &$connection);
|
if (($sensor_extrema[$sensId] = Cacher::getCache("SensorExtrema_ID_".$sensId, 10)) == false){
|
||||||
$query = "SELECT ";
|
$data = Sensor::_fetchSensorData($sensId, &$connection);
|
||||||
for ($i = 0; $i < count($extrema_fields[$data['typ']]); $i++){
|
$query = "SELECT ";
|
||||||
if ($i != 0)
|
for ($i = 0; $i < count($extrema_fields[$data['typ']]); $i++){
|
||||||
$query .= ", ";
|
if ($i != 0)
|
||||||
$query .= "min(".$extrema_fields[$data['typ']][$i].") AS min_".$extrema_fields[$data['typ']][$i].", ";
|
$query .= ", ";
|
||||||
$query .= "max(".$extrema_fields[$data['typ']][$i].") AS max_".$extrema_fields[$data['typ']][$i]." ";
|
$query .= "min(".$extrema_fields[$data['typ']][$i].") AS min_".$extrema_fields[$data['typ']][$i].", ";
|
||||||
|
$query .= "max(".$extrema_fields[$data['typ']][$i].") AS max_".$extrema_fields[$data['typ']][$i]." ";
|
||||||
|
}
|
||||||
|
$query .= "FROM ".$data['tabelle']." WHERE sens_id = ".$sensId;
|
||||||
|
$res = $connection->fetchQueryResultLine($query);
|
||||||
|
$sensor_extrema[$sensId] = $res;
|
||||||
|
Cacher::setCache("SensorExtrema_ID_".$sensId, $sensor_extrema[$sensId]);
|
||||||
}
|
}
|
||||||
$query .= "FROM ".$data['tabelle']." WHERE sens_id = ".$sensId;
|
|
||||||
$res = $connection->fetchQueryResultLine($query);
|
|
||||||
$sensor_extrema[$sensId] = $res;
|
|
||||||
}
|
}
|
||||||
return array('min' => $sensor_extrema[$sensId]['min_'.$field], 'max' => $sensor_extrema[$sensId]['max_'.$field]);
|
return array('min' => $sensor_extrema[$sensId]['min_'.$field], 'max' => $sensor_extrema[$sensId]['max_'.$field]);
|
||||||
}
|
}
|
||||||
|
|
@ -110,28 +117,31 @@ class Sensor{
|
||||||
function get_sensor_average($sensId, &$connection, $field, $init_interval = 0){
|
function get_sensor_average($sensId, &$connection, $field, $init_interval = 0){
|
||||||
global $extrema_fields, $sensor_average;
|
global $extrema_fields, $sensor_average;
|
||||||
if ((! array_key_exists($init_interval, $sensor_average)) || (! array_key_exists($sensId, $sensor_average[$init_interval]))){
|
if ((! array_key_exists($init_interval, $sensor_average)) || (! array_key_exists($sensId, $sensor_average[$init_interval]))){
|
||||||
$interval = $init_interval;
|
if (($sensor_average[$init_interval][$sensId] = Cacher::getCache("SensorAverage_ID_".$sensId."_Interval_".$init_interval, 10)) == false){
|
||||||
$data = Sensor::_fetchSensorData($sensId, &$connection);
|
$interval = $init_interval;
|
||||||
$query = "SELECT count(".$extrema_fields[$data['typ']][0].") AS count, ";
|
$data = Sensor::_fetchSensorData($sensId, &$connection);
|
||||||
for ($i = 0; $i < count($extrema_fields[$data['typ']]); $i++){
|
$query = "SELECT count(".$extrema_fields[$data['typ']][0].") AS count, ";
|
||||||
if ($i != 0)
|
for ($i = 0; $i < count($extrema_fields[$data['typ']]); $i++){
|
||||||
$query .= ", ";
|
if ($i != 0)
|
||||||
$query .= "avg(".$extrema_fields[$data['typ']][$i].") AS ".$extrema_fields[$data['typ']][$i]." ";
|
$query .= ", ";
|
||||||
}
|
$query .= "avg(".$extrema_fields[$data['typ']][$i].") AS ".$extrema_fields[$data['typ']][$i]." ";
|
||||||
$query .= "FROM ".$data['tabelle']." WHERE sens_id = ".$sensId." AND timestamp>(select(current_timestamp - INTERVAL '";
|
}
|
||||||
$res = array('count' => 0, 'average' => 0);
|
$query .= "FROM ".$data['tabelle']." WHERE sens_id = ".$sensId." AND timestamp>(select(current_timestamp - INTERVAL '";
|
||||||
$m = 0;
|
$res = array('count' => 0, 'average' => 0);
|
||||||
if ($interval == 0){
|
$m = 0;
|
||||||
while ($res['count'] < 4) {
|
if ($interval == 0){
|
||||||
$m++;
|
while ($res['count'] < 4) {
|
||||||
$interval = $m*(Config::getAvInterval());
|
$m++;
|
||||||
|
$interval = $m*(Config::getAvInterval());
|
||||||
|
$res = $connection->fetchQueryResultLine($query.$interval." minutes'))");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
$res = $connection->fetchQueryResultLine($query.$interval." minutes'))");
|
$res = $connection->fetchQueryResultLine($query.$interval." minutes'))");
|
||||||
}
|
}
|
||||||
} else {
|
$res['interval'] = $interval;
|
||||||
$res = $connection->fetchQueryResultLine($query.$interval." minutes'))");
|
$sensor_average[$init_interval][$sensId] = $res;
|
||||||
|
Cacher::setCache("SensorAverage_ID_".$sensId."_Interval_".$init_interval, $sensor_average[$init_interval][$sensId]);
|
||||||
}
|
}
|
||||||
$res['interval'] = $interval;
|
|
||||||
$sensor_average[$init_interval][$sensId] = $res;
|
|
||||||
}
|
}
|
||||||
return array('average' => $sensor_average[$init_interval][$sensId][$field], 'count' => $sensor_average[$init_interval][$sensId]['count'], 'interval' => $sensor_average[$init_interval][$sensId]['interval']);
|
return array('average' => $sensor_average[$init_interval][$sensId][$field], 'count' => $sensor_average[$init_interval][$sensId]['count'], 'interval' => $sensor_average[$init_interval][$sensId]['interval']);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,10 +32,14 @@ class Temp{
|
||||||
/* Funktion, die die Klasse mit den Weten initialisiert */
|
/* Funktion, die die Klasse mit den Weten initialisiert */
|
||||||
function _fetchTempData(){
|
function _fetchTempData(){
|
||||||
|
|
||||||
/* Aktuelle Temperatur bestimmen */
|
$nowData = null;
|
||||||
$nowQuery = "SELECT temp, to_char(timestamp, 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." ORDER BY timestamp DESC LIMIT 1";
|
if (($nowData = Cacher::getCache("TempNow_ID_".$this->sensId, 3)) == false){
|
||||||
$nowData = $this->connection->fetchQueryResultLine($nowQuery);
|
/* Aktuelle Temperatur bestimmen */
|
||||||
|
$nowQuery = "SELECT temp, to_char(timestamp, 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." ORDER BY timestamp DESC LIMIT 1";
|
||||||
|
$nowData = $this->connection->fetchQueryResultLine($nowQuery);
|
||||||
|
Cacher::setCache("TempNow_ID_".$this->sensId, $nowData);
|
||||||
|
}
|
||||||
|
|
||||||
/* Bestimmte Werte den Klassenvariablen zuordnen */
|
/* Bestimmte Werte den Klassenvariablen zuordnen */
|
||||||
$this->nowTemp = $nowData['temp'];
|
$this->nowTemp = $nowData['temp'];
|
||||||
$this->nowDate = $nowData['text_timestamp'];
|
$this->nowDate = $nowData['text_timestamp'];
|
||||||
|
|
@ -48,11 +52,15 @@ class Temp{
|
||||||
}
|
}
|
||||||
|
|
||||||
function _fetchMinMaxDate(){
|
function _fetchMinMaxDate(){
|
||||||
if($this->maxHum == "nc" || $this->minHum == "nc"){
|
if($this->maxTemp == "nc" || $this->minTemp == "nc"){
|
||||||
$this->_fetchMinMax();
|
$this->_fetchMinMax();
|
||||||
}
|
}
|
||||||
$Query = "SELECT to_char(max(timestamp), 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." AND temp=".$this->maxTemp." OR temp=".$this->minTemp." GROUP BY temp ORDER BY temp ASC LIMIT 2";
|
$Data = null;
|
||||||
$Data = $this->connection->fetchQueryResultSet($Query);
|
if (($Data = Cacher::getCache("TempExtremDate_ID_".$this->sensId."_MINMAX_".$this->maxTemp."_".$this->minTemp, 30)) == false){
|
||||||
|
$Query = "SELECT to_char(max(timestamp), 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." AND temp=".$this->maxTemp." OR temp=".$this->minTemp." GROUP BY temp ORDER BY temp ASC LIMIT 2";
|
||||||
|
$Data = $this->connection->fetchQueryResultSet($Query);
|
||||||
|
Cacher::setCache("TempExtremDate_ID_".$this->sensId."_MINMAX_".$this->maxTemp."_".$this->minTemp, $Data);
|
||||||
|
}
|
||||||
$this->minDate = $Data[0]['text_timestamp'];
|
$this->minDate = $Data[0]['text_timestamp'];
|
||||||
$this->maxDate = $Data[1]['text_timestamp'];
|
$this->maxDate = $Data[1]['text_timestamp'];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,13 +32,21 @@ class Wind{
|
||||||
/* Funktion, die die Klasse mit den Weten initialisiert */
|
/* Funktion, die die Klasse mit den Weten initialisiert */
|
||||||
function _fetchWindData(){
|
function _fetchWindData(){
|
||||||
|
|
||||||
/* Aktuelle Wind bestimmen */
|
$nowData = null;
|
||||||
$nowQuery = "SELECT geschw as wind, richt as dir, to_char(timestamp, 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." ORDER BY timestamp DESC LIMIT 1";
|
if (($nowData = Cacher::getCache("WindNow_ID_".$this->sensId, 3)) == false){
|
||||||
$nowData = $this->connection->fetchQueryResultLine($nowQuery);
|
/* Aktuelle Wind bestimmen */
|
||||||
|
$nowQuery = "SELECT geschw as wind, richt as dir, to_char(timestamp, 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." ORDER BY timestamp DESC LIMIT 1";
|
||||||
|
$nowData = $this->connection->fetchQueryResultLine($nowQuery);
|
||||||
|
Cacher::setCache("WindNow_ID_".$this->sensId, $nowData);
|
||||||
|
}
|
||||||
|
|
||||||
/* Max und Min-Werte bestimmen */
|
$maxData = null;
|
||||||
$maxQuery = "SELECT geschw as wind, richt as dir, to_char(timestamp, 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." AND geschw=(SELECT max(geschw) FROM ".$this->table." WHERE sens_id=".$this->sensId.") ORDER BY timestamp DESC LIMIT 1";
|
if (($nowData = Cacher::getCache("WindMax_ID_".$this->sensId, 3)) == false){
|
||||||
$maxData = $this->connection->fetchQueryResultLine($maxQuery);
|
/* Max und Min-Werte bestimmen */
|
||||||
|
$maxQuery = "SELECT geschw as wind, richt as dir, to_char(timestamp, 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." AND geschw=(SELECT max(geschw) FROM ".$this->table." WHERE sens_id=".$this->sensId.") ORDER BY timestamp DESC LIMIT 1";
|
||||||
|
$maxData = $this->connection->fetchQueryResultLine($maxQuery);
|
||||||
|
Cacher::setCache("WindMax_ID_".$this->sensId, $maxData);
|
||||||
|
}
|
||||||
|
|
||||||
/* Bestimmte Werte den Klassenvariablen zuordnen */
|
/* Bestimmte Werte den Klassenvariablen zuordnen */
|
||||||
$this->nowWind = $nowData['wind'];
|
$this->nowWind = $nowData['wind'];
|
||||||
|
|
@ -126,8 +134,12 @@ class Wind{
|
||||||
|
|
||||||
/* liefert den Durchschnittswert in einem bestimmtem Interval */
|
/* liefert den Durchschnittswert in einem bestimmtem Interval */
|
||||||
function _getAverage($interval){
|
function _getAverage($interval){
|
||||||
$avQuery = "SELECT avg(geschw) as average, count(geschw) as count FROM ".$this->table." WHERE sens_id=".$this->sensId." AND timestamp>(select (current_timestamp - INTERVAL '".$interval."'))";
|
$avData = null;
|
||||||
$avData = $this->connection->fetchQueryResultLine($avQuery);
|
if(($avData = Cacher::getCache("WindAv_ID_".$this->sensId."_INTERVAL_".$interval, 10)) == false){
|
||||||
|
$avQuery = "SELECT avg(geschw) as average, count(geschw) as count FROM ".$this->table." WHERE sens_id=".$this->sensId." AND timestamp>(select (current_timestamp - INTERVAL '".$interval."'))";
|
||||||
|
$avData = $this->connection->fetchQueryResultLine($avQuery);
|
||||||
|
Cacher::setCache("WindAv_ID_".$this->sensId."_INTERVAL_".$interval, $avData);
|
||||||
|
}
|
||||||
return $avData;
|
return $avData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue