Compare commits
9 Commits
1e97025bca
...
master
Author | SHA1 | Date | |
---|---|---|---|
15145d1f58
|
|||
c6b538380c
|
|||
4c1678b62c
|
|||
f79ea1df5d
|
|||
41b730d33d
|
|||
2107a05864
|
|||
dbe1c40e16
|
|||
1dcfb8a624
|
|||
52f610489f
|
@ -21,6 +21,7 @@ type (
|
|||||||
accessToken string
|
accessToken string
|
||||||
userAgent string
|
userAgent string
|
||||||
Client *http.Client
|
Client *http.Client
|
||||||
|
debug bool
|
||||||
}
|
}
|
||||||
|
|
||||||
Result struct {
|
Result struct {
|
||||||
@ -102,6 +103,12 @@ func WithUserAgent(userAgent string) func(*Client) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WithDebug(debug bool) func(*Client) {
|
||||||
|
return func(c *Client) {
|
||||||
|
c.debug = debug
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func NewClientWithUserAgent(accessToken string, userAgent string) *Client {
|
func NewClientWithUserAgent(accessToken string, userAgent string) *Client {
|
||||||
tr := &http.Transport{
|
tr := &http.Transport{
|
||||||
//TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
|
//TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
|
||||||
@ -230,7 +237,7 @@ func (c *Client) breathe(selector string, breathe Breathe) (*Response, error) {
|
|||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) setStates(selector string, states States) (*Response, error) {
|
func (c *Client) setStates(states States) (*Response, error) {
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
j []byte
|
j []byte
|
||||||
|
23
color.go
23
color.go
@ -4,7 +4,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -164,6 +163,10 @@ func (c RGBColor) ColorString() string {
|
|||||||
return fmt.Sprintf("rgb:%d,%d,%d", c.R, c.G, c.B)
|
return fmt.Sprintf("rgb:%d,%d,%d", c.R, c.G, c.B)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c RGBColor) String() string {
|
||||||
|
return c.ColorString()
|
||||||
|
}
|
||||||
|
|
||||||
func (c RGBColor) Hex() string {
|
func (c RGBColor) Hex() string {
|
||||||
return fmt.Sprintf("#%x%x%x", c.R, c.G, c.B)
|
return fmt.Sprintf("#%x%x%x", c.R, c.G, c.B)
|
||||||
}
|
}
|
||||||
@ -185,6 +188,10 @@ func (c HSBKColor) ColorString() string {
|
|||||||
return strings.Join(s, " ")
|
return strings.Join(s, " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c HSBKColor) String() string {
|
||||||
|
return c.ColorString()
|
||||||
|
}
|
||||||
|
|
||||||
func (c HSBKColor) MarshalText() ([]byte, error) {
|
func (c HSBKColor) MarshalText() ([]byte, error) {
|
||||||
return []byte(c.ColorString()), nil
|
return []byte(c.ColorString()), nil
|
||||||
}
|
}
|
||||||
@ -197,11 +204,14 @@ func (c NamedColor) ColorString() string {
|
|||||||
return string(c)
|
return string(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c NamedColor) String() string {
|
||||||
|
return c.ColorString()
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client) ValidateColor(color Color) (Color, error) {
|
func (c *Client) ValidateColor(color Color) (Color, error) {
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
s *HSBKColor
|
s *HSBKColor
|
||||||
r *http.Response
|
|
||||||
resp *Response
|
resp *Response
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -209,13 +219,12 @@ func (c *Client) ValidateColor(color Color) (Color, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err = NewResponse(r)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.IsError() {
|
||||||
|
return nil, resp.GetLifxError()
|
||||||
|
}
|
||||||
|
|
||||||
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
|
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -33,6 +33,6 @@ var (
|
|||||||
return BuildURL(Endpoint, fmt.Sprintf("/lights/%s/toggle", selector))
|
return BuildURL(Endpoint, fmt.Sprintf("/lights/%s/toggle", selector))
|
||||||
}
|
}
|
||||||
EndpointBreathe = func(selector string) string {
|
EndpointBreathe = func(selector string) string {
|
||||||
return BuildURL(Endpoint, fmt.Sprintf("/lights/%s/effect/breathe", selector))
|
return BuildURL(Endpoint, fmt.Sprintf("/lights/%s/effects/breathe", selector))
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
32
lights.go
32
lights.go
@ -3,6 +3,7 @@ package lifx
|
|||||||
import (
|
import (
|
||||||
//"crypto/tls"
|
//"crypto/tls"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -50,7 +51,7 @@ type (
|
|||||||
Group Selector `json:"group"`
|
Group Selector `json:"group"`
|
||||||
Location Selector `json:"location"`
|
Location Selector `json:"location"`
|
||||||
Product Product `json:"product"`
|
Product Product `json:"product"`
|
||||||
LastSeen time.Time `json:"last_seen"`
|
LastSeen *time.Time `json:"last_seen,omitempty"`
|
||||||
SecondsLastSeen float64 `json:"seconds_last_seen"`
|
SecondsLastSeen float64 `json:"seconds_last_seen"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,6 +99,31 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
DefaultBreatheCycles float64 = 1
|
||||||
|
DefaultBreathePeriod float64 = 1
|
||||||
|
DefaultBreathePersist bool = false
|
||||||
|
DefaultBreathePowerOn bool = true
|
||||||
|
DefaultBreathePeak float64 = 0.5
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewBreathe() Breathe {
|
||||||
|
var b Breathe
|
||||||
|
b.Period = DefaultBreathePeriod
|
||||||
|
b.Cycles = DefaultBreatheCycles
|
||||||
|
b.Persist = DefaultBreathePersist
|
||||||
|
b.PowerOn = DefaultBreathePowerOn
|
||||||
|
b.Peak = DefaultBreathePeak
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Breathe) Valid() error {
|
||||||
|
if b.Peak < 0 || b.Peak > 1 {
|
||||||
|
return errors.New("peak must be between 0.0 and 1.0")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client) SetState(selector string, state State) (*LifxResponse, error) {
|
func (c *Client) SetState(selector string, state State) (*LifxResponse, error) {
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
@ -130,14 +156,14 @@ func (c *Client) FastSetState(selector string, state State) (*LifxResponse, erro
|
|||||||
return c.SetState(selector, state)
|
return c.SetState(selector, state)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) SetStates(selector string, states States) (*LifxResponse, error) {
|
func (c *Client) SetStates(states States) (*LifxResponse, error) {
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
s *LifxResponse
|
s *LifxResponse
|
||||||
resp *Response
|
resp *Response
|
||||||
)
|
)
|
||||||
|
|
||||||
if resp, err = c.setStates(selector, states); err != nil {
|
if resp, err = c.setStates(states); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
Reference in New Issue
Block a user