Skip to content

Commit 4c54e12

Browse files
committed
Updates
1 parent d5bf3fe commit 4c54e12

File tree

14 files changed

+90
-54
lines changed

14 files changed

+90
-54
lines changed

src/Commands/Listen.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use Amp\Websocket\Server\Websocket;
1313
use Flat3\RevPi\Interfaces\Hardware\PiControl;
1414
use Flat3\RevPi\Interfaces\Hardware\Terminal;
15-
use Flat3\RevPi\JsonRpc\Peer;
15+
use Flat3\RevPi\JsonRpc\JsonRpcPeer;
1616
use Illuminate\Console\Command;
1717
use Psr\Log\LoggerInterface;
1818
use Revolt\EventLoop;
@@ -35,21 +35,16 @@ public function handle(LoggerInterface $logger): void
3535

3636
$router = new Router($server, $logger, new DefaultErrorHandler);
3737

38-
foreach ([
39-
'picontrol' => PiControl::class,
40-
'terminal' => Terminal::class,
41-
] as $route => $class) {
4238
$router->addRoute(
4339
method: 'GET',
44-
uri: '/'.$route,
40+
uri: '/',
4541
requestHandler: new Websocket(
4642
httpServer: $server,
4743
logger: $logger,
4844
acceptor: new Rfc6455Acceptor,
49-
clientHandler: app(Peer::class)->setDevice(app($class))
45+
clientHandler: app(JsonRpcPeer::class),
5046
)
5147
);
52-
}
5348

5449
$server->start(
5550
$router,

src/Hardware/Remote/RemoteCharacterDevice.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use Flat3\RevPi\Interfaces\Hardware\Stream;
88
use Flat3\RevPi\JsonRpc\Event;
9-
use Flat3\RevPi\JsonRpc\Peer;
9+
use Flat3\RevPi\JsonRpc\JsonRpcPeer;
1010
use Revolt\EventLoop;
1111

1212
abstract class RemoteCharacterDevice extends RemoteDevice implements Stream
@@ -17,7 +17,7 @@ abstract class RemoteCharacterDevice extends RemoteDevice implements Stream
1717
/** @var resource */
1818
protected mixed $remote;
1919

20-
public function __construct(Peer $peer)
20+
public function __construct(JsonRpcPeer $peer)
2121
{
2222
parent::__construct($peer);
2323

src/Hardware/Remote/RemoteDevice.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
use Amp\Websocket\WebsocketClient;
88
use Flat3\RevPi\Interfaces\Hardware\Device;
99
use Flat3\RevPi\Interfaces\Hardware\Ioctl;
10-
use Flat3\RevPi\JsonRpc\Peer;
10+
use Flat3\RevPi\JsonRpc\JsonRpcPeer;
1111

1212
abstract class RemoteDevice implements Device, Ioctl
1313
{
14-
public function __construct(protected Peer $peer) {}
14+
public function __construct(protected JsonRpcPeer $peer) {}
1515

1616
public function socket(WebsocketClient $websocket): void
1717
{
18-
$this->peer->setSocket($websocket);
18+
$this->peer->withSocket($websocket);
1919
}
2020

2121
public function open(string $pathname, int $flags): int

src/Interfaces/Module.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ public function getProcessImage(): ProcessImage;
1616
public function resume(): void;
1717

1818
public function monitor(Trigger $monitor): void;
19+
20+
public function getSerialPort(string $devicePath): SerialPort;
1921
}

src/Interfaces/Modules/Remote.php

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

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

1110
interface Remote extends Module
1211
{
13-
public function connect(WebsocketHandshake|PsrUri|string $handshake): void;
12+
public function connection(WebsocketHandshake $socket): void;
1413
}

src/JsonRpc/Event.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Flat3\RevPi\JsonRpc;
66

77
/**
8-
* @phpstan-import-type JsonRpcEventT from Peer
8+
* @phpstan-import-type JsonRpcEventT from JsonRpcPeer
99
*/
1010
class Event
1111
{

src/JsonRpc/Peer.php renamed to src/JsonRpc/JsonRpcPeer.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
use Flat3\RevPi\Exceptions\RemoteDeviceException;
1616
use Flat3\RevPi\Interfaces\Hardware\Device;
1717
use Flat3\RevPi\Interfaces\Hardware\Ioctl;
18+
use Flat3\RevPi\Interfaces\Hardware\PiControl;
1819
use Flat3\RevPi\Interfaces\Hardware\Seek;
1920
use Flat3\RevPi\Interfaces\Hardware\Stream;
2021
use Flat3\RevPi\Interfaces\Hardware\Terminal;
2122
use Flat3\RevPi\JsonRpc\Request as JsonRpcRequest;
2223
use Flat3\RevPi\JsonRpc\Response as JsonRpcResponse;
2324
use Revolt\EventLoop;
2425
use Throwable;
25-
2626
use function Amp\async;
2727

2828
/**
@@ -34,7 +34,7 @@
3434
* @phpstan-type JsonRpcResponseT array{id: string, error: ?array{ code: ?int, message: ?string }, result: JsonRpcResponseResultT }
3535
* @phpstan-type JsonRpcEventT array{type: JsonRpcEventTypeT, payload: string}
3636
*/
37-
class Peer implements WebsocketClientHandler
37+
class JsonRpcPeer implements WebsocketClientHandler
3838
{
3939
protected WebsocketClient $socket;
4040

@@ -50,15 +50,15 @@ class Peer implements WebsocketClientHandler
5050
*/
5151
protected array $pending = [];
5252

53-
public function setSocket(WebsocketClient $socket): self
53+
public function withSocket(WebsocketClient $socket): self
5454
{
5555
$this->socket = $socket;
56-
async(fn () => $this->receiveLoop());
56+
async(fn() => $this->handleResponse());
5757

5858
return $this;
5959
}
6060

61-
public function setDevice(Device $device): self
61+
public function withDevice(Device $device): self
6262
{
6363
$this->device = $device;
6464

@@ -89,7 +89,7 @@ public function request(string $method, array $params = []): Future
8989
return $deferred->getFuture();
9090
}
9191

92-
private function receiveLoop(): void
92+
protected function handleResponse(): void
9393
{
9494
try {
9595
while ($message = $this->socket->receive()) {
@@ -102,13 +102,13 @@ private function receiveLoop(): void
102102
continue;
103103
}
104104

105-
if (! $response instanceof JsonRpcResponse) {
105+
if (!$response instanceof JsonRpcResponse) {
106106
continue;
107107
}
108108

109109
$id = $response->id;
110110

111-
if (! isset($this->pending[$id])) {
111+
if (!isset($this->pending[$id])) {
112112
continue;
113113
}
114114

@@ -263,7 +263,7 @@ public function handle(string $method, array $params): mixed
263263
$request->payload = $newData;
264264

265265
$this->socket->sendBinary(serialize($request));
266-
} elseif (! is_resource($stream) || @feof($stream)) {
266+
} elseif (!is_resource($stream) || @feof($stream)) {
267267
EventLoop::cancel($callbackId);
268268
}
269269
});
@@ -274,11 +274,14 @@ public function handle(string $method, array $params): mixed
274274
throw new NotImplementedException; // @phpstan-ignore deadCode.unreachable
275275
}
276276

277-
public function handleClient(
278-
WebsocketClient $client,
279-
Request $request,
280-
Response $response
281-
): void {
277+
public function handleClient(WebsocketClient $client, Request $request, Response $response): void
278+
{
279+
$this->device = match ($request->getQueryParameter('device')) {
280+
'picontrol' => app(PiControl::class),
281+
'terminal' => app(Terminal::class),
282+
default => throw new NotImplementedException,
283+
};
284+
282285
$this->socket = $client;
283286

284287
while ($message = $client->receive()) {

src/JsonRpc/Request.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
use Illuminate\Support\Str;
88

99
/**
10-
* @phpstan-import-type JsonRpcMethodT from Peer
11-
* @phpstan-import-type JsonRpcRequestT from Peer
12-
* @phpstan-import-type JsonRpcRequestParamsT from Peer
10+
* @phpstan-import-type JsonRpcMethodT from JsonRpcPeer
11+
* @phpstan-import-type JsonRpcRequestT from JsonRpcPeer
12+
* @phpstan-import-type JsonRpcRequestParamsT from JsonRpcPeer
1313
*/
1414
class Request
1515
{

src/JsonRpc/Response.php

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

77
/**
8-
* @phpstan-import-type JsonRpcResponseT from Peer
9-
* @phpstan-import-type JsonRpcResponseResultT from Peer
8+
* @phpstan-import-type JsonRpcResponseT from JsonRpcPeer
9+
* @phpstan-import-type JsonRpcResponseResultT from JsonRpcPeer
1010
*/
1111
class Response
1212
{

src/Modules/Connect5.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Flat3\RevPi\Modules;
66

77
use Flat3\RevPi\Interfaces\Modules\Connect5 as Connect5ModuleInterface;
8+
use Flat3\RevPi\Interfaces\SerialPort;
89
use Flat3\RevPi\Led\Connect5Led;
910
use Flat3\RevPi\Led\Led;
1011
use Flat3\RevPi\Led\LedPosition;

0 commit comments

Comments
 (0)