Skip to content

Commit

Permalink
allow mutliple hamlib connections
Browse files Browse the repository at this point in the history
  • Loading branch information
ftl committed Dec 10, 2023
1 parent 2df34be commit 8804cea
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 24 deletions.
4 changes: 1 addition & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ func run(cmd *cobra.Command, args []string) {
deck := hamdeck.New(device)
deck.RegisterFactory(hamdeck.NewButtonFactory(deck))
deck.RegisterFactory(pulse.NewButtonFactory())
if rootFlags.hamlibAddress != "" {
deck.RegisterFactory(hamlib.NewButtonFactory(rootFlags.hamlibAddress))
}
deck.RegisterFactory(hamlib.NewButtonFactory(deck, rootFlags.hamlibAddress))
if rootFlags.tciAddress != "" {
deck.RegisterFactory(tci.NewButtonFactory(rootFlags.tciAddress))
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/hamdeck/connections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (p *testConnectionProvider) GetConnection(name string, connectionType strin
return p.config, true
}

func (p *testConnectionProvider) CreateConnection(config ConnectionConfig) (*testConnection, error) {
func (p *testConnectionProvider) CreateConnection(_ string, config ConnectionConfig) (*testConnection, error) {
return &testConnection{
config: config,
}, nil
Expand Down
38 changes: 32 additions & 6 deletions pkg/hamdeck/hamdeck.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,19 +313,23 @@ func (h *LongpressHandler) Released() {
h.timer.Stop()
}

const LegacyConnectionName = ""

type ConnectionConfig map[string]any

type ConnectionConfigProvider interface {
GetConnection(string, string) (ConnectionConfig, bool)
}

type ConnectionFactory[T any] func(ConnectionConfig) (T, error)
type ConnectionFactory[T any] func(string, ConnectionConfig) (T, error)

type ConnectionManager[T any] struct {
connectionType string
provider ConnectionConfigProvider
factory ConnectionFactory[T]
connections map[string]T
connectionType string
provider ConnectionConfigProvider
factory ConnectionFactory[T]
connections map[string]T
hasLegacy bool
legacyConnection T
}

func NewConnectionManager[T any](connectionType string, provider ConnectionConfigProvider, factory ConnectionFactory[T]) *ConnectionManager[T] {
Expand All @@ -337,7 +341,20 @@ func NewConnectionManager[T any](connectionType string, provider ConnectionConfi
}
}

func (m *ConnectionManager[T]) SetLegacy(legacyConnection T) {
m.hasLegacy = true
m.legacyConnection = legacyConnection
}

func (m *ConnectionManager[T]) Get(name string) (T, error) {
var connection T
if name == LegacyConnectionName {
if !m.hasLegacy {
return connection, fmt.Errorf("no legacy %s connection defined", m.connectionType)
}
return m.legacyConnection, nil
}

connection, ok := m.connections[name]
if ok {
return connection, nil
Expand All @@ -348,7 +365,7 @@ func (m *ConnectionManager[T]) Get(name string) (T, error) {
return connection, fmt.Errorf("no %s connection defined with name %s", m.connectionType, name)
}

connection, err := m.factory(config)
connection, err := m.factory(name, config)
if err != nil {
return connection, err
}
Expand All @@ -357,3 +374,12 @@ func (m *ConnectionManager[T]) Get(name string) (T, error) {

return connection, nil
}

func (m *ConnectionManager[T]) ForEach(f func(T)) {
for _, connection := range m.connections {
f(connection)
}
if m.hasLegacy {
f(m.legacyConnection)
}
}
98 changes: 84 additions & 14 deletions pkg/hamlib/factory.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hamlib

import (
"fmt"
"log"

"github.com/ftl/rigproxy/pkg/client"
Expand All @@ -9,6 +10,7 @@ import (
)

const (
ConfigAddress = "address"
ConfigCommand = "command"
ConfigArgs = "args"
ConfigMode = "mode"
Expand All @@ -24,6 +26,7 @@ const (
)

const (
ConnectionType = "hamlib"
SetModeButtonType = "hamlib.SetMode"
ToggleModeButtonType = "hamlib.ToggleMode"
SetButtonType = "hamlib.Set"
Expand All @@ -33,21 +36,39 @@ const (
SetVFOButtonType = "hamlib.SetVFO"
)

func NewButtonFactory(address string) *Factory {
client := NewClient(address)
client.KeepOpen()
func NewButtonFactory(provider hamdeck.ConnectionConfigProvider, legacyAddress string) *Factory {
result := &Factory{}
result.connections = hamdeck.NewConnectionManager(ConnectionType, provider, result.createHamlibClient)

return &Factory{
client: client,
if legacyAddress != "" {
client := NewClient(legacyAddress)
client.KeepOpen()
result.connections.SetLegacy(client)
}

return result
}

type Factory struct {
client *HamlibClient
connections *hamdeck.ConnectionManager[*HamlibClient]
}

func (f *Factory) createHamlibClient(name string, config hamdeck.ConnectionConfig) (*HamlibClient, error) {
address, ok := hamdeck.ToString(config[ConfigAddress])
if !ok {
return nil, fmt.Errorf("no address defined for hamlib connection %s", name)
}

client := NewClient(address)
client.KeepOpen()

return client, nil
}

func (f *Factory) Close() {
f.client.Close()
f.connections.ForEach(func(client *HamlibClient) {
client.Close()
})
}

func (f *Factory) CreateButton(config map[string]interface{}) hamdeck.Button {
Expand Down Expand Up @@ -79,7 +100,14 @@ func (f *Factory) createSetModeButton(config map[string]interface{}) hamdeck.But
return nil
}

return NewSetModeButton(f.client, client.Mode(mode), label)
connection, _ := hamdeck.ToString(config[hamdeck.ConfigConnection])
hamlibClient, err := f.connections.Get(connection)
if err != nil {
log.Printf("Cannot create hamlib.SetMode button: %v", err)
return nil
}

return NewSetModeButton(hamlibClient, client.Mode(mode), label)
}

func (f *Factory) createToggleModeButton(config map[string]interface{}) hamdeck.Button {
Expand All @@ -92,7 +120,14 @@ func (f *Factory) createToggleModeButton(config map[string]interface{}) hamdeck.
return nil
}

return NewToggleModeButton(f.client, client.Mode(mode1), label1, client.Mode(mode2), label2)
connection, _ := hamdeck.ToString(config[hamdeck.ConfigConnection])
hamlibClient, err := f.connections.Get(connection)
if err != nil {
log.Printf("Cannot create hamlib.ToggleMode button: %v", err)
return nil
}

return NewToggleModeButton(hamlibClient, client.Mode(mode1), label1, client.Mode(mode2), label2)
}

func (f *Factory) createSetButton(config map[string]interface{}) hamdeck.Button {
Expand All @@ -104,7 +139,14 @@ func (f *Factory) createSetButton(config map[string]interface{}) hamdeck.Button
return nil
}

return NewSetButton(f.client, label, command, args...)
connection, _ := hamdeck.ToString(config[hamdeck.ConfigConnection])
hamlibClient, err := f.connections.Get(connection)
if err != nil {
log.Printf("Cannot create hamlib.Set button: %v", err)
return nil
}

return NewSetButton(hamlibClient, label, command, args...)
}

func (f *Factory) createSwitchToBandButton(config map[string]interface{}) hamdeck.Button {
Expand All @@ -116,7 +158,14 @@ func (f *Factory) createSwitchToBandButton(config map[string]interface{}) hamdec
return nil
}

return NewSwitchToBandButton(f.client, label, band, useUpDown)
connection, _ := hamdeck.ToString(config[hamdeck.ConfigConnection])
hamlibClient, err := f.connections.Get(connection)
if err != nil {
log.Printf("Cannot create hamlib.SwitchToBand button: %v", err)
return nil
}

return NewSwitchToBandButton(hamlibClient, label, band, useUpDown)
}

func (f *Factory) createSetPowerLevelButton(config map[string]interface{}) hamdeck.Button {
Expand All @@ -127,13 +176,27 @@ func (f *Factory) createSetPowerLevelButton(config map[string]interface{}) hamde
return nil
}

return NewSetPowerLevelButton(f.client, label, value)
connection, _ := hamdeck.ToString(config[hamdeck.ConfigConnection])
hamlibClient, err := f.connections.Get(connection)
if err != nil {
log.Printf("Cannot create hamlib.SetPowerLevel button: %v", err)
return nil
}

return NewSetPowerLevelButton(hamlibClient, label, value)
}

func (f *Factory) createMOXButton(config map[string]interface{}) hamdeck.Button {
label, _ := hamdeck.ToString(config[ConfigLabel])

return NewMOXButton(f.client, label)
connection, _ := hamdeck.ToString(config[hamdeck.ConfigConnection])
hamlibClient, err := f.connections.Get(connection)
if err != nil {
log.Printf("Cannot create hamlib.MOX button: %v", err)
return nil
}

return NewMOXButton(hamlibClient, label)
}

func (f *Factory) createSetVFOButton(config map[string]interface{}) hamdeck.Button {
Expand All @@ -144,5 +207,12 @@ func (f *Factory) createSetVFOButton(config map[string]interface{}) hamdeck.Butt
return nil
}

return NewSetVFOButton(f.client, label, client.VFO(vfo))
connection, _ := hamdeck.ToString(config[hamdeck.ConfigConnection])
hamlibClient, err := f.connections.Get(connection)
if err != nil {
log.Printf("Cannot create hamlib.SetVFO button: %v", err)
return nil
}

return NewSetVFOButton(hamlibClient, label, client.VFO(vfo))
}

0 comments on commit 8804cea

Please sign in to comment.