Merge branch 'main' of https://git.rouggy.com/rouggy/ShackMaster
This commit is contained in:
@@ -3,6 +3,7 @@ package antennagenius
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
<<<<<<< HEAD
|
||||
"log"
|
||||
"net"
|
||||
"strconv"
|
||||
@@ -52,17 +53,43 @@ type Antenna struct {
|
||||
RX string `json:"rx"`
|
||||
InBand string `json:"in_band"`
|
||||
Hotkey int `json:"hotkey"`
|
||||
=======
|
||||
"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"`
|
||||
>>>>>>> 4ab192418e21065c68d59777493ea03b76c061e7
|
||||
}
|
||||
|
||||
func New(host string, port int) *Client {
|
||||
return &Client{
|
||||
<<<<<<< HEAD
|
||||
host: host,
|
||||
port: port,
|
||||
stopChan: make(chan struct{}),
|
||||
=======
|
||||
host: host,
|
||||
port: port,
|
||||
>>>>>>> 4ab192418e21065c68d59777493ea03b76c061e7
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) Connect() error {
|
||||
<<<<<<< HEAD
|
||||
c.connMu.Lock()
|
||||
defer c.connMu.Unlock()
|
||||
|
||||
@@ -70,20 +97,26 @@ func (c *Client) Connect() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
=======
|
||||
>>>>>>> 4ab192418e21065c68d59777493ea03b76c061e7
|
||||
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
|
||||
<<<<<<< HEAD
|
||||
c.reader = bufio.NewReader(c.conn)
|
||||
|
||||
// Read and discard banner
|
||||
_, _ = c.reader.ReadString('\n')
|
||||
|
||||
=======
|
||||
>>>>>>> 4ab192418e21065c68d59777493ea03b76c061e7
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) Close() error {
|
||||
<<<<<<< HEAD
|
||||
c.connMu.Lock()
|
||||
defer c.connMu.Unlock()
|
||||
|
||||
@@ -91,12 +124,15 @@ func (c *Client) Close() error {
|
||||
close(c.stopChan)
|
||||
}
|
||||
|
||||
=======
|
||||
>>>>>>> 4ab192418e21065c68d59777493ea03b76c061e7
|
||||
if c.conn != nil {
|
||||
return c.conn.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
func (c *Client) Start() error {
|
||||
if c.running {
|
||||
return nil
|
||||
@@ -263,10 +299,46 @@ func (c *Client) sendCommand(cmd string) (string, error) {
|
||||
|
||||
func (c *Client) getAntennaList() ([]Antenna, error) {
|
||||
resp, err := c.sendCommand("antenna list")
|
||||
=======
|
||||
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")
|
||||
>>>>>>> 4ab192418e21065c68d59777493ea03b76c061e7
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
var antennas []Antenna
|
||||
|
||||
// Response format: R<id>|0|antenna <num> name=<name> tx=<hex> rx=<hex> inband=<hex> hotkey=<num>
|
||||
@@ -333,10 +405,58 @@ func (c *Client) parseAntennaLine(line string) Antenna {
|
||||
|
||||
func (c *Client) subscribeToPortUpdates() error {
|
||||
resp, err := c.sendCommand("sub port all")
|
||||
=======
|
||||
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)
|
||||
>>>>>>> 4ab192418e21065c68d59777493ea03b76c061e7
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
// Parse initial port status from subscription response
|
||||
// The response may contain S0|port messages with current status
|
||||
lines := strings.Split(resp, "\n")
|
||||
@@ -345,11 +465,18 @@ func (c *Client) subscribeToPortUpdates() error {
|
||||
if strings.HasPrefix(line, "S0|port") {
|
||||
c.parsePortStatus(line)
|
||||
}
|
||||
=======
|
||||
// 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")
|
||||
>>>>>>> 4ab192418e21065c68d59777493ea03b76c061e7
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
func (c *Client) parsePortStatus(line string) {
|
||||
// Format: S0|port <id> auto=<0|1> source=<src> band=<n> freq=<f> nickname=<name> rxant=<n> txant=<n> inband=<n> tx=<0|1> inhibit=<n>
|
||||
|
||||
@@ -434,4 +561,21 @@ func (c *Client) SetAntenna(port, antenna int) error {
|
||||
func (c *Client) Reboot() error {
|
||||
_, err := c.sendCommand("reboot")
|
||||
return err
|
||||
=======
|
||||
// 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
|
||||
>>>>>>> 4ab192418e21065c68d59777493ea03b76c061e7
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user