diff --git a/opt/inverter/daemon.php b/opt/inverter/daemon.php new file mode 100644 index 0000000..8fc878b --- /dev/null +++ b/opt/inverter/daemon.php @@ -0,0 +1,68 @@ + NAME, + 'appDescription' => '', + 'authorName' => '', + 'authorEmail' => '')); + + /* Derive process name */ + $sName = basename(substr(TASK, 0, strpos(TASK, ' '))); +} + +function daemon_install() { + System_Daemon::writeAutoRun(); // update-rc.d %s defaults + + /* Write scripts for scheduling with at */ + if (isset($argv[2]) && $argv[2] == 'schedule') { + 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); + } + } +} + +function daemon_run() { + global $rProcess; + + /* Hook onto daemon termination handler */ + System_Daemon::setSigHandler(SIGTERM, 'daemon_sigterm_handler'); + + /* Start deamon */ + System_Daemon::start(); + while (!System_Daemon::isDying()) { + System_Daemon::info('Open process'); + $rProcess = proc_open(TASK, array(), $aPipes); + do { + System_Daemon::isRunning(); // required for deamon to respond properly + sleep(PROCESS_POLL); // gets interrupted on process termination + $aStatus = proc_get_status($rProcess); + } while ($aStatus['running']); + System_Daemon::info('Process ended'); + } +} + +function daemon_sigterm_handler($iSigNo) { + global $sName; + system(sprintf('pkill %s', $sName)); + System_Daemon::stop(); +} \ No newline at end of file diff --git a/opt/inverter/inverter.php b/opt/inverter/inverter.php index 5cbffab..ce5e6e3 100644 --- a/opt/inverter/inverter.php +++ b/opt/inverter/inverter.php @@ -1,17 +1,17 @@ #!/usr/bin/php /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'); + +/* Initialize */ chdir(CWD); +daemon_init(); /* Remove previous at entries */ foreach (explode("\n", trim(command('atq 2> /dev/null'))) as $sJob) { @@ -23,75 +23,36 @@ foreach (explode("\n", trim(command('atq 2> /dev/null'))) as $sJob) { } } -/* 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 = $bAlarm = false; - -while (!$bStop && !System_Daemon::isDying()) { - /* Check for current need to be awake */ - $fNow = getHour(); - if (!($bAwake = $fNow >= $fWake)) { - System_Daemon::info('Too early to wake!'); - } else if ($bSleep = $fNow >= $fSleep) { - System_Daemon::info('Time to sleep!'); - } - - if ($bAwake && !$bSleep) { - /* Schedule next sleep time */ - /*if (!$bAlarm) { - $sTime = date('H:i', strtotime($sSleep)); - System_Daemon::info(sprintf('Schedule sleep at %s', $sTime)); - command(sprintf('at -f %s %s 2> /dev/null', FILE_DAEMON_STOP, $sTime)); - $bAlarm = true; - }*/ - - /* Execute task */ - System_Daemon::info('Running task'); - command(TASK); - System_Daemon::info('Task ended'); - } else { - $bStop = true; - } +/* Check appropriate state */ +$fNow = getHour(); +if (!($bAwake = $fNow >= $fWake)) { + System_Daemon::info('Too early to wake!'); +} else if ($bSleep = $fNow >= $fSleep) { + System_Daemon::info('Time to sleep!'); } - -/* Schedule next wake time */ schedule_wake(); -/* Stop daemon */ -System_Daemon::stop(); +if ($bAwake && !$bSleep) { + schedule_sleep(); + daemon_run(); +} function schedule_wake() { global $sWake; $sTime = date('H:i', strtotime($sWake)); // ignore slight deviation for next day - System_Daemon::info(sprintf('Waiting untill %s', $sTime)); + System_Daemon::info(sprintf('Schedule wake at %s', $sTime)); command(sprintf('at -f %s %s 2> /dev/null', FILE_DAEMON_START, $sTime)); } + +function schedule_sleep() { + global $sSleep; + $sTime = date('H:i', strtotime($sSleep)); + System_Daemon::info(sprintf('Schedule sleep at %s', $sTime)); + command(sprintf('at -f %s %s 2> /dev/null', FILE_DAEMON_STOP, $sTime)); +} \ No newline at end of file