Newer
Older
use App\Model\Fan;
use App\Model\Rule;
use App\Model\Sensor;
use Hugga\Console;
use Hugga\Output\Drawing\Table;
protected $loaded = false;
/** @var array|Fan[] */
/** @var array|Sensor[] */
protected $sensors = [];
/** @var array|Rule[] */
protected $rules = [];
public function __construct(string $path)
{
$this->path = $path;
}
/** @return Fan[]|array */
public function getFans(): array
{
$this->load();
return $this->fans;
}
protected function load()
public function readConfig(): self
{
if (!file_exists($this->path)) {
return $this;
}
$this->loaded = true;
$cfg = include($this->path);
// validate config
if (!is_array($cfg)) {
throw new \InvalidArgumentException('given configuration is not a fan-ctrl config');
}
foreach ($cfg['fans'] ?? [] as $fanCfg) {
$class = $fanCfg['type'];
$fan = new $class($fanCfg['name'], $fanCfg['options']);
$this->fans[$fan->name] = $fan;
}
foreach ($cfg['sensors'] ?? [] as $sensorCfg) {
$class = $sensorCfg['type'];
$sensor = new $class($sensorCfg['name'], $sensorCfg['options']);
$this->sensors[$sensor->name] = $sensor;
}
foreach ($cfg['rules'] ?? [] as $ruleCfg) {
$class = $ruleCfg['type'];
$fans = array_map(fn($name) => $this->fans[$name] ?? null, $ruleCfg['fans']);
if (in_array(null, $fans, true)) {
continue;
}
$sensor = $this->sensors[$ruleCfg['sensor']] ?? null;
if (!$sensor) {
continue;
}
$rule = new $class($fans, $sensor, $ruleCfg['options']);
$this->rules[] = $rule;
}
return $this;
}
public function save()
{
$code = '<?php' . PHP_EOL . PHP_EOL .
'return ' . $this->exportArray([
'fans' => array_map(function (Fan $fan) {
return $fan->toArray();
}, array_values($this->fans)),
'sensors' => array_map(function (Sensor $sensor) {
return $sensor->toArray();
}, array_values($this->sensors)),
'rules' => array_map(function (Rule $rule) {
return $rule->toArray();
}, $this->rules),
]) . ';' . PHP_EOL;
file_put_contents($this->path, $code);
}
protected function exportArray(array $array, $indentation = 0)
{
$indexed = array_keys($array) === array_keys(array_keys($array));
$indentation += 4;
$var = '[' . PHP_EOL;
foreach ($array as $key => $value) {
$var .= str_repeat(' ', $indentation);
if (!$indexed) {
$var .= var_export($key, true) . ' => ';
}
$var .= is_array($value) ? $this->exportArray($value, $indentation) : var_export($value, true);
$var .= ',' . PHP_EOL;
}
$indentation -= 4;
$var .= str_repeat(' ', $indentation) . ']';
return $var;
}
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
public function display(Console $console)
{
$this->load();
$console->info('Fans');
$table = new Table($console, array_map(function (Fan $fan) {
if ($fan instanceof Fan\HwmonFan) {
$options = $fan->toArray()['options'];
return [
$fan->name,
$options['hwmon'],
$options['fan'],
$options['pwm'],
$options['start'],
$options['max'],
];
}
return [
$fan->name,
null,
null,
null,
null,
];
}, $this->fans), ['Name', 'hwmon', 'Fan', 'PWM', 'start', 'max']);
$table->column(4, ['width' => 5]);
$table->draw();
$console->line('');
$console->info('Sensors');
$table = new Table($console, array_map(function (Sensor $sensor) {
$options = $sensor->toArray()['options'];
$result = [$sensor->name];
if ($sensor instanceof Sensor\HwmonSensor) {
$result[] = $options['hwmon'];
$result[] = $options['temp'];
} elseif ($sensor instanceof Sensor\CommandSensor) {
$result[] = null;
$result[] = $options['command'];
} else {
$result[] = null;
$result[] = null;
}
return $result;
}, $this->sensors), ['Name', 'hwmon', 'sensor / command']);
$table->draw();
$console->line('');
$console->info('Rules');
$table = new Table($console, array_map(function (Rule $rule) {
return [
implode(', ', array_map(fn($fan) => $fan->name, $rule->fans)),
$rule->sensor->name,
$rule->describe(),
];
}, $this->rules), ['Fans', 'Sensor', 'Description']);
$table->draw();
}