Skip to content

Commit 87b5701

Browse files
committed
Updates
1 parent 275662e commit 87b5701

18 files changed

+645
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ use Flat3\RevPi\Interfaces\Monitor;
317317
class MyMonitor implements Monitor {
318318
public function evaluate(int|bool|null $next): bool {
319319
// Implement custom transition/action logic here
320-
// e.g. if crossing a threshold, fire webhook
320+
// e.g. if crossing a threshold
321321
// Return true if the monitor has detected sufficient change
322322
}
323323
}

src/Interfaces/Monitor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66

77
interface Monitor
88
{
9-
public function evaluate(int|bool|null $next): bool;
9+
public function evaluate(int|bool $next): bool;
1010
}

src/Monitors/DeadbandMonitor.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Flat3\RevPi\Monitors;
6+
7+
use Flat3\RevPi\Exceptions\NotSupportedException;
8+
use Flat3\RevPi\Interfaces\Monitor;
9+
10+
class DeadbandMonitor implements Monitor
11+
{
12+
protected bool $wasOutside = false;
13+
14+
public function __construct(protected float $center, protected float $deadband) {}
15+
16+
public function evaluate(int|bool $next): bool
17+
{
18+
if (is_bool($next)) {
19+
throw new NotSupportedException;
20+
}
21+
22+
$outside = abs($next - $this->center) > $this->deadband;
23+
$trigger = ($outside !== $this->wasOutside) && $outside;
24+
$this->wasOutside = $outside;
25+
26+
return $trigger;
27+
}
28+
}

src/Monitors/DigitalMonitor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class DigitalMonitor implements Monitor
1010
{
1111
protected int|bool|null $previous = null;
1212

13-
public function evaluate(int|bool|null $next): bool
13+
public function evaluate(int|bool $next): bool
1414
{
1515
$previous = $this->previous;
1616
$this->previous = $next;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Flat3\RevPi\Monitors;
6+
7+
use Flat3\RevPi\Interfaces\Monitor;
8+
9+
class MovingAverageMonitor implements Monitor
10+
{
11+
/**
12+
* @var array<int|bool>
13+
*/
14+
protected array $window = [];
15+
16+
public function __construct(protected int $windowSize, protected float $threshold) {}
17+
18+
public function evaluate(int|bool $next): bool
19+
{
20+
$this->window[] = $next;
21+
22+
if (count($this->window) > $this->windowSize) {
23+
array_shift($this->window);
24+
}
25+
26+
if (count($this->window) === 0) {
27+
return false;
28+
}
29+
30+
$average = array_sum($this->window) / count($this->window);
31+
32+
return $average > $this->threshold;
33+
}
34+
}

src/Monitors/RangeMonitor.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Flat3\RevPi\Monitors;
4+
5+
use Flat3\RevPi\Interfaces\Monitor;
6+
7+
class RangeMonitor implements Monitor
8+
{
9+
public function __construct(protected int|float $min, protected int|float $max) {}
10+
11+
public function evaluate(int|bool $next): bool
12+
{
13+
if ($next < $this->min || $next > $this->max) {
14+
return true;
15+
}
16+
17+
return false;
18+
}
19+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Flat3\RevPi\Monitors;
6+
7+
use Flat3\RevPi\Exceptions\NotSupportedException;
8+
use Flat3\RevPi\Interfaces\Monitor;
9+
10+
class RateOfChangeMonitor implements Monitor
11+
{
12+
protected ?int $previous = null;
13+
14+
public function __construct(protected float $maxRate)
15+
{
16+
if ($maxRate <= 0) {
17+
throw new NotSupportedException('maxRate must be positive');
18+
}
19+
}
20+
21+
public function evaluate(int|bool $next): bool
22+
{
23+
if (is_bool($next)) {
24+
throw new NotSupportedException;
25+
}
26+
27+
$result = false;
28+
29+
if ($this->previous !== null) {
30+
$rate = abs($next - $this->previous);
31+
$result = $rate >= $this->maxRate;
32+
}
33+
34+
$this->previous = $next;
35+
36+
return $result;
37+
}
38+
}

src/Monitors/StuckMonitor.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Flat3\RevPi\Monitors;
6+
7+
use Flat3\RevPi\Interfaces\Monitor;
8+
9+
class StuckMonitor implements Monitor
10+
{
11+
protected int|bool|null $previous = null;
12+
13+
protected int $sameCount = 0;
14+
15+
public function __construct(protected int $repeatCount) {}
16+
17+
public function evaluate(int|bool $next): bool
18+
{
19+
if ($next === $this->previous) {
20+
$this->sameCount++;
21+
} else {
22+
$this->sameCount = 1;
23+
}
24+
25+
$this->previous = $next;
26+
27+
return $this->sameCount >= $this->repeatCount;
28+
}
29+
}

src/Monitors/TrendMonitor.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Flat3\RevPi\Monitors;
6+
7+
use Flat3\RevPi\Interfaces\Monitor;
8+
9+
class TrendMonitor implements Monitor
10+
{
11+
/**
12+
* @var array<int|bool>
13+
*/
14+
protected array $window = [];
15+
16+
public function __construct(protected int $size) {}
17+
18+
public function evaluate(int|bool $next): bool
19+
{
20+
$this->window[] = $next;
21+
22+
if (count($this->window) > $this->size) {
23+
array_shift($this->window);
24+
}
25+
26+
if (count($this->window) < $this->size) {
27+
return false;
28+
}
29+
30+
for ($i = 1; $i < $this->size; $i++) {
31+
if ($this->window[$i] <= $this->window[$i - 1]) {
32+
return false;
33+
}
34+
}
35+
36+
return true;
37+
}
38+
}

src/Rpc/RpcDevice.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ protected function invoke(string $method, array $params): mixed
142142
while (true) {
143143
$data = @fread($stream, Constants::BlockSize);
144144

145-
if (!is_string($data) || $data === '') {
145+
if (! is_string($data) || $data === '') {
146146
break;
147147
}
148148

@@ -162,7 +162,7 @@ protected function invoke(string $method, array $params): mixed
162162
}
163163
}
164164

165-
if (!is_resource($stream) || @feof($stream)) {
165+
if (! is_resource($stream) || @feof($stream)) {
166166
EventLoop::cancel($callbackId);
167167
}
168168
});

0 commit comments

Comments
 (0)