use new rrd class for pvoutput.php

This commit is contained in:
2013-08-06 13:27:53 +02:00
parent 585740be58
commit 79c6bd17e0
2 changed files with 13 additions and 15 deletions

View File

@@ -1,9 +1,9 @@
#!/usr/bin/php #!/usr/bin/php
<?php <?php
require_once 'functions.php'; require_once 'functions.php';
require_once 'rrd.php';
define('RRD_FILE', 'data/inverter_%s_today.rrd'); define('RRD_FILE', 'data/inverter_%s_today.rrd');
define('RRD_FETCH', 'rrdtool fetch %s AVERAGE -r %d -s %d -e %d');
define('PVOUTPUT_URL', 'http://pvoutput.org/service/r1/addstatus.jsp'); define('PVOUTPUT_URL', 'http://pvoutput.org/service/r1/addstatus.jsp');
define('TODAY_FILE', 'data/today_%s.csv'); define('TODAY_FILE', 'data/today_%s.csv');
define('FIELD', 'PAC'); define('FIELD', 'PAC');
@@ -48,11 +48,7 @@ $iLast = $aToday[2];
/* Extract fields */ /* Extract fields */
$iTime = time(); $iTime = time();
$sData = command(sprintf(RRD_FETCH, sprintf(RRD_FILE, $sSerial), RESOLUTION, $iLast, $iTime)); list($aFields, $aData) = RRD::fetch(sprintf(RRD_FILE, $sSerial), RESOLUTION, $iLast, $iTime, 'AVERAGE');
$aData = explode("\n", trim($sData));
$aFields = preg_split("~[\s]+~", array_shift($aData));
array_shift($aData);
$aFields = array_flip($aFields);
/* Process data */ /* Process data */
$bFirst = true; $bFirst = true;
@@ -60,8 +56,7 @@ $fEnergy = 0;
if (isset($aFields[FIELD])) { if (isset($aFields[FIELD])) {
$iField = $aFields[FIELD] + 1; $iField = $aFields[FIELD] + 1;
array_shift($aData); array_shift($aData);
foreach ($aData as $sRow) { foreach ($aData as $aRow) {
$aRow = explode(' ', $sRow);
$iDate = substr($aRow[0], 0, -1); $iDate = substr($aRow[0], 0, -1);
$iInterval = $bFirst ? (($bFirst = false) || RESOLUTION) : $iDate - $iLast; // s $iInterval = $bFirst ? (($bFirst = false) || RESOLUTION) : $iDate - $iLast; // s
if (($fValue = floatval($aRow[$iField])) > 0) { // W if (($fValue = floatval($aRow[$iField])) > 0) { // W

View File

@@ -9,7 +9,7 @@ class RRD {
protected static $aPipes = array(); protected static $aPipes = array();
protected function __construct() { protected function __construct() {
self::$rProcess= proc_open('rrdtool -', array( self::$rProcess = proc_open('rrdtool -', array(
0 => array('pipe', 'r'), 0 => array('pipe', 'r'),
1 => array('pipe', 'w')), self::$aPipes); 1 => array('pipe', 'w')), self::$aPipes);
stream_set_blocking(self::$aPipes[1], false); stream_set_blocking(self::$aPipes[1], false);
@@ -19,7 +19,7 @@ class RRD {
if (!isset(self::$rProcess)) { if (!isset(self::$rProcess)) {
self::$oInstance = new self(); self::$oInstance = new self();
} }
var_dump($sCommand); //echo $sCommand . "\n";
fwrite(self::$aPipes[0], $sCommand . PHP_EOL); fwrite(self::$aPipes[0], $sCommand . PHP_EOL);
$nNull = null; $nNull = null;
$aRead = array(self::$aPipes[1]); $aRead = array(self::$aPipes[1]);
@@ -45,14 +45,17 @@ class RRD {
$aFields = array_flip($aFields); $aFields = array_flip($aFields);
array_shift($aData); array_shift($aData);
array_pop($aData); array_pop($aData);
foreach ($aData as $iKey => $sRow) { $aValues = array();
foreach ($aData as $sRow) {
$aRow = explode(':', $sRow); $aRow = explode(':', $sRow);
$iTime = current($aRow); $iTime = current($aRow);
$mValue = trim(next($aRow)); $aRow = explode(' ', trim(next($aRow)));
if ($mValue != '-nan') { foreach ($aRow as $iKey => $mValue) {
$aValues[$iTime] = floatval($mValue); if (strpos($mValue, 'nan') !== false) {
$aRow[$iKey] = null;
}
} }
//$aValues[$iTime] = $fValue == 0 ? null : $fValue; $aValues[$iTime] = $aRow;
} }
return array($aFields, $aValues); return array($aFields, $aValues);
} }