This commit is contained in:
2026-01-08 21:58:12 +01:00
parent 795248f7f2
commit 1ee0afa088
9 changed files with 725 additions and 0 deletions

View File

@@ -0,0 +1,232 @@
package rotatorgenius
import (
"bufio"
"fmt"
"net"
"strconv"
"strings"
"time"
)
type Client struct {
host string
port int
conn net.Conn
}
type Status struct {
Rotator1 RotatorData `json:"rotator1"`
Rotator2 RotatorData `json:"rotator2"`
Panic bool `json:"panic"`
}
type RotatorData struct {
CurrentAzimuth int `json:"current_azimuth"`
LimitCW int `json:"limit_cw"`
LimitCCW int `json:"limit_ccw"`
Configuration string `json:"configuration"` // "A" for azimuth, "E" for elevation
Moving int `json:"moving"` // 0=stopped, 1=CW, 2=CCW
Offset int `json:"offset"`
TargetAzimuth int `json:"target_azimuth"`
StartAzimuth int `json:"start_azimuth"`
OutsideLimit bool `json:"outside_limit"`
Name string `json:"name"`
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
}
}
// Send command
_, err := c.conn.Write([]byte(cmd))
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("|h")
if err != nil {
return nil, err
}
return parseStatusResponse(resp)
}
func parseStatusResponse(resp string) (*Status, error) {
if len(resp) < 80 {
return nil, fmt.Errorf("response too short: %d bytes", len(resp))
}
status := &Status{}
// Parse panic flag
status.Panic = resp[3] != 0x00
// Parse Rotator 1 (positions 4-38)
status.Rotator1 = parseRotatorData(resp[4:38])
// Parse Rotator 2 (positions 38-72)
if len(resp) >= 72 {
status.Rotator2 = parseRotatorData(resp[38:72])
}
return status, nil
}
func parseRotatorData(data string) RotatorData {
rd := RotatorData{}
// Current azimuth (3 bytes)
if azStr := strings.TrimSpace(data[0:3]); azStr != "999" {
rd.CurrentAzimuth, _ = strconv.Atoi(azStr)
rd.Connected = true
} else {
rd.CurrentAzimuth = 999
rd.Connected = false
}
// Limits
rd.LimitCW, _ = strconv.Atoi(strings.TrimSpace(data[3:6]))
rd.LimitCCW, _ = strconv.Atoi(strings.TrimSpace(data[6:9]))
// Configuration
rd.Configuration = string(data[9])
// Moving state
rd.Moving, _ = strconv.Atoi(string(data[10]))
// Offset
rd.Offset, _ = strconv.Atoi(strings.TrimSpace(data[11:15]))
// Target azimuth
if targetStr := strings.TrimSpace(data[15:18]); targetStr != "999" {
rd.TargetAzimuth, _ = strconv.Atoi(targetStr)
} else {
rd.TargetAzimuth = 999
}
// Start azimuth
if startStr := strings.TrimSpace(data[18:21]); startStr != "999" {
rd.StartAzimuth, _ = strconv.Atoi(startStr)
} else {
rd.StartAzimuth = 999
}
// Limit flag
rd.OutsideLimit = data[21] == '1'
// Name
rd.Name = strings.TrimSpace(data[22:34])
return rd
}
func (c *Client) MoveToAzimuth(rotator int, azimuth int) error {
if rotator < 1 || rotator > 2 {
return fmt.Errorf("rotator must be 1 or 2")
}
if azimuth < 0 || azimuth > 360 {
return fmt.Errorf("azimuth must be between 0 and 360")
}
cmd := fmt.Sprintf("|A%d%03d", rotator, azimuth)
resp, err := c.sendCommand(cmd)
if err != nil {
return err
}
if !strings.HasSuffix(resp, "K") {
return fmt.Errorf("command failed: %s", resp)
}
return nil
}
func (c *Client) RotateCW(rotator int) error {
if rotator < 1 || rotator > 2 {
return fmt.Errorf("rotator must be 1 or 2")
}
cmd := fmt.Sprintf("|P%d", rotator)
resp, err := c.sendCommand(cmd)
if err != nil {
return err
}
if !strings.HasSuffix(resp, "K") {
return fmt.Errorf("command failed: %s", resp)
}
return nil
}
func (c *Client) RotateCCW(rotator int) error {
if rotator < 1 || rotator > 2 {
return fmt.Errorf("rotator must be 1 or 2")
}
cmd := fmt.Sprintf("|M%d", rotator)
resp, err := c.sendCommand(cmd)
if err != nil {
return err
}
if !strings.HasSuffix(resp, "K") {
return fmt.Errorf("command failed: %s", resp)
}
return nil
}
func (c *Client) Stop() error {
resp, err := c.sendCommand("|S")
if err != nil {
return err
}
if !strings.HasSuffix(resp, "K") {
return fmt.Errorf("command failed: %s", resp)
}
return nil
}

View File

@@ -0,0 +1,202 @@
package tunergenius
import (
"bufio"
"fmt"
"net"
"strings"
"time"
)
type Client struct {
host string
port int
idNumber int
conn net.Conn
}
type Status struct {
Operate bool `json:"operate"` // true = OPERATE, false = STANDBY
Bypass bool `json:"bypass"` // Bypass mode
ActiveAntenna int `json:"active_antenna"` // 0=ANT1, 1=ANT2, 2=ANT3
TuningStatus string `json:"tuning_status"`
FrequencyA float64 `json:"frequency_a"`
FrequencyB float64 `json:"frequency_b"`
C1 int `json:"c1"`
L int `json:"l"`
C2 int `json:"c2"`
SWR float64 `json:"swr"`
Power float64 `json:"power"`
Temperature float64 `json:"temperature"`
Connected bool `json:"connected"`
}
func New(host string, port int, idNumber int) *Client {
return &Client{
host: host,
port: port,
idNumber: idNumber,
}
}
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
}
}
// Format command with ID
fullCmd := fmt.Sprintf("C%d|%s\n", c.idNumber, 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
}
// Parse the response - format will depend on actual device response
// This is a placeholder that should be updated based on real response format
status := &Status{
Connected: true,
}
// TODO: Parse actual status response from device
// The response format needs to be determined from real device testing
// For now, we just check if we got a response
_ = resp // Temporary: will be used when we parse the actual response format
return status, nil
}
func (c *Client) SetOperate(operate bool) error {
var state int
if operate {
state = 1
}
cmd := fmt.Sprintf("operate set=%d", state)
resp, err := c.sendCommand(cmd)
if err != nil {
return err
}
// Check if command was successful
if resp == "" {
return fmt.Errorf("empty response from device")
}
return nil
}
func (c *Client) SetBypass(bypass bool) error {
var state int
if bypass {
state = 1
}
cmd := fmt.Sprintf("bypass set=%d", state)
resp, err := c.sendCommand(cmd)
if err != nil {
return err
}
// Check if command was successful
if resp == "" {
return fmt.Errorf("empty response from device")
}
return nil
}
func (c *Client) ActivateAntenna(antenna int) error {
if antenna < 0 || antenna > 2 {
return fmt.Errorf("antenna must be 0 (ANT1), 1 (ANT2), or 2 (ANT3)")
}
cmd := fmt.Sprintf("activate ant=%d", antenna)
resp, err := c.sendCommand(cmd)
if err != nil {
return err
}
// Check if command was successful
if resp == "" {
return fmt.Errorf("empty response from device")
}
return nil
}
func (c *Client) AutoTune() error {
resp, err := c.sendCommand("autotune")
if err != nil {
return err
}
// Check if command was successful
if resp == "" {
return fmt.Errorf("empty response from device")
}
return nil
}
// TuneRelay adjusts tuning parameters manually
// relay: 0=C1, 1=L, 2=C2
// move: -1 to decrease, 1 to increase
func (c *Client) TuneRelay(relay int, move int) error {
if relay < 0 || relay > 2 {
return fmt.Errorf("relay must be 0 (C1), 1 (L), or 2 (C2)")
}
if move != -1 && move != 1 {
return fmt.Errorf("move must be -1 or 1")
}
cmd := fmt.Sprintf("tune relay=%d move=%d", relay, move)
resp, err := c.sendCommand(cmd)
if err != nil {
return err
}
// Check if command was successful
if resp == "" {
return fmt.Errorf("empty response from device")
}
return nil
}

View File

@@ -0,0 +1,94 @@
package webswitch
import (
"fmt"
"io"
"net/http"
"time"
)
type Client struct {
host string
httpClient *http.Client
}
type Status struct {
Relays []RelayState `json:"relays"`
}
type RelayState struct {
Number int `json:"number"`
State bool `json:"state"`
}
func New(host string) *Client {
return &Client{
host: host,
httpClient: &http.Client{
Timeout: 5 * time.Second,
},
}
}
func (c *Client) SetRelay(relay int, state bool) error {
if relay < 1 || relay > 5 {
return fmt.Errorf("relay number must be between 1 and 5")
}
action := "off"
if state {
action = "on"
}
url := fmt.Sprintf("http://%s/relaycontrol/%s/%d", c.host, action, relay)
resp, err := c.httpClient.Get(url)
if err != nil {
return fmt.Errorf("failed to control relay: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("unexpected status code %d: %s", resp.StatusCode, string(body))
}
return nil
}
func (c *Client) TurnOn(relay int) error {
return c.SetRelay(relay, true)
}
func (c *Client) TurnOff(relay int) error {
return c.SetRelay(relay, false)
}
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)
}
}
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)
}
}
return nil
}
// Ping checks if the device is reachable
func (c *Client) Ping() error {
url := fmt.Sprintf("http://%s/", c.host)
resp, err := c.httpClient.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}