Skip to content

Commit

Permalink
allow multiple mqtt connections
Browse files Browse the repository at this point in the history
  • Loading branch information
ftl committed Dec 10, 2023
1 parent 9b58737 commit 6acc4e1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
4 changes: 1 addition & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ func run(cmd *cobra.Command, args []string) {
deck.RegisterFactory(pulse.NewButtonFactory())
deck.RegisterFactory(hamlib.NewButtonFactory(deck, rootFlags.hamlibAddress))
deck.RegisterFactory(tci.NewButtonFactory(deck, rootFlags.tciAddress))
if rootFlags.mqttAddress != "" {
deck.RegisterFactory(mqtt.NewButtonFactory(rootFlags.mqttAddress, rootFlags.mqttUsername, rootFlags.mqttPassword))
}
deck.RegisterFactory(mqtt.NewButtonFactory(deck, rootFlags.mqttAddress, rootFlags.mqttUsername, rootFlags.mqttPassword))

err = configureHamDeck(deck, rootFlags.configFile)
if err != nil {
Expand Down
53 changes: 45 additions & 8 deletions pkg/mqtt/factory.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package mqtt

import (
"fmt"
"log"
"strings"

"github.com/ftl/hamdeck/pkg/hamdeck"
)

const (
ConfigAddress = "address"
ConfigUsername = "username"
ConfigPassword = "password"
ConfigLabel = "label"
ConfigPath = "path"
ConfigInputTopic = "inputTopic"
Expand All @@ -18,24 +22,43 @@ const (
)

const (
ConnectionType = "mqtt"
TuneButtonType = "mqtt.AT100Tune"
SwitchButtonType = "mqtt.Switch"
)

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

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

return result
}

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

func (f *Factory) createMQTTClient(name string, config hamdeck.ConnectionConfig) (*Client, error) {
address, ok := hamdeck.ToString(config[ConfigAddress])
if !ok {
return nil, fmt.Errorf("no address defined for mqtt connection %s", name)
}
username, _ := hamdeck.ToString(config[ConfigUsername])
password, _ := hamdeck.ToString(config[ConfigPassword])

client := NewClient(address, username, password)

return client, nil
}

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

func (f *Factory) CreateButton(config map[string]interface{}) hamdeck.Button {
Expand All @@ -58,7 +81,14 @@ func (f *Factory) createTuneButton(config map[string]interface{}) hamdeck.Button
return nil
}

return NewTuneButton(f.client, label, path)
connection, _ := hamdeck.ToString(config[hamdeck.ConfigConnection])
mqttClient, err := f.connections.Get(connection)
if err != nil {
log.Printf("Cannot create mqtt.ATU100Tune button: %v", err)
return nil
}

return NewTuneButton(mqttClient, label, path)
}

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

return NewSwitchButton(f.client, label, inputTopic, outputTopic, onPayload, offPayload, SwitchMode(strings.TrimSpace(strings.ToUpper(mode))))
connection, _ := hamdeck.ToString(config[hamdeck.ConfigConnection])
mqttClient, err := f.connections.Get(connection)
if err != nil {
log.Printf("Cannot create mqtt.Switch button: %v", err)
return nil
}

return NewSwitchButton(mqttClient, label, inputTopic, outputTopic, onPayload, offPayload, SwitchMode(strings.TrimSpace(strings.ToUpper(mode))))
}

0 comments on commit 6acc4e1

Please sign in to comment.