Add breathe effect

This commit is contained in:
Ryan Cavicchioni 2021-03-18 23:24:04 -05:00
parent 36df906bbb
commit 6128b7c0c3
Signed by: chill9
GPG Key ID: 877EEDAF9245103D
4 changed files with 68 additions and 0 deletions

View File

@ -201,6 +201,35 @@ func (c *Client) setState(selector string, state State) (*Response, error) {
return resp, nil
}
func (c *Client) breathe(selector string, breathe Breathe) (*Response, error) {
var (
err error
j []byte
req *http.Request
r *http.Response
resp *Response
)
if j, err = json.Marshal(breathe); err != nil {
return nil, err
}
if req, err = c.NewRequest("POST", EndpointBreathe(selector), bytes.NewBuffer(j)); err != nil {
return nil, err
}
if r, err = c.Client.Do(req); err != nil {
return nil, err
}
resp, err = NewResponse(r)
if err != nil {
return nil, err
}
return resp, nil
}
func (c *Client) setStates(selector string, states States) (*Response, error) {
var (
err error

View File

@ -32,4 +32,7 @@ var (
EndpointToggle = func(selector string) string {
return BuildURL(Endpoint, fmt.Sprintf("/lights/%s/toggle", selector))
}
EndpointBreathe = func(selector string) string {
return BuildURL(Endpoint, fmt.Sprintf("/lights/%s/effect/breathe", selector))
}
)

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module git.kill0.net/chill9/lifx-go
go 1.16

View File

@ -86,6 +86,16 @@ type (
Toggle struct {
Duration float64 `json:"duration,omitempty"`
}
Breathe struct {
Color Color `json:"color,omitempty"`
FromColor Color `json:"from_color,omitempty"`
Period float32 `json:"period,omitempty"`
Cycles float32 `json:"cycles,omitempty"`
Persist bool `json:"persist,omitempty"`
PowerOn bool `json:"power_on,omitempty"`
Peak float32 `json:"peak,omitempty"`
}
)
func (c *Client) SetState(selector string, state State) (*LifxResponse, error) {
@ -219,3 +229,26 @@ func (c *Client) PowerOn(selector string) (*LifxResponse, error) {
func (c *Client) FastPowerOn(selector string) {
c.SetState(selector, State{Power: "on", Fast: true})
}
func (c *Client) Breathe(selector string, breathe Breathe) (*LifxResponse, error) {
var (
err error
s *LifxResponse
resp *Response
)
if resp, err = c.breathe(selector, breathe); err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.IsError() {
return nil, resp.GetLifxError()
}
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
return nil, err
}
return s, nil
}