Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6299f16

Browse files
committedJul 6, 2021
Implemented board watch with discoveries
1 parent 9bf1f46 commit 6299f16

File tree

3 files changed

+49
-48
lines changed

3 files changed

+49
-48
lines changed
 

‎arduino/discovery/discoverymanager/discoverymanager.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ import (
2323
// DiscoveryManager is required to handle multiple pluggable-discovery that
2424
// may be shared across platforms
2525
type DiscoveryManager struct {
26-
discoveries map[string]*discovery.PluggableDiscovery
26+
discoveries map[string]*discovery.PluggableDiscovery
27+
globalEventCh chan *discovery.Event
2728
}
2829

2930
// New creates a new DiscoveriesManager
3031
func New() *DiscoveryManager {
3132
return &DiscoveryManager{
32-
discoveries: map[string]*discovery.PluggableDiscovery{},
33+
discoveries: map[string]*discovery.PluggableDiscovery{},
34+
globalEventCh: make(chan *discovery.Event, 5),
3335
}
3436
}
3537

@@ -65,6 +67,24 @@ func (dm *DiscoveryManager) StartAll() error {
6567
return nil
6668
}
6769

70+
// StartSyncAll the discoveries for this DiscoveryManager,
71+
// returns the first error it meets or nil
72+
func (dm *DiscoveryManager) StartSyncAll() (<-chan *discovery.Event, []error) {
73+
errs := []error{}
74+
for _, d := range dm.discoveries {
75+
eventCh := d.EventChannel(5)
76+
if err := d.StartSync(); err != nil {
77+
errs = append(errs, err)
78+
}
79+
go func() {
80+
for ev := range eventCh {
81+
dm.globalEventCh <- ev
82+
}
83+
}()
84+
}
85+
return dm.globalEventCh, errs
86+
}
87+
6888
// StopAll the discoveries for this DiscoveryManager,
6989
// returns the first error it meets or nil
7090
func (dm *DiscoveryManager) StopAll() error {

‎commands/board/list.go

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -227,40 +227,49 @@ func List(instanceID int32) (r []*rpc.DetectedPort, e error) {
227227
// The discovery process can be interrupted by sending a message to the interrupt channel.
228228
func Watch(instanceID int32, interrupt <-chan bool) (<-chan *rpc.BoardListWatchResponse, error) {
229229
pm := commands.GetPackageManager(instanceID)
230-
eventsChan, err := commands.WatchListBoards(pm)
231-
if err != nil {
230+
231+
if err := pm.DiscoveryManager().RunAll(); err != nil {
232232
return nil, err
233233
}
234234

235+
eventsChan, errs := pm.DiscoveryManager().StartSyncAll()
236+
235237
outChan := make(chan *rpc.BoardListWatchResponse)
238+
236239
go func() {
240+
for _, err := range errs {
241+
outChan <- &rpc.BoardListWatchResponse{
242+
EventType: "error",
243+
Error: err.Error(),
244+
}
245+
}
237246
for {
238247
select {
239248
case event := <-eventsChan:
240-
boards := []*rpc.BoardListItem{}
249+
serialNumber := ""
250+
if props := event.Port.Properties; props != nil {
251+
serialNumber = props.Get("serialNumber")
252+
}
253+
254+
port := &rpc.DetectedPort{
255+
Address: event.Port.Address,
256+
Protocol: event.Port.Protocol,
257+
ProtocolLabel: event.Port.ProtocolLabel,
258+
SerialNumber: serialNumber,
259+
}
260+
241261
boardsError := ""
242262
if event.Type == "add" {
243-
boards, err = identify(pm, event.Port)
263+
boards, err := identify(pm, event.Port)
244264
if err != nil {
245265
boardsError = err.Error()
246266
}
267+
port.Boards = boards
247268
}
248-
249-
serialNumber := ""
250-
if props := event.Port.Properties; props != nil {
251-
serialNumber = props.Get("serialNumber")
252-
}
253-
254269
outChan <- &rpc.BoardListWatchResponse{
255270
EventType: event.Type,
256-
Port: &rpc.DetectedPort{
257-
Address: event.Port.Address,
258-
Protocol: event.Port.Protocol,
259-
ProtocolLabel: event.Port.ProtocolLabel,
260-
Boards: boards,
261-
SerialNumber: serialNumber,
262-
},
263-
Error: boardsError,
271+
Port: port,
272+
Error: boardsError,
264273
}
265274
case <-interrupt:
266275
return

‎commands/bundled_tools_serial_discovery.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020

2121
"github.com/arduino/arduino-cli/arduino/cores"
2222
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
23-
"github.com/arduino/arduino-cli/arduino/discovery"
2423
"github.com/arduino/arduino-cli/arduino/resources"
2524
semver "go.bug.st/relaxed-semver"
2625
)
@@ -101,33 +100,6 @@ var (
101100
}
102101
)
103102

104-
// WatchListBoards returns a channel that receives events from the bundled discovery tool
105-
func WatchListBoards(pm *packagemanager.PackageManager) (<-chan *discovery.Event, error) {
106-
t := getBuiltinSerialDiscoveryTool(pm)
107-
if !t.IsInstalled() {
108-
return nil, fmt.Errorf("missing serial-discovery tool")
109-
}
110-
111-
disc, err := discovery.New("serial-discovery", t.InstallDir.Join(t.Tool.Name).String())
112-
if err != nil {
113-
return nil, err
114-
}
115-
116-
if err = disc.Run(); err != nil {
117-
return nil, fmt.Errorf("starting discovery: %v", err)
118-
}
119-
120-
if err = disc.Start(); err != nil {
121-
return nil, fmt.Errorf("starting discovery: %v", err)
122-
}
123-
124-
if err = disc.StartSync(); err != nil {
125-
return nil, fmt.Errorf("starting sync: %v", err)
126-
}
127-
128-
return disc.EventChannel(10), nil
129-
}
130-
131103
func getBuiltinSerialDiscoveryTool(pm *packagemanager.PackageManager) *cores.ToolRelease {
132104
builtinPackage := pm.Packages.GetOrCreatePackage("builtin")
133105
serialDiscoveryTool := builtinPackage.GetOrCreateTool("serial-discovery")

0 commit comments

Comments
 (0)
Please sign in to comment.