Compare commits

...

6 Commits

3 changed files with 65 additions and 36 deletions

View File

@ -18,15 +18,27 @@ type (
Client *http.Client
}
Results struct {
Results []Result `json:results`
}
Result struct {
ID string `json:"id"`
Label string `json:"label"`
Status Status `json:"status"`
}
Error struct {
Field string `json:"field"`
Message []string `json:"message"`
}
Warning struct {
Warning string `json:"warning"`
}
Response struct {
Error string `json:"error"`
Errors []Error `json:"errors"`
Warnings []Warning `json:"warnings"`
Results []Result `json:"results"`
}
)
var errorMap = map[int]error{
@ -75,7 +87,11 @@ func (c *Client) Request(method, url string, body io.Reader) (*http.Response, er
}
switch resp.StatusCode {
case http.StatusOK, http.StatusAccepted, http.StatusMultiStatus:
case http.StatusOK:
fallthrough
case http.StatusAccepted:
fallthrough
case http.StatusMultiStatus:
return resp, nil
}

View File

@ -17,31 +17,31 @@ type (
}
HSBKColor struct {
H float32 `json:"hue,omitempty"`
S float32 `json:"saturation,omitempty"`
B float32 `json:"brightness,omitempty"`
K int16 `json:"kelvin,omitempty"`
H float32 `json:"hue"`
S float32 `json:"saturation"`
B float32 `json:"brightness"`
K int16 `json:"kelvin"`
}
NamedColor string
)
var (
Candlelight = HSBKColor{H: 0, S: 0, B: -1, K: 1500}
Sunset = HSBKColor{H: 0, S: 0, B: -1, K: 2000}
UltraWarm = HSBKColor{H: 0, S: 0, B: -1, K: 2500}
Incandescent = HSBKColor{H: 0, S: 0, B: -1, K: 2700}
Warm = HSBKColor{H: 0, S: 0, B: -1, K: 3000}
Cool = HSBKColor{H: 0, S: 0, B: -1, K: 4000}
CoolDaylight = HSBKColor{H: 0, S: 0, B: -1, K: 4500}
SoftDaylight = HSBKColor{H: 0, S: 0, B: -1, K: 5000}
Daylight = HSBKColor{H: 0, S: 0, B: -1, K: 5600}
NoonDaylight = HSBKColor{H: 0, S: 0, B: -1, K: 6000}
BrightDaylight = HSBKColor{H: 0, S: 0, B: -1, K: 6500}
CloudDaylight = HSBKColor{H: 0, S: 0, B: -1, K: 7000}
BlueDaylight = HSBKColor{H: 0, S: 0, B: -1, K: 7500}
BlueOvercast = HSBKColor{H: 0, S: 0, B: -1, K: 8000}
BlueIce = HSBKColor{H: 0, S: 0, B: -1, K: 9000}
Candlelight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 1500} }
Sunset = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 2000} }
UltraWarm = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 2500} }
Incandescent = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 2700} }
Warm = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 3000} }
Cool = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 4000} }
CoolDaylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 4500} }
SoftDaylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 5000} }
Daylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 5600} }
NoonDaylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 6000} }
BrightDaylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 6500} }
CloudDaylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 7000} }
BlueDaylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 7500} }
BlueOvercast = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 8000} }
BlueIce = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 9000} }
)
func NewRGBColor(r, g, b uint8) (*RGBColor, error) {
@ -65,7 +65,7 @@ func (c RGBColor) Hex() string {
func (c HSBKColor) ColorString() string {
var s []string
if c.H >= 0 {
s = append(s, fmt.Sprintf("hue:%f", c.H))
s = append(s, fmt.Sprintf("hue:%g", c.H))
}
if c.S >= 0 {
s = append(s, fmt.Sprintf("saturation:%g", c.S))

View File

@ -4,6 +4,8 @@ import (
"bytes"
//"crypto/tls"
"encoding/json"
"log"
"net/http"
)
const (
@ -44,23 +46,33 @@ func (s Status) Success() bool {
}
func (c *Client) SetState(selector string, state State) ([]Result, error) {
j, err := json.Marshal(state)
if err != nil {
var (
err error
s *Response
j []byte
req *http.Request
resp *http.Response
)
if j, err = json.Marshal(state); err != nil {
log.Println(err)
return nil, err
}
resp, err := c.Request("PUT", EndpointState(selector), bytes.NewBuffer(j))
if err != nil {
if req, err = c.NewRequest("PUT", EndpointState(selector), bytes.NewBuffer(j)); err != nil {
log.Println(err)
return nil, err
}
if state.Fast {
return nil, nil
if resp, err = c.Client.Do(req); err != nil {
log.Println(err)
return nil, err
}
s := &Results{}
err = c.UnmarshalResponse(resp, s)
if err != nil {
defer resp.Body.Close()
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
log.Println(err)
return nil, err
}
@ -83,7 +95,7 @@ func (c *Client) SetStates(states States) ([]Result, error) {
return nil, err
}
s := &Results{}
s := &Response{}
err = c.UnmarshalResponse(resp, s)
if err != nil {
return nil, err
@ -95,6 +107,7 @@ func (c *Client) SetStates(states States) ([]Result, error) {
func (c *Client) Toggle(selector string, duration float64) ([]Result, error) {
j, err := json.Marshal(&Toggle{Duration: duration})
if err != nil {
log.Println(err)
return nil, err
}
@ -103,7 +116,7 @@ func (c *Client) Toggle(selector string, duration float64) ([]Result, error) {
return nil, err
}
s := &Results{}
s := &Response{}
err = c.UnmarshalResponse(resp, s)
if err != nil {
return nil, err