Skip to content
Snippets Groups Projects
Daemon.php 1.21 KiB
Newer Older
Thomas Flori's avatar
Thomas Flori committed
<?php

namespace App\Command;

use App\Application;
use GetOpt\GetOpt;
use Hugga\Console;

class Daemon extends AbstractCommand
{
    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;
        }
        $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);
        }
    }
}