Compare commits

...

6 Commits

3 changed files with 65 additions and 36 deletions

View File

@ -18,15 +18,27 @@ 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{
@ -75,7 +87,11 @@ func (c *Client) Request(method, url string, body io.Reader) (*http.Response, er
} }
switch resp.StatusCode { switch resp.StatusCode {
case http.StatusOK, http.StatusAccepted, http.StatusMultiStatus: case http.StatusOK:
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,omitempty"` H float32 `json:"hue"`
S float32 `json:"saturation,omitempty"` S float32 `json:"saturation"`
B float32 `json:"brightness,omitempty"` B float32 `json:"brightness"`
K int16 `json:"kelvin,omitempty"` K int16 `json:"kelvin"`
} }
NamedColor string NamedColor string
) )
var ( var (
Candlelight = HSBKColor{H: 0, S: 0, B: -1, K: 1500} Candlelight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 1500} }
Sunset = HSBKColor{H: 0, S: 0, B: -1, K: 2000} Sunset = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 2000} }
UltraWarm = HSBKColor{H: 0, S: 0, B: -1, K: 2500} UltraWarm = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 2500} }
Incandescent = HSBKColor{H: 0, S: 0, B: -1, K: 2700} Incandescent = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 2700} }
Warm = HSBKColor{H: 0, S: 0, B: -1, K: 3000} Warm = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 3000} }
Cool = HSBKColor{H: 0, S: 0, B: -1, K: 4000} Cool = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 4000} }
CoolDaylight = HSBKColor{H: 0, S: 0, B: -1, K: 4500} CoolDaylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 4500} }
SoftDaylight = HSBKColor{H: 0, S: 0, B: -1, K: 5000} SoftDaylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 5000} }
Daylight = HSBKColor{H: 0, S: 0, B: -1, K: 5600} Daylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 5600} }
NoonDaylight = HSBKColor{H: 0, S: 0, B: -1, K: 6000} NoonDaylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 6000} }
BrightDaylight = HSBKColor{H: 0, S: 0, B: -1, K: 6500} BrightDaylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 6500} }
CloudDaylight = HSBKColor{H: 0, S: 0, B: -1, K: 7000} CloudDaylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 7000} }
BlueDaylight = HSBKColor{H: 0, S: 0, B: -1, K: 7500} BlueDaylight = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 7500} }
BlueOvercast = HSBKColor{H: 0, S: 0, B: -1, K: 8000} BlueOvercast = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, K: 8000} }
BlueIce = HSBKColor{H: 0, S: 0, B: -1, K: 9000} BlueIce = func() *HSBKColor { return &HSBKColor{H: 0, S: 0, 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:%f", c.H)) s = append(s, fmt.Sprintf("hue:%g", 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,6 +4,8 @@ import (
"bytes" "bytes"
//"crypto/tls" //"crypto/tls"
"encoding/json" "encoding/json"
"log"
"net/http"
) )
const ( const (
@ -44,23 +46,33 @@ 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) {
j, err := json.Marshal(state) var (
if err != nil { 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 return nil, err
} }
resp, err := c.Request("PUT", EndpointState(selector), bytes.NewBuffer(j)) if req, err = c.NewRequest("PUT", EndpointState(selector), bytes.NewBuffer(j)); err != nil {
if err != nil { log.Println(err)
return nil, err return nil, err
} }
if state.Fast { if resp, err = c.Client.Do(req); err != nil {
return nil, nil log.Println(err)
return nil, err
} }
s := &Results{} defer resp.Body.Close()
err = c.UnmarshalResponse(resp, s)
if err != nil { if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
log.Println(err)
return nil, err return nil, err
} }
@ -83,7 +95,7 @@ func (c *Client) SetStates(states States) ([]Result, error) {
return nil, err return nil, err
} }
s := &Results{} s := &Response{}
err = c.UnmarshalResponse(resp, s) err = c.UnmarshalResponse(resp, s)
if err != nil { if err != nil {
return nil, err 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) { 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
} }
@ -103,7 +116,7 @@ func (c *Client) Toggle(selector string, duration float64) ([]Result, error) {
return nil, err return nil, err
} }
s := &Results{} s := &Response{}
err = c.UnmarshalResponse(resp, s) err = c.UnmarshalResponse(resp, s)
if err != nil { if err != nil {
return nil, err return nil, err