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';
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();
// main loop
while (true) {
foreach ($rules as $rule) {
$rule->apply($this->console);
}
sleep(1);
}
}
}