Compare commits

..

No commits in common. "243d0f6f312b8ffce8666c0782f8b025c7b20477" and "35a5b99bf422a58d7f933804b8d12f270b016eea" have entirely different histories.

3 changed files with 36 additions and 65 deletions

View File

@ -18,27 +18,15 @@ type (
Client *http.Client Client *http.Client
} }
Results struct {
Results []Result `json:results`
}
Result struct { Result struct {
ID string `json:"id"` ID string `json:"id"`
Label string `json:"label"` Label string `json:"label"`
Status Status `json:"status"` 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{ var errorMap = map[int]error{
@ -87,11 +75,7 @@ func (c *Client) Request(method, url string, body io.Reader) (*http.Response, er
} }
switch resp.StatusCode { switch resp.StatusCode {
case http.StatusOK: case http.StatusOK, http.StatusAccepted, http.StatusMultiStatus:
fallthrough
case http.StatusAccepted:
fallthrough
case http.StatusMultiStatus:
return resp, nil return resp, nil
} }

View File

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

View File

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