Skip to content

Commit

Permalink
read and manage the connection configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
ftl committed Dec 10, 2023
1 parent 4a82b24 commit fb0a41a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
32 changes: 31 additions & 1 deletion pkg/hamdeck/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
ConfigButtons = "buttons"
ConfigType = "type"
ConfigIndex = "index"
ConfigConnections = "connections"
)

func (d *HamDeck) ReadConfig(r io.Reader) error {
Expand All @@ -40,12 +41,21 @@ func (d *HamDeck) ReadConfig(r io.Reader) error {
effectiveConfiguration := findEffectiveConfiguration(configuration)

d.buttonsPerFactory = make([]int, len(d.factories))
d.connections = make(map[string]ConnectionConfig)
d.pages = make(map[string]Page)

connections, ok := (effectiveConfiguration[ConfigConnections]).(map[string]any)
if ok {
err = d.loadConnections(connections)
}
if err != nil {
return err
}

d.startPageID, ok = effectiveConfiguration[ConfigStartPageID].(string)
if !ok {
d.startPageID = legacyPageID
}

pages, ok := effectiveConfiguration[ConfigPages].(map[string]any)
if ok {
err = d.loadPages(pages)
Expand All @@ -57,6 +67,8 @@ func (d *HamDeck) ReadConfig(r io.Reader) error {
buttons, ok := effectiveConfiguration[ConfigButtons].([]any)
if ok {
err = d.loadLegacyPage(buttons)
} else {
d.loadEmptyLegacyPage()
}
if err != nil {
return err
Expand All @@ -78,6 +90,18 @@ func findEffectiveConfiguration(configuration map[string]any) map[string]any {
return subconfiguration
}

func (d *HamDeck) loadConnections(configuration map[string]any) error {
for id, config := range configuration {
connection, ok := config.(map[string]any)
if !ok {
log.Printf("%s is not a valid connection configuration", id)
continue
}
d.connections[id] = ConnectionConfig(connection)
}
return nil
}

func (d *HamDeck) loadPages(configuration map[string]any) error {
for id, rawPage := range configuration {
pageConfiguration, ok := rawPage.(map[string]any)
Expand Down Expand Up @@ -123,6 +147,12 @@ func (d *HamDeck) loadLegacyPage(configuration []any) error {
return nil
}

func (d *HamDeck) loadEmptyLegacyPage() {
d.pages[legacyPageID] = Page{
buttons: make([]Button, len(d.buttons)),
}
}

func (d *HamDeck) loadButtons(configuration []any) ([]Button, error) {
result := make([]Button, len(d.buttons))
for i, rawButtonConfig := range configuration {
Expand Down
35 changes: 35 additions & 0 deletions pkg/hamdeck/connections_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package hamdeck

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestReadConfig_Connections(t *testing.T) {
runWithConfigString(t, `{
"connections": {
"testsdrA": {
"type": "test",
"some_config": "some_value"
},
"testsdrB": {
"type": "test",
"some_config": "some_other_value"
}
}
}`, func(t *testing.T, deck *HamDeck, device *testDevice, _ chan struct{}) {
assert.Equal(t, 2, len(deck.connections))

configA, ok := deck.GetConnection("testsdrA")
assert.True(t, ok)
assert.Equal(t, "some_value", configA["some_config"])

configB, ok := deck.GetConnection("testsdrB")
assert.True(t, ok)
assert.Equal(t, "some_other_value", configB["some_config"])

_, ok = deck.GetConnection("undefined")
assert.False(t, ok)
})
}
9 changes: 9 additions & 0 deletions pkg/hamdeck/hamdeck.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ type ButtonFactory interface {
CreateButton(config map[string]interface{}) Button
}

type ConnectionConfig map[string]any

const legacyPageID = ""

type HamDeck struct {
Expand All @@ -98,6 +100,8 @@ type HamDeck struct {

startPageID string
pages map[string]Page

connections map[string]ConnectionConfig
}

type Page struct {
Expand Down Expand Up @@ -128,6 +132,11 @@ func (d *HamDeck) RegisterFactory(factory ButtonFactory) {
d.factories = append(d.factories, factory)
}

func (d *HamDeck) GetConnection(name string) (ConnectionConfig, bool) {
connection, found := d.connections[name]
return connection, found
}

func (d *HamDeck) RedrawAll(redrawImages bool) {
d.drawLock.Lock()
defer d.drawLock.Unlock()
Expand Down

0 comments on commit fb0a41a

Please sign in to comment.