rot finished
This commit is contained in:
@@ -4,6 +4,8 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -13,7 +15,8 @@ type Client struct {
|
||||
}
|
||||
|
||||
type Status struct {
|
||||
Relays []RelayState `json:"relays"`
|
||||
Relays []RelayState `json:"relays"`
|
||||
Connected bool `json:"connected"`
|
||||
}
|
||||
|
||||
type RelayState struct {
|
||||
@@ -65,23 +68,110 @@ func (c *Client) TurnOff(relay int) error {
|
||||
}
|
||||
|
||||
func (c *Client) AllOn() error {
|
||||
for i := 1; i <= 5; i++ {
|
||||
if err := c.TurnOn(i); err != nil {
|
||||
return fmt.Errorf("failed to turn on relay %d: %w", i, err)
|
||||
// Sequence for ALL ON:
|
||||
// 1. Turn on relays 1, 2, 3, 5 immediately
|
||||
// 2. Wait 5 seconds
|
||||
// 3. Turn on relay 4 (Flex Radio Start)
|
||||
|
||||
// Turn on relays 1, 2, 3, 5
|
||||
for _, relay := range []int{1, 2, 3, 5} {
|
||||
if err := c.TurnOn(relay); err != nil {
|
||||
return fmt.Errorf("failed to turn on relay %d: %w", relay, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Wait 5 seconds for power supply to stabilize
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
// Turn on relay 4 (Flex Radio)
|
||||
if err := c.TurnOn(4); err != nil {
|
||||
return fmt.Errorf("failed to turn on relay 4: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) AllOff() error {
|
||||
for i := 1; i <= 5; i++ {
|
||||
if err := c.TurnOff(i); err != nil {
|
||||
return fmt.Errorf("failed to turn off relay %d: %w", i, err)
|
||||
// Sequence for ALL OFF:
|
||||
// 1. Turn off relay 4 (Flex Radio) immediately
|
||||
// 2. Turn off relays 2, 3, 5 immediately
|
||||
// 3. Wait 35 seconds for Flex Radio to shut down
|
||||
// 4. Turn off relay 1 (Power Supply)
|
||||
|
||||
// Turn off relay 4 (Flex Radio)
|
||||
if err := c.TurnOff(4); err != nil {
|
||||
return fmt.Errorf("failed to turn off relay 4: %w", err)
|
||||
}
|
||||
|
||||
// Turn off relays 2, 3, 5
|
||||
for _, relay := range []int{2, 3, 5} {
|
||||
if err := c.TurnOff(relay); err != nil {
|
||||
return fmt.Errorf("failed to turn off relay %d: %w", relay, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Wait 35 seconds for Flex Radio to shut down properly
|
||||
time.Sleep(35 * time.Second)
|
||||
|
||||
// Turn off relay 1 (Power Supply)
|
||||
if err := c.TurnOff(1); err != nil {
|
||||
return fmt.Errorf("failed to turn off relay 1: %w", err)
|
||||
}
|
||||
|
||||
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),
|
||||
Connected: true,
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user