Newer
Older
<?php
namespace App\Subroutines;
use App\Application;
use App\Concerns\InteractiveConsole;
use App\Model\Fan\HwmonFan;
use App\Service\Utils;
use App\Subroutines\Fan\EditFan;
use Hugga\Console;
use Hugga\Input\Question\Choice;
use Hugga\Input\Question\Confirmation;
class InitializeConfig
{
use InteractiveConsole;
/** @var Application */
protected $app;
public function __construct(Application $app, Console $console)
{
$this->app = $app;
$this->console = $console;
}
public function main(): int
{
$fanConfig = $this->app->fanConfig;
$this->info('No fans configured. Detecting fans...');
$fans = $this->detectControllableFans();
if (count($fans) > 0) {
foreach ($fans as $fan) {
$this->line('');
$this->info('Fan ' . $fan->name);
$start = $this->detectStartValue($fan);
$fan->setStartValue($start);
$this->table([$fan->toRow()], ['Name', 'hwmon', 'Fan', 'PWM', 'start', 'max']);
do {
$answer = $this->ask(new Choice([
'skip fan (don\'t include fan in config)',
], 'What next?', 'edit fan'));
case 'edit fan':
$this->app->get(EditFan::class)->main($fan);
case 'add fan':
$fanConfig->addFan($fan);
break 2;
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
case 'skip fan':
break 2;
}
} while (true);
}
$fanConfig->save();
return 0;
}
$this->error('We could not find any fan to control.');
return 1;
}
/**
* @return array|HwmonFan[]
*/
protected function detectControllableFans(): array
{
$fans = [];
$inputs = glob(HWMON_PATH . '/hwmon*/fan*_input');
$inputs = array_reduce($inputs, function ($inputs, $input) {
if (!preg_match('~(hwmon(\d+)/(fan\d+))_input$~', $input, $match)) {
return $inputs;
}
$hwmon = trim(file_get_contents(HWMON_PATH . '/hwmon' . $match[2] . '/name'));
$fan = $match[3];
$pwm = preg_replace('~fan(\d+)~', 'pwm$1', $fan);
$name = $hwmon . '/' . $fan;
if (!file_exists(HWMON_PATH . '/hwmon' . $match[2] . '/' . $pwm)) {
return $inputs;
}
$options = [
'hwmon' => $hwmon,
'fan' => $fan,
'pwm' => $pwm,
];
$inputs[$name] = [
'fan' => new HwmonFan($name, $options),
'options' => $options,
'hwmon' => 'hwmon' . $match[2],
];
return $inputs;
}, []);
$unknownInputs = [];
foreach ($inputs as $name => $input) {
[$min, $max] = $this->testPwm($input['fan']);
if ($max === 0 || $min > $max*0.4) {
$unknownInputs[$name] = $input;
continue;
}
$this->line(sprintf('%s at full speed: %d; stopped: %d', $name, $max, $min));
$fans[] = $input['fan'];
}
if (count($unknownInputs) > 0) {
$this->info(PHP_EOL . 'Some fans could not be assigned to a pwm. Trying remaining pwms...');
$assignedPwms = array_map(fn(HwmonFan $fan) => $fan->toArray()['options']['pwm'], $fans);
$pwms = array_reduce(glob(HWMON_PATH . '/hwmon*/pwm*'), function ($pwms, $pwm) use ($assignedPwms) {
if (!preg_match('~(hwmon(\d+)/(pwm\d+))$~', $pwm, $match) || in_array(basename($pwm), $assignedPwms)) {
return $pwms;
}
$hwmon = $match[2];
if (!isset($pwms[$hwmon])) {
$pwms[$hwmon] = [];
}
$pwms[$hwmon][] = basename($pwm);
return $pwms;
}, []);
foreach ($unknownInputs as $id => $input) {
if (!isset($pwms[$input['hwmon']])) {
continue;
}
foreach ($pwms[$input['hwmon']] as $pwm) {
$name = $input['fan']->name;
$options = $input['options'];
if ($options['pwm'] === $pwm) {
continue;
}
$options['pwm'] = $pwm;
$fan = new HwmonFan($name, $options);
[$min, $max] = $this->testPwm($fan);
if ($max === 0 || $min > $max*0.4) {
continue;
}
$fans[] = $fan;
continue 2;
}
$this->info($id . ' not connected or not controllable');
}
}
return $fans;
}
protected function testPwm(HwmonFan $fan): array
{
$fullSpeed = $fan->getCurrentSpeed();
if ($fullSpeed === 0) {
$fan->resetState();
return [0, 0];
}
$stopSpeed = $fan->getCurrentSpeed();
$fan->resetState();
return [$stopSpeed, $fullSpeed];
}
protected function detectStartValue(HwmonFan $fan): int
{
$this->info('Detecting start value for ' . $fan->name);
$minSpeed = $fan->getCurrentSpeed() * 1.1;
$pwm = 0;
do {
$fan->setPwmAndWait($pwm += 5);
$currentSpeed = $fan->getCurrentSpeed();
$this->line(sprintf('%d rpm with pwm value %d', $currentSpeed, $pwm));
} while ($minSpeed >= $currentSpeed);
$fan->resetState();
return $pwm;
}
}