2020-02-24 03:01:12 +00:00
|
|
|
package lifx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-02-29 03:56:33 +00:00
|
|
|
//"crypto/tls"
|
2020-02-24 03:01:12 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2020-02-25 06:41:29 +00:00
|
|
|
"io/ioutil"
|
2020-02-24 03:01:12 +00:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2020-02-29 03:56:33 +00:00
|
|
|
func NewClient(token string) *Client {
|
|
|
|
tr := &http.Transport{
|
|
|
|
//TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
|
|
|
|
}
|
|
|
|
return &Client{
|
2020-02-24 03:01:12 +00:00
|
|
|
token: token,
|
2020-02-29 03:56:33 +00:00
|
|
|
Client: &http.Client{Transport: tr},
|
2020-02-24 03:01:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-29 03:56:33 +00:00
|
|
|
func (s *Client) NewRequest(method, url string, body io.Reader) (req *http.Request, err error) {
|
2020-02-25 06:41:29 +00:00
|
|
|
req, err = http.NewRequest(method, url, body)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-02-24 03:01:12 +00:00
|
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", s.token))
|
2020-02-25 06:41:29 +00:00
|
|
|
return
|
2020-02-24 03:01:12 +00:00
|
|
|
}
|
|
|
|
|
2020-02-29 03:56:33 +00:00
|
|
|
func (s *Client) Request(method, url string, body io.Reader) ([]Result, error) {
|
2020-02-25 06:41:29 +00:00
|
|
|
req, err := s.NewRequest(method, url, body)
|
|
|
|
if err != nil {
|
2020-02-29 01:34:59 +00:00
|
|
|
return nil, err
|
2020-02-25 06:41:29 +00:00
|
|
|
}
|
|
|
|
|
2020-02-24 03:01:12 +00:00
|
|
|
resp, err := s.Client.Do(req)
|
|
|
|
if err != nil {
|
2020-02-29 01:34:59 +00:00
|
|
|
return nil, err
|
2020-02-24 03:01:12 +00:00
|
|
|
}
|
2020-02-25 06:41:29 +00:00
|
|
|
|
|
|
|
switch resp.StatusCode {
|
|
|
|
case http.StatusAccepted:
|
2020-02-29 01:34:59 +00:00
|
|
|
return nil, nil
|
2020-02-25 06:41:29 +00:00
|
|
|
case http.StatusMultiStatus:
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2020-02-29 01:34:59 +00:00
|
|
|
return nil, err
|
2020-02-25 06:41:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r := Results{}
|
|
|
|
err = json.Unmarshal(body, &r)
|
|
|
|
if err != nil {
|
2020-02-29 01:34:59 +00:00
|
|
|
return nil, err
|
2020-02-25 06:41:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return r.Results, nil
|
|
|
|
}
|
2020-02-29 01:34:59 +00:00
|
|
|
return nil, nil
|
2020-02-25 06:41:29 +00:00
|
|
|
}
|
|
|
|
|
2020-02-29 06:07:32 +00:00
|
|
|
func (s *Client) SetState(selector string, state State) ([]Result, error) {
|
2020-02-25 06:41:29 +00:00
|
|
|
j, err := json.Marshal(state)
|
|
|
|
if err != nil {
|
2020-02-29 01:34:59 +00:00
|
|
|
return nil, err
|
2020-02-25 06:41:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res, err := s.Request("PUT", EndpointState(selector), bytes.NewBuffer(j))
|
|
|
|
if err != nil {
|
2020-02-29 01:34:59 +00:00
|
|
|
return nil, err
|
2020-02-25 06:41:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2020-02-29 03:56:33 +00:00
|
|
|
func (s *Client) Toggle(selector string, duration float64) ([]Result, error) {
|
2020-02-29 04:14:55 +00:00
|
|
|
j, err := json.Marshal(&Toggle{Duration: duration})
|
2020-02-29 01:34:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := s.Request("POST", EndpointToggle(selector), bytes.NewBuffer(j))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2020-02-29 03:56:33 +00:00
|
|
|
func (s *Client) PowerOff(selector string) ([]Result, error) {
|
2020-02-29 06:07:32 +00:00
|
|
|
return s.SetState(selector, State{Power: "off"})
|
2020-02-29 01:34:59 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-02-29 03:56:33 +00:00
|
|
|
func (s *Client) FastPowerOff(selector string) {
|
2020-02-29 06:07:32 +00:00
|
|
|
s.SetState(selector, State{Power: "off", Fast: true})
|
2020-02-29 01:34:59 +00:00
|
|
|
}
|
|
|
|
|
2020-02-29 03:56:33 +00:00
|
|
|
func (s *Client) PowerOn(selector string) ([]Result, error) {
|
2020-02-29 06:07:32 +00:00
|
|
|
return s.SetState(selector, State{Power: "on"})
|
2020-02-29 01:34:59 +00:00
|
|
|
}
|
|
|
|
|
2020-02-29 03:56:33 +00:00
|
|
|
func (s *Client) FastPowerOn(selector string) {
|
2020-02-29 06:07:32 +00:00
|
|
|
s.SetState(selector, State{Power: "on", Fast: true})
|
2020-02-24 03:01:12 +00:00
|
|
|
}
|