This repository has been archived on 2023-08-29. You can view files and clone it, but cannot push or open issues or pull requests.
automation/integration/wol/computer.go
Dreaded_X d42afecd67
All checks were successful
continuous-integration/drone/push Build is passing
Added option to activate computer through mqtt
2022-12-03 02:15:06 +01:00

88 lines
1.8 KiB
Go

package wol
import (
"automation/device"
"automation/integration/google"
"log"
"net/http"
"strings"
)
type computer struct {
macAddress string
name device.InternalName
url string
}
func NewComputer(macAddress string, name device.InternalName, url string) *computer {
c := &computer{macAddress, name, url}
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) {
if state {
log.Printf("Starting %s\n", c.name)
_, err := http.Get(c.url)
if err != nil {
log.Println(err)
}
} else {
// This is not implemented
}
}
// google.DeviceInterface
var _ google.DeviceInterface = (*computer)(nil)
func (*computer) IsGoogleDevice() {}
// google.DeviceInterface
func (c *computer) Sync() *google.Device {
device := google.NewDevice(c.GetID().String(), google.TypeScene)
device.AddSceneTrait(false)
device.Name = google.DeviceName{
DefaultNames: []string{
"Computer",
},
Name: strings.Title(c.GetID().Name()),
}
room := strings.Title(c.GetID().Room())
if len(room) > 1 {
device.RoomHint = room
}
return device
}
// google.DeviceInterface
func (c *computer) Query() google.DeviceState {
state := google.NewDeviceState(true)
state.Status = google.StatusSuccess
return state
}
// google.DeviceInterface
func (c *computer) Execute(execution google.Execution, updateState *google.DeviceState) (string, bool) {
errCode := ""
switch execution.Name {
case google.CommandActivateScene:
c.Activate(!execution.ActivateScene.Deactivate)
default:
errCode = "actionNotAvailable"
log.Printf("Command (%s) not supported\n", execution.Name)
}
return errCode, true
}