From 585740be5887a73fef40f8bc28b74709acc9774d Mon Sep 17 00:00:00 2001 From: Rik Veenboer Date: Tue, 6 Aug 2013 12:14:32 +0200 Subject: [PATCH] modified functions and dependencies. --- opt/inverter/create_rrd.php | 14 ++++++-- opt/inverter/functions.php | 31 ++++++++++++++++- opt/inverter/inverter.php | 9 +++-- opt/inverter/pvoutput.php | 13 +++++--- opt/inverter/rrd.php | 66 +++++++++++++++++++++++++++++++++++++ 5 files changed, 120 insertions(+), 13 deletions(-) create mode 100644 opt/inverter/rrd.php diff --git a/opt/inverter/create_rrd.php b/opt/inverter/create_rrd.php index 5e2f71f..95e7332 100644 --- a/opt/inverter/create_rrd.php +++ b/opt/inverter/create_rrd.php @@ -13,6 +13,16 @@ $aRRDCreate = array( DS:PAC:GAUGE:120:0:U DS:ETOTAL:GAUGE:120:0:U DS:ETODAY:GAUGE:120:0:U + RRA:MIN:0.5:1:720 + RRA:MIN:0.5:17:1017 + RRA:MIN:0.5:120:1008 + RRA:MIN:0.5:535:1002 + RRA:MIN:0.5:6324:1001 + RRA:MAX:0.5:1:720 + RRA:MAX:0.5:17:1017 + RRA:MAX:0.5:120:1008 + RRA:MAX:0.5:535:1002 + RRA:MAX:0.5:6324:1001 RRA:AVERAGE:0.5:1:720 RRA:AVERAGE:0.5:17:1017 RRA:AVERAGE:0.5:120:1008 @@ -54,7 +64,7 @@ foreach (glob($sDataDirectory . '/*.csv') as $sFile) { if ($bFirst) { foreach ($aRRDCreate as $sRRDFile => $sRRDCreate) { /* Create RRD database */ - echo $sCommand = str_replace("\n", ' ', sprintf($sRRDCreate, $sRRDFile, $aValues[0] - 1)) . "\n"; + $sCommand = str_replace("\n", ' ', sprintf($sRRDCreate, $sRRDFile, $aValues[0] - 1)) . "\n"; fwrite($rHandle, $sCommand); } $bFirst = false; @@ -64,7 +74,7 @@ foreach (glob($sDataDirectory . '/*.csv') as $sFile) { /* Update relevant fields in RRD database */ $aRRDValues = array_intersect_key($aValues, $aRRDKeys[$sRRDFile]); $sCommand = sprintf("update %s %d:%s\n", $sRRDFile, $aValues[0], implode(':', $aRRDValues)); - printf('[%d] %s', $i, $sCommand); + //printf('[%d] %s', $i, $sCommand); fwrite($rHandle, $sCommand); } } diff --git a/opt/inverter/functions.php b/opt/inverter/functions.php index fe6e8f5..97ecebe 100644 --- a/opt/inverter/functions.php +++ b/opt/inverter/functions.php @@ -1,8 +1,18 @@ /dev/null'); define('CWD', '/opt/inverter/'); -define('DEFAULT_WAKE', '7:00'); -define('DEFAULT_SLEEP', '19:00'); /* Initialize */ chdir(CWD); @@ -24,9 +22,10 @@ foreach (explode("\n", trim(command('atq 2> /dev/null'))) as $sJob) { } /* Wake at sunrise, sleep at sunset */ -$aTwilight = getTwilight(date('Y'), date('z')); -$fWake = getHour($sWake = isset($aTwilight) ? $aTwilight[1] : DEFAULT_WAKE); -$fSleep = getHour($sSleep = isset($aTwilight) ? $aTwilight[3] : DEFAULT_SLEEP); +$fWake = getHour(getWake($aTwilight)); +$fSleep = getHour($sSleep = getSleep($aTwilight)); +$sWake = $aTwilight[1]; +$sSleep = $aTwilight[3]; System_Daemon::info(sprintf('Be awake between %s and %s', $sWake, $sSleep)); /* Check appropriate state */ diff --git a/opt/inverter/pvoutput.php b/opt/inverter/pvoutput.php index 97bc83b..13b89d4 100644 --- a/opt/inverter/pvoutput.php +++ b/opt/inverter/pvoutput.php @@ -1,17 +1,17 @@ #!/usr/bin/php array('16e7a916d69656e354d00461a4da1d2e40cfa4f1', '12419') ); @@ -28,8 +28,7 @@ $fVoltage = floatval($argv[3]); // V $sSerial = $argv[4]; /* Fetch temperature */ -$aData = wunderground('conditions', sprintf('pws:%s', STATION)); -$fTemperature = isset($aData['current_observation']['temp_c']) ? $aData['current_observation']['temp_c'] : null; +$fTemperature = getTemperature(); /* Fetch twilight data */ $iDay = date('z'); @@ -79,7 +78,10 @@ $aToday[3] = $fTemperature; file_put_contents($sTodayFile, implode(',', $aToday)); /* Correct today data */ -$fToday = abs($aToday[1] - $fToday) > (MARGIN_ENERGY * $aToday[1]) ? $aToday[1] : $fToday; +$iWake = getWake($aTwilight); +if (($iTime - $iWake) / 3600 < TRESHOLD_CORRECT && abs($aToday[1] - $fToday) > (MARGIN_ENERGY * $aToday[1])) { + $fToday = $aToday[1]; +} /* Construct PVOutput data */ $aData = array( @@ -95,6 +97,7 @@ if (isset($fTemperature)) { $fTemperature = abs($aToday[3] - $fTemperature) > (MARGIN_TEMPERATURE * $aToday[3]) ? $aToday[3] : $fTemperature; } $aData['v5'] = $fTemperature; // ignore potential flaws in first temperature of the day + file_put_contents('temp.csv', sprintf("%d,%f\n", $iTime,$fTemperature), FILE_APPEND); } /* Store debug data */ diff --git a/opt/inverter/rrd.php b/opt/inverter/rrd.php new file mode 100644 index 0000000..ce175fc --- /dev/null +++ b/opt/inverter/rrd.php @@ -0,0 +1,66 @@ + array('pipe', 'r'), + 1 => array('pipe', 'w')), self::$aPipes); + stream_set_blocking(self::$aPipes[1], false); + } + + static function command($sCommand) { + if (!isset(self::$rProcess)) { + self::$oInstance = new self(); + } + var_dump($sCommand); + fwrite(self::$aPipes[0], $sCommand . PHP_EOL); + $nNull = null; + $aRead = array(self::$aPipes[1]); + stream_select($aRead, $nNull, $nNull, 10); + return trim(stream_get_contents(self::$aPipes[1])); + } + + static function create($sFile, $iStep, $iStart, $sContents) { + $sCommand = sprintf(self::CREATE, $sFile, $iStep, $iStart, str_replace("\n", ' ', trim($sContents))); + return RRD::command($sCommand); + } + + static function update($sFile, $iTime, $aValues) { + $sCommand = sprintf(self::UPDATE, $sFile, $iTime, implode(':', $aValues)); + return RRD::command($sCommand); + } + + static function fetch($sFile, $iResolution, $iStart, $iEnd, $sType = 'AVERAGE') { + $sCommand = sprintf(self::FETCH, $sFile, $sType, $iResolution, $iStart, $iEnd); + $sData = RRD::command($sCommand); + $aData = explode("\n", trim($sData)); + $aFields = preg_split("~[\s]+~", array_shift($aData)); + $aFields = array_flip($aFields); + array_shift($aData); + array_pop($aData); + foreach ($aData as $iKey => $sRow) { + $aRow = explode(':', $sRow); + $iTime = current($aRow); + $mValue = trim(next($aRow)); + if ($mValue != '-nan') { + $aValues[$iTime] = floatval($mValue); + } + //$aValues[$iTime] = $fValue == 0 ? null : $fValue; + } + return array($aFields, $aValues); + } + + function __destruct() { + fwrite(self::$aPipes[0], "quit\n"); + fclose(self::$aPipes[0]); + fclose(self::$aPipes[1]); + proc_close(self::$rProcess); + } +} \ No newline at end of file