4 Commits

Author SHA1 Message Date
130efeee83 up 2026-01-15 20:30:38 +01:00
4eeec6bdf6 working 2026-01-15 06:51:25 +01:00
de3fda2648 Revert "updated frontend"
This reverts commit b8884d89e3.
2026-01-15 06:44:29 +01:00
c6ceeb103b update sunset 2026-01-15 06:26:49 +01:00

View File

@@ -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,14 +83,10 @@ 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 // Update connected status
c.statusMu.Lock() c.statusMu.Lock()
if c.lastStatus != nil { if c.lastStatus != nil {
@@ -103,22 +94,19 @@ func (c *Client) Start() error {
} }
c.statusMu.Unlock() c.statusMu.Unlock()
// Subscribe to slice updates for frequency tracking c.running = true
c.subscribeToSlices()
}
// Start message listener (handles reconnection) // 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() c.connMu.Lock()
isConnected := 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 !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 { if err := c.Connect(); err != nil {
log.Printf("FlexRadio: Reconnect failed: %v", err) log.Printf("FlexRadio: Reconnect failed: %v", err)
continue 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()
if c.conn == nil || c.reader == nil {
c.connMu.Unlock()
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
} }