Skip to content

Commit

Permalink
fix: radio discovery blocking program
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsNotGoodName committed Nov 25, 2021
1 parent cac3f74 commit d41fdfb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
39 changes: 31 additions & 8 deletions pkg/radio/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,31 @@ func (h *Hub) NewRadios() ([]*Radio, error) {
return nil, err
}

// Create radios array
var radios []*Radio
rds := make(chan *Radio)
var wg sync.WaitGroup

for i := range clients {
radio, err := h.NewRadio(clients[i])
if err != nil {
log.Println("Hub.NewRadios(ERROR):", err)
continue
}
wg.Add(1)

go (func(idx int) {
radio, err := h.NewRadio(clients[idx])
if err != nil {
log.Println("Hub.NewRadios(ERROR):", err)
} else {
rds <- radio
}
wg.Done()
})(i)
}

radios = append(radios, radio)
go (func() {
wg.Wait()
close(rds)
})()

var radios []*Radio
for r := range rds {
radios = append(radios, r)
}

return radios, nil
Expand Down Expand Up @@ -85,6 +100,13 @@ func (h *Hub) NewRadio(client goupnp.ServiceClient) (*Radio, error) {
updateVolumeChan: make(chan int),
refreshPresets: make(chan bool, 1),
}

err = rd.initState()
if err != nil {
cancel()
return nil, err
}

go rd.radioLoop()

return &rd, nil
Expand Down Expand Up @@ -185,6 +207,7 @@ func (h *Hub) discoverLoop() {
radios, err := h.NewRadios()
if err != nil {
d <- err
continue
}

// Create new radios map
Expand Down
16 changes: 9 additions & 7 deletions pkg/radio/radio.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package radio
import (
"context"
"encoding/xml"
"fmt"
"log"
"strconv"

"github.com/avast/retry-go"
)
Expand Down Expand Up @@ -64,8 +66,6 @@ func (rd *Radio) Refresh() {
func (rd *Radio) radioLoop() {
log.Println("Radio.radioLoop: started")

rd.initState()

for {
select {
case <-rd.ctx.Done():
Expand Down Expand Up @@ -184,7 +184,7 @@ func (rd *Radio) sendState(state *State) {
rd.h.emitState(state)
}

func (rd *Radio) initState() {
func (rd *Radio) initState() error {
// Set name of radio
rd.state.Name = rd.Client.RootDevice.Device.FriendlyName

Expand All @@ -198,11 +198,11 @@ func (rd *Radio) initState() {
return nil
}
}, retry.Context(rd.ctx)); err != nil {
log.Println("Radio.initState(ERROR):", err)
return err
} else {
numPresets = numPresets - 2
if numPresets < 1 {
log.Println("Radio.initState(ERROR): invalid number of presets were given from radio,", numPresets)
return fmt.Errorf("invalid number of presets were given from radio, " + strconv.Itoa(numPresets))
} else {
rd.state.NumPresets = numPresets
}
Expand All @@ -218,7 +218,7 @@ func (rd *Radio) initState() {
return nil
}
}, retry.Context(rd.ctx)); err != nil {
log.Println("Radio.initState(ERROR):", err)
return err
} else {
rd.state.Volume = &volume
}
Expand All @@ -233,11 +233,13 @@ func (rd *Radio) initState() {
return nil
}
}, retry.Context(rd.ctx)); err != nil {
log.Println("Radio.initState(ERROR):", err)
return err
} else {
for i := range presets {
rd.h.PresetMutator(rd.ctx, &presets[i])
}
rd.state.Presets = presets
}

return nil
}

0 comments on commit d41fdfb

Please sign in to comment.