updated frontend

This commit is contained in:
2026-01-14 17:35:07 +01:00
parent 5332ab9dc1
commit b8884d89e3
5 changed files with 719 additions and 46 deletions

View File

@@ -32,6 +32,9 @@ type Client struct {
// Callbacks
onFrequencyChange func(freqMHz float64)
checkTransmitAllowed func() bool // Returns true if transmit allowed (motors not moving)
// Reconnection settings
reconnectInterval time.Duration
}
func New(host string, port int) *Client {
@@ -42,6 +45,7 @@ func New(host string, port int) *Client {
lastStatus: &Status{
Connected: false,
},
reconnectInterval: 5 * time.Second, // Reconnect every 5 seconds if disconnected
}
}
@@ -68,6 +72,7 @@ func (c *Client) Connect() error {
conn, err := net.DialTimeout("tcp", addr, 5*time.Second)
if err != nil {
log.Printf("FlexRadio: Connection failed: %v", err)
return fmt.Errorf("failed to connect: %w", err)
}
@@ -83,30 +88,37 @@ func (c *Client) Start() error {
return nil
}
if err := c.Connect(); err != nil {
return err
}
// Update connected status
c.statusMu.Lock()
if c.lastStatus != nil {
c.lastStatus.Connected = true
}
c.statusMu.Unlock()
c.running = true
// Start message listener
// 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)
go c.messageLoop()
// Subscribe to slice updates for frequency tracking
return nil
}
func (c *Client) subscribeToSlices() {
log.Println("FlexRadio: Subscribing to slice updates...")
_, err := c.sendCommand("sub slice all")
if err != nil {
log.Printf("FlexRadio: Warning - failed to subscribe to slices: %v", err)
}
return nil
}
func (c *Client) Stop() {
@@ -173,15 +185,52 @@ func (c *Client) sendCommand(cmd string) (string, error) {
func (c *Client) messageLoop() {
log.Println("FlexRadio: Message loop started")
reconnectTicker := time.NewTicker(c.reconnectInterval)
defer reconnectTicker.Stop()
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()
if c.conn == nil || c.reader == nil {
c.connMu.Unlock()
time.Sleep(1 * time.Second)
if err := c.Connect(); err != nil {
log.Printf("FlexRadio: Reconnect failed: %v", err)
continue
}
continue
}
@@ -211,6 +260,8 @@ func (c *Client) messageLoop() {
c.lastStatus.Connected = false
}
c.statusMu.Unlock()
log.Println("FlexRadio: Connection lost, will attempt reconnection...")
continue
}