rot finished

This commit is contained in:
2026-01-09 11:56:40 +01:00
parent 1ee0afa088
commit ac99f291a7
30 changed files with 5581 additions and 293 deletions

View File

@@ -6,227 +6,244 @@ import (
"net"
"strconv"
"strings"
"sync"
"time"
)
type Client struct {
host string
port int
conn net.Conn
host string
port int
conn net.Conn
reader *bufio.Reader
connMu sync.Mutex
lastStatus *Status
statusMu sync.RWMutex
stopChan chan struct{}
running bool
}
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"`
Heading int `json:"heading"`
Connected bool `json:"connected"`
}
func New(host string, port int) *Client {
return &Client{
host: host,
port: port,
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
}
fmt.Printf("RotatorGenius: Attempting to connect to %s:%d\n", c.host, c.port)
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", c.host, c.port), 5*time.Second)
if err != nil {
fmt.Printf("RotatorGenius: Connection failed: %v\n", err)
return fmt.Errorf("failed to connect: %w", err)
}
c.conn = conn
c.reader = bufio.NewReader(c.conn)
fmt.Println("RotatorGenius: Connected successfully")
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
}
func (c *Client) sendCommand(cmd string) (string, error) {
if c.conn == nil {
if err := c.Connect(); err != nil {
return "", err
}
func (c *Client) Start() error {
fmt.Println("RotatorGenius Start() called")
if c.running {
fmt.Println("RotatorGenius already running, skipping")
return nil
}
fmt.Println("RotatorGenius attempting initial connection...")
_ = c.Connect()
c.running = true
fmt.Println("RotatorGenius launching pollLoop...")
go c.pollLoop()
fmt.Println("RotatorGenius Start() completed")
return nil
}
func (c *Client) pollLoop() {
ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ticker.C:
c.connMu.Lock()
if c.conn == nil {
c.connMu.Unlock()
c.statusMu.Lock()
c.lastStatus = &Status{Connected: false}
c.statusMu.Unlock()
if err := c.Connect(); err != nil {
continue
}
c.connMu.Lock()
}
c.connMu.Unlock()
status, err := c.queryStatus()
if err != nil {
c.connMu.Lock()
if c.conn != nil {
c.conn.Close()
c.conn = nil
c.reader = nil
}
c.connMu.Unlock()
c.statusMu.Lock()
c.lastStatus = &Status{Connected: false}
c.statusMu.Unlock()
continue
}
status.Connected = true
c.statusMu.Lock()
c.lastStatus = status
c.statusMu.Unlock()
case <-c.stopChan:
return
}
}
}
func (c *Client) sendCommand(cmd string) error {
c.connMu.Lock()
defer c.connMu.Unlock()
if c.conn == nil || c.reader == nil {
return fmt.Errorf("not connected")
}
// Send command
_, err := c.conn.Write([]byte(cmd))
if err != nil {
c.conn = nil
return "", fmt.Errorf("failed to send command: %w", err)
c.reader = nil
return fmt.Errorf("failed to send command: %w", err)
}
// Read response
reader := bufio.NewReader(c.conn)
response, err := reader.ReadString('\n')
return nil
}
func (c *Client) queryStatus() (*Status, error) {
c.connMu.Lock()
defer c.connMu.Unlock()
if c.conn == nil || c.reader == nil {
return nil, fmt.Errorf("not connected")
}
// Send |h command
_, err := c.conn.Write([]byte("|h"))
if err != nil {
c.conn = nil
return "", fmt.Errorf("failed to read response: %w", err)
c.reader = nil
return nil, fmt.Errorf("failed to send query: %w", err)
}
return strings.TrimSpace(response), nil
// Read response - RotatorGenius doesn't send newline, read fixed amount
c.conn.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
defer c.conn.SetReadDeadline(time.Time{})
buf := make([]byte, 100)
n, err := c.reader.Read(buf)
if err != nil || n == 0 {
c.conn = nil
c.reader = nil
return nil, fmt.Errorf("failed to read response: %w", err)
}
response := string(buf[:n])
return c.parseStatus(response), nil
}
func (c *Client) parseStatus(response string) *Status {
status := &Status{}
// Response format: |h2<null><heading>...
// Example: |h2\x00183 8 10A0...
// After |h2 there's a null byte, then 3 digits for heading
if !strings.HasPrefix(response, "|h2") {
return status
}
// Skip |h2 (3 chars) and null byte (1 char), then read 3 digits
if len(response) >= 7 {
// Position 3 is the null byte, position 4-6 are the heading
headingStr := response[4:7]
heading, err := strconv.Atoi(strings.TrimSpace(headingStr))
if err == nil {
status.Heading = heading
}
}
return status
}
func (c *Client) GetStatus() (*Status, error) {
resp, err := c.sendCommand("|h")
if err != nil {
return nil, err
c.statusMu.RLock()
defer c.statusMu.RUnlock()
if c.lastStatus == nil {
return &Status{Connected: false}, nil
}
return parseStatusResponse(resp)
return c.lastStatus, nil
}
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
// SetHeading rotates to a specific azimuth
func (c *Client) SetHeading(azimuth int) error {
cmd := fmt.Sprintf("|A1%d", azimuth)
return c.sendCommand(cmd)
}
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
// RotateCW rotates clockwise
func (c *Client) RotateCW() error {
return c.sendCommand("|P1")
}
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
// RotateCCW rotates counter-clockwise
func (c *Client) RotateCCW() error {
return c.sendCommand("|M1")
}
// Stop stops rotation
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
return c.sendCommand("|S")
}