Pass parameters directly instead of through a config struct

This commit is contained in:
2022-11-18 20:09:52 +01:00
parent 2df59cdb17
commit 6368fce40d
9 changed files with 53 additions and 76 deletions

View File

@@ -1,6 +0,0 @@
package hue
type Config struct {
Token string `yaml:"token" envconfig:"HUE_TOKEN"`
IP string `yaml:"ip" envconfig:"HUE_IP"`
}

View File

@@ -37,8 +37,8 @@ func (hue *Hue) SetFlag(id int, value bool) {
}
}
func Connect(config Config) Hue {
hue := Hue{ip: config.IP, login: config.Token, Events: make(chan *sse.Event)}
func New(ip string, token string) *Hue {
hue := Hue{ip: ip, login: token, Events: make(chan *sse.Event)}
// Subscribe to eventstream
client := sse.NewClient(fmt.Sprintf("https://%s/eventstream/clip/v2", hue.ip))
@@ -52,5 +52,5 @@ func Connect(config Config) Hue {
panic(err)
}
return hue
return &hue
}

View File

@@ -1,10 +0,0 @@
package mqtt
type Config struct {
Host string `yaml:"host" envconfig:"MQTT_HOST"`
Port string `yaml:"port" envconfig:"MQTT_PORT"`
Username string `yaml:"username" envconfig:"MQTT_USERNAME"`
Password string `yaml:"password" envconfig:"MQTT_PASSWORD"`
ClientID string `yaml:"client_id" envconfig:"MQTT_CLIENT_ID"`
}

View File

@@ -12,12 +12,12 @@ var defaultHandler paho.MessageHandler = func(client paho.Client, msg paho.Messa
fmt.Printf("MSG: %s\n", msg.Payload())
}
func New(config Config) paho.Client {
opts := paho.NewClientOptions().AddBroker(fmt.Sprintf("%s:%s", config.Host, config.Port))
opts.SetClientID(config.ClientID)
func New(host string, port int, clientID string, username string, password string) paho.Client {
opts := paho.NewClientOptions().AddBroker(fmt.Sprintf("%s:%d", host, port))
opts.SetClientID(clientID)
opts.SetDefaultPublishHandler(defaultHandler)
opts.SetUsername(config.Username)
opts.SetPassword(config.Password)
opts.SetUsername(username)
opts.SetPassword(password)
opts.SetOrderMatters(false)
client := paho.NewClient(opts)

View File

@@ -1,5 +0,0 @@
package ntfy
type Config struct {
Presence string `yaml:"presence" envconfig:"NTFY_PRESENCE"`
}

View File

@@ -7,7 +7,7 @@ import (
)
type Notify struct {
presence string
topic string
}
func (n *Notify) Presence(home bool) {
@@ -22,7 +22,7 @@ func (n *Notify) Presence(home bool) {
actions = "broadcast, Set as home, extras.cmd=presence, extras.state=1, clear=true"
}
req, err := http.NewRequest("POST", fmt.Sprintf("https://ntfy.sh/%s", n.presence), strings.NewReader(description))
req, err := http.NewRequest("POST", fmt.Sprintf("https://ntfy.sh/%s", n.topic), strings.NewReader(description))
if err != nil {
panic(err)
}
@@ -35,10 +35,10 @@ func (n *Notify) Presence(home bool) {
http.DefaultClient.Do(req)
}
func New(config Config) Notify {
ntfy := Notify{presence: config.Presence}
func New(topic string) *Notify {
ntfy := Notify{topic}
// @TODO Make sure the topic is valid?
return ntfy
return &ntfy
}