Skip to content

Commit ec758b2

Browse files
committed
Updates
1 parent 7b0b8ef commit ec758b2

File tree

11 files changed

+43
-40
lines changed

11 files changed

+43
-40
lines changed

src/IO/IO.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Flat3\RevPi\IO;
66

77
use Flat3\RevPi\Interfaces\Module;
8-
use Flat3\RevPi\Monitors\DigitalMonitor;
8+
use Flat3\RevPi\Interfaces\Monitor;
99

1010
abstract class IO
1111
{
@@ -20,9 +20,9 @@ public function get(): int|bool
2020
return $this->module->getProcessImage()->readVariable($this->name);
2121
}
2222

23-
public function monitor(callable $callback): void
23+
public function monitor(Monitor $monitor, callable $callback): void
2424
{
25-
$this->module->monitor(new DigitalMonitor($this->name, $callback));
25+
$this->module->monitor($this->name, $monitor, $callback);
2626
}
2727

2828
public function default(): int|bool

src/Interfaces/Module.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Flat3\RevPi\Interfaces;
66

77
use Flat3\RevPi\Led\LedPosition;
8-
use Flat3\RevPi\Monitors\Monitor;
98

109
/**
1110
* Interface Module
@@ -38,9 +37,11 @@ public function resume(): void;
3837
/**
3938
* Attach a monitor to the module for status tracking or event watching.
4039
*
40+
* @param string $variable The variable to monitor.
4141
* @param Monitor $monitor The monitor instance to attach.
42+
* @param callable $callback The callback to call.
4243
*/
43-
public function monitor(Monitor $monitor): void;
44+
public function monitor(string $variable, Monitor $monitor, callable $callback): void;
4445

4546
/**
4647
* Get a serial port instance for communication on the specified device path.

src/Interfaces/Modules/Remote.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
use Amp\Websocket\Client\WebsocketHandshake;
88
use Flat3\RevPi\Interfaces\Module;
9+
use Psr\Http\Message\UriInterface as PsrUri;
910

1011
interface Remote extends Module
1112
{
12-
public function handshake(WebsocketHandshake $handshake): void;
13+
public function handshake(WebsocketHandshake|PsrUri|string $handshake): void;
1314
}

src/Interfaces/Monitor.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Flat3\RevPi\Interfaces;
6+
7+
interface Monitor
8+
{
9+
public function evaluate(mixed $previous, mixed $next): bool;
10+
}

src/JsonRpc/Event.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Event
2020
public function __serialize(): array
2121
{
2222
return [
23+
'jsonrpc' => '2.0',
2324
'type' => $this->type,
2425
'payload' => $this->payload,
2526
];

src/Modules/Module.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
use Flat3\RevPi\Constants;
88
use Flat3\RevPi\Events\PollingEvent;
99
use Flat3\RevPi\Interfaces\Module as ModuleInterface;
10+
use Flat3\RevPi\Interfaces\Monitor;
1011
use Flat3\RevPi\Interfaces\ProcessImage;
1112
use Flat3\RevPi\Interfaces\SerialPort;
12-
use Flat3\RevPi\Monitors\Monitor;
1313
use Illuminate\Support\Facades\Event;
1414
use Revolt\EventLoop;
1515

1616
abstract class Module implements ModuleInterface
1717
{
1818
protected ?string $pollingCallbackId = null;
1919

20-
protected float $frequency = Constants::f1Hz;
20+
protected float $frequency = Constants::f25Hz;
2121

2222
protected ?ProcessImage $processImage = null;
2323

@@ -60,15 +60,17 @@ public function cancel(): void
6060
$this->pollingCallbackId = null;
6161
}
6262

63-
public function monitor(Monitor $monitor): void
63+
public function monitor(string $variable, Monitor $monitor, callable $callback): void
6464
{
65-
Event::listen(PollingEvent::class, function () use ($monitor) {
65+
Event::listen(PollingEvent::class, function () use ($callback, $variable, $monitor) {
6666
static $previous = null;
6767

68-
$next = $this->getProcessImage()->readVariable($monitor->name);
68+
$next = $this->getProcessImage()->readVariable($variable);
6969

7070
if ($previous !== null && $previous !== $next) {
71-
EventLoop::defer(fn () => $monitor->evaluate($previous, $next));
71+
if ($monitor->evaluate($previous, $next)) {
72+
$callback($next);
73+
}
7274
}
7375

7476
$previous = $next;

src/Modules/Remote.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Flat3\RevPi\Interfaces\SerialPort;
1414
use Flat3\RevPi\Led\LedPosition;
1515
use Flat3\RevPi\Led\RemoteLed;
16+
use Psr\Http\Message\UriInterface as PsrUri;
1617

1718
use function Amp\Websocket\Client\connect;
1819

@@ -47,8 +48,12 @@ public function getSerialPort(string $devicePath): SerialPort
4748
return app(SerialPort::class, ['devicePath' => $devicePath, 'device' => $terminal]);
4849
}
4950

50-
public function handshake(WebsocketHandshake $handshake): void
51+
public function handshake(WebsocketHandshake|PsrUri|string $handshake): void
5152
{
53+
if (! $handshake instanceof WebsocketHandshake) {
54+
$handshake = new WebsocketHandshake($handshake);
55+
}
56+
5257
$this->handshake = $handshake;
5358
}
5459
}

src/Monitors/DigitalMonitor.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
namespace Flat3\RevPi\Monitors;
66

7-
class DigitalMonitor extends Monitor
7+
use Flat3\RevPi\Interfaces\Monitor;
8+
9+
class DigitalMonitor implements Monitor
810
{
9-
public function evaluate(mixed $previous, mixed $next): void
11+
public function evaluate(mixed $previous, mixed $next): bool
1012
{
11-
call_user_func($this->callback, $next);
13+
return $previous !== $next;
1214
}
1315
}

src/Monitors/Monitor.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/RevolutionPi.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
trait RevolutionPi
2424
{
25-
protected ?WebsocketHandshake $handshake = null;
25+
protected WebsocketHandshake|PsrUri|string|null $handshake = null;
2626

2727
protected ?Module $module = null;
2828

@@ -48,10 +48,6 @@ public function module(): Module
4848
*/
4949
public function remote(WebsocketHandshake|PsrUri|string $handshake): static
5050
{
51-
if (! $handshake instanceof WebsocketHandshake) {
52-
$handshake = new WebsocketHandshake($handshake);
53-
}
54-
5551
$this->handshake = $handshake;
5652

5753
return $this;

0 commit comments

Comments
 (0)