Basic functionality working
Some checks failed
Deploy and Push Docker Image / BuildAndPush (push) Failing after 14s

This commit is contained in:
Henri Burau
2025-06-19 13:57:36 +02:00
parent fac655242f
commit 64f955c4a1
8 changed files with 293 additions and 0 deletions

View 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
}