diff options
author | xengineering <me@xengineering.eu> | 2024-10-07 21:53:38 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2024-10-07 21:55:50 +0200 |
commit | b03cf32bef7e34c4886a3b7300ffc0132b310a5b (patch) | |
tree | 33191d1c5bccb324d6b3e5b37f15296bbffb4031 | |
parent | 293965c55615bb072843500cc3af44c0e266dfcd (diff) | |
download | soundbox-app-b03cf32bef7e34c4886a3b7300ffc0132b310a5b.tar soundbox-app-b03cf32bef7e34c4886a3b7300ffc0132b310a5b.tar.zst soundbox-app-b03cf32bef7e34c4886a3b7300ffc0132b310a5b.zip |
Parse config at startup
-rw-r--r-- | config.go | 23 | ||||
-rw-r--r-- | main.go | 36 |
2 files changed, 38 insertions, 21 deletions
@@ -3,15 +3,34 @@ package main import ( "encoding/json" "io" + "net" "os" "path/filepath" ) const configPathRelative = `.config/soundbox/config.json` +type MacAddress net.HardwareAddr + +func (m *MacAddress) UnmarshalJSON(data []byte) error { + var macStr string + err := json.Unmarshal(data, &macStr) + if err != nil { + return err + } + + hwAddr, err := net.ParseMAC(macStr) + if err != nil { + return err + } + + *m = MacAddress(hwAddr) + return nil +} + type SoundboxConfig struct { - Name string `json:"name"` - Mac string `json:"mac"` + Name string `json:"name"` + Mac MacAddress `json:"mac"` } type URLConfig struct { @@ -20,7 +20,12 @@ import ( ) func main() { - ui := NewUi() + config, err := loadConfig() + if err != nil { + log.Fatal(err) + } + + ui := NewUi(config) go func() { err := ui.Run() if err != nil { @@ -33,6 +38,7 @@ func main() { type State struct { sync.Mutex + Config GlobalConfig Theme *material.Theme Title string UrlEditor widget.Editor @@ -47,9 +53,11 @@ type Ui struct { State State } -func NewUi() *Ui { +func NewUi(config GlobalConfig) *Ui { ui := Ui{} + ui.State.Config = config + ui.State.Theme = material.NewTheme() ui.Window = new(app.Window) @@ -95,7 +103,11 @@ func (ui *Ui) HandleInputs(gtx layout.Context) { ui.State.PlayerCancel() } else { ui.State.PlayerContext, ui.State.PlayerCancel = context.WithCancel(context.Background()) - go play(ui.State.PlayerContext, ui.State.UrlEditor.Text(), ui) + var targets []net.HardwareAddr + for _, entry := range ui.State.Config.Soundboxes { + targets = append(targets, net.HardwareAddr(entry.Mac)) + } + go play(ui.State.PlayerContext, ui.State.UrlEditor.Text(), targets, ui) } } } @@ -131,7 +143,7 @@ func (ui *Ui) Layout(gtx layout.Context) layout.Dimensions { }) } -func play(ctx context.Context, url string, ui *Ui) { +func play(ctx context.Context, url string, targets []net.HardwareAddr, ui *Ui) { setPlayingState := func(isPlaying bool) { ui.State.Lock() defer ui.Window.Invalidate() @@ -148,21 +160,7 @@ func play(ctx context.Context, url string, ui *Ui) { setPlayingState(true) defer setPlayingState(false) - config, err := loadConfig() - if err != nil { - log.Println(err) - } - - var devices []net.HardwareAddr - for _, entry := range config.Soundboxes { - mac, err := net.ParseMAC(entry.Mac) - if err != nil { - log.Printf("Failed to parse MAC: %v", err) - } - devices = append(devices, mac) - } - - err = soundbox.StreamURLContext(ctx, url, devices) + err := soundbox.StreamURLContext(ctx, url, targets) if err != nil { log.Println(err) } |