Compare commits

..

2 Commits

Author SHA1 Message Date
2fbfe9cae8
add SetState method 2020-02-29 00:08:39 -06:00
cd04dd47b6
remove unnecessary pointers 2020-02-29 00:07:32 -06:00
3 changed files with 32 additions and 5 deletions

View File

@ -60,7 +60,7 @@ func (s *Client) Request(method, url string, body io.Reader) ([]Result, error) {
return nil, nil return nil, nil
} }
func (s *Client) SetState(selector string, state *State) ([]Result, error) { func (s *Client) SetState(selector string, state State) ([]Result, error) {
j, err := json.Marshal(state) j, err := json.Marshal(state)
if err != nil { if err != nil {
return nil, err return nil, err
@ -74,6 +74,20 @@ func (s *Client) SetState(selector string, state *State) ([]Result, error) {
return res, nil return res, nil
} }
func (s *Client) SetStates(states States) ([]Result, error) {
j, err := json.Marshal(states)
if err != nil {
return nil, err
}
res, err := s.Request("PUT", EndpointStates(), bytes.NewBuffer(j))
if err != nil {
return nil, err
}
return res, nil
}
func (s *Client) Toggle(selector string, duration float64) ([]Result, error) { func (s *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 {
@ -89,18 +103,18 @@ func (s *Client) Toggle(selector string, duration float64) ([]Result, error) {
} }
func (s *Client) PowerOff(selector string) ([]Result, error) { func (s *Client) PowerOff(selector string) ([]Result, error) {
return s.SetState(selector, &State{Power: "off"}) return s.SetState(selector, State{Power: "off"})
} }
func (s *Client) FastPowerOff(selector string) { func (s *Client) FastPowerOff(selector string) {
s.SetState(selector, &State{Power: "off", Fast: true}) s.SetState(selector, State{Power: "off", Fast: true})
} }
func (s *Client) PowerOn(selector string) ([]Result, error) { func (s *Client) PowerOn(selector string) ([]Result, error) {
return s.SetState(selector, &State{Power: "on"}) return s.SetState(selector, State{Power: "on"})
} }
func (s *Client) FastPowerOn(selector string) { func (s *Client) FastPowerOn(selector string) {
s.SetState(selector, &State{Power: "on", Fast: true}) s.SetState(selector, State{Power: "on", Fast: true})
} }

View File

@ -17,6 +17,9 @@ var (
EndpointState = func(selector string) string { EndpointState = func(selector string) string {
return BuildURL(Endpoint, fmt.Sprintf("/lights/%s/state", selector)) return BuildURL(Endpoint, fmt.Sprintf("/lights/%s/state", selector))
} }
EndpointStates = func() string {
return BuildURL(Endpoint, "/lights/states")
}
EndpointToggle = func(selector string) string { EndpointToggle = func(selector string) string {
return BuildURL(Endpoint, fmt.Sprintf("/lights/%s/toggle", selector)) return BuildURL(Endpoint, fmt.Sprintf("/lights/%s/toggle", selector))
} }

View File

@ -18,6 +18,16 @@ type (
Fast bool `json:"fast,omitempty"` Fast bool `json:"fast,omitempty"`
} }
StateWithSelector struct {
State
Selector string `json:"selector"`
}
States struct {
States []StateWithSelector `json:"states",omitempty`
Defaults State `json:"defaults",omitempty`
}
Client struct { Client struct {
token string token string
Client *http.Client Client *http.Client