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/device/computer.go
Dreaded_X 01b2d492ba
All checks were successful
continuous-integration/drone/push Build is passing
All devices are now stored in a central map, part of large refactor
2022-11-18 21:12:09 +01:00

73 lines
1.3 KiB
Go

package device
import (
"automation/integration/google"
"log"
"net/http"
)
type computer struct {
macAddress string
name string
room string
url string
}
func NewComputer(macAddress string, name string, room string, url string) *computer {
c := &computer{macAddress: macAddress, name: name, room: room}
return c
}
func (c *computer) Sync() *google.Device {
device := google.NewDevice(c.GetID(), google.TypeScene)
device.AddSceneTrait(false)
device.Name = google.DeviceName{
DefaultNames: []string{
"Computer",
},
Name: c.name,
}
device.RoomHint = c.room
return device
}
func (c *computer) Query() google.DeviceState {
state := google.NewDeviceState(true)
state.Status = google.StatusSuccess
return state
}
func (c *computer) Execute(execution google.Execution, updateState *google.DeviceState) (string, bool) {
errCode := ""
switch execution.Name {
case google.CommandActivateScene:
c.SetState(!execution.ActivateScene.Deactivate)
default:
errCode = "actionNotAvailable"
log.Printf("Command (%s) not supported\n", execution.Name)
}
return errCode, true
}
func (c *computer) GetID() string {
return c.macAddress
}
func (c *computer) GetName() string {
return c.name
}
func (c *computer) SetState(state bool) {
if state {
http.Get(c.url)
} else {
// Scene does not implement this
}
}