Basic functionality working
Some checks failed
Deploy and Push Docker Image / BuildAndPush (push) Failing after 14s
Some checks failed
Deploy and Push Docker Image / BuildAndPush (push) Failing after 14s
This commit is contained in:
57
internal/olc/office_light_client.go
Normal file
57
internal/olc/office_light_client.go
Normal file
@ -0,0 +1,57 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user