2020-02-29 21:56:03 +00:00
|
|
|
package lifx
|
|
|
|
|
|
|
|
import (
|
|
|
|
//"crypto/tls"
|
|
|
|
"encoding/json"
|
2020-03-27 01:52:11 +00:00
|
|
|
"fmt"
|
2020-03-06 07:28:39 +00:00
|
|
|
"net/http"
|
2020-03-24 05:26:22 +00:00
|
|
|
"time"
|
2020-02-29 21:56:03 +00:00
|
|
|
)
|
|
|
|
|
2020-02-29 22:01:46 +00:00
|
|
|
const (
|
|
|
|
OK Status = "ok"
|
|
|
|
TimedOut Status = "timed_out"
|
|
|
|
Offline Status = "offline"
|
|
|
|
)
|
|
|
|
|
2020-02-29 21:56:03 +00:00
|
|
|
type (
|
2020-02-29 21:58:20 +00:00
|
|
|
Status string
|
|
|
|
|
2020-03-07 05:00:21 +00:00
|
|
|
Selector struct {
|
2020-03-24 23:33:22 +00:00
|
|
|
Id string `json:"id"`
|
2020-03-07 05:00:21 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
Product struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Identifier string `json:"identifier"`
|
|
|
|
Company string `json:"company"`
|
|
|
|
Capabilities Capabilities `json:"capabilities"`
|
|
|
|
}
|
|
|
|
|
|
|
|
Capabilities struct {
|
|
|
|
HasColor bool `json:"has_color"`
|
|
|
|
HasVariableColorTemp bool `json:"has_variable_color_temp"`
|
|
|
|
HasIR bool `json:"has_ir"`
|
|
|
|
HasChain bool `json:"has_chain"`
|
|
|
|
HasMultizone bool `json:"has_multizone"`
|
|
|
|
MinKelvin float64 `json:"min_kelvin"`
|
|
|
|
MaxKelvin float64 `json:"max_kelvin"`
|
|
|
|
}
|
|
|
|
|
|
|
|
Light struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
UUID string `json:"uuid"`
|
|
|
|
Label string `json:"label"`
|
|
|
|
Connected bool `json:"connected"`
|
|
|
|
Power string `json:"power"`
|
|
|
|
Color HSBKColor `json:"color"`
|
|
|
|
Brightness float64 `json:"brightness"`
|
|
|
|
Effect string `json:"effect"`
|
|
|
|
Group Selector `json:"group"`
|
|
|
|
Location Selector `json:"location"`
|
|
|
|
Product Product `json:"product"`
|
2020-03-24 05:26:22 +00:00
|
|
|
LastSeen time.Time `json:"last_seen"`
|
2020-03-07 05:00:21 +00:00
|
|
|
SecondsLastSeen float64 `json:"seconds_last_seen"`
|
|
|
|
}
|
|
|
|
|
2020-02-29 21:56:03 +00:00
|
|
|
State struct {
|
|
|
|
Power string `json:"power,omitempty"`
|
|
|
|
Color Color `json:"color,omitempty"`
|
|
|
|
Brightness float64 `json:"brightness,omitempty"`
|
|
|
|
Duration float64 `json:"duration,omitempty"`
|
|
|
|
Infrared float64 `json:"infrared,omitempty"`
|
|
|
|
Fast bool `json:"fast,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-03-08 02:02:37 +00:00
|
|
|
StateDelta struct {
|
|
|
|
Power *string `json:"power,omitempty"`
|
|
|
|
Duration *float64 `json:"duration,omitempty"`
|
|
|
|
Infrared *float64 `json:"infrared,omitempty"`
|
|
|
|
Hue *float64 `json:"hue,omitempty"`
|
|
|
|
Saturation *float64 `json:"saturation,omitempty"`
|
|
|
|
Brightness *float64 `json:"brightness,omitempty"`
|
|
|
|
Kelvin *int `json:"kelvin,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-02-29 21:56:03 +00:00
|
|
|
StateWithSelector struct {
|
|
|
|
State
|
|
|
|
Selector string `json:"selector"`
|
|
|
|
}
|
|
|
|
|
|
|
|
States struct {
|
|
|
|
States []StateWithSelector `json:"states",omitempty`
|
|
|
|
Defaults State `json:"defaults",omitempty`
|
|
|
|
}
|
|
|
|
|
|
|
|
Toggle struct {
|
|
|
|
Duration float64 `json:"duration,omitempty"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-03-27 02:05:11 +00:00
|
|
|
func NewApiError(resp *http.Response) error {
|
2020-03-27 01:52:11 +00:00
|
|
|
var (
|
|
|
|
s *Response
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fmt.Errorf("fatal: %s", s.Error)
|
|
|
|
}
|
|
|
|
|
2020-03-27 02:05:11 +00:00
|
|
|
func IsApiError(resp *http.Response) bool {
|
|
|
|
return resp.StatusCode > 299
|
|
|
|
}
|
|
|
|
|
2020-02-29 22:01:46 +00:00
|
|
|
func (s Status) Success() bool {
|
|
|
|
return s == OK
|
|
|
|
}
|
|
|
|
|
2020-03-07 02:55:31 +00:00
|
|
|
func (c *Client) SetState(selector string, state State) (*Response, error) {
|
2020-03-06 07:28:39 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
s *Response
|
|
|
|
resp *http.Response
|
|
|
|
)
|
|
|
|
|
2020-03-08 02:06:22 +00:00
|
|
|
if resp, err = c.setState(selector, state); err != nil {
|
2020-02-29 21:56:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-06 07:28:39 +00:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2020-03-06 07:34:05 +00:00
|
|
|
if state.Fast && resp.StatusCode == http.StatusAccepted {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-03-06 07:28:39 +00:00
|
|
|
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
|
2020-02-29 21:56:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-07 02:55:31 +00:00
|
|
|
return s, nil
|
2020-02-29 21:56:03 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 02:55:31 +00:00
|
|
|
func (c *Client) FastSetState(selector string, state State) (*Response, error) {
|
2020-02-29 21:56:03 +00:00
|
|
|
state.Fast = true
|
|
|
|
return c.SetState(selector, state)
|
|
|
|
}
|
|
|
|
|
2020-03-07 02:55:31 +00:00
|
|
|
func (c *Client) SetStates(selector string, states States) (*Response, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
s *Response
|
|
|
|
resp *http.Response
|
|
|
|
)
|
2020-02-29 21:56:03 +00:00
|
|
|
|
2020-03-08 02:06:22 +00:00
|
|
|
if resp, err = c.setStates(selector, states); err != nil {
|
2020-02-29 21:56:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-07 02:55:31 +00:00
|
|
|
defer resp.Body.Close()
|
2020-02-29 21:56:03 +00:00
|
|
|
|
2020-03-07 02:55:31 +00:00
|
|
|
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
|
2020-03-01 05:15:39 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-07 02:55:31 +00:00
|
|
|
return s, nil
|
2020-02-29 21:56:03 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 02:02:37 +00:00
|
|
|
func (c *Client) StateDelta(selector string, delta StateDelta) (*Response, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
s *Response
|
|
|
|
resp *http.Response
|
|
|
|
)
|
|
|
|
|
|
|
|
if resp, err = c.stateDelta(selector, delta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2020-03-07 02:55:31 +00:00
|
|
|
func (c *Client) Toggle(selector string, duration float64) (*Response, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
s *Response
|
|
|
|
resp *http.Response
|
|
|
|
)
|
2020-02-29 21:56:03 +00:00
|
|
|
|
2020-03-08 02:06:22 +00:00
|
|
|
if resp, err = c.toggle(selector, duration); err != nil {
|
2020-03-01 05:15:39 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-07 02:55:31 +00:00
|
|
|
defer resp.Body.Close()
|
2020-03-01 05:15:39 +00:00
|
|
|
|
2020-03-27 02:05:11 +00:00
|
|
|
if IsApiError(resp) {
|
|
|
|
return nil, NewApiError(resp)
|
2020-03-27 01:52:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 02:55:31 +00:00
|
|
|
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
|
2020-02-29 21:56:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-07 02:55:31 +00:00
|
|
|
return s, nil
|
2020-02-29 21:56:03 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 05:00:21 +00:00
|
|
|
func (c *Client) ListLights(selector string) ([]Light, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
s []Light
|
|
|
|
resp *http.Response
|
|
|
|
)
|
|
|
|
|
|
|
|
if resp, err = c.listLights(selector); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2020-03-27 01:52:11 +00:00
|
|
|
if resp.StatusCode > 299 {
|
2020-03-27 02:05:11 +00:00
|
|
|
return nil, NewApiError(resp)
|
2020-03-27 01:52:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 05:00:21 +00:00
|
|
|
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2020-03-07 02:55:31 +00:00
|
|
|
func (c *Client) PowerOff(selector string) (*Response, error) {
|
2020-02-29 21:56:03 +00:00
|
|
|
return c.SetState(selector, State{Power: "off"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) FastPowerOff(selector string) {
|
|
|
|
c.SetState(selector, State{Power: "off", Fast: true})
|
|
|
|
}
|
|
|
|
|
2020-03-07 02:55:31 +00:00
|
|
|
func (c *Client) PowerOn(selector string) (*Response, error) {
|
2020-02-29 21:56:03 +00:00
|
|
|
return c.SetState(selector, State{Power: "on"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) FastPowerOn(selector string) {
|
|
|
|
c.SetState(selector, State{Power: "on", Fast: true})
|
|
|
|
}
|