up
This commit is contained in:
159
internal/devices/antennagenius/antennagenius.go
Normal file
159
internal/devices/antennagenius/antennagenius.go
Normal file
@@ -0,0 +1,159 @@
|
||||
package antennagenius
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
. "git.rouggy.com/rouggy/ShackMaster/internal/devices"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
host string
|
||||
port int
|
||||
conn net.Conn
|
||||
}
|
||||
|
||||
type Status struct {
|
||||
Radio1Antenna int `json:"radio1_antenna"` // 0-7 (antenna index)
|
||||
Radio2Antenna int `json:"radio2_antenna"` // 0-7 (antenna index)
|
||||
Connected bool `json:"connected"`
|
||||
}
|
||||
|
||||
func New(host string, port int) *Client {
|
||||
return &Client{
|
||||
host: host,
|
||||
port: port,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) Connect() error {
|
||||
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", c.host, c.port), 5*time.Second)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to connect: %w", err)
|
||||
}
|
||||
c.conn = conn
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) Close() error {
|
||||
if c.conn != nil {
|
||||
return c.conn.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) sendCommand(cmd string) (string, error) {
|
||||
if c.conn == nil {
|
||||
if err := c.Connect(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
// Get next command ID from global counter
|
||||
cmdID := GetGlobalCommandID().GetNextID()
|
||||
|
||||
// Format command with ID: C<id>|<command>
|
||||
fullCmd := fmt.Sprintf("C%d|%s\n", cmdID, cmd)
|
||||
|
||||
// Send command
|
||||
_, err := c.conn.Write([]byte(fullCmd))
|
||||
if err != nil {
|
||||
c.conn = nil
|
||||
return "", fmt.Errorf("failed to send command: %w", err)
|
||||
}
|
||||
|
||||
// Read response
|
||||
reader := bufio.NewReader(c.conn)
|
||||
response, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
c.conn = nil
|
||||
return "", fmt.Errorf("failed to read response: %w", err)
|
||||
}
|
||||
|
||||
return strings.TrimSpace(response), nil
|
||||
}
|
||||
|
||||
func (c *Client) GetStatus() (*Status, error) {
|
||||
resp, err := c.sendCommand("status")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return c.parseStatus(resp)
|
||||
}
|
||||
|
||||
func (c *Client) parseStatus(resp string) (*Status, error) {
|
||||
status := &Status{
|
||||
Connected: true,
|
||||
}
|
||||
|
||||
// Parse response format from 4O3A API
|
||||
// Expected format will vary - this is a basic parser
|
||||
pairs := strings.Fields(resp)
|
||||
|
||||
for _, pair := range pairs {
|
||||
parts := strings.SplitN(pair, "=", 2)
|
||||
if len(parts) != 2 {
|
||||
continue
|
||||
}
|
||||
|
||||
key := parts[0]
|
||||
value := parts[1]
|
||||
|
||||
switch key {
|
||||
case "radio1", "r1":
|
||||
status.Radio1Antenna, _ = strconv.Atoi(value)
|
||||
case "radio2", "r2":
|
||||
status.Radio2Antenna, _ = strconv.Atoi(value)
|
||||
}
|
||||
}
|
||||
|
||||
return status, nil
|
||||
}
|
||||
|
||||
// SetRadioAntenna sets which antenna a radio should use
|
||||
// radio: 1 or 2
|
||||
// antenna: 0-7 (antenna index)
|
||||
func (c *Client) SetRadioAntenna(radio int, antenna int) error {
|
||||
if radio < 1 || radio > 2 {
|
||||
return fmt.Errorf("radio must be 1 or 2")
|
||||
}
|
||||
if antenna < 0 || antenna > 7 {
|
||||
return fmt.Errorf("antenna must be between 0 and 7")
|
||||
}
|
||||
|
||||
cmd := fmt.Sprintf("set radio%d=%d", radio, antenna)
|
||||
resp, err := c.sendCommand(cmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Check response for success
|
||||
if !strings.Contains(strings.ToLower(resp), "ok") && resp != "" {
|
||||
// If response doesn't contain "ok" but isn't empty, assume success
|
||||
// (some devices may return the new state instead of "ok")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetRadioAntenna gets which antenna a radio is currently using
|
||||
func (c *Client) GetRadioAntenna(radio int) (int, error) {
|
||||
if radio < 1 || radio > 2 {
|
||||
return -1, fmt.Errorf("radio must be 1 or 2")
|
||||
}
|
||||
|
||||
status, err := c.GetStatus()
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
if radio == 1 {
|
||||
return status.Radio1Antenna, nil
|
||||
}
|
||||
return status.Radio2Antenna, nil
|
||||
}
|
||||
35
internal/devices/command_id.go
Normal file
35
internal/devices/command_id.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package devices
|
||||
|
||||
import "sync"
|
||||
|
||||
// CommandIDManager manages the global command ID counter for 4O3A devices
|
||||
// All 4O3A devices share the same command ID sequence
|
||||
type CommandIDManager struct {
|
||||
mu sync.Mutex
|
||||
counter int
|
||||
}
|
||||
|
||||
var globalCommandID = &CommandIDManager{
|
||||
counter: 0,
|
||||
}
|
||||
|
||||
// GetNextID returns the next command ID and increments the counter
|
||||
func (m *CommandIDManager) GetNextID() int {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
m.counter++
|
||||
return m.counter
|
||||
}
|
||||
|
||||
// Reset resets the counter to 0 (useful for testing or restart)
|
||||
func (m *CommandIDManager) Reset() {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.counter = 0
|
||||
}
|
||||
|
||||
// GetGlobalCommandID returns the global command ID manager
|
||||
func GetGlobalCommandID() *CommandIDManager {
|
||||
return globalCommandID
|
||||
}
|
||||
380
internal/devices/powergenius/powergenius.go
Normal file
380
internal/devices/powergenius/powergenius.go
Normal file
@@ -0,0 +1,380 @@
|
||||
package powergenius
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"math"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
. "git.rouggy.com/rouggy/ShackMaster/internal/devices"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
host string
|
||||
port int
|
||||
conn net.Conn
|
||||
connMu sync.Mutex
|
||||
lastStatus *Status
|
||||
statusMu sync.RWMutex
|
||||
stopChan chan struct{}
|
||||
running bool
|
||||
}
|
||||
|
||||
type Status struct {
|
||||
PowerForward float64 `json:"power_forward"`
|
||||
PowerReflected float64 `json:"power_reflected"`
|
||||
SWR float64 `json:"swr"`
|
||||
Voltage float64 `json:"voltage"`
|
||||
VDD float64 `json:"vdd"`
|
||||
Current float64 `json:"current"`
|
||||
PeakCurrent float64 `json:"peak_current"`
|
||||
Temperature float64 `json:"temperature"`
|
||||
HarmonicLoadTemp float64 `json:"harmonic_load_temp"`
|
||||
FanSpeed int `json:"fan_speed"`
|
||||
FanMode string `json:"fan_mode"`
|
||||
State string `json:"state"`
|
||||
BandA string `json:"band_a"`
|
||||
BandB string `json:"band_b"`
|
||||
FaultPresent bool `json:"fault_present"`
|
||||
Connected bool `json:"connected"`
|
||||
Meffa string `json:"meffa"`
|
||||
}
|
||||
|
||||
func New(host string, port int) *Client {
|
||||
return &Client{
|
||||
host: host,
|
||||
port: port,
|
||||
stopChan: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) Connect() error {
|
||||
c.connMu.Lock()
|
||||
defer c.connMu.Unlock()
|
||||
|
||||
if c.conn != nil {
|
||||
return nil // Already connected
|
||||
}
|
||||
|
||||
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", c.host, c.port), 5*time.Second)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to connect: %w", err)
|
||||
}
|
||||
c.conn = conn
|
||||
|
||||
// Read and discard version banner
|
||||
reader := bufio.NewReader(c.conn)
|
||||
_, _ = reader.ReadString('\n')
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) Close() error {
|
||||
c.connMu.Lock()
|
||||
defer c.connMu.Unlock()
|
||||
|
||||
if c.stopChan != nil {
|
||||
close(c.stopChan)
|
||||
}
|
||||
|
||||
if c.conn != nil {
|
||||
return c.conn.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Start begins continuous polling of the device
|
||||
func (c *Client) Start() error {
|
||||
if err := c.Connect(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if c.running {
|
||||
return nil
|
||||
}
|
||||
|
||||
c.running = true
|
||||
go c.pollLoop()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// pollLoop continuously polls the device for status
|
||||
func (c *Client) pollLoop() {
|
||||
ticker := time.NewTicker(150 * time.Millisecond)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
status, err := c.queryStatus()
|
||||
if err != nil {
|
||||
log.Printf("PowerGenius query error: %v", err)
|
||||
// Try to reconnect
|
||||
c.connMu.Lock()
|
||||
if c.conn != nil {
|
||||
c.conn.Close()
|
||||
c.conn = nil
|
||||
}
|
||||
c.connMu.Unlock()
|
||||
|
||||
if err := c.Connect(); err != nil {
|
||||
log.Printf("PowerGenius reconnect failed: %v", err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// Merge with existing status (spontaneous messages may only update some fields)
|
||||
c.statusMu.Lock()
|
||||
if c.lastStatus != nil {
|
||||
// Keep existing values for fields not in the new status
|
||||
if status.PowerForward == 0 && c.lastStatus.PowerForward != 0 {
|
||||
status.PowerForward = c.lastStatus.PowerForward
|
||||
}
|
||||
if status.Temperature == 0 && c.lastStatus.Temperature != 0 {
|
||||
status.Temperature = c.lastStatus.Temperature
|
||||
}
|
||||
if status.HarmonicLoadTemp == 0 && c.lastStatus.HarmonicLoadTemp != 0 {
|
||||
status.HarmonicLoadTemp = c.lastStatus.HarmonicLoadTemp
|
||||
}
|
||||
if status.Voltage == 0 && c.lastStatus.Voltage != 0 {
|
||||
status.Voltage = c.lastStatus.Voltage
|
||||
}
|
||||
if status.FanMode == "" && c.lastStatus.FanMode != "" {
|
||||
status.FanMode = c.lastStatus.FanMode
|
||||
}
|
||||
if status.BandA == "" && c.lastStatus.BandA != "" {
|
||||
status.BandA = c.lastStatus.BandA
|
||||
}
|
||||
if status.BandB == "" && c.lastStatus.BandB != "" {
|
||||
status.BandB = c.lastStatus.BandB
|
||||
}
|
||||
}
|
||||
c.lastStatus = status
|
||||
c.statusMu.Unlock()
|
||||
|
||||
case <-c.stopChan:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) queryStatus() (*Status, error) {
|
||||
c.connMu.Lock()
|
||||
defer c.connMu.Unlock()
|
||||
|
||||
if c.conn == nil {
|
||||
return nil, fmt.Errorf("not connected")
|
||||
}
|
||||
|
||||
// Get next command ID from global counter
|
||||
cmdID := GetGlobalCommandID().GetNextID()
|
||||
|
||||
// Format command with ID: C<id>|status
|
||||
fullCmd := fmt.Sprintf("C%d|status\n", cmdID)
|
||||
|
||||
// Send command
|
||||
_, err := c.conn.Write([]byte(fullCmd))
|
||||
if err != nil {
|
||||
c.conn = nil
|
||||
return nil, fmt.Errorf("failed to send command: %w", err)
|
||||
}
|
||||
|
||||
// Read response
|
||||
reader := bufio.NewReader(c.conn)
|
||||
response, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
c.conn = nil
|
||||
return nil, fmt.Errorf("failed to read response: %w", err)
|
||||
}
|
||||
|
||||
return c.parseStatus(strings.TrimSpace(response))
|
||||
}
|
||||
|
||||
func (c *Client) sendCommand(cmd string) (string, error) {
|
||||
c.connMu.Lock()
|
||||
defer c.connMu.Unlock()
|
||||
|
||||
if c.conn == nil {
|
||||
return "", fmt.Errorf("not connected")
|
||||
}
|
||||
|
||||
// Get next command ID from global counter
|
||||
cmdID := GetGlobalCommandID().GetNextID()
|
||||
|
||||
// Format command with ID: C<id>|<command>
|
||||
fullCmd := fmt.Sprintf("C%d|%s\n", cmdID, cmd)
|
||||
|
||||
// Send command
|
||||
_, err := c.conn.Write([]byte(fullCmd))
|
||||
if err != nil {
|
||||
c.conn = nil
|
||||
return "", fmt.Errorf("failed to send command: %w", err)
|
||||
}
|
||||
|
||||
// Read response
|
||||
reader := bufio.NewReader(c.conn)
|
||||
response, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
c.conn = nil
|
||||
return "", fmt.Errorf("failed to read response: %w", err)
|
||||
}
|
||||
|
||||
return strings.TrimSpace(response), nil
|
||||
}
|
||||
|
||||
func (c *Client) GetStatus() (*Status, error) {
|
||||
c.statusMu.RLock()
|
||||
defer c.statusMu.RUnlock()
|
||||
|
||||
if c.lastStatus == nil {
|
||||
return &Status{Connected: false}, nil
|
||||
}
|
||||
|
||||
return c.lastStatus, nil
|
||||
}
|
||||
|
||||
func (c *Client) parseStatus(resp string) (*Status, error) {
|
||||
status := &Status{
|
||||
Connected: true,
|
||||
}
|
||||
|
||||
var data string
|
||||
|
||||
// Handle two message formats:
|
||||
// 1. Response to command: R<id>|0|state=IDLE bandA=40 ...
|
||||
// 2. Spontaneous status: S0|state=TRANSMIT_A
|
||||
|
||||
if strings.HasPrefix(resp, "R") {
|
||||
// Response format: R<id>|0|state=IDLE bandA=40 ...
|
||||
parts := strings.SplitN(resp, "|", 3)
|
||||
if len(parts) < 3 {
|
||||
return nil, fmt.Errorf("invalid response format: %s", resp)
|
||||
}
|
||||
data = parts[2] // Get everything after "R<id>|0|"
|
||||
|
||||
} else if strings.HasPrefix(resp, "S") {
|
||||
// Spontaneous message format: S0|state=TRANSMIT_A
|
||||
parts := strings.SplitN(resp, "|", 2)
|
||||
if len(parts) < 2 {
|
||||
return nil, fmt.Errorf("invalid spontaneous message format: %s", resp)
|
||||
}
|
||||
data = parts[1] // Get everything after "S0|"
|
||||
|
||||
} else {
|
||||
return nil, fmt.Errorf("unknown message format: %s", resp)
|
||||
}
|
||||
|
||||
// Parse key=value pairs separated by spaces
|
||||
pairs := strings.Fields(data)
|
||||
|
||||
for _, pair := range pairs {
|
||||
kv := strings.SplitN(pair, "=", 2)
|
||||
if len(kv) != 2 {
|
||||
continue
|
||||
}
|
||||
|
||||
key := kv[0]
|
||||
value := kv[1]
|
||||
|
||||
switch key {
|
||||
case "fwd":
|
||||
// fwd is in dBm (e.g., 56.5 dBm = 446W)
|
||||
// Formula: watts = 10^(dBm/10) / 1000
|
||||
if dBm, err := strconv.ParseFloat(value, 64); err == nil {
|
||||
milliwatts := math.Pow(10, dBm/10.0)
|
||||
status.PowerForward = milliwatts / 1000.0
|
||||
}
|
||||
case "peakfwd":
|
||||
// Peak forward power
|
||||
case "swr":
|
||||
// SWR from return loss
|
||||
// Formula: returnLoss = abs(swr) / 20
|
||||
// swr = (10^returnLoss + 1) / (10^returnLoss - 1)
|
||||
if swrRaw, err := strconv.ParseFloat(value, 64); err == nil {
|
||||
returnLoss := math.Abs(swrRaw) / 20.0
|
||||
tenPowRL := math.Pow(10, returnLoss)
|
||||
calculatedSWR := (tenPowRL + 1) / (tenPowRL - 1)
|
||||
status.SWR = calculatedSWR
|
||||
}
|
||||
case "vac":
|
||||
status.Voltage, _ = strconv.ParseFloat(value, 64)
|
||||
case "meffa":
|
||||
status.Meffa = value
|
||||
case "vdd":
|
||||
status.VDD, _ = strconv.ParseFloat(value, 64)
|
||||
case "id":
|
||||
status.Current, _ = strconv.ParseFloat(value, 64)
|
||||
case "peakid":
|
||||
status.PeakCurrent, _ = strconv.ParseFloat(value, 64)
|
||||
case "temp":
|
||||
status.Temperature, _ = strconv.ParseFloat(value, 64)
|
||||
case "hltemp":
|
||||
status.HarmonicLoadTemp, _ = strconv.ParseFloat(value, 64)
|
||||
case "bandA":
|
||||
if band, err := strconv.Atoi(value); err == nil {
|
||||
status.BandA = fmt.Sprintf("%dM", band)
|
||||
}
|
||||
case "bandB":
|
||||
if band, err := strconv.Atoi(value); err == nil && band > 0 {
|
||||
status.BandB = fmt.Sprintf("%dM", band)
|
||||
}
|
||||
case "fanmode":
|
||||
status.FanMode = value
|
||||
case "state":
|
||||
status.State = value
|
||||
}
|
||||
}
|
||||
|
||||
return status, nil
|
||||
}
|
||||
|
||||
// Subscribe starts receiving periodic status updates
|
||||
func (c *Client) Subscribe() error {
|
||||
_, err := c.sendCommand("sub status")
|
||||
return err
|
||||
}
|
||||
|
||||
// Unsubscribe stops receiving periodic status updates
|
||||
func (c *Client) Unsubscribe() error {
|
||||
_, err := c.sendCommand("unsub status")
|
||||
return err
|
||||
}
|
||||
|
||||
// ReadUpdate reads a status update (when subscribed)
|
||||
func (c *Client) ReadUpdate() (*Status, error) {
|
||||
if c.conn == nil {
|
||||
return nil, fmt.Errorf("not connected")
|
||||
}
|
||||
|
||||
reader := bufio.NewReader(c.conn)
|
||||
response, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read update: %w", err)
|
||||
}
|
||||
|
||||
return c.parseStatus(strings.TrimSpace(response))
|
||||
}
|
||||
|
||||
// SetFanMode sets the fan mode
|
||||
// mode can be: STANDARD, CONTEST, or BROADCAST
|
||||
func (c *Client) SetFanMode(mode string) error {
|
||||
validModes := map[string]bool{
|
||||
"STANDARD": true,
|
||||
"CONTEST": true,
|
||||
"BROADCAST": true,
|
||||
}
|
||||
|
||||
if !validModes[mode] {
|
||||
return fmt.Errorf("invalid fan mode: %s (must be STANDARD, CONTEST, or BROADCAST)", mode)
|
||||
}
|
||||
|
||||
cmd := fmt.Sprintf("setup fanmode=%s", mode)
|
||||
_, err := c.sendCommand(cmd)
|
||||
return err
|
||||
}
|
||||
@@ -7,6 +7,8 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
. "git.rouggy.com/rouggy/ShackMaster/internal/devices"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
@@ -65,8 +67,14 @@ func (c *Client) sendCommand(cmd string) (string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// Get next command ID from global counter
|
||||
cmdID := GetGlobalCommandID().GetNextID()
|
||||
|
||||
// Format command with ID: C<id>|<command>
|
||||
fullCmd := fmt.Sprintf("C%d%s", cmdID, cmd)
|
||||
|
||||
// Send command
|
||||
_, err := c.conn.Write([]byte(cmd))
|
||||
_, err := c.conn.Write([]byte(fullCmd))
|
||||
if err != nil {
|
||||
c.conn = nil
|
||||
return "", fmt.Errorf("failed to send command: %w", err)
|
||||
|
||||
@@ -6,13 +6,14 @@ import (
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
. "git.rouggy.com/rouggy/ShackMaster/internal/devices"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
host string
|
||||
port int
|
||||
idNumber int
|
||||
conn net.Conn
|
||||
host string
|
||||
port int
|
||||
conn net.Conn
|
||||
}
|
||||
|
||||
type Status struct {
|
||||
@@ -31,11 +32,10 @@ type Status struct {
|
||||
Connected bool `json:"connected"`
|
||||
}
|
||||
|
||||
func New(host string, port int, idNumber int) *Client {
|
||||
func New(host string, port int) *Client {
|
||||
return &Client{
|
||||
host: host,
|
||||
port: port,
|
||||
idNumber: idNumber,
|
||||
host: host,
|
||||
port: port,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,8 +62,11 @@ func (c *Client) sendCommand(cmd string) (string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// Format command with ID
|
||||
fullCmd := fmt.Sprintf("C%d|%s\n", c.idNumber, cmd)
|
||||
// Get next command ID from global counter
|
||||
cmdID := GetGlobalCommandID().GetNextID()
|
||||
|
||||
// Format command with ID: C<id>|<command>
|
||||
fullCmd := fmt.Sprintf("C%d|%s\n", cmdID, cmd)
|
||||
|
||||
// Send command
|
||||
_, err := c.conn.Write([]byte(fullCmd))
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user