Compare commits
4 Commits
feature/fl
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 130efeee83 | |||
| 4eeec6bdf6 | |||
| de3fda2648 | |||
| c6ceeb103b |
@@ -32,9 +32,6 @@ type Client struct {
|
|||||||
// Callbacks
|
// Callbacks
|
||||||
onFrequencyChange func(freqMHz float64)
|
onFrequencyChange func(freqMHz float64)
|
||||||
checkTransmitAllowed func() bool // Returns true if transmit allowed (motors not moving)
|
checkTransmitAllowed func() bool // Returns true if transmit allowed (motors not moving)
|
||||||
|
|
||||||
// Reconnection settings
|
|
||||||
reconnectInterval time.Duration
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(host string, port int) *Client {
|
func New(host string, port int) *Client {
|
||||||
@@ -45,7 +42,6 @@ func New(host string, port int) *Client {
|
|||||||
lastStatus: &Status{
|
lastStatus: &Status{
|
||||||
Connected: false,
|
Connected: false,
|
||||||
},
|
},
|
||||||
reconnectInterval: 5 * time.Second, // Reconnect every 5 seconds if disconnected
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,7 +68,6 @@ func (c *Client) Connect() error {
|
|||||||
|
|
||||||
conn, err := net.DialTimeout("tcp", addr, 5*time.Second)
|
conn, err := net.DialTimeout("tcp", addr, 5*time.Second)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("FlexRadio: Connection failed: %v", err)
|
|
||||||
return fmt.Errorf("failed to connect: %w", err)
|
return fmt.Errorf("failed to connect: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,37 +83,30 @@ func (c *Client) Start() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
c.running = true
|
if err := c.Connect(); err != nil {
|
||||||
|
return err
|
||||||
// Try initial connection but don't fail if it doesn't work
|
|
||||||
// The messageLoop will handle reconnection
|
|
||||||
err := c.Connect()
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("FlexRadio: Initial connection failed, will retry: %v", err)
|
|
||||||
} else {
|
|
||||||
// Update connected status
|
|
||||||
c.statusMu.Lock()
|
|
||||||
if c.lastStatus != nil {
|
|
||||||
c.lastStatus.Connected = true
|
|
||||||
}
|
|
||||||
c.statusMu.Unlock()
|
|
||||||
|
|
||||||
// Subscribe to slice updates for frequency tracking
|
|
||||||
c.subscribeToSlices()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start message listener (handles reconnection)
|
// Update connected status
|
||||||
|
c.statusMu.Lock()
|
||||||
|
if c.lastStatus != nil {
|
||||||
|
c.lastStatus.Connected = true
|
||||||
|
}
|
||||||
|
c.statusMu.Unlock()
|
||||||
|
|
||||||
|
c.running = true
|
||||||
|
|
||||||
|
// Start message listener
|
||||||
go c.messageLoop()
|
go c.messageLoop()
|
||||||
|
|
||||||
return nil
|
// Subscribe to slice updates for frequency tracking
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) subscribeToSlices() {
|
|
||||||
log.Println("FlexRadio: Subscribing to slice updates...")
|
log.Println("FlexRadio: Subscribing to slice updates...")
|
||||||
_, err := c.sendCommand("sub slice all")
|
_, err := c.sendCommand("sub slice all")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("FlexRadio: Warning - failed to subscribe to slices: %v", err)
|
log.Printf("FlexRadio: Warning - failed to subscribe to slices: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Stop() {
|
func (c *Client) Stop() {
|
||||||
@@ -185,52 +173,15 @@ func (c *Client) sendCommand(cmd string) (string, error) {
|
|||||||
func (c *Client) messageLoop() {
|
func (c *Client) messageLoop() {
|
||||||
log.Println("FlexRadio: Message loop started")
|
log.Println("FlexRadio: Message loop started")
|
||||||
|
|
||||||
reconnectTicker := time.NewTicker(c.reconnectInterval)
|
|
||||||
defer reconnectTicker.Stop()
|
|
||||||
|
|
||||||
for c.running {
|
for c.running {
|
||||||
c.connMu.Lock()
|
|
||||||
isConnected := c.conn != nil && c.reader != nil
|
|
||||||
c.connMu.Unlock()
|
|
||||||
|
|
||||||
if !isConnected {
|
|
||||||
// Update status to disconnected
|
|
||||||
c.statusMu.Lock()
|
|
||||||
if c.lastStatus != nil {
|
|
||||||
c.lastStatus.Connected = false
|
|
||||||
}
|
|
||||||
c.statusMu.Unlock()
|
|
||||||
|
|
||||||
// Wait for reconnect interval
|
|
||||||
select {
|
|
||||||
case <-reconnectTicker.C:
|
|
||||||
log.Println("FlexRadio: Attempting to reconnect...")
|
|
||||||
if err := c.Connect(); err != nil {
|
|
||||||
log.Printf("FlexRadio: Reconnect failed: %v", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Successfully reconnected
|
|
||||||
c.statusMu.Lock()
|
|
||||||
if c.lastStatus != nil {
|
|
||||||
c.lastStatus.Connected = true
|
|
||||||
}
|
|
||||||
c.statusMu.Unlock()
|
|
||||||
|
|
||||||
// Re-subscribe to slices after reconnection
|
|
||||||
c.subscribeToSlices()
|
|
||||||
|
|
||||||
case <-c.stopChan:
|
|
||||||
log.Println("FlexRadio: Message loop stopping (stop signal received)")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read from connection
|
|
||||||
c.connMu.Lock()
|
c.connMu.Lock()
|
||||||
if c.conn == nil || c.reader == nil {
|
if c.conn == nil || c.reader == nil {
|
||||||
c.connMu.Unlock()
|
c.connMu.Unlock()
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
if err := c.Connect(); err != nil {
|
||||||
|
log.Printf("FlexRadio: Reconnect failed: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -260,8 +211,6 @@ func (c *Client) messageLoop() {
|
|||||||
c.lastStatus.Connected = false
|
c.lastStatus.Connected = false
|
||||||
}
|
}
|
||||||
c.statusMu.Unlock()
|
c.statusMu.Unlock()
|
||||||
|
|
||||||
log.Println("FlexRadio: Connection lost, will attempt reconnection...")
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user