Newer
Older
<?php
namespace App\Command;
use App\Application;
use GetOpt\GetOpt;
use Hugga\Console;
class Daemon extends AbstractCommand
{
const PID_PATH = '/run/fan-ctrl.pid';
const UPDATE_INTERVAL = 500_000; // in micro seconds
protected $name = 'daemon';
protected $description = 'Run the monitoring and control the fans';
public function __construct(Application $app, Console $console)
{
parent::__construct($app, $console);
}
public function handle(GetOpt $getOpt): int
{
if (posix_getuid() !== 0) {
$this->warn('This programm has to run as root');
return 1;
}
if (file_exists(self::PID_PATH) && posix_getpgid((int)file_get_contents(self::PID_PATH))) {
$this->warn('Fan Control is already running');
return 1;
}
file_put_contents(self::PID_PATH, posix_getpid());
$fanConfig = $this->app->fanConfig;
try {
$fanConfig->readConfig();
} catch (\Throwable $e) {
$this->error('Unable to read config: ' . $e->getMessage());
return 2;
}
if (!$fanConfig->hasFans()) {
$this->error('No fans configured. Run config first');
return 3;
}
// @todo check that the fans are not in manual controll already
// prepare
$rules = $fanConfig->getRules();
$this->console->setLogger($this->app->logger);
$this->info('Fan Control started');
$showStatus = false;
if (microtime(true) > $last+5) {
$last = microtime(true);
$showStatus = true;
}
usleep(self::UPDATE_INTERVAL);