1
+ import { Struct , type JsonValue } from '@bufbuild/protobuf' ;
2
+ import type { CallOptions , PromiseClient } from '@connectrpc/connect' ;
3
+ import { SwitchService } from '../../gen/component/switch/v1/switch_connect' ;
4
+ import {
5
+ SetPositionRequest ,
6
+ GetPositionRequest ,
7
+ GetNumberOfPositionsRequest ,
8
+ } from '../../gen/component/switch/v1/switch_pb' ;
9
+ import type { RobotClient } from '../../robot' ;
10
+ import type { Options } from '../../types' ;
11
+ import { doCommandFromClient } from '../../utils' ;
12
+ import type { Switch } from './switch' ;
13
+
14
+ /**
15
+ * A gRPC-web client for the Switch component.
16
+ *
17
+ * @group Clients
18
+ */
19
+ export class SwitchClient implements Switch {
20
+ private client : PromiseClient < typeof SwitchService > ;
21
+ private readonly name : string ;
22
+ private readonly options : Options ;
23
+ public callOptions : CallOptions = { headers : { } as Record < string , string > } ;
24
+
25
+ constructor ( client : RobotClient , name : string , options : Options = { } ) {
26
+ this . client = client . createServiceClient ( SwitchService ) ;
27
+ this . name = name ;
28
+ this . options = options ;
29
+ }
30
+
31
+ async setPosition ( position : number , extra = { } , callOptions = this . callOptions ) {
32
+ const request = new SetPositionRequest ( {
33
+ name : this . name ,
34
+ position,
35
+ extra : Struct . fromJson ( extra ) ,
36
+ } ) ;
37
+
38
+ this . options . requestLogger ?.( request ) ;
39
+
40
+ await this . client . setPosition ( request , callOptions ) ;
41
+ }
42
+
43
+ async getPosition ( extra = { } , callOptions = this . callOptions ) {
44
+ const request = new GetPositionRequest ( {
45
+ name : this . name ,
46
+ extra : Struct . fromJson ( extra ) ,
47
+ } ) ;
48
+
49
+ this . options . requestLogger ?.( request ) ;
50
+
51
+ const resp = await this . client . getPosition ( request , callOptions ) ;
52
+ return resp . position ;
53
+ }
54
+
55
+ async getNumberOfPositions ( extra = { } , callOptions = this . callOptions ) {
56
+ const request = new GetNumberOfPositionsRequest ( {
57
+ name : this . name ,
58
+ extra : Struct . fromJson ( extra ) ,
59
+ } ) ;
60
+
61
+ this . options . requestLogger ?.( request ) ;
62
+
63
+ const resp = await this . client . getNumberOfPositions ( request , callOptions ) ;
64
+ return resp . numberOfPositions ;
65
+ }
66
+
67
+ async doCommand (
68
+ command : Struct ,
69
+ callOptions = this . callOptions
70
+ ) : Promise < JsonValue > {
71
+ return doCommandFromClient (
72
+ this . client . doCommand ,
73
+ this . name ,
74
+ command ,
75
+ this . options ,
76
+ callOptions
77
+ ) ;
78
+ }
79
+ }
0 commit comments