rewrite inverter.php to function as daemon

add logrotate file
This commit is contained in:
2012-11-27 09:49:11 +01:00
parent e215af4d86
commit 728e41c5dc
4 changed files with 232 additions and 79 deletions

View File

@@ -1,44 +1,86 @@
#!/usr/bin/php
<?php
$sWake = '7:30';
$sSleep = '17:30';
$sTask = '/opt/inverter/inverter.pl > /dev/null';
require_once 'functions.php';
require_once 'System/Daemon.php'; // pear install -f System_Daemon
printf("Be awake between %s and %s\n", $sWake, $sSleep);
$fWake = getHour($sWake);
$fSleep = getHour($sSleep);
chdir('/opt/inverter/');
define('NAME', 'inverter');
define('TASK', '/opt/inverter/inverter.pl > /dev/null');
define('CWD', '/opt/inverter/');
define('MODE', 0755);
define('FILE_DAEMON_START', 'daemon_start.sh');
define('FILE_DAEMON_STOP', 'daemon_stop.sh');
define('DEFAULT_WAKE', '7:00');
define('DEFAULT_SLEEP', '19:00');
chdir(CWD);
while (true) {
// Check for current need to be awake
/* Remove previous at entries */
foreach (explode("\n", trim(command('atq'))) as $sJob) {
$sId = substr($sJob, 0, strpos($sJob, "\t"));
$sJob = command(sprintf('at -c %s ' . "\n", $sId));
$aJob = explode("\n", trim(command(sprintf('at -c %s', $sId))));
if (strpos(array_pop($aJob), NAME) !== false) {
command(sprintf('atrm %s', $sId));
}
}
/* Inverter daemon */
System_Daemon::setOptions(array(
'appName' => NAME,
'appDescription' => '',
'authorName' => '',
'authorEmail' => ''));
/* Install service */
if (isset($argv[1]) && $argv[1] == 'install') {
System_Daemon::writeAutoRun(); // update-rc.d %s defaults
/* Write scripts for scheduling with at */
if (!file_exists(FILE_DAEMON_START)) {
file_put_contents(FILE_DAEMON_START, sprintf("#!/bin/bash\nservice %s start", NAME));
chmod(FILE_DAEMON_START, MODE);
}
if (!file_exists(FILE_DAEMON_STOP)) {
file_put_contents(FILE_DAEMON_STOP, sprintf("#!/bin/bash\nservice %s stop", NAME));
chmod(FILE_DAEMON_STOP, MODE);
}
exit;
}
/* 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);
System_Daemon::info(sprintf('Be awake between %s and %s', $sWake, $sSleep));
/* Start deamon */
System_Daemon::start();
$bStop = false;
while (!$bStop && !System_Daemon::isDying()) {
/* Check for current need to be awake */
$fNow = getHour();
if (!($bAwake = $fNow >= $fWake)) {
printf("[%s] Too early to wake!\n", date('r'));
System_Daemon::info('Too early to wake!');
} else if ($bSleep = $fNow >= $fSleep) {
printf("[%s] Time to sleep!\n", date('r'));
System_Daemon::info('Time to sleep!');
}
if ($bAwake && !$bSleep) {
// Need to be awake now
printf("[%s] Running task\n", date('r'));
system($sTask);
printf("[%s] Task ended\n", date('r'));
/* Schedule next sleep time */
$sTime = date('H:i', strtotime($sWake));
command(sprintf('at -f %s %s 2> /dev/null', FILE_DAEMON_STOP, $sTime));
/* Execute task */
System_Daemon::info('Running task');
command(TASK);
System_Daemon::info('Task ended');
} else {
// Don't need to be awake now
if (!$bAwake) {
// Sleep untill wake time
$iTime = strtotime($sWake);
} else {
// Sleep untill next day wake time
$iTime = strtotime(sprintf('%s + 1 day', $sWake));
}
printf("[%s] Sleeping untill: %s\n", date('r'), date('r', $iTime));
time_sleep_until($iTime);
/* Schedule next wake time */
$sTime = date('H:i', strtotime($sWake)); // ignore slight deviation for next day
command(sprintf('at -f %s %s 2> /dev/null', FILE_DAEMON_START, $sTime));
System_Daemon::info(sprintf('Waiting untill %s', $sTime));
$bStop = true;
}
}
echo "\n";
function getHour($sTime = null) {
$iTime = $sTime === null ? time() : strtotime($sTime);
return date('H', $iTime) + date('i', $iTime) / 60;
}
/* Stop daemon */
System_Daemon::stop();

View File

@@ -87,40 +87,4 @@ if (isset($aSystems[$sSerial])) {
CURLOPT_RETURNTRANSFER => true));
$sResult = curl_exec($rCurl);
file_put_contents('pvtest', sprintf("[%s] %s\n", date('r'), $sResult), FILE_APPEND);
}
/*
$a = file_get_contents('test');
$b = explode("\n", $a);
array_pop($b);
foreach ($b as $c) {
$d = explode(',', $c);
$iTime = $d[2];
$fToday = $d[4];
$fEnergy = $d[6];
$fToday = $fToday > ((1 + MARGIN) * $fEnergy) ? $fEnergy : $fToday;
$fPower = $d[5];
$sSerial = $d[3];
$e = sprintf("%s,%s,%s\n", date('H:i', $iTime), $fToday * 1000, $fPower);
file_put_contents('do.csv', $e, FILE_APPEND);
/*
$rCurl = curl_init();
curl_setopt_array($rCurl, array(
CURLOPT_URL => PVOUTPUT_URL,
CURLOPT_HTTPHEADER => array(
sprintf('X-Pvoutput-Apikey: %s', $aSystems[$sSerial][0]),
sprintf('X-Pvoutput-SystemId: %s', $aSystems[$sSerial][1])),
CURLOPT_POSTFIELDS => http_build_query(array(
'd' => date('Ymd', $iTime),
't' => date('H:i', $iTime),
'v1' => 1000 * $fToday, // Wh
'v2' => $fPower)),
CURLOPT_RETURNTRANSFER => true));
echo $sResult = curl_exec($rCurl);
sleep(61);*
}
exit;*/
}