2020-02-29 21:56:03 +00:00
|
|
|
package lifx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
//"crypto/tls"
|
|
|
|
"encoding/json"
|
2020-03-06 07:28:39 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
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-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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-29 22:01:46 +00:00
|
|
|
func (s Status) Success() bool {
|
|
|
|
return s == OK
|
|
|
|
}
|
|
|
|
|
2020-02-29 21:56:03 +00:00
|
|
|
func (c *Client) SetState(selector string, state State) ([]Result, error) {
|
2020-03-06 07:28:39 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
s *Response
|
|
|
|
j []byte
|
|
|
|
req *http.Request
|
|
|
|
resp *http.Response
|
|
|
|
)
|
|
|
|
|
|
|
|
if j, err = json.Marshal(state); err != nil {
|
|
|
|
log.Println(err)
|
2020-02-29 21:56:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-06 07:28:39 +00:00
|
|
|
if req, err = c.NewRequest("PUT", EndpointState(selector), bytes.NewBuffer(j)); err != nil {
|
|
|
|
log.Println(err)
|
2020-03-01 05:15:39 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-29 21:56:03 +00:00
|
|
|
|
2020-03-06 07:28:39 +00:00
|
|
|
if resp, err = c.Client.Do(req); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return nil, err
|
2020-03-01 05:15:39 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
log.Println(err)
|
2020-02-29 21:56:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-01 05:15:39 +00:00
|
|
|
return s.Results, nil
|
2020-02-29 21:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) FastSetState(selector string, state State) ([]Result, error) {
|
|
|
|
state.Fast = true
|
|
|
|
return c.SetState(selector, state)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) SetStates(states States) ([]Result, error) {
|
|
|
|
j, err := json.Marshal(states)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-01 05:15:39 +00:00
|
|
|
resp, err := c.Request("PUT", EndpointStates(), bytes.NewBuffer(j))
|
2020-02-29 21:56:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-06 07:28:39 +00:00
|
|
|
s := &Response{}
|
2020-03-01 05:15:39 +00:00
|
|
|
err = c.UnmarshalResponse(resp, s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.Results, nil
|
2020-02-29 21:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Toggle(selector string, duration float64) ([]Result, error) {
|
|
|
|
j, err := json.Marshal(&Toggle{Duration: duration})
|
|
|
|
if err != nil {
|
2020-03-06 07:28:39 +00:00
|
|
|
log.Println(err)
|
2020-02-29 21:56:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-01 05:15:39 +00:00
|
|
|
resp, err := c.Request("POST", EndpointToggle(selector), bytes.NewBuffer(j))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-06 07:28:39 +00:00
|
|
|
s := &Response{}
|
2020-03-01 05:15:39 +00:00
|
|
|
err = c.UnmarshalResponse(resp, s)
|
2020-02-29 21:56:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-01 05:15:39 +00:00
|
|
|
return s.Results, nil
|
2020-02-29 21:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) PowerOff(selector string) ([]Result, error) {
|
|
|
|
return c.SetState(selector, State{Power: "off"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) FastPowerOff(selector string) {
|
|
|
|
c.SetState(selector, State{Power: "off", Fast: true})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) PowerOn(selector string) ([]Result, error) {
|
|
|
|
return c.SetState(selector, State{Power: "on"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) FastPowerOn(selector string) {
|
|
|
|
c.SetState(selector, State{Power: "on", Fast: true})
|
|
|
|
}
|