Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
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
197
198
199
200
201
202
203
<?php
namespace App\Command;
use App\Application;
use GetOpt\GetOpt;
use Hugga\Console;
use Hugga\Input\Question\Choice;
use Hugga\Input\Question\Confirmation;
class Config extends AbstractCommand
{
protected $name = 'config';
protected $description = 'Generate or update fan configuration';
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 ($this->getOption('quiet')) {
$this->warn('This command requires user interaction');
return 1;
}
$fanConfig = $this->app->fanConfig;
if (!$fanConfig->hasFans()) {
if (!$this->initializeConfig()) {
return 1;
}
}
$actions = new Choice([
'edit a fan',
'add a sensor',
'edit a sensor',
'add a rule',
'edit a rule',
'save and exit',
'exit without saving',
], 'What next?', 'save and exit');
do {
$answer = $this->console->ask($actions);
switch ($answer) {
case 'exit without saving':
break 2;
case 'save and exit':
// $fanConfig->save();
break 2;
default:
$this->info('not implemented');
}
} while (true);
// - edit a fan
// - add a sensor
// - edit a sensor
// - add a rule
// - edit a rule
// - save and leave
// - leave without saving
return 0;
}
protected function initializeConfig()
{
$fanConfig = $this->app->fanConfig;
$this->info('No fans configured. Detecting fans...');
$fans = $this->detectControllableFans();
if (count($fans) > 0) {
// for each fan
// ask if it should be added
// ask for a name
// test min speed
// ask for a pwm limit
// add the fan to the config
$fanConfig->setFans($fans);
$fanConfig->save();
return true;
}
$this->error('We could not find any fan to control.');
return false;
}
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;
$inputs[$name] = [
'input' => $input,
'pwm' => preg_replace('~fan(\d+)_input~', 'pwm$1', $input),
'hwmon' => 'hwmon' . $match[2],
'name' => $name,
'type' => 'hwmon',
'options' => [
'hwmon' => $hwmon,
'fan' => $fan,
'pwm' => $pwm,
],
];
return $inputs;
}, []);
$unknownInputs = [];
foreach ($inputs as $name => $input) {
[$min, $max] = $this->testPwm($input['pwm'], $input['input']);
if ($max === 0 || $min > $max*0.4) {
$unknownInputs[$name] = $input;
continue;
}
$this->info(sprintf('%s at full speed: %d; stopped: %d', $name, $max, $min));
$fans[] = $input;
}
if (count($unknownInputs) > 0) {
$this->info('Some fans could not be assigned to a pwm. Trying remaining pwms...');
$assignedPwms = array_map(fn($fan) => $fan['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($pwm, $assignedPwms)) {
return $pwms;
}
$hwmon = $match[2];
if (!isset($pwms[$hwmon])) {
$pwms[$hwmon] = [];
}
$pwms[$hwmon][] = $pwm;
return $pwms;
}, []);
foreach ($unknownInputs as $id => $input) {
if (!isset($pwms[$input['hwmon']])) {
continue;
}
foreach ($pwms[$input['hwmon']] as $pwm) {
if ($input['pwm'] !== $pwm) {
continue;
}
[$min, $max] = $this->testPwm($pwm, $input['input']);
if ($max === 0 || $min > $max*0.4) {
continue;
}
$input['pwm'] = $pwm;
$input['options']['pwm'] = basename($pwm);
$fans[] = $input;
continue 2;
}
$this->info($id . ' not connected or not controllable');
}
}
return array_map(function ($fan) {
unset($fan['input'], $fan['pwm']);
return $fan;
}, $fans);
}
protected function testPwm($pwm, $input): array
{
$currentEnable = file_get_contents($pwm . '_enable');
file_put_contents($pwm . '_enable', '1');
file_put_contents($pwm, '255');
sleep(4);
$fullSpeed = (int)file_get_contents($input);
if ($fullSpeed === 0) {
file_put_contents($pwm . '_enable', $currentEnable);
return [0, 0];
}
file_put_contents($pwm, '0');
sleep(10);
$stopSpeed = (int)file_get_contents($input);
file_put_contents($pwm . '_enable', $currentEnable);
return [$stopSpeed, $fullSpeed];
}
}