15
15
use Flat3 \RevPi \Exceptions \RemoteDeviceException ;
16
16
use Flat3 \RevPi \Interfaces \Hardware \Device ;
17
17
use Flat3 \RevPi \Interfaces \Hardware \Ioctl ;
18
+ use Flat3 \RevPi \Interfaces \Hardware \PiControl ;
18
19
use Flat3 \RevPi \Interfaces \Hardware \Seek ;
19
20
use Flat3 \RevPi \Interfaces \Hardware \Stream ;
20
21
use Flat3 \RevPi \Interfaces \Hardware \Terminal ;
21
22
use Flat3 \RevPi \JsonRpc \Request as JsonRpcRequest ;
22
23
use Flat3 \RevPi \JsonRpc \Response as JsonRpcResponse ;
23
24
use Revolt \EventLoop ;
24
25
use Throwable ;
25
-
26
26
use function Amp \async ;
27
27
28
28
/**
34
34
* @phpstan-type JsonRpcResponseT array{id: string, error: ?array{ code: ?int, message: ?string }, result: JsonRpcResponseResultT }
35
35
* @phpstan-type JsonRpcEventT array{type: JsonRpcEventTypeT, payload: string}
36
36
*/
37
- class Peer implements WebsocketClientHandler
37
+ class JsonRpcPeer implements WebsocketClientHandler
38
38
{
39
39
protected WebsocketClient $ socket ;
40
40
@@ -50,15 +50,15 @@ class Peer implements WebsocketClientHandler
50
50
*/
51
51
protected array $ pending = [];
52
52
53
- public function setSocket (WebsocketClient $ socket ): self
53
+ public function withSocket (WebsocketClient $ socket ): self
54
54
{
55
55
$ this ->socket = $ socket ;
56
- async (fn () => $ this ->receiveLoop ());
56
+ async (fn () => $ this ->handleResponse ());
57
57
58
58
return $ this ;
59
59
}
60
60
61
- public function setDevice (Device $ device ): self
61
+ public function withDevice (Device $ device ): self
62
62
{
63
63
$ this ->device = $ device ;
64
64
@@ -89,7 +89,7 @@ public function request(string $method, array $params = []): Future
89
89
return $ deferred ->getFuture ();
90
90
}
91
91
92
- private function receiveLoop (): void
92
+ protected function handleResponse (): void
93
93
{
94
94
try {
95
95
while ($ message = $ this ->socket ->receive ()) {
@@ -102,13 +102,13 @@ private function receiveLoop(): void
102
102
continue ;
103
103
}
104
104
105
- if (! $ response instanceof JsonRpcResponse) {
105
+ if (!$ response instanceof JsonRpcResponse) {
106
106
continue ;
107
107
}
108
108
109
109
$ id = $ response ->id ;
110
110
111
- if (! isset ($ this ->pending [$ id ])) {
111
+ if (!isset ($ this ->pending [$ id ])) {
112
112
continue ;
113
113
}
114
114
@@ -263,7 +263,7 @@ public function handle(string $method, array $params): mixed
263
263
$ request ->payload = $ newData ;
264
264
265
265
$ this ->socket ->sendBinary (serialize ($ request ));
266
- } elseif (! is_resource ($ stream ) || @feof ($ stream )) {
266
+ } elseif (!is_resource ($ stream ) || @feof ($ stream )) {
267
267
EventLoop::cancel ($ callbackId );
268
268
}
269
269
});
@@ -274,11 +274,14 @@ public function handle(string $method, array $params): mixed
274
274
throw new NotImplementedException ; // @phpstan-ignore deadCode.unreachable
275
275
}
276
276
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
+
282
285
$ this ->socket = $ client ;
283
286
284
287
while ($ message = $ client ->receive ()) {
0 commit comments