58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
package olc
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"time"
|
|
)
|
|
|
|
type OfficeLightClient struct {
|
|
BaseURL string
|
|
Client *http.Client
|
|
}
|
|
|
|
func NewClient(baseURL string) *OfficeLightClient {
|
|
return &OfficeLightClient{
|
|
BaseURL: baseURL,
|
|
Client: &http.Client{
|
|
Timeout: 10 * time.Second,
|
|
},
|
|
}
|
|
}
|
|
|
|
func (olc *OfficeLightClient) SetLightState(state bool) error {
|
|
endpoint := fmt.Sprintf("%s/state", olc.BaseURL)
|
|
stateString := "off"
|
|
if state {
|
|
stateString = "on"
|
|
}
|
|
|
|
params := url.Values{}
|
|
params.Set("state", stateString)
|
|
|
|
reqURL := fmt.Sprintf("%s?%s", endpoint, params.Encode())
|
|
resp, err := olc.Client.Post(reqURL, "application/x-www-form-urlencoded", nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
return nil
|
|
}
|
|
|
|
func (olc *OfficeLightClient) SetColor(r, g, b uint8) error {
|
|
endpoint := fmt.Sprintf("%s/color", olc.BaseURL)
|
|
params := url.Values{}
|
|
params.Set("r", fmt.Sprintf("%d", r))
|
|
params.Set("g", fmt.Sprintf("%d", g))
|
|
params.Set("b", fmt.Sprintf("%d", b))
|
|
|
|
reqURL := fmt.Sprintf("%s?%s", endpoint, params.Encode())
|
|
resp, err := olc.Client.Post(reqURL, "application/x-www-form-urlencoded", nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
return nil
|
|
}
|