@@ -17,20 +17,17 @@ package main
17
17
18
18
import (
19
19
"context"
20
+ "errors"
20
21
"fmt"
22
+ "io"
21
23
"log"
22
24
"time"
23
25
24
- monitor "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/monitor /v1"
26
+ "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands /v1"
25
27
"google.golang.org/grpc"
26
- "google.golang.org/protobuf/types/known/structpb"
27
28
)
28
29
29
- var (
30
- dataDir string
31
- )
32
-
33
- // This program exercise monitor rate limiting functionality.
30
+ // This program exercise CLI monitor functionality.
34
31
35
32
func main () {
36
33
conn , err := grpc .Dial ("localhost:50051" , grpc .WithInsecure (), grpc .WithBlock ())
@@ -39,58 +36,90 @@ func main() {
39
36
}
40
37
defer conn .Close ()
41
38
42
- // Open a monitor instance
43
- mon := monitor .NewMonitorServiceClient (conn )
44
- stream , err := mon .StreamingOpen (context .Background ())
45
- if err != nil {
46
- log .Fatal ("Error opening stream:" , err )
47
- }
39
+ // Create and initialize a CLI instance
40
+ cli := commands .NewArduinoCoreServiceClient (conn )
48
41
49
- additionalConf , err := structpb . NewStruct (
50
- map [ string ] interface {}{ "OutputRate" : float64 ( 1000000 )},
51
- )
52
- if err != nil {
53
- log . Fatal ( "Error creating config:" , err )
42
+ var instance * commands. Instance
43
+ if resp , err := cli . Create ( context . Background (), & commands. CreateRequest {}); err != nil {
44
+ log . Fatal ( "Create:" , err )
45
+ } else {
46
+ instance = resp . Instance
54
47
}
55
48
56
- if err := stream .Send (& monitor.StreamingOpenRequest {
57
- Content : & monitor.StreamingOpenRequest_Config {
58
- Config : & monitor.MonitorConfig {
59
- Type : monitor .MonitorConfig_TARGET_TYPE_NULL ,
60
- AdditionalConfig : additionalConf ,
61
- RecvRateLimitBuffer : 1024 ,
62
- },
63
- },
64
- }); err != nil {
65
- log .Fatal ("Error opening stream:" , err )
49
+ if respStream , err := cli .Init (context .Background (), & commands.InitRequest {Instance : instance }); err != nil {
50
+ log .Fatal ("Init:" , err )
51
+ } else {
52
+ for {
53
+ resp , err := respStream .Recv ()
54
+ if errors .Is (err , io .EOF ) {
55
+ break
56
+ }
57
+ if err != nil {
58
+ log .Fatal ("Init:" , err )
59
+ }
60
+ fmt .Println (resp )
61
+ }
66
62
}
67
63
68
- if err := stream .Send (& monitor.StreamingOpenRequest {
69
- Content : & monitor.StreamingOpenRequest_RecvAcknowledge {
70
- RecvAcknowledge : 5 ,
71
- },
72
- }); err != nil {
73
- log .Fatal ("Error replenishing recv window:" , err )
64
+ // List boards and take the first available port
65
+ var port * commands.Port
66
+ if resp , err := cli .BoardList (context .Background (), & commands.BoardListRequest {Instance : instance }); err != nil {
67
+ log .Fatal ("BoardList:" , err )
68
+ } else {
69
+ ports := resp .GetPorts ()
70
+ if len (ports ) == 0 {
71
+ log .Fatal ("No port to connect!" )
72
+ }
73
+ port = ports [0 ].Port
74
74
}
75
+ fmt .Println ("Detected port:" , port .Label , port .ProtocolLabel )
75
76
76
- for {
77
- r , err := stream .Recv ()
78
- if err != nil {
79
- log .Fatal ("Error receiving from server:" , err )
77
+ // Connect to the port monitor
78
+ fmt .Println ("Connecting to monitor" )
79
+ ctx , cancel := context .WithCancel (context .Background ())
80
+ if respStream , err := cli .Monitor (ctx ); err != nil {
81
+ log .Fatal ("Monitor:" , err )
82
+ } else {
83
+ if err := respStream .Send (& commands.MonitorRequest {
84
+ Instance : instance ,
85
+ Port : port ,
86
+ }); err != nil {
87
+ log .Fatal ("Monitor send-config:" , err )
80
88
}
81
- if l := len (r .Data ); l > 0 {
82
- fmt .Printf ("RECV %d bytes\n " , l )
89
+ time .Sleep (1 * time .Second )
90
+
91
+ go func () {
92
+ for {
93
+ if resp , err := respStream .Recv (); err != nil {
94
+ fmt .Println (" RECV:" , err )
95
+ break
96
+ } else {
97
+ fmt .Println (" RECV:" , resp )
98
+ }
99
+ }
100
+ }()
101
+
102
+ hello := & commands.MonitorRequest {
103
+ TxData : []byte ("HELLO!" ),
83
104
}
84
- if r .Dropped > 0 {
85
- fmt .Printf ("DROPPED %d bytes!!!\n " , r .Dropped )
105
+ fmt .Println ("Send:" , hello )
106
+ if err := respStream .Send (hello ); err != nil {
107
+ log .Fatal ("Monitor send HELLO:" , err )
86
108
}
87
- if err := stream .Send (& monitor.StreamingOpenRequest {
88
- Content : & monitor.StreamingOpenRequest_RecvAcknowledge {
89
- RecvAcknowledge : 1 ,
90
- },
91
- }); err != nil {
92
- log .Fatal ("Error replenishing recv window:" , err )
109
+
110
+ fmt .Println ("Send:" , hello )
111
+ if err := respStream .Send (hello ); err != nil {
112
+ log .Fatal ("Monitor send HELLO:" , err )
113
+ }
114
+
115
+ time .Sleep (5 * time .Second )
116
+
117
+ fmt .Println ("Closing Monitor" )
118
+ if err := respStream .CloseSend (); err != nil {
119
+ log .Fatal ("Monitor close send:" , err )
93
120
}
94
- time .Sleep (5 * time .Millisecond )
121
+ time .Sleep (5 * time .Second )
95
122
}
123
+ cancel ()
124
+ time .Sleep (5 * time .Second )
96
125
}
0 commit comments