- Zustand, Effekte (auch per Teil des Namens), Autowechsel, Zufall, Helligkeit, Regler Effekt 1/2, Favoriten, Lampe, Laufschrift, Wetter und Ort (Suche über Open-Meteo), Spezialeffekte - Manueller Modus: pixel, fuellen, bild (125 Farben aus Datei/stdin), zeigen (Pixelbild der Etagen in 24-Bit-Farben im Terminal) - Farben als #rrggbb oder deutsche Namen; -host/CUBE_HOST, -json - nur Standardbibliothek, nur IPv4 (cube.local ohne 5 s Wartezeit) - make cli baut cli/cube Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
226 lines
5.9 KiB
Go
226 lines
5.9 KiB
Go
package main
|
|
|
|
// Zugriff auf die REST-API des Würfels (siehe docs/api.md) und die Ortssuche
|
|
// von Open-Meteo.
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"time"
|
|
)
|
|
|
|
// State ist der Zustand aus GET/PATCH /api/state.
|
|
type State struct {
|
|
Effect string `json:"effect"`
|
|
Auto bool `json:"auto"`
|
|
AllCategories bool `json:"allCategories"`
|
|
PlayFavorites bool `json:"playFavorites"`
|
|
Manual bool `json:"manual"`
|
|
Brightness int `json:"brightness"`
|
|
Param int `json:"param"`
|
|
Param2 int `json:"param2"`
|
|
Favorites []string `json:"favorites"`
|
|
LampColor string `json:"lampColor"`
|
|
Text string `json:"text"`
|
|
Place string `json:"place"`
|
|
Latitude *float64 `json:"latitude,omitempty"`
|
|
Longitude *float64 `json:"longitude,omitempty"`
|
|
}
|
|
|
|
// Effect ist ein Eintrag aus GET /api/effects.
|
|
type Effect struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
Category string `json:"category"`
|
|
Param string `json:"param"` // was der Regler "Effekt 1" verändert
|
|
Param2 string `json:"param2"` // ... und "Effekt 2"
|
|
}
|
|
|
|
// Category ist ein Eintrag aus GET /api/categories.
|
|
type Category struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
// Weather ist die Antwort von GET /api/weather; ohne Daten fehlen die Werte.
|
|
type Weather struct {
|
|
Place string `json:"place"`
|
|
Temperature *float64 `json:"temperature,omitempty"`
|
|
Code *int `json:"code,omitempty"`
|
|
IsDay *bool `json:"isDay,omitempty"`
|
|
Age *int `json:"age,omitempty"`
|
|
}
|
|
|
|
// Pixels ist die Antwort von GET /api/pixels: 125 Farben, Index x + 5*y + 25*z.
|
|
type Pixels struct {
|
|
Manual bool `json:"manual"`
|
|
Pixels []string `json:"pixels"`
|
|
}
|
|
|
|
// Client spricht mit einem Würfel.
|
|
type Client struct {
|
|
host string
|
|
base string
|
|
http *http.Client
|
|
}
|
|
|
|
func NewClient(host string) *Client {
|
|
dialer := &net.Dialer{Timeout: 5 * time.Second}
|
|
transport := &http.Transport{
|
|
// Nur IPv4: bei cube.local wartet die Namensauflösung sonst etwa 5 s
|
|
// vergeblich auf eine IPv6-Adresse (wie curl ohne -4).
|
|
DialContext: func(ctx context.Context, _, addr string) (net.Conn, error) {
|
|
return dialer.DialContext(ctx, "tcp4", addr)
|
|
},
|
|
}
|
|
return &Client{
|
|
host: host,
|
|
base: "http://" + host + "/api/",
|
|
http: &http.Client{Timeout: 8 * time.Second, Transport: transport},
|
|
}
|
|
}
|
|
|
|
// do schickt eine Anfrage; body wird als JSON gesendet, die Antwort nach out
|
|
// gelesen. Fehlermeldungen des Würfels ({"error": "..."}) werden zu Go-Fehlern.
|
|
func (c *Client) do(method, path string, body, out any) ([]byte, error) {
|
|
var reader io.Reader
|
|
if body != nil {
|
|
data, err := json.Marshal(body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
reader = bytes.NewReader(data)
|
|
}
|
|
req, err := http.NewRequest(method, c.base+path, reader)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if body != nil {
|
|
req.Header.Set("Content-Type", "application/json")
|
|
}
|
|
resp, err := c.http.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Würfel %s nicht erreichbar: %w", c.host, err)
|
|
}
|
|
defer resp.Body.Close()
|
|
data, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if resp.StatusCode >= 300 {
|
|
var e struct {
|
|
Error string `json:"error"`
|
|
}
|
|
if json.Unmarshal(data, &e) == nil && e.Error != "" {
|
|
return nil, errors.New(e.Error)
|
|
}
|
|
return nil, fmt.Errorf("HTTP %d", resp.StatusCode)
|
|
}
|
|
if out != nil && len(data) > 0 {
|
|
if err := json.Unmarshal(data, out); err != nil {
|
|
return nil, fmt.Errorf("unerwartete Antwort: %w", err)
|
|
}
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
func (c *Client) State() (State, []byte, error) {
|
|
var s State
|
|
raw, err := c.do("GET", "state", nil, &s)
|
|
return s, raw, err
|
|
}
|
|
|
|
// Patch ändert nur die angegebenen Felder und liefert den neuen Zustand.
|
|
func (c *Client) Patch(fields map[string]any) (State, []byte, error) {
|
|
var s State
|
|
raw, err := c.do("PATCH", "state", fields, &s)
|
|
return s, raw, err
|
|
}
|
|
|
|
func (c *Client) Effects() ([]Effect, []byte, error) {
|
|
var e []Effect
|
|
raw, err := c.do("GET", "effects", nil, &e)
|
|
return e, raw, err
|
|
}
|
|
|
|
func (c *Client) Categories() ([]Category, []byte, error) {
|
|
var k []Category
|
|
raw, err := c.do("GET", "categories", nil, &k)
|
|
return k, raw, err
|
|
}
|
|
|
|
func (c *Client) Weather() (Weather, []byte, error) {
|
|
var w Weather
|
|
raw, err := c.do("GET", "weather", nil, &w)
|
|
return w, raw, err
|
|
}
|
|
|
|
func (c *Client) Pixels() (Pixels, []byte, error) {
|
|
var p Pixels
|
|
raw, err := c.do("GET", "pixels", nil, &p)
|
|
return p, raw, err
|
|
}
|
|
|
|
func (c *Client) PatchPixels(fields map[string]any) error {
|
|
_, err := c.do("PATCH", "pixels", fields, nil)
|
|
return err
|
|
}
|
|
|
|
func (c *Client) PutPixels(colors []string) error {
|
|
_, err := c.do("PUT", "pixels", map[string]any{"pixels": colors}, nil)
|
|
return err
|
|
}
|
|
|
|
func (c *Client) Special(id string) error {
|
|
_, err := c.do("POST", "specials/"+id, nil, nil)
|
|
return err
|
|
}
|
|
|
|
// Place ist ein Treffer der Ortssuche.
|
|
type Place struct {
|
|
Name string `json:"name"`
|
|
Latitude float64 `json:"latitude"`
|
|
Longitude float64 `json:"longitude"`
|
|
Admin1 string `json:"admin1"`
|
|
Country string `json:"country"`
|
|
}
|
|
|
|
// Region z. B. "Île-de-France, Frankreich"
|
|
func (p Place) Region() string {
|
|
r := ""
|
|
for _, part := range []string{p.Admin1, p.Country} {
|
|
if part != "" && part != p.Name {
|
|
if r != "" {
|
|
r += ", "
|
|
}
|
|
r += part
|
|
}
|
|
}
|
|
return r
|
|
}
|
|
|
|
// SearchPlaces sucht Orte bei Open-Meteo (direkt, nicht über den Würfel).
|
|
func SearchPlaces(name string) ([]Place, error) {
|
|
q := url.Values{"name": {name}, "count": {"5"}, "language": {"de"}, "format": {"json"}}
|
|
client := &http.Client{Timeout: 8 * time.Second}
|
|
resp, err := client.Get("https://geocoding-api.open-meteo.com/v1/search?" + q.Encode())
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Ortssuche fehlgeschlagen: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
var r struct {
|
|
Results []Place `json:"results"`
|
|
}
|
|
if err := json.NewDecoder(resp.Body).Decode(&r); err != nil {
|
|
return nil, fmt.Errorf("Ortssuche: unerwartete Antwort: %w", err)
|
|
}
|
|
return r.Results, nil
|
|
}
|