separete light API

This commit is contained in:
Ryan Cavicchioni 2020-02-29 15:56:03 -06:00
parent 821bd26595
commit 1c99d42224
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
3 changed files with 98 additions and 89 deletions

View File

@ -1,7 +1,6 @@
package lifx package lifx
import ( import (
"bytes"
//"crypto/tls" //"crypto/tls"
"encoding/json" "encoding/json"
"errors" "errors"
@ -79,68 +78,3 @@ func (c *Client) Request(method, url string, body io.Reader) ([]Result, error) {
return nil, nil return nil, nil
} }
func (c *Client) SetState(selector string, state State) ([]Result, error) {
j, err := json.Marshal(state)
if err != nil {
return nil, err
}
fmt.Println(string(j))
res, err := c.Request("PUT", EndpointState(selector), bytes.NewBuffer(j))
if err != nil {
return nil, err
}
return res, nil
}
func (c *Client) FastSetState(selector string, state State) ([]Result, error) {
state.Fast = true
return c.SetState(selector, state)
}
func (c *Client) SetStates(states States) ([]Result, error) {
j, err := json.Marshal(states)
if err != nil {
return nil, err
}
res, err := c.Request("PUT", EndpointStates(), bytes.NewBuffer(j))
if err != nil {
return nil, err
}
return res, nil
}
func (c *Client) Toggle(selector string, duration float64) ([]Result, error) {
j, err := json.Marshal(&Toggle{Duration: duration})
if err != nil {
return nil, err
}
res, err := c.Request("POST", EndpointToggle(selector), bytes.NewBuffer(j))
if err != nil {
return nil, err
}
return res, nil
}
func (c *Client) PowerOff(selector string) ([]Result, error) {
return c.SetState(selector, State{Power: "off"})
}
func (c *Client) FastPowerOff(selector string) {
c.SetState(selector, State{Power: "off", Fast: true})
}
func (c *Client) PowerOn(selector string) ([]Result, error) {
return c.SetState(selector, State{Power: "on"})
}
func (c *Client) FastPowerOn(selector string) {
c.SetState(selector, State{Power: "on", Fast: true})
}

98
lights.go Normal file
View File

@ -0,0 +1,98 @@
package lifx
import (
"bytes"
//"crypto/tls"
"encoding/json"
"fmt"
)
type (
State struct {
Power string `json:"power,omitempty"`
Color Color `json:"color,omitempty"`
Brightness float64 `json:"brightness,omitempty"`
Duration float64 `json:"duration,omitempty"`
Infrared float64 `json:"infrared,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`
}
Toggle struct {
Duration float64 `json:"duration,omitempty"`
}
)
func (c *Client) SetState(selector string, state State) ([]Result, error) {
j, err := json.Marshal(state)
if err != nil {
return nil, err
}
fmt.Println(string(j))
res, err := c.Request("PUT", EndpointState(selector), bytes.NewBuffer(j))
if err != nil {
return nil, err
}
return res, nil
}
func (c *Client) FastSetState(selector string, state State) ([]Result, error) {
state.Fast = true
return c.SetState(selector, state)
}
func (c *Client) SetStates(states States) ([]Result, error) {
j, err := json.Marshal(states)
if err != nil {
return nil, err
}
res, err := c.Request("PUT", EndpointStates(), bytes.NewBuffer(j))
if err != nil {
return nil, err
}
return res, nil
}
func (c *Client) Toggle(selector string, duration float64) ([]Result, error) {
j, err := json.Marshal(&Toggle{Duration: duration})
if err != nil {
return nil, err
}
res, err := c.Request("POST", EndpointToggle(selector), bytes.NewBuffer(j))
if err != nil {
return nil, err
}
return res, nil
}
func (c *Client) PowerOff(selector string) ([]Result, error) {
return c.SetState(selector, State{Power: "off"})
}
func (c *Client) FastPowerOff(selector string) {
c.SetState(selector, State{Power: "off", Fast: true})
}
func (c *Client) PowerOn(selector string) ([]Result, error) {
return c.SetState(selector, State{Power: "on"})
}
func (c *Client) FastPowerOn(selector string) {
c.SetState(selector, State{Power: "on", Fast: true})
}

View File

@ -15,25 +15,6 @@ type (
type ( type (
Status string Status string
State struct {
Power string `json:"power,omitempty"`
Color Color `json:"color,omitempty"`
Brightness float64 `json:"brightness,omitempty"`
Duration float64 `json:"duration,omitempty"`
Infrared float64 `json:"infrared,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 {
accessToken string accessToken string
Client *http.Client Client *http.Client
@ -49,10 +30,6 @@ type (
Status Status `json:"status"` Status Status `json:"status"`
} }
Toggle struct {
Duration float64 `json:"duration,omitempty"`
}
RGBColor struct { RGBColor struct {
R, G, B uint8 R, G, B uint8
} }