Pass parameters directly instead of through a config struct
This commit is contained in:
parent
2df59cdb17
commit
6368fce40d
|
@ -1,14 +0,0 @@
|
|||
package connect
|
||||
|
||||
import (
|
||||
"automation/integration/hue"
|
||||
"automation/integration/ntfy"
|
||||
|
||||
paho "github.com/eclipse/paho.mqtt.golang"
|
||||
)
|
||||
|
||||
type Connect struct {
|
||||
Client paho.Client
|
||||
Hue hue.Hue
|
||||
Notify ntfy.Notify
|
||||
}
|
|
@ -1,6 +0,0 @@
|
|||
package hue
|
||||
|
||||
type Config struct {
|
||||
Token string `yaml:"token" envconfig:"HUE_TOKEN"`
|
||||
IP string `yaml:"ip" envconfig:"HUE_IP"`
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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"`
|
||||
}
|
||||
|
|
@ -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)
|
||||
|
|
|
@ -1,5 +0,0 @@
|
|||
package ntfy
|
||||
|
||||
type Config struct {
|
||||
Presence string `yaml:"presence" envconfig:"NTFY_PRESENCE"`
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
|
44
main.go
44
main.go
|
@ -1,7 +1,6 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"automation/connect"
|
||||
"automation/device"
|
||||
"automation/integration/hue"
|
||||
"automation/integration/kasa"
|
||||
|
@ -19,9 +18,22 @@ import (
|
|||
)
|
||||
|
||||
type config struct {
|
||||
Hue hue.Config `yaml:"hue"`
|
||||
NTFY ntfy.Config `yaml:"ntfy"`
|
||||
MQTT mqtt.Config `yaml:"mqtt"`
|
||||
Hue struct {
|
||||
Token string `yaml:"token" envconfig:"HUE_TOKEN"`
|
||||
IP string `yaml:"ip" envconfig:"HUE_IP"`
|
||||
} `yaml:"hue"`
|
||||
|
||||
NTFY struct {
|
||||
topic string `yaml:"topic" envconfig:"NTFY_TOPIC"`
|
||||
} `yaml:"ntfy"`
|
||||
|
||||
MQTT struct {
|
||||
Host string `yaml:"host" envconfig:"MQTT_HOST"`
|
||||
Port int `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"`
|
||||
} `yaml:"mqtt"`
|
||||
|
||||
Kasa struct {
|
||||
Outlets map[string]string `yaml:"outlets"`
|
||||
|
@ -125,25 +137,23 @@ func main() {
|
|||
|
||||
devices = make(map[string]interface{})
|
||||
|
||||
var connect connect.Connect
|
||||
// Setup all the connections to other services
|
||||
client := mqtt.New(config.MQTT.Host, config.MQTT.Port, config.MQTT.ClientID, config.MQTT.Username, config.MQTT.Password)
|
||||
defer client.Disconnect(250)
|
||||
notify := ntfy.New(config.NTFY.topic)
|
||||
hue := hue.New(config.Hue.IP, config.Hue.Token)
|
||||
|
||||
// MQTT
|
||||
connect.Client = mqtt.New(config.MQTT)
|
||||
defer connect.Client.Disconnect(250)
|
||||
// Setup presence system
|
||||
p := presence.New(client, hue, notify)
|
||||
defer p.Delete()
|
||||
|
||||
// ntfy.sh
|
||||
connect.Notify = ntfy.New(config.NTFY)
|
||||
|
||||
// Hue
|
||||
connect.Hue = hue.Connect(config.Hue)
|
||||
|
||||
// Kasa
|
||||
// Register all kasa devies
|
||||
for name, ip := range config.Kasa.Outlets {
|
||||
devices[name] = kasa.New(ip)
|
||||
}
|
||||
|
||||
// Devices that we control and expose to google home
|
||||
provider := device.NewProvider(config.Google, connect.Client)
|
||||
provider := device.NewProvider(config.Google, client)
|
||||
|
||||
r := mux.NewRouter()
|
||||
r.HandleFunc("/assistant", provider.Service.FullfillmentHandler)
|
||||
|
@ -153,8 +163,6 @@ func main() {
|
|||
}
|
||||
|
||||
// Presence
|
||||
p := presence.New(&connect)
|
||||
defer p.Delete()
|
||||
|
||||
addr := ":8090"
|
||||
srv := http.Server{
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
package presence
|
||||
|
||||
import (
|
||||
"automation/connect"
|
||||
"automation/integration/hue"
|
||||
"automation/integration/ntfy"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
|
@ -13,7 +14,10 @@ import (
|
|||
)
|
||||
|
||||
type Presence struct {
|
||||
connect *connect.Connect
|
||||
client paho.Client
|
||||
hue *hue.Hue
|
||||
ntfy *ntfy.Notify
|
||||
|
||||
devices map[string]bool
|
||||
presence bool
|
||||
}
|
||||
|
@ -83,10 +87,10 @@ func (p *Presence) overallPresenceHandler(client paho.Client, msg paho.Message)
|
|||
|
||||
fmt.Printf("Presence: %t\n", message.State)
|
||||
// Notify users of presence update
|
||||
p.connect.Notify.Presence(p.presence)
|
||||
p.ntfy.Presence(p.presence)
|
||||
|
||||
// Set presence on the hue bridge
|
||||
p.connect.Hue.SetFlag(41, message.State)
|
||||
p.hue.SetFlag(41, message.State)
|
||||
|
||||
if !message.State {
|
||||
log.Println("Turn off all the devices")
|
||||
|
@ -109,14 +113,14 @@ func (p *Presence) overallPresenceHandler(client paho.Client, msg paho.Message)
|
|||
}
|
||||
}
|
||||
|
||||
func New(connect *connect.Connect) *Presence {
|
||||
p := &Presence{connect: connect, devices: make(map[string]bool), presence: false}
|
||||
func New(client paho.Client, hue *hue.Hue, ntfy *ntfy.Notify) *Presence {
|
||||
p := &Presence{client: client, hue: hue, ntfy: ntfy, devices: make(map[string]bool), presence: false}
|
||||
|
||||
if token := connect.Client.Subscribe("automation/presence", 1, p.overallPresenceHandler); token.Wait() && token.Error() != nil {
|
||||
if token := p.client.Subscribe("automation/presence", 1, p.overallPresenceHandler); token.Wait() && token.Error() != nil {
|
||||
log.Println(token.Error())
|
||||
}
|
||||
|
||||
if token := connect.Client.Subscribe("automation/presence/+", 1, p.devicePresenceHandler); token.Wait() && token.Error() != nil {
|
||||
if token := p.client.Subscribe("automation/presence/+", 1, p.devicePresenceHandler); token.Wait() && token.Error() != nil {
|
||||
log.Println(token.Error())
|
||||
}
|
||||
|
||||
|
@ -124,11 +128,11 @@ func New(connect *connect.Connect) *Presence {
|
|||
}
|
||||
|
||||
func (p *Presence) Delete() {
|
||||
if token := p.connect.Client.Unsubscribe("automation/presence"); token.Wait() && token.Error() != nil {
|
||||
if token := p.client.Unsubscribe("automation/presence"); token.Wait() && token.Error() != nil {
|
||||
log.Println(token.Error())
|
||||
}
|
||||
|
||||
if token := p.connect.Client.Unsubscribe("automation/presence/+"); token.Wait() && token.Error() != nil {
|
||||
if token := p.client.Unsubscribe("automation/presence/+"); token.Wait() && token.Error() != nil {
|
||||
log.Println(token.Error())
|
||||
}
|
||||
}
|
||||
|
|
Reference in New Issue
Block a user