This commit is contained in:
2026-01-09 11:56:40 +01:00
parent 1ee0afa088
commit 4ab192418e
30 changed files with 3455 additions and 16 deletions

View File

@@ -4,6 +4,8 @@ import (
"fmt"
"io"
"net/http"
"strconv"
"strings"
"time"
)
@@ -82,6 +84,56 @@ func (c *Client) AllOff() error {
return nil
}
// GetStatus queries the actual state of all relays
func (c *Client) GetStatus() (*Status, error) {
url := fmt.Sprintf("http://%s/relaystate/get2/1$2$3$4$5$", c.host)
resp, err := c.httpClient.Get(url)
if err != nil {
return nil, fmt.Errorf("failed to get relay status: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response: %w", err)
}
// Parse response format: "1,1\n2,1\n3,1\n4,1\n5,0\n"
status := &Status{
Relays: make([]RelayState, 0, 5),
}
lines := strings.Split(strings.TrimSpace(string(body)), "\n")
for _, line := range lines {
parts := strings.Split(strings.TrimSpace(line), ",")
if len(parts) != 2 {
continue
}
relayNum, err := strconv.Atoi(parts[0])
if err != nil {
continue
}
relayState, err := strconv.Atoi(parts[1])
if err != nil {
continue
}
status.Relays = append(status.Relays, RelayState{
Number: relayNum,
State: relayState == 1,
})
}
return status, nil
}
// Ping checks if the device is reachable
func (c *Client) Ping() error {
url := fmt.Sprintf("http://%s/", c.host)