Skip to content
Snippets Groups Projects
Utils.php 1.38 KiB
Newer Older
<?php

namespace App\Service;

use App\Application;
use App\Model\Fan\HwmonFan;
use Hugga\Console;

class Utils
{
    public static function waitForStableRpm(HwmonFan $fan): int
    {
        static $waitTime = 600_000; // in micro seconds
        static $count = 4;
        $readings = [];

        // get the first readings
        for ($i = 0; $i < $count; $i++) {
            $readings[] = $fan->getCurrentSpeed();
            usleep($waitTime);
        }
        $min = min($readings);
        $max = max($readings);
        $average = array_sum($readings) / $count;
        $variance = $average > 0 ? ($max - $min) / $average * 100 : 0;

        // wait till variance is less than 2
        while (array_sum($readings) > 0 && // fan has not stopped
            $variance > 2
        ) {
            Application::console()->line(sprintf(
                'min: %d rpm, max: %d rpm, avg: %d rpm, variance: %f%%',
                $min,
                $max,
                $average,
                $variance
            ), Console::WEIGHT_DEBUG);
            array_shift($readings);
            $readings[] = $fan->getCurrentSpeed();
            $min = min($readings);
            $max = max($readings);
            $average = array_sum($readings) / $count;
            $variance = $average > 0 ? ($max - $min) / $average * 100 : 0;
            usleep($waitTime);
        }
        return $average;
    }
}