From 6128b7c0c3a10282e03f352dae93cdcc48091b8c Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Thu, 18 Mar 2021 23:24:04 -0500 Subject: [PATCH] Add breathe effect --- client.go | 29 +++++++++++++++++++++++++++++ endpoints.go | 3 +++ go.mod | 3 +++ lights.go | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 go.mod diff --git a/client.go b/client.go index 49743a2..e6878f5 100644 --- a/client.go +++ b/client.go @@ -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 diff --git a/endpoints.go b/endpoints.go index 6db0c5b..55ecd51 100644 --- a/endpoints.go +++ b/endpoints.go @@ -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)) + } ) diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e020976 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.kill0.net/chill9/lifx-go + +go 1.16 diff --git a/lights.go b/lights.go index 6137cd2..bce546f 100644 --- a/lights.go +++ b/lights.go @@ -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 +} \ No newline at end of file