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 {
|
func New(ip string, token string) *Hue {
|
||||||
hue := Hue{ip: config.IP, login: config.Token, Events: make(chan *sse.Event)}
|
hue := Hue{ip: ip, login: token, Events: make(chan *sse.Event)}
|
||||||
|
|
||||||
// Subscribe to eventstream
|
// Subscribe to eventstream
|
||||||
client := sse.NewClient(fmt.Sprintf("https://%s/eventstream/clip/v2", hue.ip))
|
client := sse.NewClient(fmt.Sprintf("https://%s/eventstream/clip/v2", hue.ip))
|
||||||
|
@ -52,5 +52,5 @@ func Connect(config Config) Hue {
|
||||||
panic(err)
|
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())
|
fmt.Printf("MSG: %s\n", msg.Payload())
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(config Config) paho.Client {
|
func New(host string, port int, clientID string, username string, password string) paho.Client {
|
||||||
opts := paho.NewClientOptions().AddBroker(fmt.Sprintf("%s:%s", config.Host, config.Port))
|
opts := paho.NewClientOptions().AddBroker(fmt.Sprintf("%s:%d", host, port))
|
||||||
opts.SetClientID(config.ClientID)
|
opts.SetClientID(clientID)
|
||||||
opts.SetDefaultPublishHandler(defaultHandler)
|
opts.SetDefaultPublishHandler(defaultHandler)
|
||||||
opts.SetUsername(config.Username)
|
opts.SetUsername(username)
|
||||||
opts.SetPassword(config.Password)
|
opts.SetPassword(password)
|
||||||
opts.SetOrderMatters(false)
|
opts.SetOrderMatters(false)
|
||||||
|
|
||||||
client := paho.NewClient(opts)
|
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 {
|
type Notify struct {
|
||||||
presence string
|
topic string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Notify) Presence(home bool) {
|
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"
|
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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -35,10 +35,10 @@ func (n *Notify) Presence(home bool) {
|
||||||
http.DefaultClient.Do(req)
|
http.DefaultClient.Do(req)
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(config Config) Notify {
|
func New(topic string) *Notify {
|
||||||
ntfy := Notify{presence: config.Presence}
|
ntfy := Notify{topic}
|
||||||
|
|
||||||
// @TODO Make sure the topic is valid?
|
// @TODO Make sure the topic is valid?
|
||||||
|
|
||||||
return ntfy
|
return &ntfy
|
||||||
}
|
}
|
||||||
|
|
44
main.go
44
main.go
|
@ -1,7 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"automation/connect"
|
|
||||||
"automation/device"
|
"automation/device"
|
||||||
"automation/integration/hue"
|
"automation/integration/hue"
|
||||||
"automation/integration/kasa"
|
"automation/integration/kasa"
|
||||||
|
@ -19,9 +18,22 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type config struct {
|
type config struct {
|
||||||
Hue hue.Config `yaml:"hue"`
|
Hue struct {
|
||||||
NTFY ntfy.Config `yaml:"ntfy"`
|
Token string `yaml:"token" envconfig:"HUE_TOKEN"`
|
||||||
MQTT mqtt.Config `yaml:"mqtt"`
|
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 {
|
Kasa struct {
|
||||||
Outlets map[string]string `yaml:"outlets"`
|
Outlets map[string]string `yaml:"outlets"`
|
||||||
|
@ -125,25 +137,23 @@ func main() {
|
||||||
|
|
||||||
devices = make(map[string]interface{})
|
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
|
// Setup presence system
|
||||||
connect.Client = mqtt.New(config.MQTT)
|
p := presence.New(client, hue, notify)
|
||||||
defer connect.Client.Disconnect(250)
|
defer p.Delete()
|
||||||
|
|
||||||
// ntfy.sh
|
// Register all kasa devies
|
||||||
connect.Notify = ntfy.New(config.NTFY)
|
|
||||||
|
|
||||||
// Hue
|
|
||||||
connect.Hue = hue.Connect(config.Hue)
|
|
||||||
|
|
||||||
// Kasa
|
|
||||||
for name, ip := range config.Kasa.Outlets {
|
for name, ip := range config.Kasa.Outlets {
|
||||||
devices[name] = kasa.New(ip)
|
devices[name] = kasa.New(ip)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Devices that we control and expose to google home
|
// 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 := mux.NewRouter()
|
||||||
r.HandleFunc("/assistant", provider.Service.FullfillmentHandler)
|
r.HandleFunc("/assistant", provider.Service.FullfillmentHandler)
|
||||||
|
@ -153,8 +163,6 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Presence
|
// Presence
|
||||||
p := presence.New(&connect)
|
|
||||||
defer p.Delete()
|
|
||||||
|
|
||||||
addr := ":8090"
|
addr := ":8090"
|
||||||
srv := http.Server{
|
srv := http.Server{
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
package presence
|
package presence
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"automation/connect"
|
"automation/integration/hue"
|
||||||
|
"automation/integration/ntfy"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
@ -13,7 +14,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Presence struct {
|
type Presence struct {
|
||||||
connect *connect.Connect
|
client paho.Client
|
||||||
|
hue *hue.Hue
|
||||||
|
ntfy *ntfy.Notify
|
||||||
|
|
||||||
devices map[string]bool
|
devices map[string]bool
|
||||||
presence bool
|
presence bool
|
||||||
}
|
}
|
||||||
|
@ -83,10 +87,10 @@ func (p *Presence) overallPresenceHandler(client paho.Client, msg paho.Message)
|
||||||
|
|
||||||
fmt.Printf("Presence: %t\n", message.State)
|
fmt.Printf("Presence: %t\n", message.State)
|
||||||
// Notify users of presence update
|
// Notify users of presence update
|
||||||
p.connect.Notify.Presence(p.presence)
|
p.ntfy.Presence(p.presence)
|
||||||
|
|
||||||
// Set presence on the hue bridge
|
// Set presence on the hue bridge
|
||||||
p.connect.Hue.SetFlag(41, message.State)
|
p.hue.SetFlag(41, message.State)
|
||||||
|
|
||||||
if !message.State {
|
if !message.State {
|
||||||
log.Println("Turn off all the devices")
|
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 {
|
func New(client paho.Client, hue *hue.Hue, ntfy *ntfy.Notify) *Presence {
|
||||||
p := &Presence{connect: connect, devices: make(map[string]bool), presence: false}
|
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())
|
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())
|
log.Println(token.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,11 +128,11 @@ func New(connect *connect.Connect) *Presence {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Presence) Delete() {
|
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())
|
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())
|
log.Println(token.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue
Block a user