From 05566f480f02a3a6df6bcb463dc442828d3de552 Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Sat, 7 Mar 2020 20:02:37 -0600 Subject: [PATCH] add SetDelta function --- client.go | 23 +++++++++++++++++++++++ endpoints.go | 3 +++ lights.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/client.go b/client.go index f169426..9e591ac 100644 --- a/client.go +++ b/client.go @@ -185,3 +185,26 @@ func (c *Client) listLights(selector string) (*http.Response, error) { return resp, nil } + +func (c *Client) stateDelta(selector string, delta StateDelta) (*http.Response, error) { + var ( + err error + j []byte + req *http.Request + resp *http.Response + ) + + if j, err = json.Marshal(delta); err != nil { + return nil, err + } + + if req, err = c.NewRequest("POST", EndpointStateDelta(selector), bytes.NewBuffer(j)); err != nil { + return nil, err + } + + if resp, err = c.Client.Do(req); err != nil { + return nil, err + } + + return resp, nil +} diff --git a/endpoints.go b/endpoints.go index 9523332..6db0c5b 100644 --- a/endpoints.go +++ b/endpoints.go @@ -17,6 +17,9 @@ var ( EndpointState = func(selector string) string { return BuildURL(Endpoint, fmt.Sprintf("/lights/%s/state", selector)) } + EndpointStateDelta = func(selector string) string { + return BuildURL(Endpoint, fmt.Sprintf("/lights/%s/state/delta", selector)) + } EndpointListLights = func(selector string) string { return BuildURL(Endpoint, fmt.Sprintf("/lights/%s", selector)) } diff --git a/lights.go b/lights.go index aba4306..bd7e915 100644 --- a/lights.go +++ b/lights.go @@ -62,6 +62,16 @@ type ( Fast bool `json:"fast,omitempty"` } + StateDelta struct { + Power *string `json:"power,omitempty"` + Duration *float64 `json:"duration,omitempty"` + Infrared *float64 `json:"infrared,omitempty"` + Hue *float64 `json:"hue,omitempty"` + Saturation *float64 `json:"saturation,omitempty"` + Brightness *float64 `json:"brightness,omitempty"` + Kelvin *int `json:"kelvin,omitempty"` + } + StateWithSelector struct { State Selector string `json:"selector"` @@ -128,6 +138,25 @@ func (c *Client) SetStates(selector string, states States) (*Response, error) { return s, nil } +func (c *Client) StateDelta(selector string, delta StateDelta) (*Response, error) { + var ( + err error + s *Response + resp *http.Response + ) + + if resp, err = c.stateDelta(selector, delta); err != nil { + return nil, err + } + defer resp.Body.Close() + + if err = json.NewDecoder(resp.Body).Decode(&s); err != nil { + return nil, err + } + + return s, nil +} + func (c *Client) Toggle(selector string, duration float64) (*Response, error) { var ( err error