47 lines
734 B
Go
47 lines
734 B
Go
package ha
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
|
|
"gitnet.fr/deblan/ha-rgb-screen/internal/color"
|
|
"gitnet.fr/deblan/ha-rgb-screen/internal/config"
|
|
)
|
|
|
|
type Client struct {
|
|
params *config.Config
|
|
client *http.Client
|
|
}
|
|
|
|
func NewClient(params *config.Config) *Client {
|
|
return &Client{
|
|
params: params,
|
|
client: new(http.Client),
|
|
}
|
|
}
|
|
|
|
func (c *Client) Update(rgb color.RGB) error {
|
|
data := fmt.Sprintf(`{"rgb": [%.0f, %.0f, %.0f]}`, rgb.R, rgb.G, rgb.B)
|
|
|
|
if c.params.Verbose {
|
|
log.Println(data)
|
|
}
|
|
|
|
req, err := http.NewRequest(
|
|
"POST",
|
|
c.params.Url,
|
|
bytes.NewBuffer([]byte(data)),
|
|
)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
_, err = c.client.Do(req)
|
|
|
|
return err
|
|
}
|