Added option to activate computer through mqtt
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Dreaded_X 2022-12-03 02:15:06 +01:00
parent 51958a0542
commit d42afecd67
Signed by: Dreaded_X
GPG Key ID: 76BDEC4E165D8AD9
4 changed files with 37 additions and 6 deletions

View File

@ -42,4 +42,5 @@ func RegisterAutomations(client paho.Client, prefix string, hue *hue.Hue, notify
kettleAutomation(client, prefix, home) kettleAutomation(client, prefix, home)
darknessAutomation(client, hue) darknessAutomation(client, hue)
frontdoorAutomation(client, prefix, presence) frontdoorAutomation(client, prefix, presence)
zeusAutomation(client, home)
} }

23
automation/zeus.go Normal file
View File

@ -0,0 +1,23 @@
package automation
import (
"automation/device"
"automation/home"
"fmt"
"log"
paho "github.com/eclipse/paho.mqtt.golang"
)
func zeusAutomation(client paho.Client, home *home.Home) {
const name = "living_room/zeus"
on(client, fmt.Sprintf("automation/appliance/%s", name), func(message struct{Activate bool `json:"activate"`}) {
computer, err := device.GetDevice[device.Activate](&home.Devices, name)
if err != nil {
log.Println(err)
return
}
computer.Activate(message.Activate)
})
}

View File

@ -13,6 +13,10 @@ type OnOff interface {
GetOnOff() bool GetOnOff() bool
} }
type Activate interface {
Activate(state bool)
}
func GetDevices[K any](devices *map[InternalName]Basic) map[InternalName]K { func GetDevices[K any](devices *map[InternalName]Basic) map[InternalName]K {
devs := make(map[InternalName]K) devs := make(map[InternalName]K)

View File

@ -20,8 +20,17 @@ func NewComputer(macAddress string, name device.InternalName, url string) *compu
return c return c
} }
// device.Basic
var _ device.Basic = (*computer)(nil)
func (c *computer) GetID() device.InternalName {
return device.InternalName(c.name)
}
// device.Activate
var _ device.Activate = (*computer)(nil)
func (c *computer) Activate(state bool) { func (c *computer) Activate(state bool) {
if state { if state {
log.Printf("Starting %s\n", c.name)
_, err := http.Get(c.url) _, err := http.Get(c.url)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
@ -31,12 +40,6 @@ func (c *computer) Activate(state bool) {
} }
} }
// device.Basic
var _ device.Basic = (*computer)(nil)
func (c *computer) GetID() device.InternalName {
return device.InternalName(c.name)
}
// google.DeviceInterface // google.DeviceInterface
var _ google.DeviceInterface = (*computer)(nil) var _ google.DeviceInterface = (*computer)(nil)
func (*computer) IsGoogleDevice() {} func (*computer) IsGoogleDevice() {}