Skip to content

Commit a5d896a

Browse files
committed
Refactored gRPC Monitor API
1 parent a47e35f commit a5d896a

File tree

6 files changed

+298
-159
lines changed

6 files changed

+298
-159
lines changed

commands/daemon/daemon.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ func (s *ArduinoCoreServerImpl) Monitor(stream rpc.ArduinoCoreService_MonitorSer
494494
syncSend.Send(&rpc.MonitorResponse{Error: err.Error()})
495495
return
496496
}
497-
if conf := msg.GetPortConfiguration(); conf != nil {
497+
if conf := msg.GetUpdatedConfiguration(); conf != nil {
498498
for _, c := range conf.GetSettings() {
499499
if err := portProxy.Config(c.SettingId, c.Value); err != nil {
500500
syncSend.Send(&rpc.MonitorResponse{Error: err.Error()})

commands/daemon/term_example/main.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ func connectToPort(cli commands.ArduinoCoreServiceClient, instance *commands.Ins
9090
log.Fatal("Error opening Monitor:", err)
9191
}
9292
if err := monitorClient.Send(&commands.MonitorRequest{
93-
Instance: instance,
94-
Port: port,
93+
Message: &commands.MonitorRequest_OpenRequest{OpenRequest: &commands.MonitorPortOpenRequest{
94+
Instance: instance,
95+
Port: port,
96+
}},
9597
}); err != nil {
9698
log.Fatal("Error sending Monitor config:", err)
9799
}
@@ -107,9 +109,9 @@ func connectToPort(cli commands.ArduinoCoreServiceClient, instance *commands.Ins
107109
}
108110
}()
109111

110-
hello := &commands.MonitorRequest{
112+
hello := &commands.MonitorRequest{Message: &commands.MonitorRequest_TxData{
111113
TxData: []byte("HELLO!"),
112-
}
114+
}}
113115
fmt.Println("Send:", hello)
114116
if err := monitorClient.Send(hello); err != nil {
115117
log.Fatal("Monitor send HELLO:", err)

commands/monitor/monitor.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,17 @@ func (p *PortProxy) Close() error {
6161
// Monitor opens a communication port. It returns a PortProxy to communicate with the port and a PortDescriptor
6262
// that describes the available configuration settings.
6363
func Monitor(ctx context.Context, req *rpc.MonitorRequest) (*PortProxy, *pluggableMonitor.PortDescriptor, error) {
64-
pme, release := commands.GetPackageManagerExplorer(req)
64+
openReq := req.GetOpenRequest()
65+
if openReq == nil {
66+
return nil, nil, &arduino.InvalidInstanceError{}
67+
}
68+
pme, release := commands.GetPackageManagerExplorer(openReq)
6569
if pme == nil {
6670
return nil, nil, &arduino.InvalidInstanceError{}
6771
}
6872
defer release()
6973

70-
m, boardSettings, err := findMonitorAndSettingsForProtocolAndBoard(pme, req.GetPort().GetProtocol(), req.GetFqbn())
74+
m, boardSettings, err := findMonitorAndSettingsForProtocolAndBoard(pme, openReq.GetPort().GetProtocol(), openReq.GetFqbn())
7175
if err != nil {
7276
return nil, nil, err
7377
}
@@ -83,7 +87,7 @@ func Monitor(ctx context.Context, req *rpc.MonitorRequest) (*PortProxy, *pluggab
8387
}
8488

8589
// Apply user-requested settings
86-
if portConfig := req.GetPortConfiguration(); portConfig != nil {
90+
if portConfig := openReq.GetPortConfiguration(); portConfig != nil {
8791
for _, setting := range portConfig.Settings {
8892
boardSettings.Remove(setting.SettingId) // Remove board settings overridden by the user
8993
if err := m.Configure(setting.SettingId, setting.Value); err != nil {
@@ -96,13 +100,13 @@ func Monitor(ctx context.Context, req *rpc.MonitorRequest) (*PortProxy, *pluggab
96100
m.Configure(setting, value)
97101
}
98102

99-
monIO, err := m.Open(req.GetPort().GetAddress(), req.GetPort().GetProtocol())
103+
monIO, err := m.Open(openReq.GetPort().GetAddress(), openReq.GetPort().GetProtocol())
100104
if err != nil {
101105
m.Quit()
102106
return nil, nil, &arduino.FailedMonitorError{Cause: err}
103107
}
104108

105-
logrus.Infof("Port %s successfully opened", req.GetPort().GetAddress())
109+
logrus.Infof("Port %s successfully opened", openReq.GetPort().GetAddress())
106110
return &PortProxy{
107111
rw: monIO,
108112
changeSettingsCB: m.Configure,

internal/cli/monitor/monitor.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,13 @@ func runMonitorCmd(cmd *cobra.Command, args []string) {
140140
}
141141
}
142142
}
143-
portProxy, _, err := monitor.Monitor(context.Background(), &rpc.MonitorRequest{
144-
Instance: instance,
145-
Port: &rpc.Port{Address: portAddress, Protocol: portProtocol},
146-
Fqbn: fqbn.String(),
147-
PortConfiguration: configuration,
148-
})
143+
portProxy, _, err := monitor.Monitor(context.Background(), &rpc.MonitorRequest{Message: &rpc.MonitorRequest_OpenRequest{
144+
OpenRequest: &rpc.MonitorPortOpenRequest{
145+
Instance: instance,
146+
Port: &rpc.Port{Address: portAddress, Protocol: portProtocol},
147+
Fqbn: fqbn.String(),
148+
PortConfiguration: configuration,
149+
}}})
149150
if err != nil {
150151
feedback.FatalError(err, feedback.ErrGeneric)
151152
}

0 commit comments

Comments
 (0)