Skip to content
Snippets Groups Projects
Verified Commit 70aeef2d authored by Thomas Flori's avatar Thomas Flori
Browse files

implement daemon command

parent f3fcc560
No related branches found
No related tags found
No related merge requests found
Pipeline #644 canceled with stages
<?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);
}
}
}
...@@ -48,6 +48,18 @@ class FanConfig ...@@ -48,6 +48,18 @@ class FanConfig
return $this->fans; return $this->fans;
} }
public function getSensors(): array
{
$this->load();
return $this->sensors;
}
public function getRules(): array
{
$this->load();
return $this->rules;
}
protected function load() protected function load()
{ {
if (!$this->loaded) { if (!$this->loaded) {
...@@ -162,36 +174,40 @@ class FanConfig ...@@ -162,36 +174,40 @@ class FanConfig
$table->column(4, ['width' => 5]); $table->column(4, ['width' => 5]);
$table->draw(); $table->draw();
$console->line(''); if (!empty($this->sensors)) {
$console->line('');
$console->info('Sensors');
$table = new Table($console, array_map(function (Sensor $sensor) { $console->info('Sensors');
$options = $sensor->toArray()['options']; $table = new Table($console, array_map(function (Sensor $sensor) {
$result = [$sensor->name]; $options = $sensor->toArray()['options'];
if ($sensor instanceof Sensor\HwmonSensor) { $result = [$sensor->name];
$result[] = $options['hwmon']; if ($sensor instanceof Sensor\HwmonSensor) {
$result[] = $options['temp']; $result[] = $options['hwmon'];
} elseif ($sensor instanceof Sensor\CommandSensor) { $result[] = $options['temp'];
$result[] = null; } elseif ($sensor instanceof Sensor\CommandSensor) {
$result[] = $options['command']; $result[] = null;
} else { $result[] = $options['command'];
$result[] = null; } else {
$result[] = null; $result[] = null;
} $result[] = null;
return $result; }
}, $this->sensors), ['Name', 'hwmon', 'sensor / command']); return $result;
$table->draw(); }, $this->sensors), ['Name', 'hwmon', 'sensor / command']);
$table->draw();
}
$console->line(''); if (!empty($this->rules)) {
$console->line('');
$console->info('Rules'); $console->info('Rules');
$table = new Table($console, array_map(function (Rule $rule) { $table = new Table($console, array_map(function (Rule $rule) {
return [ return [
implode(', ', array_map(fn($fan) => $fan->name, $rule->fans)), implode(', ', array_map(fn($fan) => $fan->name, $rule->fans)),
$rule->sensor->name, $rule->sensor->name,
$rule->describe(), $rule->describe(),
]; ];
}, $this->rules), ['Fans', 'Sensor', 'Description']); }, $this->rules), ['Fans', 'Sensor', 'Description']);
$table->draw(); $table->draw();
}
} }
} }
...@@ -17,6 +17,7 @@ class Kernel extends \Riki\Kernel ...@@ -17,6 +17,7 @@ class Kernel extends \Riki\Kernel
protected static $commands = [ protected static $commands = [
Command\Config::class, Command\Config::class,
Command\ShowConfig::class, Command\ShowConfig::class,
Command\Daemon::class,
]; ];
/** @var \Riki\Application|Application */ /** @var \Riki\Application|Application */
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace App\Model; namespace App\Model;
use App\Model; use App\Model;
use Hugga\Console;
abstract class Rule extends Model abstract class Rule extends Model
{ {
...@@ -12,7 +13,7 @@ abstract class Rule extends Model ...@@ -12,7 +13,7 @@ abstract class Rule extends Model
/** @var Sensor */ /** @var Sensor */
public $sensor; public $sensor;
abstract public function apply(); abstract public function apply(Console $console);
abstract public function describe(): string; abstract public function describe(): string;
} }
...@@ -5,6 +5,7 @@ namespace App\Model\Rule; ...@@ -5,6 +5,7 @@ namespace App\Model\Rule;
use App\Model\Fan; use App\Model\Fan;
use App\Model\Rule; use App\Model\Rule;
use App\Model\Sensor; use App\Model\Sensor;
use Hugga\Console;
class CurveRule extends Rule class CurveRule extends Rule
{ {
...@@ -43,29 +44,50 @@ class CurveRule extends Rule ...@@ -43,29 +44,50 @@ class CurveRule extends Rule
} }
} }
public function apply() public function apply(Console $console)
{ {
$temp = $this->sensor->getTemperature(); $temp = $this->sensor->getTemperature();
$lastPercentage = null; $lastPercentage = null;
$lastLimit = null; $lastLimit = null;
foreach ($this->points as $percentage => $limit) { foreach ($this->points as $percentage => $limit) {
if ($temp >= $limit) { if ($temp > $limit) {
$lastPercentage = $percentage; $lastPercentage = $percentage;
$lastLimit = $limit; $lastLimit = $limit;
continue; continue;
// somewhere between $lastV and $v // somewhere between $lastV and $v
} }
if (!$this->alwaysOn && $lastPercentage === null) { if (!$this->alwaysOn && $lastPercentage === null) {
// stop the fans if they should not run all the time and first limit not reached // stop the fans if they should not run all the time and first limit not reached
$this->stopFans(); $this->stopFans();
} elseif ($lastPercentage === null) { } elseif ($lastPercentage === null) {
// run the fans with the lowest percentage value before the first point // run the fans with the lowest percentage value before the first point
$console->line(sprintf(
'Temperature of %s: %d° C; Speed: %.2f%%',
$this->sensor->name,
$temp,
$percentage,
), Console::WEIGHT_DEBUG);
$this->setSpeed($percentage); $this->setSpeed($percentage);
} else { } else {
$factor = $temp - $lastLimit / ($limit - $lastLimit); $factor = ($temp - $lastLimit) / ($limit - $lastLimit);
$this->setSpeed(($percentage - $lastPercentage) * $factor); $speed = round(($percentage - $lastPercentage) * $factor + $lastPercentage, 2);
$console->line(sprintf(
'Temperature of %s: %d° C; Speed: %.2f%% (Calculation: (%d - %d) / (%d - %d) * (%d - %d) + %d)',
$this->sensor->name,
$temp,
$speed,
$temp,
$lastLimit,
$limit,
$lastLimit,
$percentage,
$lastPercentage,
$lastPercentage
), Console::WEIGHT_DEBUG);
$this->setSpeed($speed);
} }
break;
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment